Lua Modding: Making Buildings Appear and Disappear

Anondod

Chieftain
Joined
Apr 26, 2009
Messages
74
So I'm working on my first mod using lua, and while I'm an old hand at coding this is a new language for me so I'm still a bit unsure on how things work.

Anyway, I've managed to set up steal a bit of code that will make sure each city a player has contains certain buildings if he has certain techs:
Spoiler :
Code:
function OnTeamTechResearched(teamID, techID, change)

	if (techID == GameInfoTypes.TECH_GATHERING) then
		for iPlayer = 0, GameDefines.MAX_MAJOR_CIVS - 1, 1 do 
			if (Players[iPlayer] ~= nil) then
				local player = Players[iPlayer]

				if (player:GetTeam() == teamID) then
					for city in player:Cities() do
						city:SetNumRealBuilding(GameInfoTypes.BUILDING_YIELD_REGULATOR_GATHERING, 1)
					end
				end
			end
		end
	end
	if (techID == GameInfoTypes.TECH_HUNTING) then
		for iPlayer = 0, GameDefines.MAX_MAJOR_CIVS - 1, 1 do 
			if (Players[iPlayer] ~= nil) then
				local player = Players[iPlayer]

				if (player:GetTeam() == teamID) then
					for city in player:Cities() do
						city:SetNumRealBuilding(GameInfoTypes.BUILDING_YIELD_REGULATOR_HUNTING, 1)
					end
				end
			end
		end
	end
end

GameEvents.TeamTechResearched.Add(OnTeamTechResearched)

function OnCityFounded(playerID, cityX, cityY)

	local player = Players[playerID]
	local teamID = player:GetTeam()
	local team = Teams[teamID]
	local plot = Map:GetPlot(cityX, cityY)
	local city = plot:GetPlotCity()
	
	if (team:HasTech(GameInfoTypes.TECH_GATHERING)) then
		city:SetNumRealBuilding(GameInfoTypes.BUILDING_YIELD_REGULATOR_GATHERING, 1)
	end
	if (team:HasTech(GameInfoTypes.TECH_HUNTING)) then
		city:SetNumRealBuilding(GameInfoTypes.BUILDING_YIELD_REGULATOR_HUNTING, 1)
	end
end

GameEvents.PlayerCityFounded.Add(OnCityFounded)

function OnCityCaptured(oldOwnerID, isCapital, cityX, cityY, newOwnerID, population, bConquest)

	local player = Players[newOwnerID]
	local plot = Map:GetPlot(cityX, cityY)
	local city = plot:GetPlotCity()
	local team = Teams[player:GetTeam()]
	
	if (team:HasTech(GameInfoTypes.TECH_GATHERING)) then
		city:SetNumRealBuilding(GameInfoTypes.BUILDING_YIELD_REGULATOR_GATHERING, 1)
	end
	if (team:HasTech(GameInfoTypes.TECH_HUNTING)) then
		city:SetNumRealBuilding(GameInfoTypes.BUILDING_YIELD_REGULATOR_HUNTING, 1)
	end
end

GameEvents.CityCaptureComplete.Add(OnCityCaptured)
I'm not going to pretend I understand all the details of this (see: stolen code), but it seems to work like I want it to, at least the first bit: when the player discovers Gathering his cities will all get the YIELD_REGULATOR_GATHERING building, and similarly for Hunting. The other bits, which I haven't tested yet, are supposed to make sure the same thing happens when a city is founded after the tech is discovered and when a city is conquered, respectively.

However, now I need to make sure that when a city is conquered by a civ that doesn't have the tech the buildings are removed. Would the following change achieve that?

Spoiler :
Code:
function OnCityCaptured(oldOwnerID, isCapital, cityX, cityY, newOwnerID, population, bConquest)

	local player = Players[newOwnerID]
	local plot = Map:GetPlot(cityX, cityY)
	local city = plot:GetPlotCity()
	local team = Teams[player:GetTeam()]
	
	if (team:HasTech(GameInfoTypes.TECH_GATHERING)) then
		city:SetNumRealBuilding(GameInfoTypes.BUILDING_YIELD_REGULATOR_GATHERING, 1)
		[COLOR="Red"]else
		city:SetNumRealBuilding(GameInfoTypes.BUILDING_YIELD_REGULATOR_GATHERING, 0)[/COLOR]
	end
	if (team:HasTech(GameInfoTypes.TECH_HUNTING)) then
		city:SetNumRealBuilding(GameInfoTypes.BUILDING_YIELD_REGULATOR_HUNTING, 1)
		[COLOR="Red"]else
		city:SetNumRealBuilding(GameInfoTypes.BUILDING_YIELD_REGULATOR_HUNTING, 0)[/COLOR]
	end
end
 
Okay, so apparently the indentation got messed up when I copy-pasted that. I'll see if I can fix it.
 
Okay, so apparently the indentation got messed up when I copy-pasted that. I'll see if I can fix it.

Use [ CODE ] ... [ /CODE ] tags (without the spaces around the square brackets)
 
Yes, that should work. But you don't need any Lua at all to make a building so it can't be captured. Just set NeverCapture=true for the building (xml or sql mod).

(Very small aesthetic point: Your indenting is wrong. "else" should be at the same indent level as "if" and "end", since it is enclosing the blocks of Lua that come before and after it.)
 
Yes, that should work. But you don't need any Lua at all to make a building so it can't be captured. Just set NeverCapture=true for the building (xml or sql mod).
Right! That sounds like a better way to do it.
(Very small aesthetic point: Your indenting is wrong. "else" should be at the same indent level as "if" and "end", since it is enclosing the blocks of Lua that come before and after it.)
Thanks! I tend to be picky about that myself, but here I just went with where the ModBuddy editor put them. :)
 
Back
Top Bottom