Craig_Sutter
Deity
I've been trying to get a building built via lua if an improved resource is in the city hexes. The function is working, but only sometimes. In testing, some civilizations are getting buildings as expected, however, my test civ only rarely gets a building when I fulfill the criteria (having a resource of the appropriate type improved in city owned hexes.
I think it is something I've done wrong with looping through the player or cities such that the function is stopping before going through all of the players. I could set up a separate function for each resource, but I feel that is not the most efficient way to write the code.
What am I missing?
I think it is something I've done wrong with looping through the player or cities such that the function is stopping before going through all of the players. I could set up a separate function for each resource, but I feel that is not the most efficient way to write the code.
What am I missing?
Spoiler :
Code:
-- for player civ , loop through the players
-- for loop through cities to see eligible for Merchant House via improved resource and place if eligible
function MerchantHouseSpawn()
-- find eligible cities
for PlayerID=0, GameDefines.MAX_CIV_PLAYERS - 1 do
local pPlayer = Players[PlayerID];
Player = pPlayer
if Player:IsEverAlive() then
-- Enumerate cities
for pCity in Player:Cities() do
-- City exists and and has improved resource... build Mechant House if eligible
MerchantHouse = pCity:GetNumRealBuilding(GameInfoTypes["BUILDING_MERCHANT_HOUSE"])
if pCity ~= nil and MerchantHouse == 0 then
if pCity:IsHasResourceLocal(GameInfoTypes["RESOURCE_COTTON"]) or
pCity:IsHasResourceLocal(GameInfoTypes["RESOURCE_IRON"]) or
pCity:IsHasResourceLocal(GameInfoTypes["RESOURCE_INCENSE"]) or
pCity:IsHasResourceLocal(GameInfoTypes["RESOURCE_COPPER"]) then
pCity:SetNumRealBuilding(GameInfoTypes["BUILDING_MERCHANT_HOUSE"], 1);
print(Player:GetName(), "...is spawning Merchant House in...", pCity:GetName())
end
end
end
end
end
end
--to set changes
local g_lastTurn = -1
local function OnPlayerDoTurn(iPlayer)
local gameTurn = Game.GetGameTurn()
if g_lastTurn < gameTurn then
g_lastTurn = gameTurn
--per game turn function here
MerchantHouseSpawn()
end
end
GameEvents.PlayerDoTurn.Add(OnPlayerDoTurn)