Coding a promotion that gives combat units a strength bonus when below n% health?

Toggaf

Chieftain
Joined
Jan 7, 2013
Messages
32
I'm completely new to lua and not really sure how this works.

From what I can tell, since what I want to do is connected to a civ's UA, I could just define the civ and have the game check whether the player is playing as that civ.

Then, add a function that gets any unit's health and then applies a promotion to it if it is below, say, 50%. It does this per turn, so that it removes the promotion if it detects that its health is not below 50%.

I think I kinda get it 'theoretically' but have no actual idea how to start. Any tips please?
 
If you want to perform the check at the end of the civilization's turn, you would need to create a hook on the PlayerDoTurn event. Then you could iterate through all of the units that player owned and check each one to see if they had under half of their health.

This is how I would do it (of course, you can name the function whatever):
Code:
function CheckYourCivForHalfHealth(iPlayer)
local pPlayer = Players[iPlayer];
if (pPlayer:IsEverAlive() and pPlayer:GetCivilizationType() == GameInfoTypes.CIVILIZATION_YOUR_CIV) then
    for pUnit in pPlayer:Units() do
        if (pUnit:GetCurrHitPoints() < pUnit:GetMaxHitPoints() / 2) then
            pUnit:SetHasPromotion(GameInfoTypes.PROMOTION_WOUNDED_BONUS, true);
        else
            pUnit:SetHasPromotion(GameInfoTypes.PROMOTION_WOUNDED_BONUS, false);
        end
    end
end

-- This only adds the listener if your civilization is in the current game
for iPlayer = 0, GameDefines.MAX_MAJOR_CIVS-1, 1 do
    local pPlayer = Players[iPlayer];
        if (pPlayer:IsEverAlive() and pPlayer:GetCivilizationType() == GameInfoTypes.CIVILIZATION_YOUR_CIV) then
            GameEvents.PlayerDoTurn.Add(CheckYourCivForHalfHealth);
            break
        end
    end
end

Note that this will only check at the start of this civilization's turn, so if one of its units were in the middle of a turn and one of its units attacked, went under 50% and attacked again, the unit wouldn't have the promotion. Also, it would only consider this civilization's turn, so if someone else attacked during their turn and put a unit under 50%, that unit would not get the promotion until its turn rolled around again.

You could have the check run at the end of everyone's turn, if you wish (in that case you would have to iterate through the player list every time, and for every instance of your civilization in game - remember, you can have multiple players as the same civilization - check their units), and you could also add a hook to UnitSetXY event to check every time a unit moved (you shouldn't have to worry about overhead too much), but I don't know if it fires if a unit attacks another without moving into its space - probably not. Unfortunately there are no combat hooks except for when a unit is killed.

Anyway, I hope this is helpful to you in some way! :)
 
Back
Top Bottom