--~~~Referenced from LeeS, CivFanatics.com
local iCityWorkRange = 3
function GetCityPlots(pCity)
local tTempTable = {}
if pCity ~= nil then
local iCityOwner = pCity:GetOwner()
local iCityX, iCityY = pCity:GetX(), pCity:GetY()
for dx = (iCityWorkRange * -1), iCityWorkRange do
for dy = (iCityWorkRange * -1), iCityWorkRange do
local pPlotNearCity = Map.GetPlotXYWithRangeCheck(iCityX, iCityY, dx, dy, (iCityWorkRange + 1));
if pPlotNearCity and (pPlotNearCity:GetOwner() == iCityOwner) and (pCity == Cities.GetPlotPurchaseCity(pPlotNearCity:GetIndex())) then
table.insert(tTempTable, pPlotNearCity)
end
end
end
end
return tTempTable
end
--~~End of Reference
function AttemptToPlotUnit(UnitIndex, pPlayer)
local pCities = pPlayer:GetCities()
local pUnits = pPlayer:GetUnits()
local pCity
--Iterate through every city
for ii, pCity in pCities:Members() do
local tCityPlots = GetCityPlots(pCity)
--Iterate through every workable plot in that city.
for Item,pPlot in pairs(tCityPlots) do
--If Create succeeded, i.e. returned a unit, then send a success message and return
if(pUnits:Create(tonumber(UnitIndex), pPlot:GetX(), pPlot:GetY()) ~= nil) then
print(print("Luffy finds a new nakama after dealing a critical defeat!!!"))
return true
end
end
end
print(print("Couldn't find a suitable location...."))
return false
end
function FindUnitIndex(pPlayerID, UnitTypeID)
--Function attempts to find the type of the killed unit.
local pPlayer = Players[pPlayerID];
if pPlayer:WasEverAlive() then
local pUnits = pPlayer:GetUnits();
local pUnit;
for ii, pUnit in pUnits:Members() do
local UnitType = GameInfo.Units[pUnit:GetType()].Index
if(tostring(pUnit:GetID()) == tostring(UnitTypeID)) then
return UnitType
end
end
--Default to a warrior
return GameInfo.Units["UNIT_WARRIOR"].Index
end
end
function FindUnit(pPlayerID, UnitTypeID)
--Function attempts to find the type of the killed unit.
local pPlayer = Players[pPlayerID];
if pPlayer:WasEverAlive() then
local pUnits = pPlayer:GetUnits();
local pUnit;
for ii, pUnit in pUnits:Members() do
local UnitType = GameInfo.Units[pUnit:GetType()].Index
if(tostring(pUnit:GetID()) == tostring(UnitTypeID)) then
return pUnit
end
end
--Default to a warrior
return nil
end
end
function OnUnitKill(w,x,y,z)
local pPlayer = Players[y]
local playerConfig = PlayerConfigurations[y]
--Don't continue if the winning civ is Barbarians. They dont have a capital
if(playerConfig:GetCivilizationTypeName() == "CIVILIZATION_BARBARIAN") then
return
end
local capitalCity = pPlayer:GetCities():GetCapitalCity()
local pUnits = pPlayer:GetUnits()
local UnitIndex = FindUnitIndex(w, x)
local Unit = FindUnit(w, x)
if playerConfig:GetCivilizationTypeName() == "CIVILIZATION_STRAWHAT" then
--pUnits:Create(tonumber(UnitIndex), capitalCity:GetX(), capitalCity:GetY())
--print("The defeated unit joins the Strawhats!")
randomChance = math.random(1, 5)
if (randomChance == 5) then
AttemptToPlotUnit(UnitIndex, pPlayer)
else
print("No unit this time....")
end
end
end
Events.UnitKilledInCombat.Add(OnUnitKill)