-- JOHNNY BRAVO UA
-- Author: LeeS
-- Edited by: Harkodos
--------------------------------------------------------------
local iBuildingForCompletedPolicyBranches = GameInfoTypes.BUILDING_AMERICA_POLICY_DUMMYBUILDING
local iRequiredCivilization = GameInfoTypes.CIVILIZATION_AMERICA
local tPolicyAndBranchCorrespondances = {}
--=======================================================================================================================
-- Utility Functions
--=======================================================================================================================
-- JFD_IsCivilisationActive
--------------------------------------------------------------
function JFD_IsCivilisationActive(civilisationID)
for iSlot = 0, GameDefines.MAX_MAJOR_CIVS-1, 1 do
local slotStatus = PreGame.GetSlotStatus(iSlot)
if (slotStatus == SlotStatus["SS_TAKEN"] or slotStatus == SlotStatus["SS_COMPUTER"]) then
if PreGame.GetCivilization(iSlot) == civilisationID then
return true
end
end
end
return false
end
function SearchThroughCitiesAndCorrectDummyBuildingPlacements(iPlayer, iBuilding, iNumBuildingsRequired, sMethod)
local pPlayer = Players[iPlayer]
if sMethod == nil then
print("!!!!!!!!!!!!!!!! sMethod was nil when SearchThroughCitiesAndCorrectDummyBuildingPlacements was called: cannot do anything with that !!!!!!!!!!!!!")
return
end
if pPlayer:GetNumCities() < 1 then
print("SearchThroughCitiesAndCorrectDummyBuildingPlacements was called but the player has no cities at the moment")
return
end
local pCapitalCity = pPlayer:GetCapitalCity()
local pOriginalCapital = "NONE"
if sMethod == "CapitalCity" then
if pCapitalCity ~= nil then
for pCity in pPlayer:Cities() do
if pCity:IsOriginalMajorCapital() and pCity:IsCapital() and (pCity:GetOriginalOwner() == iPlayer) then
pOriginalCapital = pCity
break
end
end
if pOriginalCapital ~= "NONE" then
pOriginalCapital:SetNumRealBuilding(iBuilding, iNumBuildingsRequired)
if pCapitalCity ~= pOriginalCapital then
pCapitalCity:SetNumRealBuilding(iBuilding, 0)
end
end
if pPlayer:CountNumBuildings(iBuilding) ~= iNumBuildingsRequired then
--something is still not correct, we need to fix it
for pCity in pPlayer:Cities() do
if (pOriginalCapital ~= "NONE") then
if (pCity == pOriginalCapital) then
pCity:SetNumRealBuilding(iBuilding, iNumBuildingsRequired)
else
pCity:SetNumRealBuilding(iBuilding, 0)
end
else
if pCity == pCapitalCity then
pCity:SetNumRealBuilding(iBuilding, iNumBuildingsRequired)
else
pCity:SetNumRealBuilding(iBuilding, 0)
end
end
end
end
else
print("SearchThroughCitiesAndCorrectDummyBuildingPlacements: we should not have been able to get here: sMethod == 'CapitalCity' and 'pCapitalCity == nil'")
return
end
elseif sMethod == "All" then
for pCity in pPlayer:Cities() do
pCity:SetNumRealBuilding(iBuilding, iNumBuildingsRequired)
end
end
end
--=======================================================================================================================
-- End of Utility Functions
--=======================================================================================================================
local bIsCivActive = JFD_IsCivilisationActive(iRequiredCivilization)
if bIsCivActive then
for Policy in DB.Query("SELECT ID, Type, PolicyBranchType FROM Policies WHERE Level = '0'") do
local iPolicyId, sPolicyType, sPolicyBranchName, iPolicyBranchID = Policy.ID, Policy.Type, Policy.PolicyBranchType, -1
if (sPolicyBranchName ~= nil) then
for PolicyBranch in GameInfo.PolicyBranchTypes("Type='" .. sPolicyBranchName .. "'") do
iPolicyBranchID = PolicyBranch.ID
end
end
if iPolicyBranchID ~= -1 then
if (GameInfo.PolicyBranchTypes[iPolicyBranchID].FreePolicy ~= sPolicyType) and (GameInfo.PolicyBranchTypes[iPolicyBranchID].FreeFinishingPolicy ~= sPolicyType) then
local bPolicyIsAPreReq = false
for row in GameInfo.Policy_PrereqPolicies("PrereqPolicy = '" .. sPolicyType .. "'") do
bPolicyIsAPreReq = true
break
end
if not bPolicyIsAPreReq then
tPolicyAndBranchCorrespondances[iPolicyId] = iPolicyBranchID
end
end
end
end
end
function PolicyBranchFinished(PlayerID, policyID)
if tPolicyAndBranchCorrespondances[policyID] then
local pPlayer = Players[PlayerID]
if pPlayer:GetCivilizationType() ~= iRequiredCivilization then
return
end
if pPlayer:IsPolicyBranchFinished(tPolicyAndBranchCorrespondances[policyID]) then
local pCapitalCity = pPlayer:GetCapitalCity()
if pCapitalCity ~= nil then
print("We Should have added " .. pPlayer:GetNumPolicyBranchesFinished() .. " Dummy Buildings in the city of " .. pCapitalCity:GetName())
pCapitalCity:SetNumRealBuilding(iBuildingForCompletedPolicyBranches, pPlayer:GetNumPolicyBranchesFinished())
end
end
end
end
function PlayerCapitalCaptured(iOldOwner, bIsCapital, iX, iY, iNewOwner, iPop, bConquest)
local pOldOwner = Players[iOldOwner]
if pOldOwner:GetCivilizationType() ~= iRequiredCivilization then
return
end
if pOldOwner:CountNumBuildings(iBuildingForCompletedPolicyBranches) ~= pOldOwner:GetNumPolicyBranchesFinished() then
SearchThroughCitiesAndCorrectDummyBuildingPlacements(iOldOwner, iBuildingForCompletedPolicyBranches, pOldOwner:GetNumPolicyBranchesFinished(), "CapitalCity")
end
-- ensuring that the new owner has none of the dummy buildings, even by some code-accident, from capturing the city
local pCity = Map.GetPlot(iX, iY):GetPlotCity();
pCity:SetNumRealBuilding(iBuildingForCompletedPolicyBranches, 0)
end
function PlayerDoTurn(iPlayer)
local pPlayer = Players[iPlayer]
if pPlayer:GetCivilizationType() ~= iRequiredCivilization then
return
end
local pCapitalCity = pPlayer:GetCapitalCity()
if pCapitalCity ~= nil then
if pCapitalCity:GetNumRealBuilding(iBuildingForCompletedPolicyBranches) ~= pPlayer:GetNumPolicyBranchesFinished() then
SearchThroughCitiesAndCorrectDummyBuildingPlacements(iPlayer, iBuildingForCompletedPolicyBranches, pPlayer:GetNumPolicyBranchesFinished(), "CapitalCity")
end
end
end
function CityFounded(iPlayer, iCityX, iCityY)
local pPlayer = Players[iPlayer]
if pPlayer:GetCivilizationType() ~= iRequiredCivilization then
return
end
if pPlayer:CountNumBuildings(iBuildingForCompletedPolicyBranches) ~= pPlayer:GetNumPolicyBranchesFinished() then
SearchThroughCitiesAndCorrectDummyBuildingPlacements(iPlayer, iBuildingForCompletedPolicyBranches, pPlayer:GetNumPolicyBranchesFinished(), "CapitalCity")
end
end
if bIsCivActive then
GameEvents.PlayerAdoptPolicy.Add(PolicyBranchFinished); -- theoretically we don't need this anymore but it does make the effect instant on policy branch completion
-- instead of waiting for the next player turn processing
GameEvents.PlayerDoTurn.Add(PlayerDoTurn)
GameEvents.PlayerCityFounded.Add(CityFounded)
GameEvents.CityCaptureComplete.Add(PlayerCapitalCaptured)
end
print("JOHNNY BRAVO UA script loaded all the way to the end and did not barf")