sman1975
Emperor
Hello,
I'm working on a mod that adds a bunch of units throughout the tech tree. As such, it would be nice if the AI would actually upgrade some of their units during the game, so the human player isn't facing Classical units in the Info Era...
I've written a little function to accomplish this forced upgrade. Once a turn, it checks to see if any AI units should be upgraded. It is only called when pPlayer isn't human, so that's why this test isn't visible in the function:
Note: the 'ArcaneAlliesLevel ' element is one I added to the Units table to depict the "level" of the new units in the mod - which is used to assign promotions, naming conventions, etc. It defaults to 0 for normal game units. These new units in the mod are the only ones I want to "force upgrade" - not normal game units, although there may be an argument for that...
The function works fine, from what I can see, but was wondering:
Question: what other traps, checks, fixes, etc., should go into the function to help make it more 'bulletproof' during game play?
Appreciate it!
I'm working on a mod that adds a bunch of units throughout the tech tree. As such, it would be nice if the AI would actually upgrade some of their units during the game, so the human player isn't facing Classical units in the Info Era...
I've written a little function to accomplish this forced upgrade. Once a turn, it checks to see if any AI units should be upgraded. It is only called when pPlayer isn't human, so that's why this test isn't visible in the function:
Code:
for pUnit in pPlayer:Units() do -- Cycle through all AI civ's units
local iUnitType = pUnit:GetUnitType()
if GameInfo.Units[iUnitType].ArcaneAlliesLevel ~= 0 then -- Element 'ArcaneAlliesLevel' added to Units table, default=0. Equals 1..4 for ArcaneAlliesLevel
if (pUnit:CanUpgradeRightNow()) then -- Can this unit upgrade now?
local iUpgradeUnitType = pUnit:GetUpgradeUnitType() or -1
if ( (iUpgradeUnitType ~= -1) and (not pUnit:IsEmbarked()) ) then
local iX, iY = pUnit:GetX(), pUnit:GetY()
print("Original unit: " .. Locale.ConvertTextKey(GameInfo.Units[iUnitType].Description) .. " Upgrades to: " .. Locale.ConvertTextKey(GameInfo.Units[iUpgradeUnitType].Description) .. " Location: " .. iX.. " / " .. iY)
pNewUnit = pPlayer:InitUnit(iUpgradeUnitType, iX, iY) -- Spawn the new unit
pNewUnit:Convert(pUnit) -- Convert the old unit to the new unit
pNewUnit:JumpToNearestValidPlot() -- Move actual unit spawn point to nearest valid area
pNewUnit:SetMoves(0)
end
end
end
end
Note: the 'ArcaneAlliesLevel ' element is one I added to the Units table to depict the "level" of the new units in the mod - which is used to assign promotions, naming conventions, etc. It defaults to 0 for normal game units. These new units in the mod are the only ones I want to "force upgrade" - not normal game units, although there may be an argument for that...

The function works fine, from what I can see, but was wondering:
Question: what other traps, checks, fixes, etc., should go into the function to help make it more 'bulletproof' during game play?
Appreciate it!