- Joined
- Apr 30, 2011
- Messages
- 359
PreciseTimer and TimedFunc
Timer Stack Draft (no documenttation):
Timed Function Draft (no documentation) - Interface:
Timed Function Draft (no documentation) - Requires TimerStack:
Feel free to comment, your suggestions are gladly appreciated!
Timer Stack Draft (no documenttation):
JASS:
library TimerStack
globals
private integer c = 0
private integer array r
private integer array t
private integer array nt
private integer array pt
private integer array lt
private conditionfunc array cft
private integer tc = 0
private integer array tr
private integer array tn
private integer array tp
private integer tl
private real array tt
private real array trt
private integer array ttc
private trigger trg = CreateTrigger()
private timer tmr
private integer cur
private real let
endglobals
private function GetType takes real time returns integer
local integer this = tl
local boolean exit = false
loop
exitwhen tp [tn [this]] == 0 or exit
if tt [this] == time then
set exit = true
else
set this = tp [this]
endif
endloop
if not exit then
set this = tr [0]
if this == 0 then
set tc = tc + 1
set this = tc
else
set tr [0] = tr [this]
endif
if tl == 0 then
set tp [this] = 0
else
set tp [this] = tl
endif
set tn [this] = 0
set tl = this
set tr [this] = -1
endif
set tt [this] = time
set trt [this] = time
set ttc [this] = ttc [this] + 1
return this
endfunction
private function LoseType takes integer this returns nothing
if tr [this] == -1 then
set ttc [this] = ttc [this] - 1
if ttc [this] == 0 then
if tn [this] == 0 then
if tp [this] == 0 then
set tl = 0
else
set tl = tp [this]
set tn [tp [this]] = 0
endif
else
set tn [tp [this]] = tn [this]
set tp [tn [this]] = tp [this]
endif
set tr [this] = tr [0]
set tr [0] = this
set tt [this] = 0
set trt [this] = 0
set ttc [this] = 0
endif
endif
endfunction
private function RefreshType takes nothing returns nothing
local integer this = tl
local real time = TimerGetElapsed(tmr) - let
set let = TimerGetElapsed(tmr)
loop
exitwhen tp [tn [this]] == 0
set trt [this] = trt [this] - time
set this = tp [this]
endloop
endfunction
private function Expire takes nothing returns nothing
local integer this = lt [cur]
loop
exitwhen pt [nt [this]] == 0
call TriggerClearConditions(trg)
call TriggerAddCondition(trg, cft [this])
call TriggerEvaluate(trg)
set this = pt [this]
endloop
call RefreshType()
set trt [cur] = tt [cur]
set this = tl
loop
exitwhen tp [tn [this]] == 0
if cur == 0 or trt [cur] > trt [this] then
set cur = this
endif
set this = tp [this]
endloop
set let = 0
call DestroyTimer(tmr)
set tmr = CreateTimer()
call TimerStart(tmr, trt [cur], false, function Expire)
endfunction
private function Create takes real time, code func returns integer
local integer this = r [0]
local integer that
if this == 0 then
set c = c + 1
set this = c
else
set r [0] = r [this]
endif
call RefreshType()
set t [this] = GetType(time)
set cft [this] = Condition(func)
if lt [t [this]] == 0 then
set nt [this] = 0
set pt [this] = 0
set lt [t [this]] = this
else
set nt [this] = 0
set pt [this] = lt [t [this]]
set lt [t [this]] = this
endif
set that = tl
loop
exitwhen tp [tn [that]] == 0
if cur == 0 or trt [cur] > trt [that] then
set cur = that
endif
set that = tp [that]
endloop
set let = 0
call DestroyTimer(tmr)
set tmr = CreateTimer()
call TimerStart(tmr, trt [cur], false, function Expire)
return this
endfunction
private function Destroy takes integer that returns nothing
local integer this = that
call LoseType(t [this])
if ttc [t [this]] == 0 and cur == t [this] then
call RefreshType()
set this = tl
set cur = 0
loop
exitwhen tp [tn [this]] == 0
if cur == 0 or trt [cur] > trt [this] then
set cur = this
endif
set this = tp [this]
endloop
set let = 0
call DestroyTimer(tmr)
set tmr = CreateTimer()
call TimerStart(tmr, trt [cur], false, function Expire)
endif
if nt [that] == 0 then
if pt [that] == 0 then
set lt [t [that]] = 0
else
set lt [t [that]] = pt [that]
set nt [pt [that]] = 0
endif
else
set nt [pt [that]] = nt [that]
set pt [nt [that]] = pt [that]
endif
set r [that] = r [0]
set r [0] = that
set t [that] = 0
set cft [that] = null
endfunction
public module Implement
static method tsCreate takes real time, code func returns integer
return Create(time, func)
endmethod
method tsDestroy takes nothing returns nothing
call Destroy(this)
endmethod
endmodule
endlibrary
Timed Function Draft (no documentation) - Interface:
JASS:
library TimedFunc requires /*New*/Table, TimerUtils
globals
private Table table
private integer total
endglobals
public function interface TimedFunction takes nothing returns boolean defaults false
private function Expire takes nothing returns nothing
local integer i = GetTimerData(GetExpiredTimer())
if not TimedFunction(table [i]).evaluate() then
call ReleaseTimer(GetExpiredTimer())
call table.remove(i)
endif
endfunction
public function Start takes real time, TimedFunction func returns nothing
local integer a = 1
local integer b = total + 1
local integer i = 0
loop
exitwhen a == b
if not table.has(a) then
set i = a
set a = b
endif
set a = a + 1
endloop
if i == 0 then
set total = total + 1
set i = total
endif
set table [i] = func
call TimerStart(NewTimerEx(i), time, true, function Expire)
endfunction
private module Init
static method onInit takes nothing returns nothing
set table = Table.create()
endmethod
endmodule
private struct Struct extends array
implement Init
endstruct
endlibrary
Timed Function Draft (no documentation) - Requires TimerStack:
JASS:
library TimedFunc requires TimerStack
private struct TS extends array
implement TimerStack_Implement
endstruct
struct TF extends array
private static integer c
private static TS array i
private static integer array r
static method start takes real time, code func returns integer
local integer this = r [0]
if this == 0 then
set c = c + 1
set this = c
else
set r [0] = r [this]
endif
set i [this] = TS.tsCreate(time, func)
return this
endmethod
method stop takes nothing returns nothing
call i [this].tsDestroy()
set r [this] = r [0]
set r [0] = this
endmethod
endstruct
endlibrary
Feel free to comment, your suggestions are gladly appreciated!
Last edited: