Tile improvement yield bonus

Mezzzzz

Chieftain
Joined
Jun 1, 2014
Messages
29
Location
Paris area, France
Ok, I've been naive. The solution I was sure would work, did not. So I regret if anyone re-used the content of my original post and felt disappointed after trying it :s
Even though, I still think the code from Danmacsch and DarkScythe is a good base so this is what I've so far;

Code:
function Horsespasturebonus(playerID)
	local player = Players[playerID]
	if player:GetCivilizationType() == GameInfoTypes["CIVILIZATION_MYCIVILIZATION"] and player:IsAlive() and not player:IsMinorCiv() and not player:IsBarbarian() then
		for city in player:Cities() do
			if plot:GetResourceType() == GameInfoTypes["RESOURCE_HORSE"] then
				if plot:GetImprovementType() == GameInfoTypes["IMPROVEMENT_PASTURE"] then
					city:SetNumRealBuilding(GameInfoTypes["BUILDING_IMPROVED_PASTURE"], 1)
				else
				if plot:GetImprovementType() ~= GameInfoTypes["IMPROVEMENT_PASTURE"] then
					city:SetNumRealBuilding(GameInfoTypes["BUILDING_IMPROVED_PASTURE"], 0)
				end
			end
		end
	end
end
GameEvents.PlayerDoTurn.Add(Horsespasturebonus)

The purpose is having a xml dummy building activated when both condition resource_horse and improvement_pasture are validated on any tile worked by a city.
I'd appreciate if someone could tell me what am I doing wrong exactly. Thanks!
 
You haven't defined "plot". It's just a dangling undefined variable which is going to give you "nil" all the time.

This is something I've been working on so is a bit W.I.P. but shows you how I am putting together a function to see whether a city has a resource, the resource is improved with the correct improvement type, is not pillaged, and the city is working the plot.
It is called elsewhere in a PlayerDoTurn function, and returns true/false as to whether all my desired conditions are met.
Spoiler :
Code:
 [COLOR="Green"]gPrintStatements = 0 --set to 0 to eliminate lua log print outputs[/COLOR]

function CityWorkingResourceCheck(pCity, pPlayer, RequiredResource, RequiredImprovement)
	local iNumPlots = pCity:GetNumCityPlots()
	for i = 0, iNumPlots - 1 do
		local pPlot = pCity:GetCityIndexPlot(i)
		local iPlotIndex = pPlot:GetPlotIndex()	--does not look like I actually need this for anything
		local plotvisible = pPlot:IsVisible(pPlayer:GetTeam(), false)
		local plotowner = pPlot:GetOwner()
		[COLOR="green"]if gPrintStatements == 1 then print(plotvisible) end
		if gPrintStatements == 1 then print(plotowner) end[/COLOR]
		if plotowner ~= -1 then
			[COLOR="green"]if gPrintStatements == 1 then print(Players[plotowner]:GetCivilizationType()) end[/COLOR]
			if (pPlot:GetResourceType() ~= -1) and (pPlot:GetResourceType() == RequiredResource) then
				[COLOR="green"]if gPrintStatements == 1 then print("Resource ID# " .. tostring(RequiredResource) .. " is present on a plot") end[/COLOR]
				if pPlot:IsVisible(pPlayer:GetTeam(), false) and Players[plotowner]:GetCivilizationType() == pPlayer:GetCivilizationType() then
					[COLOR="green"]if gPrintStatements == 1 then print("The plot is visible to and owned by the correct player") end[/COLOR]
					if pPlot:GetImprovementType() > -1 then
						if not pPlot:IsImprovementPillaged() then
							[COLOR="green"]if gPrintStatements == 1 then print("The plot has an improvement and has not been pillaged") end[/COLOR]
							if pPlot:GetImprovementType() == RequiredImprovement then
								[COLOR="green"]if gPrintStatements == 1 then print("The plot has the correct improvement") end[/COLOR]
								if pPlot:IsBeingWorked() then
									if pPlot:GetWorkingCity() == pCity then
										[COLOR="green"]if gPrintStatements == 1 then print("The city of " .. tostring(pCity:GetName()) .. " was found TO BE WORKING an improved plot with the correct required resource") end[/COLOR]
										return true
									end
								end
							end
						end
					end
				end
			end
		else [COLOR="green"]if gPrintStatements == 1 then print("the plot is not owned") end[/COLOR]
		end
	end
	[COLOR="green"]if gPrintStatements == 1 then print("The city of " .. tostring(pCity:GetName()) .. " was found to NOT be working an improved plot with the correct required resource") end[/COLOR]
	return false
end
Here is the function where I am calling CityWorkingResourceCheck
Spoiler :
Code:
function WonderResources(iPlayer)
	local pPlayer = Players[iPlayer]
	if pPlayer:GetCivilizationType() == GameInfoTypes["CIVILIZATION_BARBARIAN"] then return end
	if not pPlayer:IsAlive() then return end
	if pPlayer:IsMinorCiv() then return end
	for pCity in pPlayer:Cities() do
		[COLOR="Red"]sCityProduction = Locale.ConvertTextKey(pCity:GetProductionNameKey())
		if CityProductionWonderNameCheck(gStoneWonders, sCityProduction) then
			if pCity:IsHasResourceLocal(GameInfoTypes.RESOURCE_STONE) then[/COLOR]
				if [COLOR="blue"]CityWorkingResourceCheck(pCity, pPlayer, GameInfoTypes.RESOURCE_STONE, GameInfoTypes.IMPROVEMENT_QUARRY)[/COLOR] then
					[COLOR="green"]if gPrintStatements == 1 then print("The city of " .. tostring(pCity:GetName()) .. " was found TO BE WORKING an improved plot with the correct required resource") end[/COLOR]
				else [COLOR="green"]if gPrintStatements == 1 then print("The city of " .. tostring(pCity:GetName()) .. " was found to NOT be working an improved plot with the correct required resource") end[/COLOR]
				end
			end
		end
	end
end
GameEvents.PlayerDoTurn.Add(WonderResources)

So far this appears to be working for me and is giving the correct true/false condition, but as yet I have not coded-in anything that is going to happen when CityWorkingResourceCheck returns true for an individual city except that I have two different print statements so I can "see" how the thing is functioning by looking at the lua log (or in live tuner). It should give you the correct true/false result for any combination of Resource/Improvement but remember that I am also requiring that the city also be working the plot in question.

The lines in red you don't really need to worry about, but what I am working on with those is to check what the city is producing, and if it is a wonder from a list I am interested in I am going to up the city's production rate if it has an improved stone resource that it is working. And I am thinking that the line with pCity:IsHasResourceLocal is probably overkill. Hopefully this will give you some idea of what else you need to do to get your lua to work.

[edit] I made the debug print statements stuff show highlighted in green based on DarkScythe's comment. Hope this will make it a little easier for others to see what is debugging "fluff" as opposed to the actual functioning stuff. :eek:
 
Another error with your code is that you're not properly looping through all of a city's plots.
Actually, your code doesn't loop over any plots. Your code is telling the game to check each city a player possesses.. then, even if 'plot' was defined, to only check that one plot the city is sitting on, which would mean your code would always resolve to false -- a city is not a pasture, even if you founded on top of Horses.

LeeS' code would probably work, although there are a ton of debugging statements in there, so it's a bit of a wall of text, but the main thing is the loop iterator defined at the top of his first function, which is what you're missing in order to actually check every plot around each city. After that, it's just nesting the appropriate conditions to check for.

I do the same thing in my mod, except I have far more complicated logic going on, checking for 4 unique resource/improvement types, and doing different things with each of them. It gets to be a nightmare if you use SetPlotExtraYield() like I did, though.
 
Thank you both for your replies and insight, it is really appreciated. I'm gonna work on it and post a solution which works A.S.A.P. Also, I'll not miss calling you out in the credits of my next mod ;-)
 
Good Evening Gentlemen!

I realized that "City:IsHasResourceLocal(Resource_id)" is what I needed, just as explained by LeeS in his other thread (#535999).
As a fact, I want to get a bonus from a specific improved resource by activating a dummy building in LUA. Here's what I got;

Spoiler :
Code:
function horse_pasture(playerID)
	local player = Players[playerID]
	if player:GetCivilizationType() == GameInfoTypes["CIVILIZATION_MYCIV"] then
		for city in player:Cities() do
			if city:IsHasResourceLocal(GameInfoTypes["RESOURCE_HORSE"]) then
				city:SetNumRealBuilding(GameInfoTypes["BUILDING_HORSE_PASTURE_FEATURE"], 1)
			end
		end
	end
end
GameEvents.PlayerDoTurn.Add(horse_pasture)

It did not work. Then, sobbing like someone just kicked my puppy, I realized a dummy forge-like building, activated by LUA, may work.
Here's what I tried;

Spoiler :
Code:
function horse_pasture(playerID)
	local player = Players[playerID]
	if player:GetCivilizationType() == GameInfoTypes["CIVILIZATION_MYCIV"] then
		for city in player:Cities() do
			city:SetNumRealBuilding(GameInfoTypes["BUILDING_HORSE_PASTURE_FEATURE"], 1)
		end
	end
end

Failed too.

Insight Solution, please?
 
You may wish to enable logging and check those logs to see if there are any mistakes you have made showing up there.

Otherwise, you may also want to attach your entire mod and/or provide the details of your dummy building.
 
You may wish to enable logging and check those logs to see if there are any mistakes you have made showing up there.

Otherwise, you may also want to attach your entire mod and/or provide the details of your dummy building.
2nd to both! See whoward69's zip your mods and attach tutorial for which version of the mod to zip and attach to a post. It is quite possible the error is in usage of ModBuddy rather than the lua code or XML code. Being able to look at the built version of your mod will allow us to see.

Also see these:
  1. whoward69's what ModBuddy setting for what file types tutorial
  2. whoward69's enable error logging tutorial

Errors with XML code of buildings and such will show in the DataBase.log file, and errors with the lua code will show in the Lua.log file.
 
Hello DarkScythe,

Thank you for bearing with me. I realized that my intention regarding the tile improvement would make the mod OP and should be part of a dedicated mod. Anyway, I kept looking for a solution and found a way to activate a dummy building in each city of a given civilization. All credits go to TarcisioCM as it comes from his mod about the "Parthians". For whom it may be interesting, see the code below;

Spoiler :
Code:
function TCM_Persian_Horses(playerID)
	local player = Players[playerID]
	if player:GetCivilizationType() == GameInfoTypes["CIVILIZATION_PARTHIA"] and player:IsAlive() then
		for city in player:Cities() do
			city:SetNumRealBuilding(GameInfoTypes["BUILDING_PARTHIA_FREEHORSES_1"], 0)
			if not city:IsCapital() then
				city:SetNumRealBuilding(GameInfoTypes["BUILDING_PARTHIA_FREEHORSES_1"], 1)
			end
		end
	end
end
GameEvents.PlayerDoTurn.Add(TCM_Persian_Horses)
That's all folks!

LeeS; I just saw your answer. Thank you as well for your time and I will do as recommended.
 
Back
Top Bottom