//gc is a global gamecache (localvars or something)
//H2I is handle2integer function
function DRSpell_Timer takes nothing returns nothing
//Adds leftover HP to unit's life, clears data, and destroys timer
local integer uid = GetStoredInteger(gc, "DRSpell", "t"+I2S(H2I(GetExpiredTimer())))
local real x = GetStoredReal(gc, "DRSpell", I2S(uid))
call SetUnitState(I2U(uid), UNIT_STATE_LIFE, GetUnitState(I2U(uid), UNIT_STATE_LIFE) + x)
call FlushStoredInteger(gc, "DRSpell", "t"+I2S(H2I(GetExpiredTimer())))
call FlushStoredReal(gc, "DRSpell", I2S(uid))
call DestroyTimer(GetExpiredTimer())
endfunction
function DRSpell_Damaged takes nothing returns nothing
//Action when unit is damaged
local real x = GetEventDamage() + GetUnitState(GetTriggerUnit(), UNIT_STATE_LIFE)
local timer t
local integer uid
//If there will be leftover hitpoints then we have to process the damage before we can negate all of it
if x - GetUnitState(GetTriggerUnit(), UNIT_STATE_MAX_LIFE) > 0 then
call SetUnitState(GetTriggerUnit(), UNIT_STATE_LIFE, GetUnitState(GetTriggerUnit(), UNIT_STATE_MAX_LIFE))
set uid = H2I(GetTriggerUnit())
set x = x - GetUnitState(GetTriggerUnit(), UNIT_STATE_MAX_LIFE)
//set x equal to the amount that will spill over maximum
call StoreReal(gc, "DRSpell", I2S(uid), x)
set t = CreateTimer()
call StoreInteger(gc, "DRSpell", "t"+I2S(H2I(t)), uid)
call TimerStart(t, 0., function DRSpell_Timer)
else
//If there won't be any leftover hitpoints then simply negate the damage
call SetUnitState(GetTriggerUnit(), UNIT_STATE_LIFE, x)
endif
set t = null
endfunction
function DRSpell_Damage_Conditions takes nothing returns boolean
return GetUnitAbilityLevel(GetTriggerUnit(), 'Basd') > 0 //'Basd' is the rawcode for the buff your ability uses - use whatever condition you need to use
endfunction
function DRSpell_Learn takes nothing returns nothing
//Create a new trigger when your unit learns the ability - this is necessary since there is not an EVENT_PLAYER_UNIT_DAMAGED event
local trigger tr = CreateTrigger()
call TriggerRegisterUnitEvent(tr, EVENT_UNIT_DAMAGED, GetTriggerUnit())
call TriggerAddAction(tr, function DRSpell_Damaged)
call TriggerAddConditions(tr, Condition(function DRSpell_Damage_Conditions))
set tr = null
endfunction
function InitTrig_DRSpell takes nothing returns nothing
//Create trigger the "on learn" trigger - this must be called during initialization
local trigger tr = CreateTrigger()
call TriggerRegisterAnyUnitEventBJ(tr, EVENT_HERO_SKILL, 'asdf') //'asdf' is the rawcode for your ability
call TriggerAddAction(tr, function DRSpell_Learn)
set tr = null
endfunction