function GetPlayer ()
local iPlayerID = Game.GetActivePlayer();
if (iPlayerID < 0) then
print("Error - player index not correct");
return nil;
end
return Players[iPlayerID];
end
function isValidPlayer(pPlayer)
return pPlayer ~= nil and pPlayer:IsAlive();
end
function CheckSetup()
if PlayerData == nil then
PlayerData = {};
for iPlayerLoop = 0, GameDefines.MAX_MAJOR_CIVS-1, 1 do
local pPlayer = Players[ iPlayerLoop ];
if isValidPlayer(pPlayer) then
PlayerData[iPlayerLoop] = {};
PlayerData[iPlayerLoop].buildingData = {};
SetupBuildingData(iPlayerLoop);
end
end
end
end
function SetupCityData(iPlayerLoop, iCityLoop)
print ("Setting initial city data for ISEventSystem");
local pPlayer = Players[iPlayerLoop];
local pCity = pPlayer:GetCityByID(iCityLoop);
PlayerData[iPlayerLoop].citydata[iCityLoop] = {};
PlayerData[iPlayerLoop].citydata[iCityLoop].buildingdata = {};
PlayerData[iPlayerLoop].citydata[iCityLoop].population = pCity:GetPopulation();
for b in GameInfo.Buildings() do
PlayerData[iPlayerLoop].citydata[iCityLoop].buildingdata[b.ID] = pCity:IsHasBuilding(b.ID);
if PlayerData[iPlayerLoop].citydata[iCityLoop].buildingdata[b.ID]==true then
LuaEvents:ISBuildingCreated(pCity, b);
end
end
end
PlayerData = nil;
function SetupBuildingData(iPlayerLoop)
print ("Setting initial building data for ISEventSystem");
PlayerData[iPlayerLoop].citydata = {};
PlayerData[iPlayerLoop].buildingHas= {};
local pPlayer = Players[iPlayerLoop];
for building in GameInfo.Buildings() do
PlayerData[iPlayerLoop].buildingHas[building.ID] = pPlayer:CountNumBuildings(building.ID);
end
for pCity in pPlayer:Cities() do
if PlayerData[iPlayerLoop].citydata == nil then
PlayerData[iPlayerLoop].citydata = {};
end
if PlayerData[iPlayerLoop].citydata[pCity:GetID()] == nil then
SetupCityData(iPlayerLoop, pCity:GetID());
end
end
end
--**************************************************************************
-- Internal handling of city being destroyed. Raises ISCityDestroyed event.
--**************************************************************************
function OnCityInternalDestroyed(hexPos, playerID, cityID, newPlayerID)
local pPlayer = Players[playerID];
local pOtherPlayer = Players[newPlayerID];
for i, j in pairs(PlayerData[playerID].citydata[cityID].buildingdata) do
if j==true then
LuaEvents:ISBuildingLost(pPlayer, pPlayer:GetCityByID(cityID), b);
PlayerData[playerID].citydata[cityID].buildingdata[i] = false;
end
end
LuaEvents.ISCityDestroyed(hexPos, pPlayer, pOtherPlayer, pPlayer:GetCityByID(cityID));
end
Events.SerialEventCityDestroyed.Add( OnCityInternalDestroyed );
--**************************************************************************
-- Internal handling of city being captured. Raises ISCityCaptured event.
--**************************************************************************
function OnCityInternalCaptured (hexPos, playerID, cityID, newPlayerID)
local pPlayer = Players[playerID];
local pOtherPlayer = Players[newPlayerID];
LuaEvents.ISCityCaptured(hexPos, pPlayer, pOtherPlayer, pPlayer:GetCityByID(cityID));
for i, j in pairs(PlayerData[playerID].citydata[cityID].buildingdata) do
if j==true then
LuaEvents:ISBuildingLost(pPlayer, pPlayer:GetCityByID(cityID), b);
LuaEvents:ISBuildingAcquired(pOtherPlayer, pOtherPlayer:GetCityByID(cityID), b);
PlayerData[playerID].citydata[cityID].buildingdata[i] = false;
end
end
end
Events.SerialEventCityCaptured.Add( OnCityInternalCaptured );
--**************************************************************************
-- Internal handling of city being created. Raises ISCityCaptured event.
--**************************************************************************
function OnInternalCityCreated ( hexPos, playerID, cityID, cultureType, eraType, culture, population, size, fowState )
-- make the first city a pop 2 city
local pPlayer = Players[playerID];
LuaEvents.ISCityCreated(hexPos, pPlayer, pPlayer:GetCityByID(cityID));
end
Events.SerialEventCityCreated.Add( OnInternalCityCreated );
--**************************************************************************
-- Updates the building data compiled to determine when buildings are
-- built / destroyed.
--**************************************************************************
function UpdatePlayerData(iPlayerLoop)
local pPlayer = Players[iPlayerLoop];
for pCity in pPlayer:Cities() do
if PlayerData[iPlayerLoop].citydata == nil then
PlayerData[iPlayerLoop].citydata = {};
end
if PlayerData[iPlayerLoop].citydata[pCity:GetID()] == nil then
SetupCityData(iPlayerLoop, pCity:GetID());
end
if PlayerData[iPlayerLoop].citydata[pCity:GetID()].population ~= pCity:GetPopulation() then
LuaEvents.ISCityPopulationChanged(pPlayer, pCity, pCity:GetPopulation(), PlayerData[iPlayerLoop].citydata[pCity:GetID()].population );
PlayerData[iPlayerLoop].citydata[pCity:GetID()].population = pCity:GetPopulation();
end
for b in GameInfo.Buildings() do
if b ~= nil then
local newNum = pPlayer:CountNumBuildings(b.ID);
if b.ID == nil then
else
local had = PlayerData[iPlayerLoop].citydata[pCity:GetID()].buildingdata[b.ID];
local got = pCity:IsHasBuilding(b.ID);
if had ~= got then
if got then
LuaEvents:ISBuildingAcquired(pPlayer, pCity, b);
else
LuaEvents:ISBuildingLost(pPlayer,pCity, b);
end
end
PlayerData[iPlayerLoop].citydata[pCity:GetID()].buildingdata[b.ID] = pCity:IsHasBuilding(b.ID);
end
PlayerData[iPlayerLoop].buildingHas[b.ID] = pPlayer:CountNumBuildings(b.ID);
PlayerData[iPlayerLoop].buildingData[b.ID] = bHas;
end
end
--end
end
end
--**************************************************************************
-- Happens once every 60 frames.
--**************************************************************************
function Tick()
end
local iTimer = 0;
local iTimerMax = 60;
--**************************************************************************
-- Runs constantly during player's turn.
--**************************************************************************
function HeartbeatChecker()
if (Game.IsGameMultiPlayer()) then
return;
end
if (Game.IsPaused()) then
return;
end
local player = GetPlayer();
if (player == nil) then
return;
end
if (UI.IsPopupUp()) then
return;
end
iTimer = iTimer + 1;
if iTimer > iTimerMax then
iTimer = 0;
Tick();
end
end
Events.LocalMachineAppUpdate.Add( HeartbeatChecker );
--**************************************************************************
-- Internal handling of active player start. Used to simulate events for
-- AI players.
--**************************************************************************
function doInternalActivePlayerTurnStart()
CheckSetup();
UpdatePlayerData(Game.GetActivePlayer());
for iPlayerLoop = 0, GameDefines.MAX_MAJOR_CIVS-1, 1 do
local pPlayer = Players[ iPlayerLoop ];
if isValidPlayer(pPlayer) then
if iPlayerLoop ~= Game.GetActivePlayer() then
if Game.GetGameTurn() > 0 then
LuaEvents.ISPlayerTurnEnd(pPlayer);
end
end
end
end
LuaEvents.ISPlayerTurnStart(Players[Game.GetActivePlayer()]);
end
Events.ActivePlayerTurnStart.Add(doInternalActivePlayerTurnStart);
--**************************************************************************
-- Internal handling of active player end. Used to simulate events for
-- AI players.
--**************************************************************************
function doInternalActivePlayerTurnEnd()
LuaEvents.ISPlayerTurnEnd(Players[Game.GetActivePlayer()]);
for iPlayerLoop = 0, GameDefines.MAX_MAJOR_CIVS-1, 1 do
local pPlayer = Players[ iPlayerLoop ];
if isValidPlayer(pPlayer) then
if iPlayerLoop ~= Game.GetActivePlayer() then
UpdatePlayerData(iPlayerLoop);
LuaEvents.ISPlayerTurnStart(pPlayer);
end
end
end
end
Events.ActivePlayerTurnEnd.Add(doInternalActivePlayerTurnEnd);