- Joined
- Jan 30, 2013
- Messages
- 12,376
How does this stun system works internally?
/*
STUN SYSTEM 1.12
Credits to Maker & IcemanBo for the original concept
API DOCUMENTATION:
public function Unit takes unit u, real dur, boolean stacks returns nothing
- Stuns a unit using a real duration and boolean for duration stacking.
- For non-stacking & multiple applications, the highest duration will be applied.
public function End takes unit u returns nothing
- Immediately ends a stun on a unit.
public function SetRemaining takes unit u, real dur returns nothing
- Sets the direct duration of a stun.
public function GetRemaining takes unit u returns real
- Gets the remaining stun duration.
function IsUnitStunned takes unit u returns boolean
- Returns a unit's stun status.
*/
library Stun initializer init
globals
private constant integer SPELL_ID = 'STUN'
private constant integer ORDER_ID = 852239 //String Order: "request_hero"
private constant hashtable HASHTABLE = InitHashtable()
private constant string EFFECT = "Abilities\\Spells\\Human\\Thunderclap\\ThunderclapTarget"
private constant string EFFECT_POINT = "overhead"
private timer TIMER
private conditionfunc CONDITION
endglobals
public function End takes unit u returns nothing
local integer unitId = GetHandleId(u)
local timer t= LoadTimerHandle(HASHTABLE, unitId, 1)
local integer timerId = GetHandleId(t)
local trigger trig = LoadTriggerHandle(HASHTABLE, unitId, 2)
call DestroyEffect(LoadEffectHandle(HASHTABLE, unitId, 3))
call FlushChildHashtable(HASHTABLE, timerId)
call DestroyTimer(t)
call TriggerClearConditions(trig)
call DestroyTrigger(trig)
call FlushChildHashtable(HASHTABLE, unitId)
call UnitRemoveAbility(u,SPELL_ID)
set t = null
endfunction
private function Elapsed takes nothing returns nothing
call End(LoadUnitHandle(HASHTABLE, GetHandleId(GetExpiredTimer()), 0))
endfunction
private function OrderCheck takes nothing returns boolean
local trigger trig = GetTriggeringTrigger()
call DisableTrigger(trig)
call IssueImmediateOrderById(GetTriggerUnit(), ORDER_ID)
call EnableTrigger(trig)
set trig = null
return false
endfunction
public function Unit takes unit u, real dur, boolean stacks returns nothing
local timer t
local trigger trig
local real stunRemain
local integer unitId = GetHandleId(u)
local integer i = 0
if LoadBoolean(HASHTABLE,unitId,0) then
set t = LoadTimerHandle(HASHTABLE, unitId, 1)
set stunRemain = TimerGetRemaining(t) - TimerGetElapsed(t)
call PauseTimer(t)
if stacks then
set stunRemain = stunRemain + dur
elseif stunRemain < dur then
set stunRemain = dur
endif
call TimerStart(t, stunRemain, false, function Elapsed)
else
call UnitAddAbility(u,SPELL_ID)
call IssueImmediateOrder(u,"stop")
call BlzUnitDisableAbility(u, SPELL_ID, false, false)
call IssueImmediateOrderById(u, ORDER_ID)
set trig = CreateTrigger()
call TriggerRegisterUnitEvent(trig, u, EVENT_UNIT_ISSUED_ORDER )
call TriggerAddCondition(trig, CONDITION)
set t = CreateTimer()
call SaveBoolean(HASHTABLE, unitId, 0, true)
call SaveTimerHandle(HASHTABLE, unitId, 1, t)
call SaveTriggerHandle(HASHTABLE, unitId, 2, trig)
call SaveEffectHandle(HASHTABLE, unitId, 3, AddSpecialEffectTarget(EFFECT,u,EFFECT_POINT))
call SaveUnitHandle(HASHTABLE, GetHandleId(t),0,u)
call TimerStart(t, dur, false, function Elapsed)
endif
set t = null
set trig = null
endfunction
public function SetRemaining takes unit u, real dur returns nothing
local timer t= LoadTimerHandle(HASHTABLE, GetHandleId(u), 1)
call PauseTimer(t)
call TimerStart(t, dur, false, function Elapsed)
set t = null
endfunction
function IsUnitStunned takes unit u returns boolean
return LoadBoolean(HASHTABLE, GetHandleId(u),0)
endfunction
public function GetRemaining takes unit u returns real
set TIMER = LoadTimerHandle(HASHTABLE, GetHandleId(u), 1)
return TimerGetRemaining(TIMER) - TimerGetElapsed(TIMER)
endfunction
private function init takes nothing returns nothing
set CONDITION = Condition( function OrderCheck)
endfunction
endlibrary