System Small Timer Stacking System

kingkingyyk3

Visitor (Welcome to the Jungle, Baby!)
Reaction score
216
This is a system use a timer to do all jobs and it is small and lite. Other interval can user defined. You may modify this system to match your needs. Speed does not important, the most important is this system can reduce the creating or recycling of timers.

JASS:
/////////////////////////////////////////////////////////////////////////////////////////////
//                        Static Timer System v0.1 beta                                    //
//                             present by kingking                                         //
//                                                                                         //
//   A small and lite timer stacking system.                                               //
//   The timer interval can be adjusted at TimeInterval to match your needs.               //
//                                                                                         //
//   Maximum can supports up to 8191 executions on this system.                            //
//   Change MAX_SIMULTANEOUS_THREAD to increase the number of maximum execution.           //
//   However, if the number is too big, Warcraft will become choopy.                       //
//                                                                                         //
//   Requires : Jass NewGen Pack v5b or later                                              //
//                                                                                         //
//   Functions provided :                                                                  //
//   call AddTimerQueue(code callback,struct)                                              //
//   call AddCustomTimerQueue(code callback, struct, real->time interval)                  //
//   call GetStruct() -> Returns struct                                                    //
//                                                                                         //
//   Callback function must be returning a boolean                                         //
//   return false when you want to stop executing.                                         //
//                                                                                         //
//   How to use?                                                                           //
//   Refer to the spell example given!                                                     //
/////////////////////////////////////////////////////////////////////////////////////////////
library STS initializer Init

globals

///////////////////////Maximum Simultaneous thread at same time//////////////////////////////
    private constant integer MAX_SIMULTANEOUS_THREAD = 100
///////////////////Increases it if your map uses this system heavily/////////////////////////
///////////////////////Maximum value is 8190/////////////////////////////////////////////////

///////////////////////Timer Callback Interval///////////////////////////////////////////////
    private constant real TIME_INTERVAL = .03125
///////FPS : ////////////////////////////////////////////////////////////////////////////////
// 0.05 -> 20fps                  // 0.025 -> 40fps
// 0.04 -> 25fps                  // 0.02 -> 50fps
// 0.03125 -> 32fps               // 0.015 -> 66fps
/////////////////////////////////////////////////////////////////////////////////////////////

/////////////Structs are stored at here~/////////////////////////////////////////////////////
    private integer array STORED_STRUCTS
/////////////////////////////////////////////////////////////////////////////////////////////

///////////Callbacks are using triggers :////////////////////////////////////////////////////
    private trigger array EXECUTORS
/////////////////////////////////////////////////////////////////////////////////////////////

//////////Numbers of triggers, will be an array value////////////////////////////////////////
    private integer N_EXECUTORS
/////////////////////////////////////////////////////////////////////////////////////////////

//////////The array table is used?///////////////////////////////////////////////////////////
    private boolean array IN_USE
/////////////////////////////////////////////////////////////////////////////////////////////

/////////Our main core is this ^^ ///////////////////////////////////////////////////////////
    private timer CORE_TIMER
/////////////////////////////////////////////////////////////////////////////////////////////

/////////Return Struct///////////////////////////////////////////////////////////////////////
    private integer CURRENT
/////////////////////////////////////////////////////////////////////////////////////////////

////////Used to increase performance at the early stage//////////////////////////////////////
    private integer SMALLEST_NUMBER
//////Prevent excess execution cause to poor peformance//////////////////////////////////////
/////////Useful when MAX_SIMULTANEOUS_THREAD is set to large value///////////////////////////

///////////////////User defined Callbacks////////////////////////////////////////////////////
    private integer MAX_USER_DEFINED_TIMERS = 48
    private timer array USER_DEFINED_TIMERS
    private trigger array USER_DEFINED_CALLBACKS
    private integer array USER_DEFINED_STRUCTS
    private boolean array IS_CUSTOM_TIMER_IN_USE
    private integer TABLE_POSITION
    private integer array USER_DEFINED_TIMERS_ID
/////////////////////////////////////////////////////////////////////////////////////////////
endglobals

private function H2I takes handle h returns integer//Simply Classic
    return h
    return 0
endfunction

private function HASH_HANDLE_ID takes handle h returns integer//Hash algorithm
    return (H2I(h)*2134900736)/524288+4096
endfunction

////////////////////////Search Blank Table///////////////////////////////////////////////////
private function SearchCustomBlank takes nothing returns integer
    local integer i = MAX_USER_DEFINED_TIMERS
    local boolean TableFull = true
    
    loop
        if IS_CUSTOM_TIMER_IN_USE<i> == false then
            set TableFull = false
        endif
    exitwhen IS_CUSTOM_TIMER_IN_USE<i> == false or i == 0
        set i = i - 1
    endloop
    
    if TableFull == false then
        return i
    else
        call BJDebugMsg(&quot;Error Found, Custom Timer Queue Fulled!&quot;)
        return MAX_USER_DEFINED_TIMERS + 1//Debug
    endif
endfunction
/////////////////////////////////////////////////////////////////////////////////////////////

//////////////////User defined time interval/////////////////////////////////////////////////
private function Call takes nothing returns nothing
    local timer t = GetExpiredTimer()
    local integer TimerId = USER_DEFINED_TIMERS_ID[HASH_HANDLE_ID(t)]
    set CURRENT= USER_DEFINED_STRUCTS[TimerId]
    set IS_CUSTOM_TIMER_IN_USE[TimerId] = TriggerEvaluate(USER_DEFINED_CALLBACKS[TimerId])
    if IS_CUSTOM_TIMER_IN_USE[TimerId] == false then
        call PauseTimer(t)
        call DestroyTimer(t)
    endif
    set t = null
endfunction

function AddCustomTimerQueue takes code c, integer structz, real interval returns nothing
    if TABLE_POSITION &lt; 0 then
        set TABLE_POSITION = SearchCustomBlank()//If tables are used up, search a black table for new instance
    else
        set TABLE_POSITION = TABLE_POSITION - 1//If the table havnt used up, use a new instance
    endif
    call TriggerClearConditions(USER_DEFINED_CALLBACKS[TABLE_POSITION])//Clean Ups
    call TriggerAddCondition(USER_DEFINED_CALLBACKS[TABLE_POSITION],Condition(c))//Add Execution
    set USER_DEFINED_TIMERS[TABLE_POSITION] = CreateTimer()
    set USER_DEFINED_STRUCTS[TABLE_POSITION] = structz//Store the struct value
    set IS_CUSTOM_TIMER_IN_USE[TABLE_POSITION] = true//Indicate the table is in use.
    set USER_DEFINED_TIMERS_ID[HASH_HANDLE_ID(USER_DEFINED_TIMERS[TABLE_POSITION])] = TABLE_POSITION
    call TimerStart(USER_DEFINED_TIMERS[TABLE_POSITION],interval,true,function Call)
endfunction

/////////////////////////////////////////////////////////////////////////////////////////////

///////////When table is used up, find any unused array for new instance!////////////////////
private function SearchBlank takes nothing returns integer
    local integer i = MAX_SIMULTANEOUS_THREAD
    local boolean TableFull = true
    
    loop
        if IN_USE<i> == false then
            set TableFull = false
        endif
    exitwhen IN_USE<i> == false or i == 0
        set i = i - 1
    endloop
    
    if TableFull == false then
        return i
    else
        call BJDebugMsg(&quot;Error Found, Timer Queue Fulled!&quot;)
        return MAX_SIMULTANEOUS_THREAD + 1//Debug
    endif
endfunction
/////////////////////////////////////////////////////////////////////////////////////////////


/////////////////Main Core functions////////////////////////////////////////////////////////
function AddTimerQueue takes code c, integer structz returns nothing
    if N_EXECUTORS &lt; 0 then
        set N_EXECUTORS = SearchBlank()//If tables are used up, search a black table for new instance
    else
        set N_EXECUTORS = N_EXECUTORS - 1//If the table havnt used up, use a new instance
        set SMALLEST_NUMBER = SMALLEST_NUMBER - 1//Limit the execution thread at the early stage
    endif
    call TriggerClearConditions(EXECUTORS[N_EXECUTORS])//Clean Ups
    call TriggerAddCondition(EXECUTORS[N_EXECUTORS],Condition(c))//Add Execution
    set STORED_STRUCTS[N_EXECUTORS] = structz//Store the struct value
    set IN_USE[N_EXECUTORS] = true//Indicate the table is in use.
endfunction
/////////////////////////////////////////////////////////////////////////////////////////////

/////////////////////Get Struct value////////////////////////////////////////////////////////
function GetStruct takes nothing returns integer
    return CURRENT
endfunction
/////////////////////////////////////////////////////////////////////////////////////////////

//////////////////Main Core Execution Calls//////////////////////////////////////////////////
private function Exec takes nothing returns nothing
    local integer i = MAX_SIMULTANEOUS_THREAD
    
    loop//Loops and executes
    exitwhen i == SMALLEST_NUMBER
    
        if IN_USE<i> == true then
            set CURRENT = STORED_STRUCTS<i>
            //
            set IN_USE<i> = TriggerEvaluate(EXECUTORS<i>)
            //If return false, no execution more
            //If return true, continue execute next time
        endif
        
        set i = i - 1
    endloop
