-- Queens_Estate
-- Author: LeeS
-- DateCreated: 8/14/2014 6:00:43 AM
--------------------------------------------------------------
include("TableSaverLoader.lua")
gNumberCSAllies = 0
gNumberCSFriends = 0
gNumberOfBuildings = 0
gRequiredCivName = "CIVILIZATION_AMERICA"
gBuildingForCode = GameInfoTypes.BUILDING_QUEENS_ESTATE
gAllyHappyDummy = GameInfoTypes.BUILDING_CS_ALLY_HAPPINESS
gFriendHappyDummy = GameInfoTypes.BUILDING_CS_FRIEND_HAPPINESS
--print("The Required Civ Name is " .. tostring(gRequiredCivName))
gT = {}
gT.gNumberCSAllies = 0
gT.gNumberCSFriends = 0
TableName = "CSAllies"
-------------------------------------------------------------------------------------------------
--TableSaverLoader Hookups
-------------------------------------------------------------------------------------------------
function OnModLoaded() --called from end of last mod file to load
local bNewGame = not TableLoad(gT, TableName)
if bNewGame then
print("New Game")
else print("Loaded from Saved Game")
gNumberCSAllies = gT.gNumberCSAllies
gNumberCSFriends = gT.gNumberCSFriends
--print("While game loading: " .. tostring(gRequiredCivName) .. " has the following number of City-State Allies: " .. tostring(gNumberCSAllies))
--print("While game loading: " .. tostring(gRequiredCivName) .. " has the following number of City-State Friends: " .. tostring(gNumberCSFriends))
end
TableSave(gT, TableName)
end
function OnEnterGame() --Runs when Begin or Continue Your Journey pressed
print("Player entering game ...")
ContextPtr:LookUpControl("/InGame/GameMenu/SaveGameButton"):RegisterCallback(Mouse.eLClick, SaveGameIntercept)
ContextPtr:LookUpControl("/InGame/GameMenu/QuickSaveButton"):RegisterCallback(Mouse.eLClick, QuickSaveIntercept)
end
Events.LoadScreenClose.Add(OnEnterGame)
function SaveGameIntercept() --overrides Civ5 code when player presses Save Game from Game Menu or Cntr-s
TableSave(gT, TableName)
UIManager:QueuePopup(ContextPtr:LookUpControl("/InGame/GameMenu/SaveMenu"), PopupPriority.SaveMenu)
end
function QuickSaveIntercept() --overrides Civ5 code when player presses Quick Save from Game Menu or F11
TableSave(gT, TableName)
UI.QuickSave()
end
local autoSaveFreq = OptionsManager.GetTurnsBetweenAutosave_Cached()
function OnGameOptionsChanged()
autoSaveFreq = OptionsManager.GetTurnsBetweenAutosave_Cached()
end
Events.GameOptionsChanged.Add(OnGameOptionsChanged)
function OnAIProcessingEndedForPlayer(iPlayer)
if iPlayer == 63 then --runs on barb turn AFTER barb unit moves (very close to the regular autosave)
if Game.GetGameTurn() % autoSaveFreq == 0 then --only need to do on autosave turns
TableSave(gT, TableName)
end
end
end
Events.AIProcessingEndedForPlayer.Add(OnAIProcessingEndedForPlayer)
function InputHandler(uiMsg, wParam, lParam)
if uiMsg == KeyEvents.KeyDown then
if wParam == Keys.VK_F11 then
QuickSaveIntercept() --F11 Quicksave
return true
elseif wParam == Keys.S and UIManager:GetControl() then
SaveGameIntercept() --ctrl-s
return true
end
end
end
ContextPtr:SetInputHandler(InputHandler)
-------------------------------------------------------------------------------------------------
--Calculate during turn processing the number of City State Allies and Friends
--this updates the number of city state allies during game play
-------------------------------------------------------------------------------------------------
function QueensEstateData(iPlayer)
local pPlayer = Players[iPlayer]
if pPlayer:GetCivilizationType() == GameInfoTypes[gRequiredCivName] then
local pCity = pPlayer:GetCapitalCity();
--print(tostring(gRequiredCivName) .. " is as iPlayer number " .. tostring(iPlayer))
gNumberCSAllies = 0
gNumberCSFriends = 0
iCivPlayerNumber = iPlayer
for i = 0, GameDefines.MAX_PLAYERS - 1 do
if i ~= iCivPlayerNumber then
if Players[i]:IsMinorCiv() then
local iFriend = Players[i]:GetMinorCivFriendshipLevelWithMajor(iCivPlayerNumber)
--print("results from iFriend = Players[i]:GetMinorCivFriendshipLevelWithMajor(iCivPlayerNumber) is " .. tostring(iFriend))
if iFriend == 1 then
--print("Is a Friend")
gNumberCSFriends = gNumberCSFriends + 1
end
if iFriend == 2 then
--print("Is an Ally")
gNumberCSAllies = gNumberCSAllies + 1
end
end
end
end
--print(tostring(gRequiredCivName) .. " has the following number of City-State Friends: " .. tostring(gNumberCSFriends))
--print(tostring(gRequiredCivName) .. " has the following number of City-State Allies: " .. tostring(gNumberCSAllies))
-------------------------------------------------------------------------
--Ally and Friend happiness dummy in capital commands here
-------------------------------------------------------------------------
iNumCapitalAllyHappyBuildings = pCity:GetNumRealBuilding(gAllyHappyDummy)
iNumCapitalFriendHappyBuildings = pCity:GetNumRealBuilding(gFriendHappyDummy)
if iNumCapitalAllyHappyBuildings ~= gNumberCSAllies then
pCity:SetNumRealBuilding(gAllyHappyDummy, gNumberCSAllies);
end
if iNumCapitalFriendHappyBuildings ~= gNumberCSFriends then
pCity:SetNumRealBuilding(gFriendHappyDummy, gNumberCSFriends);
end
gT.gNumberCSAllies = gNumberCSAllies
gT.gNumberCSFriends = gNumberCSFriends
end
end
GameEvents.PlayerDoTurn.Add(QueensEstateData)
-------------------------------------------------------------------------------------------------
--Calculate the number of Queens Estate built or building
--this updates the number of buildings during game play
--disables building more than the number allowed by City-State Allies
-------------------------------------------------------------------------------------------------
function LeesBuildingConstructionOverride(iPlayer, iCity, iBuilding)
local pPlayer = Players[iPlayer]
local result = true
local pCity = pPlayer:GetCityByID(iCity)
if iBuilding == gBuildingForCode then
if pPlayer:GetCivilizationType() ~= GameInfoTypes[gRequiredCivName] then
result = false
end
if pPlayer:GetCivilizationType() == GameInfoTypes[gRequiredCivName] then
local sCityName = pCity:GetName()
gNumberOfBuildings = 0
for pCity in pPlayer:Cities() do
if pCity:IsHasBuilding(iBuilding) then
gNumberOfBuildings = gNumberOfBuildings + 1
else if pCity:GetProductionBuilding() == iBuilding then
gNumberOfBuildings = gNumberOfBuildings + 1
end
end
end
print("Number of Buildings made or making is " .. tostring(gNumberOfBuildings))
if gNumberCSAllies <= gNumberOfBuildings then
print("LeesBuildingConstructionOverride: disabling Queen's Estate construction in the city of " .. sCityName)
result = false
else
print("LeesBuildingConstructionOverride: enabling Queen's Estate construction in the city of " .. sCityName)
end
end
end
return result
end
GameEvents.CityCanConstruct.Add(LeesBuildingConstructionOverride)
OnModLoaded()