- Joined
- Jun 23, 2007
- Messages
- 4,066
Allows you to automatically respawn units from where they originated at (as opposed to where they died).
Requires
System Code
Changelog
Requires
- TimerUtils
- UnitDex
- (Optional) ReviveUnit
System Code
JASS:
library CreepRespawn initializer onInit uses TimerUtils, UnitDex, optional ReviveUnit
/***************************************************************
*
* v1.0.4, by TriggerHappy
* ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
*
* CreepRespawn revives units once they die, at the place they originated
* at. It also supports reviving the units which prevents a leak that would
* otherwise be irremovable.
*
* ReviveUnit: -http://www.hiveworkshop.com/forums/jass-resources-412/snippet-reviveunit-186696/
*
* Configuration
* ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
*/
globals
// You may revive the unit instead of creating a new handle
// because units cause an irremovable leak. If we ressurect the
// unit it can prevent this leak
private constant boolean REVIVE_CREEP = true // (Requires the ReviveUnit library)
// Removes the corpse upon re-creating the creep.
private constant boolean REMOVE_CORPSE = true
endglobals
private constant function REVIVE_TIME takes unit u returns real
return 30.
endfunction
private constant function UNIT_FILTER takes unit u returns boolean
return (not IsUnitType(u, UNIT_TYPE_HERO))/*
*/ and (not IsUnitType(u, UNIT_TYPE_STRUCTURE))/*
*/ and (GetOwningPlayer(u) == Player(PLAYER_NEUTRAL_AGGRESSIVE))
endfunction
/*
* Don't edit below this line
* ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
*/
globals
private real array X
private real array Y
private real array FACING
private player array PLAYER
private integer array ID
endglobals
private function OnRevive takes nothing returns nothing
local timer t = GetExpiredTimer()
local integer i = GetTimerData(t)
// Check if we should revive the unit to prevent a leak
static if (REVIVE_CREEP and LIBRARY_ReviveUnit) then
// if it fails, the function will continue
if (ReviveUnit(GetUnitById(i))) then
call SetUnitX(GetUnitById(i), X[i])
call SetUnitY(GetUnitById(i), Y[i])
return
endif
endif
static if (REMOVE_CORPSE) then
call RemoveUnit(GetUnitById(i))
endif
call CreateUnit(PLAYER[i], ID[i], X[i], Y[i], FACING[i])
call ReleaseTimer(t)
set t = null
endfunction
private function OnDeath takes nothing returns boolean
local unit u = GetFilterUnit()
local integer i
if (IsUnitIndexed(u) and UNIT_FILTER(u)) then
set i = GetUnitId(u)
static if (LIBRARY_ReviveUnit) then
call SetUnitFacing(u, FACING[i])
endif
call TimerStart(NewTimerEx(i), REVIVE_TIME(u), false, function OnRevive)
endif
set u = null
return false
endfunction
private function OnIndex takes nothing returns boolean
local player owner = GetOwningPlayer(GetIndexedUnit())
// Check if the dying unit passes the filters
if (UNIT_FILTER(GetIndexedUnit())) then
// Assign the unit data
set X[GetIndexedUnitId()] = GetUnitX(GetIndexedUnit())
set Y[GetIndexedUnitId()] = GetUnitY(GetIndexedUnit())
set FACING[GetIndexedUnitId()] = GetUnitFacing(GetIndexedUnit())
set ID[GetIndexedUnitId()] = GetUnitTypeId(GetIndexedUnit())
set PLAYER[GetIndexedUnitId()] = owner
endif
return false
endfunction
private function onInit takes nothing returns nothing
local trigger t = CreateTrigger()
call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_DEATH)
call TriggerAddCondition(t, Filter(function OnDeath))
call RegisterUnitIndexEvent(Filter(function OnIndex), EVENT_UNIT_INDEX)
endfunction
endlibrary
Code:
v1.0.4
- UNIT_FILTER checked on death.
- Shortened code.
v1.0.3
- Units position now resets if the unit is revived.
- REMOVE_CORPSE now functions properly.
- Updated to comply with the newest version of ReviveUnit.
v1.0.2
- PlayerFilter removed.
- All constant functions have been renamed to all caps.
- REMOVE_CORPSE will remove the old unit before after it's replacement.
- REVIVE_CREEP will revive the unit to prevent memory leaking. This also means the ReviveUnit library is required for this to be enabled.
v1.0.1
- UnitFilter implemented.
- Filtered structures and heroes by default.
Last edited: