Shield damage taken on a unit:
Why?
Solution?
Issues?
Snippet?
Use?
Why?
A unit taking damage can be detected through a trigger. This trigger fires the moment before the target takes the damage. As such, adding said damage back to the targets health, if they are at maximum hp, does not actually block the damage (or some decrement thereof). Since the unit will be at max hp when the damage is actually done, the portion between the difference in the current hp and max hp and the damage done will be taken as damage after the life is added to the unit.
i.e. - Units hp is 75, units max hp is 100, unit takes 50 damage. Add 50 hp to unit, unit now has 100 hp, unit then takes 50 damage, unit is left with 50 hp, not the 75 they should have.
This is undesirable.
i.e. - Units hp is 75, units max hp is 100, unit takes 50 damage. Add 50 hp to unit, unit now has 100 hp, unit then takes 50 damage, unit is left with 50 hp, not the 75 they should have.
This is undesirable.
Solution?
Add the health back after the damage is done, in some optimal fashion.
Issues?
If the damage done is greater then the maximum health the unit has, no amount of added hp before or after the damage will keep it alive. In those cases, you need to actually increase the max hp of the unit temporarily. This snippet does not account for such cases.
Snippet?
JASS:
library ShieldUnit initializer Init
globals
private timer Tim=CreateTimer()
private unit array U
private real array Heal
private integer I = 0
endglobals
private function Restore takes nothing returns nothing
loop
exitwhen I==0
call SetWidgetLife(U<i>,Heal<i>)
set I=I-1
endloop
endfunction
function ShieldUnit takes unit u, real r, real dam returns boolean
if r<=0. or GetUnitState(u,UNIT_STATE_MAX_LIFE)<dam+.405 or (GetWidgetLife(u)+r)-dam<.405 then
return false
debug elseif r>dam then
debug call DisplayTimedTextToPlayer(GetLocalPlayer(),0.,0.,5.,GetUnitName(u)+" is being shielded for more damage then it took.")
endif
set I = I + 1
set U<i> = u
set Heal<i> = GetWidgetLife(u)-(dam-r)
call SetWidgetLife(u,GetUnitState(u,UNIT_STATE_MAX_LIFE))
call ResumeTimer(Tim)
return true
endfunction
private function Init takes nothing returns nothing
call TimerStart(Tim,0.0,false,function Restore)
endfunction
endlibrary</i></i></i></i>
Use?