Moderator
M
Moderator
18:45, 19th Aug 2013
PurgeandFire: Changes made. Approved.
PurgeandFire: Changes made. Approved.
//! zinc
library RespawnSystem requires Table, TimerUtils, optional RegisterPlayerUnitEvent /* v1.1
*************************************************************************************
*
* by mckill2009
* - My first ever system written in Zinc.
*
*************************************************************************************
*
* Respawns the same unit(s) at the same point and facing angle with customable effects and delay.
*
*************************************************************************************
*
* Required Libraries:
* Table by Bribe (www.hiveworkshop.com/forums/jass-resources-412/snippet-new-table-188084)
* TimerUtils by Vexorian (www.wc3c.net/showthread.php?t=101322)
*
* Optional Library:
* RegisterPlayerUnitEvent by Magtheridon96 (http://www.hiveworkshop.com/forums/jass-resources-412/snippet-registerplayerunitevent-203338/)
*
*************************************************************************************
*
* Installation:
* Just copy all the required libraries and this script to your map and follow the
* API callings or refer to the DEMO trigger.
*
*************************************************************************************
*
* API:
* public static method register(unit u)
* - call RespawnSystem.register(UNIT)
* - Sets the unit to respawn in simple manner
*
* public static method registerEx(unit u, real delay, string sfx, player pl)
* - call RespawnSystem.registerEx(UNIT, custom delay, custom effect, owner player)
* - Respawns with configurables
*
* public static method remove(unit u)
* - call RespawnSystem.remove(UNIT)
* - Unit will not be respawned again
*
*************************************************************************************/
{
constant real DEFAULT_DELAY = 5;
constant string DEFAULT_SFX = "";
Table chk;
public struct RespawnSystem
{
unit u;
real x;
real y;
real f;
real del;
string sfx;
player pl;
integer id;
static method respawnNow()
{
timer t = GetExpiredTimer();
thistype this = GetTimerData(t);
thistype new = thistype.allocate();
unit nonHero;
if (IsUnitType(this.u, UNIT_TYPE_HERO)) {
ReviveHero(this.u, this.x, this.y, true);
chk[GetHandleId(this.u)] = new;
} else {
nonHero = CreateUnit(this.pl, this.id, this.x, this.y, this.f);
if (this.sfx != "")
{DestroyEffect(AddSpecialEffectTarget(this.sfx, u, "origin"));}
chk[GetHandleId(nonHero)] = new;
nonHero = null;
}
new.x = this.x;
new.y = this.y;
new.f = this.f;
new.del = this.del;
new.sfx = this.sfx;
new.pl = this.pl;
this.pl = null;
this.u = null;
ReleaseTimer(t);
chk.remove(id);
this.deallocate();
t = null;
}
static method death()->boolean
{
thistype this;
unit u = GetTriggerUnit();
if (chk.has(GetHandleId(u))) {
this = chk[GetHandleId(u)];
this.id = GetUnitTypeId(u);
this.u = u;
TimerStart(NewTimerEx(this), this.del, false, function thistype.respawnNow);
}
u = null;
return false;
}
static method onInit()
{
static if (LIBRARY_RegisterPlayerUnitEvent) {
RegisterPlayerUnitEvent(EVENT_PLAYER_UNIT_DEATH, function thistype.death);
} else {
trigger t = CreateTrigger();
TriggerRegisterAnyUnitEventBJ(t,EVENT_PLAYER_UNIT_DEATH);
TriggerAddCondition(t,function thistype.death);
t = null;
}
chk = Table.create();
}
/*****************************************************************
* API:
******************************************************************/
public static method register(unit u)
{
thistype this = thistype.allocate();
chk[GetHandleId(u)] = this;
this.id = GetUnitTypeId(u);
this.x = GetUnitX(u);
this.y = GetUnitY(u);
this.f = GetUnitFacing(u);
this.del = DEFAULT_DELAY;
this.sfx = DEFAULT_SFX;
this.pl = GetOwningPlayer(u);
}
public static method registerEx(unit u, real delay, string sfx, player pl)
{
thistype this = thistype.allocate();
chk[GetHandleId(u)] = this;
this.x = GetUnitX(u);
this.y = GetUnitY(u);
this.f = GetUnitFacing(u);
this.del = delay;
this.sfx = sfx;
this.pl = GetOwningPlayer(u);
if (pl==null) {
this.pl = GetOwningPlayer(u);
}
}
public static method remove(unit u)
{chk.remove(GetHandleId(u));}
}
}
//! endzinc