/************************************
*
* ElapsedGameTime
* v2.0.0.0
* By Magtheridon96
*
* - Fires a code given an elapsed game time.
* - Retrieves:
* - A Formatted Game-time String
* - The Total Elapsed Game-time in Seconds
* - The Game-time Seconds (0 <= x <= 59)
* - The Game-time Minutes (0 <= x <= 59)
* - The Game-time Hours (0 <= x)
*
* Optional Requirements:
* ----------------------
*
* - Table by Bribe
* - hiveworkshop.com/forums/jass-resources-412/snippet-new-table-188084/
*
* API:
* ----
*
* - struct ElapsedGameTime extends array
*
* - static method registerEvent takes real time, code c returns nothing
* - Registers a code that will execute at the given time.
*
* - static method start takes nothing returns nothing
* - static method pause takes nothing returns nothing
* - static method resume takes nothing returns nothing
* - These are used to start/pause/resume the game timer.
*
* - static method operator paused takes nothing returns boolean
* - static method operator running takes nothing returns boolean
* - These determine whether the system is running or not.
*
* - static method getTime takes nothing returns real
* - Gets the total elapsed game time in seconds.
*
* - static method getSeconds takes nothing returns integer
* - static method getMinutes takes nothing returns integer
* - static method getHours takes nothing returns integer
* - These static methods get the clock values.
*
* - static method getTimeSeconds takes nothing returns integer
* - Gets the elapsed game time string (Formatted)
*
* - function RegisterElapsedGameTimeEvent takes real time, code c returns nothing
* - Registers a code that will execute at the given time.
*
* - function StartGameTimer takes nothing returns nothing
* - function PauseGameTimer takes nothing returns nothing
* - function ResumeGameTimer takes nothing returns nothing
* - These are used to start/pause/resume the game timer.
*
* - function IsGameTimerPaused takes nothing returns boolean
* - function IsGameTimerRunning takes nothing returns boolean
* - These determine whether the system is running or not.
*
* - function GetElapsedGameTime takes nothing returns real
* - Gets the total elapsed game time in seconds.
*
* - function GetGameTimeSeconds takes nothing returns integer
* - function GetGameTimeMinutes takes nothing returns integer
* - function GetGameTimeHours takes nothing returns integer
* - Gets the elapsed game time hours
*
* - function GetGameTimeString takes nothing returns string
* - Gets the elapsed game time string (Formatted)
*
************************************/
library ElapsedGameTime requires optional Table
globals
// If this is set to true, you need to call ElapsedGameTime.start() manually.
private constant boolean CUSTOM_START_TIME =
false // This timer interval. If accuracy means nothing to you, increase it.
private constant real INTERVAL = 0.03125
endglobals
private module Init
private static method onInit takes nothing returns nothing
static if LIBRARY_Table then
set cache = TableArray[0x200]
set hash = Table.create()
set data = Table.create()
endif
static if not CUSTOM_START_TIME then
call start()
endif
endmethod
endmodule
struct ElapsedGameTime extends array
private static constant real FPSR = 1 / INTERVAL
private static integer current = -1
private static boolean runningX =
false private static boolean done0 =
false
// This ugliness is required.
static if LIBRARY_Table then
private static TableArray cache
private static Table hash
private static Table data
endmethod
private static method saveData
takes integer i,
boolean b
returns nothing endmethod
return hash[i]
endmethod
private static method saveHash
takes integer i,
integer d
returns nothing set hash[i] = d
endmethod
endmethod
endmethod
endmethod
else
private static constant integer HASH = -1
private static constant integer DATA = -2
endmethod
private static method saveData
takes integer i,
boolean b
returns nothing endmethod
endmethod
private static method saveHash
takes integer i,
integer d
returns nothing endmethod
endmethod
endmethod
endmethod
endif
private static method run takes nothing returns nothing
if loadData(current) then
// Increase current index
set current = current + 1
// Load number of codes registered
set count = loadHash(current)
// Clear all the conditions from the trigger.
loop
// Enqueue boolexprs
exitwhen count == 0
set count = count - 1
endloop
// Fire the boolexprs
endif
if done0 then
// Game-time Data manager (For the user)
if current == current / FPSI * FPSI then
set seconds = seconds + 1
if seconds == 60 then
set seconds = 0
set minutes = minutes + 1
if minutes == 60 then
set minutes = 0
set hours = hours + 1
endif
endif
endif
else
set done0 = true
call TimerStart(gameTimer, INTERVAL,
true,
function thistype.run)
endif
endmethod
static method operator paused
takes nothing returns boolean return not runningX
endmethod
static method operator running
takes nothing returns boolean return runningX
endmethod
static method start takes nothing returns nothing
if done0 then
call TimerStart(gameTimer, INTERVAL,
true,
function thistype.run)
else
call TimerStart(gameTimer, 0,
false,
function thistype.run)
endif
set runningX = true
endmethod
static method pause takes nothing returns nothing
set runningX = false
endmethod
static method resume takes nothing returns nothing
call start()
endmethod
static method registerEvent
takes real time,
code c
returns nothing
// Check if the data for this time doesn't exist
if not haveData(index) then
// If it doesn't exist, we hash the first
// boolexpr and set the count to 0
call saveHash(index, 0)
call saveData(index, true)
call saveCache(0, index,
Filter(c))
else
// If it exists, we hash the boolexpr and increase the count
set n = loadHash(index) + 1
call saveHash(index, n)
call saveCache(n, index,
Filter(c))
endif
endmethod
static method getTime
takes nothing returns real // You mad TimerGetElapsed?
return current / FPSR
endmethod
static method getSeconds
takes nothing returns integer return seconds
endmethod
static method getMinutes
takes nothing returns integer return minutes
endmethod
static method getHours
takes nothing returns integer return hours
endmethod
static method getTimeString
takes nothing returns string if seconds < 10 then
set s = "0" + s
endif
set s =
I2S(minutes) +
":" + s
if minutes < 10 then
set s = "0" + s
endif
set s =
I2S(hours) +
":" + s
if hours < 10 then
set s = "0" + s
endif
return s
endmethod
implement Init
endstruct
function RegisterElapsedGameTimeEvent
takes real t,
code c
returns nothing call ElapsedGameTime.registerEvent(t, c)
endfunction
function StartGameTimer takes nothing returns nothing
call ElapsedGameTime.start()
endfunction
function PauseGameTimer takes nothing returns nothing
call ElapsedGameTime.pause()
endfunction
function ResumeGameTimer takes nothing returns nothing
call ElapsedGameTime.resume()
endfunction
function IsGameTimerPaused
takes nothing returns boolean return ElapsedGameTime.paused
endfunction
function IsGameTimerRunning
takes nothing returns boolean return ElapsedGameTime.running
endfunction
function GetElapsedGameTime
takes nothing returns real return ElapsedGameTime.getTime()
endfunction
function GetGameTimeHours
takes nothing returns integer return ElapsedGameTime.getHours()
endfunction
function GetGameTimeMinutes
takes nothing returns integer return ElapsedGameTime.getMinutes()
endfunction
function GetGameTimeSeconds
takes nothing returns integer return ElapsedGameTime.getSeconds()
endfunction
function GetGameTimeString
takes nothing returns string return ElapsedGameTime.getTimeString()
endfunction
endlibrary