library respawnsys initializer init requires respawn
globals
//system dependent variables:
private respawngroup array sysGroup
private integer sysGroupCount = 0
endglobals
//**********************************************************************************************
//* utility
//*
//*
//newGroup:
// --creates a new group and adds the specified respawn to it
private function newGroup takes nothing returns respawngroup
set sysGroup[sysGroupCount] = respawngroup.create()
set sysGroupCount = sysGroupCount + 1
return (sysGroup[sysGroupCount - 1])
endfunction
// checkGroups:
// --checks to see if a respawn 'r' is within the boundaries of
// a previously created group. if so we can add to that group.
// --returns the array index in "sysGroup" of the group.
private function checkGroups takes respawn r returns integer
local integer i = 0
loop
exitwhen (i == sysGroupCount)
if (sysGroup[i].check(r)) then
return (i)
endif //return the index of group
set i = i + 1
endloop
return (-1) //no nearby groups
endfunction
//**********************************************************************************************
//* main
//*
//*
private function onRespawn takes nothing returns nothing
local respawntimer exp = respawntimer(GetTimerData(GetExpiredTimer()))
local respawngroup grp
local respawncategory cat
local respawn elem
if (exp == 0) then
debug call BJDebugMsg("Non-respawn-unit has died. No action.")
return
endif //return if the dying unit is not a respawn
set grp = exp.rgroup
set cat = exp.rcat
call grp.members.iteratef()
loop
set elem = grp.members.iterate()
exitwhen (elem == -1)
//iterate group and spawn units if pending and same category.
if ((elem.pending) and (elem.rtype.rcat == cat)) then
call elem.spawn(respawn_RESPAWN_PLAYER)
endif
endloop
endfunction
private function onDeath takes nothing returns boolean
local respawnunit u = respawnunit[GetTriggerUnit()]
local respawn r
local respawngroup grp
local respawntimer rtmr
local respawn elem
if (u != 0) then //only check respawn-able units
set r = respawn.lookup(u)
set grp = respawngroup.findGroup(r)
//the timer we are operating with:
set rtmr = grp.rtimer[r.rtype.rcat]
//start the timer (ignore previous arrangements for respawns)
call TimerStart(rtmr.tmr, r.rtype.rcat.timeout, false, function onRespawn)
set r.pending = true
endif
return (false)
endfunction
//**********************************************************************************************
//* initialization
//*
//*
public function init takes nothing returns nothing
local group all = CreateGroup()
local unit first
local trigger t = CreateTrigger()
local respawn r
local respawntype rtyp
local respawngroup rgrp
local integer i = 0
call GroupEnumUnitsOfPlayer(all, respawn_RESPAWN_PLAYER, null)
loop
set first = FirstOfGroup(all)
exitwhen (first == null)
set rtyp = respawntype.find(GetUnitTypeId(first))
if (rtyp != 0) then
set r = respawn.create(GetUnitX(first), GetUnitY(first), rtyp)
call r.setunit(first)
set rgrp = sysGroup[checkGroups(r)] //check for any eligable groups.
if (rgrp == 0) then
set rgrp = newGroup()
endif //get new group off stack array
call rgrp.add(r)
endif //add respawn to group found/created
call GroupRemoveUnit(all, first)
endloop
call DestroyGroup(all)
set all = null
set first = null
//register trigger/actions:
call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_DEATH)
call TriggerAddCondition(t, Filter(function onDeath))
endfunction
endlibrary