Karatekid5
Warlord
Making another silly addition for my AI games and decided to do something simple. This is intended to be an Classical Era unit that deletes all of a player's cities but takes it a step further and flips their capital to the barbarians. I found the correct routine and am running it within the PlayerKermited function in hopes that variables from it will carry, as the thread I found the city flip code in mentioned. It works for the most part but the code never finishes, giving me an Instance Does Not Exist error on Line 12 in the ChangeCityOwner function. I have iBarbarianCiv as an argument and it should be plugging into newOwner, but I assume that the player ID for the Barbarians isn't being grabbed it seems.
Thanks in advance to anyone who can help!
Thanks in advance to anyone who can help!
Code:
include("FLuaVector.lua")
print("Stay woke!")
local iUnitKermit = GameInfoTypes.UNIT_KERMIT
local iPalace = GameInfoTypes.BUILDING_PALACE
local iBarbarianCiv = GameInfoTypes.CIVILIZATION_BARBARIAN
function ChangeCityOwner(oldOwner, cityID, newOwner)
local pPlayer = Players[newOwner];
local pCity = cityID
pPlayer:AcquireCity(pCity, true, false);
end
function PlayerKermited(iPlayer,iUnit,_,iPlotX, iPlotY, bDelay)
local pPlayer = Players[iPlayer]
local pPlot = Map.GetPlot(iPlotX, iPlotY)
local pUnit = pPlayer:GetUnitByID(iUnit);
local iOriginalPlotOwner = pPlot:GetOwner()
if (iOriginalPlotOwner ~= -1) and pUnit:GetUnitType()==iUnitKermit then -- (iOriginalPlotOwner ~= iPlayer) then
local pPlotOwnerPlayer = Players[iOriginalPlotOwner]
if bDelay == true then
Events.AudioPlay2DSound("AS2D_KERMIT_MUSIC")
end
for city in pPlotOwnerPlayer:Cities() do
if city:IsCapital() == false and city:IsOriginalMajorCapital() == false and city:IsHolyCityAnyReligion() == false then
local plot = city:Plot()
local hexpos = ToHexFromGrid(Vector2(plot:GetX(), plot:GetY()));
local cityID = city:GetID()
city:SetPopulation(0)
city:Kill()
print("Kermited!")
Events.SerialEventCityDestroyed(hexpos, iOriginalPlotOwner, cityID, -1)
else
city:SetPopulation(1)
local cityID = city:GetID()
ChangeCityOwner(pPlotOwnerPlayer, cityID, iBarbarianCiv)
end
end
end
end
GameEvents.UnitPrekill.Add(PlayerKermited);