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 toset up steal a bit of code that will make sure each city a player has contains certain buildings if he has certain techs:
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?
Anyway, I've managed to
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)
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