--[[-----------------------------------------
Name: GetCivSpecificUnit
Purpose: Get the UnitType for a specific
civ from a UnitClassType.
-------------------------------------------]]
function fnGetCivSpecificUnit(pPly, sUnitClass) -- Thanks whoward69
-- BEGIN DEFINES
local sUnitType = nil
local sCivType = GameInfo.Civilizations[pPly:GetCivilizationType()].Type
-- END DEFINES
-- Loop through civilization-specific UnitClass overrides, id est their unique units, and yield to be returned the proper UnitType.
for pOverride in GameInfo.Civilization_UnitClassOverrides{CivilizationType = sCivType, UnitClassType = sUnitClass} do
sUnitType = pOverride.UnitType
break
end
-- If we didn't get anything, yield to be returned the default UnitType for the UnitClass.
if (sUnitType == nil) then
sUnitType = GameInfo.UnitClasses[sUnitClass].DefaultUnit
end
-- Give whatever function called this the UnitType we yielded.
return sUnitType
end
--[[-----------------------------------------
Name: ReturnBestInfantryUnit
Purpose: Return the best land melee unit
that a city can build.
-------------------------------------------]]
function fnReturnBestInfantryUnit( pCity )
--BEGIN DEFINES
local pPly = pCity:GetOwner()
local possibleUnitClasses = {
GameInfoTypes.UNITCLASS_MECH,
GameInfoTypes.UNITCLASS_MECHANIZED_INFANTRY,
GameInfoTypes.UNITCLASS_INFANTRY,
GameInfotypes.UNITCLASS_GREAT_WAR_INFANTRY,
GameInfoTypes.UNITCLASS_RIFLEMAN,
GameInfoTypes.UNITCLASS_MUSKETMAN,
GameInfoTypes.UNITCLASS_LONGSWORDSMAN,
GameInfoTypes.UNITCLASS_PIKEMAN,
GameInfoTypes.UNITCLASS_SWORDSMAN,
GameInfoTypes.UNITCLASS_SPEARMAN,
GameInfoTypes.UNITCLASS_WARRIOR
} -- Thanks whoward69
--END DEFINES
-- Loop through each UnitClassType in the above defined table, see if the city can
-- train it's owner's specific UnitType. Yield to be returned the best land melee
-- UnitType the city can build.
for _, iUnitClass in ipairs(possibleUnitClasses) do -- Thanks whoward69
if pCity:CanTrain( fn_GetCivSpecificUnit( pPly, iUnitClass ) ) then
return fn_GetCivSpecificUnit( pPly, iUnitClass )
end
end
-- Uh-oh!
return -1
end
GameEvents.SetPopulation.Add( function( xOffset, yOffset, fOldPop, fNewPop )
-- BEGIN DEFINES
local pPlot = Map.GetPlot( xOffset, yOffset )
local pCity = pPlot:GetPlotCity()
local pPly = pCity:GetOwner()
local iUnitMostCurrent = nil
local nTrait = GameInfo.Traits[pPly].UnitPerCapitalGrowths
local sUnitAIType = GameInfo.UnitAITypes.UNITAI_DEFENSE.ID
local nZenith = math.floor( pCity:GetHighestPopulation() )
-- END DEFINES
-- On every growth of every city, run through the following list of conditions:
-- 1: Does the city's owner have the trait, and is its population evenly divisible by the trait?
-- 2: Is the city a capital?
-- 3: Has the city grown instead of shrinking, and is this the city's highest population?
-- If all of the above are true, get the best land infantry unit we can, and spawn it at the city.
if ( nTrait > 0 &&
( math.floor( fNewPop ) % nTrait ) == 0 ) && then
if pCity:IsCapital() then
if ( fNewPop > fOldPop ) &&
fNewPop >= nZenith then
iFreeUnit = fnReturnBestInfantryUnit( pCity )
pPly:AddFreeUnit( iFreeUnit, iUnitAIType ) -- What are the implications of this line?
end
end
end
end )