Warcraft III resources & community, 2003–2006 · archived

RespawnUnitAfterDeath

not rated
Submitted by KaTTaNaFunctions0 downloads
Respawns the given unit after it has been killed or removed through triggers. It will be respawned at a random point within the given rect.
Parameters:
whichUnit: The unit to be respawned.
delay: Time before respawn.
where: Rect in which to respawn the unit. Use null to keep original spot
count: Max. times the unit can be respawned.

Delay can be set to 0 for instant respawning.

Source

function RespawnUnitAfterDeath_Child takes nothing returns nothing
    local unit whichUnit = bj_groupRandomCurrentPick
    local real delay     = bj_meleeNearestMineDist
    local real sleep = 1
    local real x = GetUnitX(whichUnit)
    local real y = GetUnitY(whichUnit)
    local integer id = GetUnitTypeId(whichUnit)
    local trigger trg
    local rect where = bj_isUnitGroupInRectRect
    local integer count = bj_destRandomConsidered
    local player owner = GetOwningPlayer(whichUnit)
    if ( where != null ) then
        set x = GetRandomReal(GetRectMinX(where), GetRectMaxX(where))
        set y = GetRandomReal(GetRectMinY(where), GetRectMaxY(where))
    endif
    if ( delay <= 3.0 ) then 
        set sleep = 0.5 // If delay is small, increase the accuracy
    elseif ( delay >= 60.0 ) then
        set sleep = 2.0 // If delay is big, decrease accuracy to save performance
    endif
    loop // Loop until unit is killed or has disappeared
        exitwhen (whichUnit == null) or (GetUnitState(whichUnit, UNIT_STATE_LIFE) <= 0)
        call PolledWait(sleep)
    endloop
    if ( delay > 0 ) then
        call PolledWait(delay) // Wait respawn time    
    endif
    set whichUnit = CreateUnit(owner, id, x, y, GetRandomDirectionDeg()) // Respawn the unit
    // If respawn is continual, repeat this trigger for the newly spawned unit
    if ( count != 1 ) then
        set trg = CreateTrigger()
        call TriggerAddAction(trg, function RespawnUnitAfterDeath_Child)
        set bj_groupRandomCurrentPick = whichUnit
        set bj_meleeNearestMineDist  = delay
        set bj_isUnitGroupInRectRect = where
        set bj_destRandomConsidered  = count - 1
        call TriggerExecute(trg)
        call DestroyTrigger(trg)
        set trg = null
    endif
    set whichUnit = null
endfunction

// Pass null in 'where' to make the unit respawn at its current position
function RespawnUnitAfterDeath takes unit whichUnit, real delay, rect where, integer count returns nothing
    local trigger trg = CreateTrigger()
    call TriggerAddAction(trg, function RespawnUnitAfterDeath_Child)
    set bj_groupRandomCurrentPick = whichUnit
    set bj_meleeNearestMineDist  = delay
    set bj_isUnitGroupInRectRect = where
    set bj_destRandomConsidered  = count
    call TriggerExecute(trg)
    call DestroyTrigger(trg)
    set trg = null
endfunction

// Demonstration of how it can be used.
// This function makes the units of a group respawnable.
function RespawnGroupAfterDeath takes group whichGroup, real delay returns nothing
    local unit u
    local group temp = CreateGroup()
    call GroupAddGroup(whichGroup, temp)
    loop
        set u = FirstOfGroup(temp)
        exitwhen u == null
        
        call RespawnUnitAfterDeath(u, delay, null, 0)
        
        call GroupRemoveUnit(temp, u)
    endloop
    call DestroyGroup(temp)
endfunction

Comments

0

No comments.