Using LUA to Force AI to Upgrade Units?

sman1975

Emperor
Joined
Aug 27, 2016
Messages
1,376
Location
Dallas, TX
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:

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... :lol:

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!
 
You're essentially giving the upgrade for free.

If the new unit-type has a goodyupgrade designated it can get a goody upgrade even if it had previously done so before you converted it, as I recall.
 
Thanks, @LeeS.

The units in question don't have any "goodyupgrades" defined. I'm "assuming...." this means they'd never get that bonus ever, no? That's definitely what I'd intended - that they would not.

I didn't think about the "cost" of upgrades for this function. Although, I did go through several extra steps in the mod to make sure that there is a "realistic" cost applied when the human player upgrades. I guess I figure that since the AI is sooooo sketchy, any little help couldn't hurt? (probably not the best design philosophy....)

Appreciate the feedback!
 
Back
Top Bottom