Adding a culture yield to resources

alpaca

King of Ungulates
Joined
Aug 3, 2006
Messages
2,322
I wrote a small script that adds culture to plots at the start of the turn if they have a resource. The amount and resources to do this for are defined in XML.

This is the script which can be added as, for example, an InGameUIAddin:
Code:
--[[
	ResourceCulture.lua
	
	Creator: alpaca
	Last Change: 17.01.2011
	
	Description: 
	Adds culture to certain resources defined via XML
]]--

-- define extra culture for each resource
local ResourceIDsToCultureTable = {}

for pResourceCultureChange in GameInfo.Resource_CultureChanges() do
	ResourceIDsToCultureTable[GameInfo.Resources[pResourceCultureChange.ResourceType].ID] = pResourceCultureChange.Yield
end

-- loop through all plots, check for applicable resources, and change them
for iPlot = 0, Map.GetNumPlots() - 1 do
	local pPlot = Map.GetPlotByIndex(iPlot)
	
	local resType = pPlot:GetResourceType()
	
	if ResourceIDsToCultureTable[resType] ~= nil and pPlot:GetCulture() == 0 then
		pPlot:ChangeCulture(ResourceIDsToCultureTable[resType])
	end
end

And this is what you need to add into an XML file that's updated into the database (obviously, replace the resources and yields with your values):

Code:
<Table name="Resource_CultureChanges">
		<Column name="ResourceType" type="text" reference="Resources(Type)"/>
		<Column name="Yield" type="integer" default="0"/>
	</Table>
	<Resource_CultureChanges>
		<Row>
			<ResourceType>RESOURCE_GOLD</ResourceType>
			<Yield>1</Yield>
		</Row>
		<Row>
			<ResourceType>RESOURCE_DYE</ResourceType>
			<Yield>1</Yield>
		</Row>
		<Row>
			<ResourceType>RESOURCE_INCENSE</ResourceType>
			<Yield>2</Yield>
		</Row>
	</Resource_CultureChanges>
 
Few questions - does this go to the city with 'ownership' of the tile or just the empire-wide pool, does it apply even if not worked, and I'm sure I had another question but can't think what it was...

Edit: Oh yeah, does that change anything graphically?
 
It adds culture to a plot like if a landmark was built there, only less of it. So it obviously only does anything if a city works the tile.
 
More nice work. :thumbsup:
 
So, in the underlying structure, culture is a tile yield, but not accessible in XML; instead, it's an inherent property of each tile. This is how the monument works.

So, if one wanted an improvement to add culture, one would merely need to hook into events (if they exist) for the building/restoring from pillage and removal/pillage of improvements. Interesting...

PS: good work. Obviously quite simple in execution, but required the imagination to try and the discovery of the relevant APIs...
 
As an aside, on the changeCulture method, does this operate as an arithmetic change (adding or subtracting), or does it work as a change-the-value-to-the-provided-value?
 
As an aside, on the changeCulture method, does this operate as an arithmetic change (adding or subtracting), or does it work as a change-the-value-to-the-provided-value?

If it's change-the-value-to-the-provided-value, then look at my Building Resources mod for how to handle a similar situation. You could just keep track of when yields have already been applied and watch for improvements lost.
 
ChangeCulture works like x = x + y, functions that set a value are usually called set while this kind of function is called change.

It wasn't difficult to do at all, so no need to go into praise-mode :lol: I just had to implement it anyways for PWM and decided to share it as an example script because it shows you some tricks like how to add something to XML and use it in Lua.

In principle, you can do arbitrary culture changes by doing something similar but I would be careful to do things that, for example, cycle through all plots, too often. It's not too bad on normal-sized maps but I could imagine it would take a little on huge maps if you execute this stuff every turn. This script is only executed once at game start/reload. I haven't checked if the culture change is saved, though.

And no, internally culture does not work as a yield. It's something else I think. All places where it's used use it differently than a yield, although it beats me why they didn't just use the same interface for it. Everything is simply done for yields and then there's a copy for culture. Very stupid, if you ask me.
 
Everything is simply done for yields and then there's a copy for culture. Very stupid, if you ask me.

You know, the developers could make us modders VERY happy if they just added Culture and Happiness as yields in a patch. Totally transparent to the users, but the possibilities it'd open up...
 
You know, the developers could make us modders VERY happy if they just added Culture and Happiness as yields in a patch. Totally transparent to the users, but the possibilities it'd open up...

Yes. And they could have saved themselves a lot of time if they had done so in the first place...
 
They could probably even bolt it in in such a way that the existing API (in C and Lua) would still work, but it would also work as a yield...
 
Back
Top Bottom