endfunction
/////////////////////////////////////////////////////////////////////////////////////////////

//////////////Init our system!!//////////////////////////////////////////////////////////////
private function Init takes nothing returns nothing
    local integer i = 0
/////////Init values/////////////////////////////////////////
    set SMALLEST_NUMBER = MAX_SIMULTANEOUS_THREAD - 1
    set N_EXECUTORS = MAX_SIMULTANEOUS_THREAD
/////////////////////////////////////////////////////////////
////////Init the triggers to be ready working////////////////
    loop
    exitwhen i == MAX_SIMULTANEOUS_THREAD
        set IN_USE<i> = false
        set EXECUTORS<i> = CreateTrigger()
        set i = i + 1
    endloop
/////////////////////////////////////////////////////////////
/////Init our main core timer////////////////////////////////
    set CORE_TIMER = CreateTimer()
    call TimerStart(CORE_TIMER,TIME_INTERVAL,true,function Exec)
/////////////////////////////////////////////////////////////

/////Init User defined timer/////////////////////////////////
    set i = 1
    loop
    exitwhen i == MAX_USER_DEFINED_TIMERS
        set USER_DEFINED_CALLBACKS<i> = CreateTrigger()
        set IS_CUSTOM_TIMER_IN_USE<i> = false
        set i = i + 1
    endloop
    set TABLE_POSITION = MAX_USER_DEFINED_TIMERS
///////////////////////////////////////////////////////////
endfunction
/////////////////////////////////////////////////////////////////////////////////////////////
endlibrary
</i></i></i></i></i></i></i></i></i></i></i></i>


This system's name matches it's size -> SMALL. :D :p :)
 

Azlier

Old World Ghost
Reaction score
461
How does it act like KT... at all? KT lets you specify a period, it doesn't lag after just 100 instances, it uses more than one timer...
 

Jesus4Lyf

Good Idea™
Reaction score
397
Mmm
+Graveyard vote.

Timer systems: Jass Help first? *Shrugs* I'll say a few words.

  1. KT2 is much faster. There is a reason there is a substantial amount of code: It works and works well, and was developed to do so over a long period of time.
  2. You have an unnecessary O(n) complexity search on adding.
  3. Less code doesn't actually matter if costs efficiency. Code compresses to next to nothing in an MPQ.
  4. This has been done before. It's called TimerTicker. TT does this MUCH more efficiently (and dynamically), and it's still significantly slower than KT2 which allows multiple periods.

Uhh... I was gonna give a benchmark estimate (since there's no need to actually benchmark this...), but then I realised that you loop through 100 instances by default. If you DIDN'T do this, I'd have estimated 30% slower than KT2. Since you do do this, I say this system should never be used. :) (And it does not have a linear comparison to KT2, percentage-wise. I'd expect it to range between 98% slower and 30% slower, and then not work at all.)

I suggest you study the code in KT2 a bit. It does not loop over a bunch of triggers and evaluate them, TT does that. KT2 does something else, and if this system did the same, it would only have one trigger. ;)
 

Jesus4Lyf

Good Idea™
Reaction score
397
I definitely like the (true/false)/(one line start) functionality more than the classic style of TU and such. It's neater to use. You could make this faster than TU Blue (it may already be for certain numbers of instances) but TU red is pretty efficient. KT2 will smash this even if you made it faster, though (and it smashes TU too).
 

Jesus4Lyf

Good Idea™
Reaction score
397
It's very complicated. That's why I'm the only person to ever do it (except for CaptainGriffen, but mine was faster and with a much simpler interface).

Oh, and you NEED to write the whole data storage in hack-job linked lists to achieve it. But you didn't manage to get even a static array to have a dynamic length... So to be honest, I don't think you'd understand.

Nonetheless, KT2 is technically open source, so you could like... read it... to find out... lol =/
 

kingkingyyk3

Visitor (Welcome to the Jungle, Baby!)
Reaction score
216
KT2 too confused me. T.T I am using this system in all my map without problem. lol. :D
That's why I made this system for the sake of SMALL and SIMPLE.
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +2
  • V-SNES V-SNES:
    Happy Friday!
    +1
  • The Helper The Helper:
    New recipe is another summer dessert Berry and Peach Cheesecake - https://www.thehelper.net/threads/recipe-berry-and-peach-cheesecake.194169/

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top