Genghis.Khan
Person
So, I've been working on the implementation of a Colony System, and recently I decided to expand my improvement Mod with a new concept, and integrate those mechanics into UnOfficial Expansion: Rise and Fall of Empires. The Mechanics I designed for the Colony System are fairly simple, but I have not coded all of them. I've ran a test with one of the first rules, that there is a chance that, when a Colonist founds a Colony, the second starts a revolution. I've used code from Gedemon's Revolutions and whoward's Area Plot Iterators. For some reason, the test I ran, nothing happened. Not even a print statement in Lua.log and I have logging enabled... I have no idea why did this happened, can you understand what is wrong with this code
Code:
-- ColoniesMain.lua
-- Author: Genghis.Khan
-- Special Thanks: FramedArchitecture, whoward69, Hambil, Irkalla, Gedemon
-- Date Created: 03-07-2013 13:22
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
print(" ... please... work" )
RESERVED_CITY_STATES = 10
SECTOR_NORTH = 1
SECTOR_NORTHEAST = 2
SECTOR_SOUTHEAST = 3
SECTOR_SOUTH = 4
SECTOR_SOUTHWEST = 5
SECTOR_NORTHWEST = 6
DIRECTION_CLOCKWISE = false
DIRECTION_ANTICLOCKWISE = true
DIRECTION_OUTWARDS = false
DIRECTION_INWARDS = true
CENTRE_INCLUDE = true
CENTRE_EXCLUDE = false
function PlotAreaSpiralIterator(pPlot, r, sector, anticlock, inwards, centre) -- by whoward69
local next = coroutine.create(function ()
if (centre and not inwards) then
coroutine.yield(pPlot)
end
if (inwards) then
for i=r, 1, -1 do
for pEdgePlot in PlotRingIterator(pPlot, i, sector, anticlock) do
coroutine.yield(pEdgePlot)
end
end
else
for i=1, r, 1 do
for pEdgePlot in PlotRingIterator(pPlot, i, sector, anticlock) do
coroutine.yield(pEdgePlot)
end
end
end
if (centre and inwards) then
coroutine.yield(pPlot)
end
return nil
end)
-- This function returns the next plot in the sequence
return function ()
local success, pAreaPlot = coroutine.resume(next)
return success and pAreaPlot or nil
end
end
function RebelUnitsToSpawnSettler()
local pPlayer = Players[playerID]
local team = pPlayer:GetTeam();
if team:HasTech(teamTechs:HasTech( GameInfo.Techs.TECH_MOBILE_TACTICS ) then
return GameInfo.Units.UNIT_GREAT_WAR_INFANTRY.ID
elseif teamTechs:HasTech( GameInfo.Techs.TECH_PLASTIC.ID ) then
return GameInfo.Units.UNIT_RIFLEMAN.ID
elseif teamTechs:HasTech( GameInfo.Techs.TECH_REPLACEABLE_PARTS.ID ) then
return GameInfo.Units.UNIT_MUSKETMAN.ID
elseif teamTechs:HasTech( GameInfo.Techs.TECH_RIFLING.ID ) then
return GameInfo.Units.UNIT_PIKEMAN.ID
elseif teamTechs:HasTech( GameInfo.Techs.TECH_GUNPOWDER.ID ) then
return GameInfo.Units.UNIT_SPEARMEN.ID
else
return GameInfo.Units.UNIT_WARRIOR.ID
end
end
function RebelUnitsToSpawnPrince()
local pPlayer = Players[playerID]
local team = pPlayer:GetTeam();
if team:HasTech(teamTechs:HasTech( GameInfo.Techs.TECH_MOBILE_TACTICS ) then
return GameInfo.Units.UNIT_INFANTRY.ID
elseif teamTechs:HasTech( GameInfo.Techs.TECH_PLASTIC.ID ) then
return GameInfo.Units.UNIT_GREAT_WAR_INFANTRY.ID
elseif teamTechs:HasTech( GameInfo.Techs.TECH_REPLACEABLE_PARTS.ID ) then
return GameInfo.Units.UNIT_RIFLEMAN.ID
elseif teamTechs:HasTech( GameInfo.Techs.TECH_RIFLING.ID ) then
return GameInfo.Units.UNIT_MUSKETMAN.ID
elseif teamTechs:HasTech( GameInfo.Techs.TECH_GUNPOWDER.ID ) then
return GameInfo.Units.UNIT_PIKEMAN.ID
else
return GameInfo.Units.UNIT_SPEARMEN.ID
end
end
function RebelUnitsToSpawnEmperor()
local pPlayer = Players[playerID]
local team = pPlayer:GetTeam();
if team:HasTech(teamTechs:HasTech( GameInfo.Techs.TECH_MOBILE_TACTICS ) then
return GameInfo.Units.UNIT_MECHANIZED_INFANTRY.ID
elseif teamTechs:HasTech( GameInfo.Techs.TECH_PLASTIC.ID ) then
return GameInfo.Units.UNIT_INFANTRY.ID
elseif teamTechs:HasTech( GameInfo.Techs.TECH_REPLACEABLE_PARTS.ID ) then
return GameInfo.Units.UNIT_GREAT_WAR_INFANTRY.ID
elseif teamTechs:HasTech( GameInfo.Techs.TECH_RIFLING.ID ) then
return GameInfo.Units.UNIT_RIFLEMAN.ID
elseif teamTechs:HasTech( GameInfo.Techs.TECH_GUNPOWDER.ID ) then
return GameInfo.Units.UNIT_MUSKETMAN.ID
else
return GameInfo.Units.UNIT_PIKEMAN.ID
end
end
function ReserveCS() -- by Gedemon
local bDebug = true
Dprint ("------------------ ", bDebug)
Dprint ("Initializing City-States... ", bDebug)
local numCS = modUserData.GetValue("NumMinorCivs")
Dprint (" - Number of CS required by the player = " .. numCS, bDebug)
local loadedCS = {}
for playerID = GameDefines.MAX_MAJOR_CIVS, GameDefines.MAX_CIV_PLAYERS - 1 do
local player = Players[playerID]
local minorCivID = player:GetMinorCivType()
-- Does this civ exist ?
if minorCivID ~= -1 then
table.insert(loadedCS, playerID)
end
end
Dprint (" - Number of active CS in game = " .. #loadedCS, bDebug)
local reservedCS = {}
if RESERVED_CITY_STATES > #loadedCS then
Dprint (" - WARNING : Loaded CS < RESERVED_CITY_STATES ")
elseif numCS + RESERVED_CITY_STATES > #loadedCS then
Dprint (" - Not enough CS active for all request, keeping " .. #loadedCS - RESERVED_CITY_STATES .. " alive..." , bDebug)
for i = #loadedCS - RESERVED_CITY_STATES, #loadedCS do
local playerID = loadedCS[i]
RemoveCiv (playerID)
reservedCS[playerID] = { Action = nil, Type = nil, Reference = nil}
end
else
Dprint (" - Keeping " .. numCS .. " alive..." , bDebug)
for i = numCS + 1, #loadedCS do
local playerID = loadedCS[i]
RemoveCiv (playerID)
reservedCS[playerID] = { Action = nil, Type = nil, Reference = nil}
end
end
SaveData( "ReservedCS", reservedCS )
end
function Shuffle(t) -- by Gedemon
local n = #t
while n >= 2 do
-- n is now the last pertinent index
local k = math.random(n) -- 1 <= k <= n
-- Quick swap
t[n], t[k] = t[k], t[n]
n = n - 1
end
return t
end
function EstabilishColonyInitialRebellion ()
for plotLoop = 0, Map.GetNumPlots() - 1, 1 do
local improvementType = plot:GetImprovementType()
if ( improvementType == GameInfoTypes["IMPROVEMENT_COLONY"] ) then
local iColonyPlotX = pPlot:GetX
local iColonyPlotY = pPlot:GetY
local iHandicapModifier = (10 * Game.GetHandicapType())
local pTeam = Teams[teamID]
if ( pTeam:GetCurrentEra() == GameInfoTypes["ERA_COLONIAL"] ) then
local iEraModifier == 0
elseif ( pTeam:GetCurrentEra() == GameInfoTypes["ERA_INDUSTRIAL"] ) then
local iEraModifier == 10
elseif ( pTeam:GetCurrentEra() == GameInfoTypes["ERA_MODERN"] ) then
local iEraModifier == 20
-- Colony build is obsolete when any Atomic Era Technology is researched (see CIV5RFEBuilds.lua and CIV5RFEBuilds.sql)
end
if math.random(1, 100) > math.floor(iHandicapModifier + iEraModifier) then
local pPlayer = Players[playerID]
local rebelID = nil
if not rebelID then
if #freeSlots > 0 then
rebelID = freeSlots[1]
DeclarePermanentWar(rebelID, playerID)
local freePlots = {}
local pColonyPlot = Plot:At(iColonyPlotX, iColonyPlotY)
local plotNumUnits = plot:GetNumUnits()
if not (plot:IsWater()) and not (plotNumUnits > 3) then
local iRadius = 2
for pAreaPlot in PlotAreaSpiralIterator(pColonyPlot, iRadius, SECTOR_NORTH, DIRECTION_ANTICLOCK, DIRECTION_INWARDS, CENTRE_INCLUDE) do
print(pAreaPlot:GetX(), pAreaPlot:GetY())
table.insert(freePlots, pAreaPlot)
end
end
Shuffle(freePlots)
for n, plot in pairs(freePlots) do
local freeUnitSettler = RebelUnitsToSpawnSettler()
local freeUnitPrince = RebelUnitsToSpawnPrince()
local freeUnitEmperor = RebelUnitsToSpawnEmperor()
if Game.GetHandicapType() == 0 then
if math.random(1,5) < 6 then
Player[rebelID]:AddFreeUnit(freeUnitSettler, UNITAI_OFFENSE)
else
return false
end
if Game.GetHandicapType() == 1 then
Player[rebelID]:AddFreeUnit(freeUnitPrince, UNITAI_OFFENSE)
if Game.GetHandicapType() == 2 then
Player[rebelID]:AddFreeUnit(freeUnitSettler, UNITAI_OFFENSE)
Player[rebelID]:AddFreeUnit(freeUnitSettler, UNITAI_OFFENSE)
if Game.GetHandicapType() == 3 then
Player[rebelID]:AddFreeUnit(freeUnitPrince, UNITAI_OFFENSE)
Player[rebelID]:AddFreeUnit(freeUnitPrince, UNITAI_OFFENSE)
if Game.GetHandicapType() == 4 then
Player[rebelID]:AddFreeUnit(freeUnitPrince, UNITAI_OFFENSE)
Player[rebelID]:AddFreeUnit(freeUnitPrince, UNITAI_OFFENSE)
Player[rebelID]:AddFreeUnit(freeUnitSettler, UNITAI_OFFENSE)
if Game.GetHandicapType() == 5 then
Player[rebelID]:AddFreeUnit(freeUnitEmperor, UNITAI_OFFENSE)
Player[rebelID]:AddFreeUnit(freeUnitEmperor, UNITAI_OFFENSE)
Player[rebelID]:AddFreeUnit(freeUnitPrince, UNITAI_OFFENSE)
if Game.GetHandicapType() == 6 then
Player[rebelID]:AddFreeUnit(freeUnitEmperor, UNITAI_OFFENSE)
Player[rebelID]:AddFreeUnit(freeUnitEmperor, UNITAI_OFFENSE)
Player[rebelID]:AddFreeUnit(freeUnitEmperor, UNITAI_OFFENSE)
Player[rebelID]:AddFreeUnit(freeUnitPrince, UNITAI_OFFENSE)
if Game.GetHandicapType() == 7 then
Player[rebelID]:AddFreeUnit(freeUnitEmperor, UNITAI_OFFENSE)
Player[rebelID]:AddFreeUnit(freeUnitEmperor, UNITAI_OFFENSE)
Player[rebelID]:AddFreeUnit(freeUnitEmperor, UNITAI_OFFENSE)
Player[rebelID]:AddFreeUnit(freeUnitEmperor, UNITAI_OFFENSE)
Player[rebelID]:AddFreeUnit(freeUnitPrince, UNITAI_OFFENSE)
else return false end
end
else return false end
else return false end
else return false end
else return false end
else return false end
end
end
Events.SerialEventImprovementCreated.Add(EstabilishColonyInitialRebellion)