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.
This system's name matches it's size -> SMALL.
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("Error Found, Custom Timer Queue Fulled!")
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 < 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("Error Found, Timer Queue Fulled!")
return MAX_SIMULTANEOUS_THREAD + 1//Debug
endif
endfunction
/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////Main Core functions////////////////////////////////////////////////////////
function AddTimerQueue takes code c, integer structz returns nothing
if N_EXECUTORS < 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.