14 years
edited 8 years
Don't be fooled by the title, this is more than just a simple Elapsed Game Time manager.
This system also allows you to register elapsed game-time events.
With the current configuration, the limit is 3.41 years in the future. I hope this isn't inconvenient.
I apologize for this limitation and I also hope it won't make this resource seem useless to you.
You can also get a formatted elapsed game-time string.
You're probably asking why using this is a plus. These are the reasons:
- One timer to handle all game-time events
- Efficient Boolexpr Enqueuing to reduce number of TriggerEvaluations to one per time and not one per boolexpr.
- No need for another Game-time library to get the game-time.
- Insanely fast Game-time 'Get' function
Feel free to comment..
Constructive criticism will be accepted and is highly appreciated.
This system also allows you to register elapsed game-time events.
With the current configuration, the limit is 3.41 years in the future. I hope this isn't inconvenient.
I apologize for this limitation and I also hope it won't make this resource seem useless to you.
You can also get a formatted elapsed game-time string.
1 /************************************2 *3 * ElapsedGameTime4 * v2.0.0.05 * By Magtheridon966 * 7 * - Fires a code given an elapsed game time.8 * - Retrieves:9 * - A Formatted Game-time String10 * - The Total Elapsed Game-time in Seconds11 * - The Game-time Seconds (0 <= x <= 59)12 * - The Game-time Minutes (0 <= x <= 59)13 * - The Game-time Hours (0 <= x)14 *15 * Optional Requirements:16 * ----------------------17 *18 * - Table by Bribe19 * - hiveworkshop.com/forums/jass-resources-412/snippet-new-table-188084/20 *21 * API:22 * ----23 *24 * - struct ElapsedGameTime extends array25 *26 * - static method registerEvent takes real time, code c returns nothing27 * - Registers a code that will execute at the given time.28 *29 * - static method start takes nothing returns nothing30 * - static method pause takes nothing returns nothing31 * - static method resume takes nothing returns nothing32 * - These are used to start/pause/resume the game timer.33 *34 * - static method operator paused takes nothing returns boolean35 * - static method operator running takes nothing returns boolean36 * - These determine whether the system is running or not.37 *38 * - static method getTime takes nothing returns real39 * - Gets the total elapsed game time in seconds.40 *41 * - static method getSeconds takes nothing returns integer42 * - static method getMinutes takes nothing returns integer43 * - static method getHours takes nothing returns integer44 * - These static methods get the clock values.45 *46 * - static method getTimeSeconds takes nothing returns integer47 * - Gets the elapsed game time string (Formatted)48 *49 * - function RegisterElapsedGameTimeEvent takes real time, code c returns nothing50 * - Registers a code that will execute at the given time.51 *52 * - function StartGameTimer takes nothing returns nothing53 * - function PauseGameTimer takes nothing returns nothing54 * - function ResumeGameTimer takes nothing returns nothing55 * - These are used to start/pause/resume the game timer.56 *57 * - function IsGameTimerPaused takes nothing returns boolean58 * - function IsGameTimerRunning takes nothing returns boolean59 * - These determine whether the system is running or not.60 *61 * - function GetElapsedGameTime takes nothing returns real62 * - Gets the total elapsed game time in seconds.63 *64 * - function GetGameTimeSeconds takes nothing returns integer65 * - function GetGameTimeMinutes takes nothing returns integer66 * - function GetGameTimeHours takes nothing returns integer67 * - Gets the elapsed game time hours68 *69 * - function GetGameTimeString takes nothing returns string70 * - Gets the elapsed game time string (Formatted)71 *72 ************************************/73 library ElapsedGameTime requires optional Table
74 75 globals76 // If this is set to true, you need to call ElapsedGameTime.start() manually.77 private constant boolean CUSTOM_START_TIME = false
78 // This timer interval. If accuracy means nothing to you, increase it.79 private constant real INTERVAL = 0.03125
80 endglobals81
82 private module Init
83 private static method onInit takes nothing returns nothing
84 static if LIBRARY_Table then
85 set cache = TableArray[0x200]
86 set hash = Table.create()
87 set data = Table.create()
88 endif89 static if not CUSTOM_START_TIME then
90 call start()
91 endif92 endmethod93 endmodule94
95 struct ElapsedGameTime extends array
96 private static constant real FPSR = 1 / INTERVAL
97 private static constant integer FPSI = R2I(FPSR)
98
99 private static integer seconds = 0
100 private static integer minutes = 0
101 private static integer hours = 0
102
103 private static integer current = -1
104 private static trigger t = CreateTrigger()
105 private static timer gameTimer = CreateTimer()
106
107 private static boolean runningX = false
108 private static boolean done0 = false
109
110 // This ugliness is required.111 static if LIBRARY_Table then
112 private static TableArray cache
113 private static Table hash
114 private static Table data
115 private static method loadData takes integer i returns boolean
116 return data.boolean[i]
117 endmethod118 private static method saveData takes integer i, boolean b returns nothing
119 set data.boolean[i] = b
120 endmethod121 private static method loadHash takes integer i returns integer
122 return hash[i]
123 endmethod124 private static method saveHash takes integer i, integer d returns nothing
125 set hash[i] = d
126 endmethod127 private static method loadCache takes integer i1, integer i2 returns boolexpr
128 return cache[i1].boolexpr[i2]
129 endmethod130 private static method saveCache takes integer i1, integer i2, boolexpr b returns nothing
131 set cache[i1].boolexpr[i2] = b
132 endmethod133 private static method haveData takes integer i1 returns boolean
134 return data.boolean.has(i1)
135 endmethod136 else137 private static hashtable cache = InitHashtable()
138 private static constant integer HASH = -1
139 private static constant integer DATA = -2
140 private static method loadData takes integer i returns boolean
141 return LoadBoolean(cache, DATA, i)
142 endmethod143 private static method saveData takes integer i, boolean b returns nothing
144 call SaveBoolean(cache, DATA, i, b)
145 endmethod146 private static method loadHash takes integer i returns integer
147 return LoadInteger(cache, HASH, i)
148 endmethod149 private static method saveHash takes integer i, integer d returns nothing
150 call SaveInteger(cache, HASH, i, d)
151 endmethod152 private static method loadCache takes integer i1, integer i2 returns boolexpr
153 return LoadBooleanExprHandle(cache, i1, i2)
154 endmethod155 private static method saveCache takes integer i1, integer i2, boolexpr b returns nothing
156 call SaveBooleanExprHandle(cache, i1, i2, b)
157 endmethod158 private static method haveData takes integer i1 returns boolean
159 return HaveSavedBoolean(cache, DATA, i1)
160 endmethod161 endif162
163 private static method run takes nothing returns nothing
164 local integer count
165
166 if loadData(current) then
167 // Increase current index168 set current = current + 1
169
170 // Load number of codes registered171 set count = loadHash(current)
172
173 // Clear all the conditions from the trigger.174 call TriggerClearConditions(t)
175
176 loop177 // Enqueue boolexprs178 call TriggerAddCondition(t, loadCache(count, current))
179 exitwhen count == 0
180 set count = count - 1
181 endloop182
183 // Fire the boolexprs184 call TriggerEvaluate(t)
185 endif186
187 if done0 then
188 // Game-time Data manager (For the user)189 if current == current / FPSI * FPSI then
190 set seconds = seconds + 1
191 if seconds == 60 then
192 set seconds = 0
193 set minutes = minutes + 1
194 if minutes == 60 then
195 set minutes = 0
196 set hours = hours + 1
197 endif198 endif199 endif200 else201 set done0 = true
202 call TimerStart(gameTimer, INTERVAL, true, function thistype.run)
203 endif204 endmethod205
206 static method operator paused takes nothing returns boolean
207 return not runningX
208 endmethod209
210 static method operator running takes nothing returns boolean
211 return runningX212 endmethod213
214 static method start takes nothing returns nothing
215 if done0 then
216 call TimerStart(gameTimer, INTERVAL, true, function thistype.run)
217 else218 call TimerStart(gameTimer, 0, false, function thistype.run)
219 endif220 set runningX = true
221 endmethod222
223 static method pause takes nothing returns nothing
224 call PauseTimer(gameTimer)
225 set runningX = false
226 endmethod227
228 static method resume takes nothing returns nothing
229 call start()
230 endmethod231
232 static method registerEvent takes real time, code c returns nothing
233 local integer index = R2I(time * FPSI)
234 local integer n
235
236 // Check if the data for this time doesn't exist237 if not haveData(index) then
238 // If it doesn't exist, we hash the first239 // boolexpr and set the count to 0240 call saveHash(index, 0)
241 call saveData(index, true)
242 call saveCache(0, index, Filter(c))
243 else244 // If it exists, we hash the boolexpr and increase the count245 set n = loadHash(index) + 1
246 call saveHash(index, n)
247 call saveCache(n, index, Filter(c))
248 endif249 endmethod250
251 static method getTime takes nothing returns real
252 // You mad TimerGetElapsed?253 return current / FPSR
254 endmethod255
256 static method getSeconds takes nothing returns integer
257 return seconds258 endmethod259
260 static method getMinutes takes nothing returns integer
261 return minutes262 endmethod263
264 static method getHours takes nothing returns integer
265 return hours266 endmethod267
268 static method getTimeString takes nothing returns string
269 local string s = I2S(seconds)
270 if seconds < 10 then
271 set s = "0" + s
272 endif273 set s = I2S(minutes) + ":" + s
274 if minutes < 10 then
275 set s = "0" + s
276 endif277 set s = I2S(hours) + ":" + s
278 if hours < 10 then
279 set s = "0" + s
280 endif281 return s282 endmethod283
284 implement Init285 endstruct286
287 function RegisterElapsedGameTimeEvent takes real t, code c returns nothing
288 call ElapsedGameTime.registerEvent(t, c)
289 endfunction290
291 function StartGameTimer takes nothing returns nothing
292 call ElapsedGameTime.start()
293 endfunction294
295 function PauseGameTimer takes nothing returns nothing
296 call ElapsedGameTime.pause()
297 endfunction298
299 function ResumeGameTimer takes nothing returns nothing
300 call ElapsedGameTime.resume()
301 endfunction302
303 function IsGameTimerPaused takes nothing returns boolean
304 return ElapsedGameTime.paused
305 endfunction306
307 function IsGameTimerRunning takes nothing returns boolean
308 return ElapsedGameTime.running
309 endfunction310
311 function GetElapsedGameTime takes nothing returns real
312 return ElapsedGameTime.getTime()
313 endfunction314
315 function GetGameTimeHours takes nothing returns integer
316 return ElapsedGameTime.getHours()
317 endfunction318
319 function GetGameTimeMinutes takes nothing returns integer
320 return ElapsedGameTime.getMinutes()
321 endfunction322
323 function GetGameTimeSeconds takes nothing returns integer
324 return ElapsedGameTime.getSeconds()
325 endfunction326
327 function GetGameTimeString takes nothing returns string
328 return ElapsedGameTime.getTimeString()
329 endfunction330 331 endlibrary
You're probably asking why using this is a plus. These are the reasons:
- One timer to handle all game-time events
- Efficient Boolexpr Enqueuing to reduce number of TriggerEvaluations to one per time and not one per boolexpr.
- No need for another Game-time library to get the game-time.
- Insanely fast Game-time 'Get' function
Feel free to comment..
Constructive criticism will be accepted and is highly appreciated.