scope OwnerSwap initializer init
private function SwapOwner takes unit u, real hp, player p returns nothing
local integer spawnType = GetUnitTypeId(u)
local real x = GetUnitX(u)
local real y = GetUnitY(u)
local real f = GetUnitFacing(u)
call RemoveUnit(u)
call CreateUnit(p, spawnType, x, y, f)
call SetUnitState(u, UNIT_STATE_LIFE, hp)
set u = null
set p = null
endfunction
private function ConditionRun takes nothing returns nothing
local unit u = GetTriggerUnit()
local real hp = GetUnitState(u, UNIT_STATE_LIFE)
local player p = GetOwningPlayer(GetAttacker())
if GetUnitTypeId(u) == 'htow' and hp < 50. then
call SwapOwner(u, hp, p)
endif
set u = null
set p = null
endfunction
private function init takes nothing returns nothing
local trigger t = CreateTrigger()
local integer i = 0
loop
call TriggerRegisterPlayerUnitEvent(t, Player(i), EVENT_PLAYER_UNIT_ATTACKED, null)
set i = i + 1
exitwhen i > 11
endloop
call TriggerAddAction(t, function ConditionRun)
set t = null
endfunction
endscope