I have tried absolutely everything but I cannot get this LUA code to, well, do anything. It prints "JFK LUA Loaded" and "JFK Ability Triggered" but nothing else happens or even prints. I know barely anything about LUA and am not a programmer so this is probably very bad, messy code but if anyone could help I'd appreciate it.
The goal of the code is to add a dummy building to all cities of the John F. Kennedy player provided that the John F. Kennedy player is at war with someone of a different government, and then to remove that building if John F. Kennedy is no longer at war with someone of a different government.
Here's the full code:
The goal of the code is to add a dummy building to all cities of the John F. Kennedy player provided that the John F. Kennedy player is at war with someone of a different government, and then to remove that building if John F. Kennedy is no longer at war with someone of a different government.
Here's the full code:
Code:
print ("JFK LUA Loaded")
-----------------------------------------------
-- FUNCTION C15_ValidTrait
-- Credits: Chrisy15
-----------------------------------------------
function C15_ValidTrait(sTrait)
local tValid = {}
for k, v in ipairs(PlayerManager.GetWasEverAliveIDs()) do
local leaderType = PlayerConfigurations[v]:GetLeaderTypeName()
for trait in GameInfo.LeaderTraits() do
if trait.LeaderType == leaderType and trait.TraitType == sTrait then
tValid[v] = true
end;
end
if not tValid[v] then
local civType = PlayerConfigurations[v]:GetCivilizationTypeName()
for trait in GameInfo.CivilizationTraits() do
if trait.CivilizationType == civType and trait.TraitType == sTrait then
tValid[v] = true
end;
end
end
end
return tValid
end
local iDummy = GameInfo.Buildings["BUILDING_JFK_DIFFGOVWAR_DUMMY"].Index;
local sTrait = "TRAIT_LEADER_SNACK_JFK"
local tTraitPlayers = C15_ValidTrait(sTrait)
function JFK_DiffGovCheck(pAttacker, pDefender)
print ("JFK Diff Gov Check starting");
if (Players[pAttacker]:GetCulture():GetCurrentGovernment() ~= Players[pDefender]:GetCulture():GetCurrentGovernment()) and (pAttacker:GetDiplomacy():IsAtWarWith(pDefender) or pAttacker:GetDiplomacy():IsAtWarWith(pDefender) ) then
print ("JFK Diff Gov Check succeeded")
return true;
else
return false;
end
end
function JFKDiffGovWarAbility( eAttackingPlayerID, eDefendingPlayerID )
print ("JFK Ability Triggered")
local pAttacker = Players[eAttackingPlayerID]
local pDefender = Players[eDefendingPlayerID]
local pCities = pAttacker:GetCities()
local pCity = pCities:FindID(cityID)
local pCities2 = pDefender:GetCities()
local pCity2 = pCities2:FindID(cityID)
local pCityBuildings = pCity:GetBuildings()
local pCity2Buildings = pCity2:GetBuildings()
if tTraitPlayers[pAttacker] then
print ("Attacker is JFK")
if JFK_DiffGovCheck(pAttacker, pDefender) == true then
print ("JFK Government + War Check is true")
if pCityBuildings:HasBuilding(iDummy) ~= true then
local pPlot = Map.GetPlot(pCity:GetX(), pCity:GetY())
--print("pPlot:", pPlot)
local iPlot = pPlot:GetIndex()
--print("iPlot:", iPlot)
pCity:GetBuildQueue():CreateIncompleteBuilding(iDummy, iPlot, 100)
print ("Dummy added")
end
end
else
if tTraitPlayers[pDefender] then
print ("Defender is JFK")
if JFK_DiffGovCheck(pAttacker, pDefender) == true then
print ("JFK Government + War Check is true and JFK is defender")
if pCity2Buildings:HasBuilding(iDummy) ~= true then
local pPlot = Map.GetPlot(pCity:GetX(), pCity:GetY())
--print("pPlot:", pPlot)
local iPlot = pPlot:GetIndex()
--print("iPlot:", iPlot)
pCity:GetBuildQueue():CreateIncompleteBuilding(iDummy, iPlot, 100)
print ("Dummy added")
end
else
if pCity2Buildings:HasBuilding(iDummy) == true then
--print("pPlot:", pPlot)
local pPlot = Map.GetPlot(pCity:GetX(), pCity:GetY())
local iPlot = pPlot:GetIndex()
--print("iPlot:", iPlot)
pCityBuildings:RemoveBuilding(iDummy)
print ("Dummy removed")
else
print ("All checks failed.")
end
end
end
end
end
Events.DiplomacyDeclareWar.Add(JFKDiffGovWarAbility)
Events.DiplomacyMakePeace.Add(JFKDiffGovWarAbility)