library CreepRespawn initializer init
globals
private constant integer KEY = 0
private hashtable ht = InitHashtable()
private trigger GetDying = CreateTrigger()
endglobals
private struct Data
real x
real y
real d
real f
integer uid
integer id
player p
static method create takes real x, real y, real d, unit u returns thistype
local thistype this = thistype.allocate()
set .x = x
set .y = y
set .d = d
set .uid = GetHandleId(u)
set .f = GetUnitFacing(u)
set .id = GetUnitTypeId(u)
set .p = GetOwningPlayer(u)
return this
endmethod
endstruct
function SetCreepRespawn takes unit u, real delay returns boolean
local integer uid = GetHandleId(u)
local Data t = LoadInteger(ht,uid,KEY)
local Data data = 0
if t >= 1 then // unit already added to the system
if delay <= 0. then // if delay < 0 then disable respawn of the unit
call SaveInteger(ht,uid,KEY, 0)
call RemoveSavedInteger(ht,uid,KEY)
return false
elseif t.uid == uid then // if delay > 0 then set respawn time to new value
set t.d = delay
return false
endif
endif
set data = Data.create(GetUnitX(u),GetUnitY(u),delay,u)
call SaveInteger(ht,uid,KEY,data)
return true
endfunction
private function ActionDelayed takes Data data returns nothing
local integer uid = 0
call TriggerSleepAction(data.d)
set uid = GetHandleId(CreateUnit(data.p,data.id,data.x,data.y,data.f))
call SaveInteger(ht,data.uid,KEY, 0)
call RemoveSavedInteger(ht,data.uid,KEY)
call SaveInteger(ht,uid,KEY,data)
set data.uid = uid
endfunction
private function Action takes nothing returns boolean
local Data data = LoadInteger(ht,GetHandleId(GetTriggerUnit()),KEY)
if data <= 0 then
return false
endif
call ActionDelayed.execute(data)
return false
endfunction
private function init takes nothing returns nothing
call Data.create(0.,0.,0.,null)
call TriggerRegisterAnyUnitEventBJ( GetDying, EVENT_PLAYER_UNIT_DEATH )
call TriggerAddCondition(GetDying, Condition(function Action))
endfunction
endlibrary