- Joined
- Apr 27, 2011
- Messages
- 272
This thing is actually inspired from the knockback system i submitted last time, because i just found out that i can make a good timer utility based on its coding structure. That's why i decided to break it down into this to make give it more applications.
system code:
sample usage:
system code:
JASS:
library PseudoTimer // v1.0.0
//===========================================================================
// PseudoTimer by Alain.Mark
//
// -Info-
// -This is a great timer system, simply because it allows you to create and manipulate
// "PseudoTimers", these things aren't handles but they work like timers in a much better
// fashion. In addition, these "PseudoTimers" have an index of their own that you can use
// to easily attach values to them. The indexing is very safe and the index retrieval
// is also very fast. Unlike timers you can actually add multiple callback functions to "PseudoTimers"
//
// -API-
// -function CreatePseudoTimer takes nothing returns integer
// -function ReleasePseudoTimer takes integer whichPseudoTimer returns nothing
// -function StartPseudoTimer takes integer whichPseudoTimer, real duration returns nothing
// -function OnPseudoTimerCallback takes integer whichPseudoTimer, code whichCode returns nothing
// -function IsOneSecond takes nothing returns boolean
// -function ThisPseudoTimer takes nothing returns integer // returns the current pseudo-timer
// -function GetPseudoTimerId takes integer whichPseudoTimer returns integer
//
//===========================================================================
globals
private constant timer Tt = CreateTimer() // this is the only timer that the system will need
private constant real BASE_EXPIRATION = 0.031250000 // pseudotimers will run loop at this constant period
endglobals
//===========================================================================
globals
private integer n=-1
private integer array O
private integer e
private integer thisPseudoTimer
endglobals
//===========================================================================
globals
private integer id =-1
private integer array idQ // pseudo-timer id queue
endglobals
//===========================================================================
globals
private integer t =-1
private integer m = 0
private integer array rt // recycled pseudo-timers
private trigger array cQ // trigger queue
endglobals
//===========================================================================
globals
private real array rQ // pseudo-timer duration queue
endglobals
//===========================================================================
globals
private real sec=0.00
endglobals
//===========================================================================
private function OnLoop takes nothing returns nothing
set e=n
set sec=sec+BASE_EXPIRATION
loop
set thisPseudoTimer=O[e] // 2-d array acces
if(rQ[thisPseudoTimer]!=0.00)then
set rQ[thisPseudoTimer]=rQ[thisPseudoTimer]-BASE_EXPIRATION
call TriggerEvaluate(cQ[thisPseudoTimer])
else
call TriggerClearConditions(cQ[thisPseudoTimer])
set rQ[thisPseudoTimer]=rQ[O[n]] // takes the top and places it on a free one, example: [1][2][3] -> [-free-][2][3] -> [3][2]
set cQ[thisPseudoTimer]=cQ[O[n]]
set idQ[thisPseudoTimer]=idQ[O[n]]
set rt[m]=thisPseudoTimer
set m=m+1
set n=n-1
if(n==-1)then
call PauseTimer(Tt)
endif
endif
set e=e-1
exitwhen e<0
endloop
if(sec==1.00)then
set sec=0.00
endif
endfunction
//===========================================================================
function CreatePseudoTimer takes nothing returns integer
if(m==0)then
set t=t+1
set id=id+1
set cQ[t]=CreateTrigger() // initialize the pseudo-timer's trigger sheet
set idQ[t]=id // set the psuedo-timer's index
return t
endif
set m=m-1
return rt[m] // returns a recycled pseudo-timer
endfunction
//===========================================================================
function FreezePseudoTimer takes integer i returns nothing
set rQ[i]=0.00
endfunction
//===========================================================================
function StartPseudoTimer takes integer i, real r returns nothing
set n=n+1
set rQ[i]=r
set O[n]=i
if(n==0)then
call TimerStart(Tt,BASE_EXPIRATION,true,function OnLoop)
endif
endfunction
//===========================================================================
function OnPseudoTimerCallback takes integer i, code c returns nothing
call TriggerAddCondition(cQ[i],Filter(c))
return
endfunction
//===========================================================================
function IsOneSecond takes nothing returns boolean
return sec==1.00
endfunction
//===========================================================================
function ThisPseudoTimer takes nothing returns integer
return thisPseudoTimer
endfunction
//===========================================================================
function GetPseudoTimerId takes integer i returns integer
return idQ[i] // say goodbye to dangerous offsetting and slow hashtables
endfunction
//===========================================================================
endlibrary
sample usage:
JASS:
scope sample initializer onInit
globals
private string array STR
endglobals
private function A takes nothing returns nothing
call BJDebugMsg(STR[GetPseudoTimerId(ThisTimer())])
endfunction
private function B takes nothing returns nothing
if(IsOneSecond())then
call BJDebugMsg(STR[GetPseudoTimerId(ThisTimer())])
endif
endfunction
private function onInit takes nothing returns nothing
local integer pseudoTimerA=CreatePseudoTimer() // as you can see a PseudoTimer is actually an integer, but i do not suggest using it as an index for arrays and stuff. (that's the reason why i provided the GetPseudoTimerId for that purpose)
local integer pseudoTimerB=CreatePseudoTimer()
local integer idA=GetPseudoTimerId(A)
local integer idB=GetPseudoTimerId(B)
call OnPseudoTimerCallback(pseudoTimerA,function A)
call OnPseudoTimerCallback(pseudoTimerA,function B)
set STR[idA]="0.03125"
set STR[idB]="1.00000"
call StartPseudoTimer(pseudoTimerA,100.00)
call StartPseudoTimer(pseudoTimerB,100.00)
endfunction
endscope
Last edited: