Allowing certain improvements to be built without chopping forest/jungle

Halpo

Chieftain
Joined
Feb 5, 2016
Messages
5
Hi. I'm new to modding with Lua, and I've written the following code in order to allow the Iroquois to build certain improvements on forests or jungles. It doesn't work.

Code:
function Forest(playerID) --allows construction of some improvements on forest
	local pPlayer = Players[playerID]
	local pCiv = pPlayer:GetCivilizationType()

	if pPlayer:IsEverAlive() and pCiv == GameInfoTypes.CIVILIZATION_IROQUOIS then
		for pCity in pPlayer:Cities() do
			for i = 0, pCity:GetNumCityPlots() - 1, 1 do
				local pPlot = pCity:GetCityIndexPlot(i)
				if pPlot ~= nil and pPlot:GetOwner() == pCity:GetOwner() then
					if pPlot:GetFeatureType() == GameInfoTypes.FEATURE_FOREST or pPlot:GetFeatureType() == GameInfoTypes.FEATURE_JUNGLE then
						pPlot:CanBuild(GameInfoTypes.IMPROVEMENT_PLANTATION, pPlayer, 1)
						if pPlot:GetTerrainType() ~= GameInfoTypes.TERRAIN_HILL then
							pPlot:CanBuild(GameInfoTypes.IMPROVEMENT_FARM, pPlayer, 1)
						end
						pPlot:CanBuild(GameInfoTypes.IMPROVEMENT_HOLY_SITE, pPlayer, 1)
						pPlot:CanBuild(GameInfoTypes.IMPROVEMENT_ACADEMY, pPlayer, 1)
						pPlot:CanBuild(GameInfoTypes.IMPROVEMENT_CUSTOMS_HOUSE, pPlayer, 1)
						pPlot:CanBuild(GameInfoTypes.IMPROVEMENT_LANDMARK, pPlayer, 1)
						local iX = pPlot:GetX()
						local iY = pPlot:GetY()
						if Teams[pPlayer:GetTeam()]:IsHasTech(GameInfoTypes.TECH_CIVIL_SERVICE) and pPlot:IsFreshWater() and pPlot:GetImprovementType() == GameInfoTypes.IMPROVEMENT_FARM then
							Game.SetPlotExtraYield(iX, iY, YieldTypes.YIELD_FOOD, -1)
						end
						if Teams[pPlayer:GetTeam()]:IsHasTech(GameInfoTypes.TECH_FERTILIZER) and not pPlot:IsFreshWater() and pPlot:GetImprovementType() == GameInfoTypes.IMPROVEMENT_FARM then
							Game.SetPlotExtraYield(iX, iY, YieldTypes.YIELD_FOOD, -1)
						end
					end
				end
			end
		end
	end
end
GameEvents.PlayerDoTurn.Add(Forest)

Any help is appreciated.
 
  1. You're mis-using Plot:CanBuild()
  2. The 1st argument wants a BUILD type and not an IMPROVEMENT type.
  3. However, even with the issue of using BUILD_HOLY_SITE or BUILD_PLANTATION fixed, you would still be misusing Plot:CanBuild() since it does not cause a player to be able to perform a BUILD mission on the plot. It merely answers the question "Can This Player Perform This BUILD on this Plot?" by returning true or false
  4. This is true of any method within the API which is Plot:CanComething(), Player:CanSomething(), Unit:CanSomething(), City:CanSomething(), etc.
 
I guess I have to use unique improvements, then. Is there a way to prevent the Iroquois from constructing the original improvements, so that the unique ones would effectively replace the originals?
 
Not without Whoward's DLL Modification or Community Patch, no.
 
You might be able to use a unique worker (but I've had varying degrees of success with the AI understanding those) and then remove the original build(s) from the unique worker and give them the civ specific replacements
 
Back
Top Bottom