function HexCREndTurn(iPlayer)
print ("Hex CR razing check started")
local pPlayer = Players[iPlayer]
for city in pPlayer:Cities() do
local pCity = city
local pPlot = pCity:Plot()
if pCity ~= nil and (pCity:IsRazing()) and (pCity:GetPopulation() == 1) then
print ("Hex CR: city being razed has reached one pop")
local pCityOwner = Players[pCity:GetOwner()]
local pOwnerTeam = Teams[pCityOwner:GetTeam()]
local iCityOriginalOwner = pCity:GetOriginalOwner()
for i = 0, pCity:GetNumCityPlots()-1, 1 do
local lPlot = pCity:GetCityIndexPlot(i)
local ourCityAvailable = false
local teamAvailable = false
local allyAvailable = false
local friendAvailable = false
local noWarAvailable = false
local dPactAvailable = false
if (lPlot ~= nil) and ( (lPlot:GetOwner()) == (pCity:GetOwner()) ) then
local CityCandidates = {}
for pID = 0, GameDefines.MAX_MAJOR_CIVS-1, 1 do
local nearCity, dist = getNearestCityWithException( pID, lPlot, pCity)
if (nearCity ~= nil) and (dist < 4) then
local us = (Players[pID] == pCityOwner)
local sameteam = (not us) and (Teams[Players[pID]:GetTeam()] == pOwnerTeam)
local friends = (not us) and (pCityOwner:IsDoF(pID))
local allies = (not us) and (pCityOwner:IsAllies(pID))
local war = (not us) and (pOwnerTeam:IsAtWar(Players[pID]:GetTeam()))
local dpact = (not us) and (pOwnerTeam:IsDefensivePact(Players[pID]:GetTeam()))
if us then
ourCityAvailable = true
end
if sameteam then
teamAvailable = true
end
if friends then
friendAvailable = true
end
if allies then
allyAvailable = true
end
if not war then
noWarAvailable = true
end
if dpact then
dPactAvailable = true
end
table.insert(CityCandidates, {iPlayer = pID, bUs = us, bTeam = sameteam, bFriends = friends, bAllies = allies, bWar = war, bDpact = dpact, cNearCity = nearCity})
end
end
if #CityCandidates > 0 then
if ourCityAvailable then
for i = 1, #CityCandidates, 1 do
if CityCandidates[i] ~= nil then
local lUs = CityCandidates[i].bUs
if not lUs then
table.remove(CityCandidates, i)
i = i - 1
end
end
end
elseif teamAvailable then
for i = 1, #CityCandidates, 1 do
if CityCandidates[i] ~= nil then
local lTeam = CityCandidates[i].bTeam
if not lTeam then
table.remove(CityCandidates, i)
i = i - 1
end
end
end
elseif dPactAvailable then
for i = 1, #CityCandidates, 1 do
if CityCandidates[i] ~= nil then
local lDpact = CityCandidates[i].bDpact
if not lDpact then
table.remove(CityCandidates, i)
i = i - 1
end
end
end
elseif allyAvailable then
for i = 1, #CityCandidates, 1 do
if CityCandidates[i] ~= nil then
local lAlly = CityCandidates[i].bAllies
if not lAlly then
table.remove(CityCandidates, i)
i = i - 1
end
end
end
elseif friendAvailable then
for i = 1, #CityCandidates, 1 do
if CityCandidates[i] ~= nil then
local lFriend = CityCandidates[i].bFriends
if not lFriend then
table.remove(CityCandidates, i)
i = i - 1
end
end
end
elseif noWarAvailable then
for i = 1, #CityCandidates, 1 do
if CityCandidates[i] ~= nil then
local lNoWar = not CityCandidates[i].bWar
if not lNoWar then
table.remove(CityCandidates, i)
i = i - 1
end
end
end
end
local randPlayer = math.random(#CityCandidates)
local iAssignPlayer = CityCandidates[randPlayer].iPlayer
local AssignCity = CityCandidates[randPlayer].cNearCity
if AssignCity == nil then return end -- city not found?
if Map.PlotDistance( AssignCity:GetX(), AssignCity:GetY(), lPlot:GetX(), lPlot:GetY() ) < 4 then
print("reassigning plot belonging to nearly razed city")
if ourCityAvailable then
lPlot:SetOwner(-1, -1, false, false)
end
lPlot:SetOwner( iAssignPlayer, AssignCity, true, true )
end
end
end
end
end
end
end
GameEvents.PlayerEndTurnCompleted.Add(HexCREndTurn)