Warcraft 3 documentation
vJASS & Zinc Documentation
For the latest documentation about how it works vJASS and Zinc language layers for Warcraft III, please follow these links:
Jasshelper documentation - Zinc documentation - WC3 Optimizer documentation

[System] ElapsedGameTime No New Posts Codes & Snippets

Started by
Magtheridon96

0 Members and 1 Guest are viewing this topic.

[System] ElapsedGameTime
on: January 11, 2012, 01:30:59 PM
Category: Variables, System
Language: vJASS

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.

Code: jass
  1. /************************************
  2. *
  3. *   ElapsedGameTime
  4. *   v2.0.0.0
  5. *   By Magtheridon96
  6. *   
  7. *   - Fires a code given an elapsed game time.
  8. *   - Retrieves:
  9. *       - A Formatted Game-time String
  10. *       - The Total Elapsed Game-time in Seconds
  11. *       - 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 Bribe
  19. *           - hiveworkshop.com/forums/jass-resources-412/snippet-new-table-188084/
  20. *
  21. *   API:
  22. *   ----
  23. *
  24. *       - struct ElapsedGameTime extends array
  25. *
  26. *           - static method registerEvent takes real time, code c returns nothing
  27. *               - Registers a code that will execute at the given time.
  28. *
  29. *           - static method start takes nothing returns nothing
  30. *           - static method pause takes nothing returns nothing
  31. *           - static method resume takes nothing returns nothing
  32. *               - These are used to start/pause/resume the game timer.
  33. *
  34. *           - static method operator paused takes nothing returns boolean
  35. *           - static method operator running takes nothing returns boolean
  36. *               - These determine whether the system is running or not.
  37. *
  38. *           - static method getTime takes nothing returns real
  39. *               - Gets the total elapsed game time in seconds.
  40. *
  41. *           - static method getSeconds takes nothing returns integer
  42. *           - static method getMinutes takes nothing returns integer
  43. *           - static method getHours takes nothing returns integer
  44. *               - These static methods get the clock values.
  45. *
  46. *           - static method getTimeSeconds takes nothing returns integer
  47. *               - Gets the elapsed game time string (Formatted)
  48. *
  49. *       - function RegisterElapsedGameTimeEvent takes real time, code c returns nothing
  50. *           - Registers a code that will execute at the given time.
  51. *
  52. *       - function StartGameTimer takes nothing returns nothing
  53. *       - function PauseGameTimer takes nothing returns nothing
  54. *       - function ResumeGameTimer takes nothing returns nothing
  55. *           - These are used to start/pause/resume the game timer.
  56. *
  57. *       - function IsGameTimerPaused takes nothing returns boolean
  58. *       - function IsGameTimerRunning takes nothing returns boolean
  59. *           - These determine whether the system is running or not.
  60. *
  61. *       - function GetElapsedGameTime takes nothing returns real
  62. *           - Gets the total elapsed game time in seconds.
  63. *
  64. *       - function GetGameTimeSeconds takes nothing returns integer
  65. *       - function GetGameTimeMinutes takes nothing returns integer
  66. *       - function GetGameTimeHours takes nothing returns integer
  67. *           - Gets the elapsed game time hours
  68. *
  69. *       - function GetGameTimeString takes nothing returns string
  70. *           - Gets the elapsed game time string (Formatted)
  71. *
  72. ************************************/
  73. library ElapsedGameTime requires optional Table
  74.  
  75.     globals
  76.         // 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.     endglobals
  81.    
  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.             endif
  89.             static if not CUSTOM_START_TIME then
  90.                 call start()
  91.             endif
  92.         endmethod
  93.     endmodule
  94.    
  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.             endmethod
  118.             private static method saveData takes integer i, boolean b returns nothing
  119.                 set data.boolean[i] = b
  120.             endmethod
  121.             private static method loadHash takes integer i returns integer
  122.                 return hash[i]
  123.             endmethod
  124.             private static method saveHash takes integer i, integer d returns nothing
  125.                 set hash[i] = d
  126.             endmethod
  127.             private static method loadCache takes integer i1, integer i2 returns boolexpr
  128.                 return cache[i1].boolexpr[i2]
  129.             endmethod
  130.             private static method saveCache takes integer i1, integer i2, boolexpr b returns nothing
  131.                 set cache[i1].boolexpr[i2] = b
  132.             endmethod
  133.             private static method haveData takes integer i1 returns boolean
  134.                 return data.boolean.has(i1)
  135.             endmethod
  136.         else
  137.             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.             endmethod
  143.             private static method saveData takes integer i, boolean b returns nothing
  144.                 call SaveBoolean(cache, DATA, i, b)
  145.             endmethod
  146.             private static method loadHash takes integer i returns integer
  147.                 return LoadInteger(cache, HASH, i)
  148.             endmethod
  149.             private static method saveHash takes integer i, integer d returns nothing
  150.                 call SaveInteger(cache, HASH, i, d)
  151.             endmethod
  152.             private static method loadCache takes integer i1, integer i2 returns boolexpr
  153.                 return LoadBooleanExprHandle(cache, i1, i2)
  154.             endmethod
  155.             private static method saveCache takes integer i1, integer i2, boolexpr b returns nothing
  156.                 call SaveBooleanExprHandle(cache, i1, i2, b)
  157.             endmethod
  158.             private static method haveData takes integer i1 returns boolean
  159.                 return HaveSavedBoolean(cache, DATA, i1)
  160.             endmethod
  161.         endif
  162.        
  163.         private static method run takes nothing returns nothing
  164.             local integer count
  165.            
  166.             if loadData(current) then
  167.                 // Increase current index
  168.                 set current = current + 1
  169.                
  170.                 // Load number of codes registered
  171.                 set count = loadHash(current)
  172.                
  173.                 // Clear all the conditions from the trigger.
  174.                 call TriggerClearConditions(t)
  175.                
  176.                 loop
  177.                     // Enqueue boolexprs
  178.                     call TriggerAddCondition(t, loadCache(count, current))
  179.                     exitwhen count == 0
  180.                     set count = count - 1
  181.                 endloop
  182.                
  183.                 // Fire the boolexprs
  184.                 call TriggerEvaluate(t)
  185.             endif
  186.            
  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.                         endif
  198.                     endif
  199.                 endif
  200.             else
  201.                 set done0 = true
  202.                 call TimerStart(gameTimer, INTERVAL, true, function thistype.run)
  203.             endif
  204.         endmethod
  205.        
  206.         static method operator paused takes nothing returns boolean
  207.             return not runningX
  208.         endmethod
  209.        
  210.         static method operator running takes nothing returns boolean
  211.             return runningX
  212.         endmethod
  213.        
  214.         static method start takes nothing returns nothing
  215.             if done0 then
  216.                 call TimerStart(gameTimer, INTERVAL, true, function thistype.run)
  217.             else
  218.                 call TimerStart(gameTimer, 0, false, function thistype.run)
  219.             endif
  220.             set runningX = true
  221.         endmethod
  222.        
  223.         static method pause takes nothing returns nothing
  224.             call PauseTimer(gameTimer)
  225.             set runningX = false
  226.         endmethod
  227.        
  228.         static method resume takes nothing returns nothing
  229.             call start()
  230.         endmethod
  231.        
  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 exist
  237.             if not haveData(index) then
  238.                 // If it doesn't exist, we hash the first
  239.                 // boolexpr and set the count to 0
  240.                 call saveHash(index, 0)
  241.                 call saveData(index, true)
  242.                 call saveCache(0, index, Filter(c))
  243.             else
  244.                 // If it exists, we hash the boolexpr and increase the count
  245.                 set n = loadHash(index) + 1
  246.                 call saveHash(index, n)
  247.                 call saveCache(n, index, Filter(c))
  248.             endif
  249.         endmethod
  250.        
  251.         static method getTime takes nothing returns real
  252.             // You mad TimerGetElapsed?
  253.             return current / FPSR
  254.         endmethod
  255.        
  256.         static method getSeconds takes nothing returns integer
  257.             return seconds
  258.         endmethod
  259.        
  260.         static method getMinutes takes nothing returns integer
  261.             return minutes
  262.         endmethod
  263.        
  264.         static method getHours takes nothing returns integer
  265.             return hours
  266.         endmethod
  267.        
  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.             endif
  273.             set s = I2S(minutes) + ":" + s
  274.             if minutes < 10 then
  275.                 set s = "0" + s
  276.             endif
  277.             set s = I2S(hours) + ":" + s
  278.             if hours < 10 then
  279.                 set s = "0" + s
  280.             endif
  281.             return s
  282.         endmethod
  283.        
  284.         implement Init
  285.     endstruct
  286.    
  287.     function RegisterElapsedGameTimeEvent takes real t, code c returns nothing
  288.         call ElapsedGameTime.registerEvent(t, c)
  289.     endfunction
  290.    
  291.     function StartGameTimer takes nothing returns nothing
  292.         call ElapsedGameTime.start()
  293.     endfunction
  294.    
  295.     function PauseGameTimer takes nothing returns nothing
  296.         call ElapsedGameTime.pause()
  297.     endfunction
  298.    
  299.     function ResumeGameTimer takes nothing returns nothing
  300.         call ElapsedGameTime.resume()
  301.     endfunction
  302.    
  303.     function IsGameTimerPaused takes nothing returns boolean
  304.         return ElapsedGameTime.paused
  305.     endfunction
  306.    
  307.     function IsGameTimerRunning takes nothing returns boolean
  308.         return ElapsedGameTime.running
  309.     endfunction
  310.    
  311.     function GetElapsedGameTime takes nothing returns real
  312.         return ElapsedGameTime.getTime()
  313.     endfunction
  314.    
  315.     function GetGameTimeHours takes nothing returns integer
  316.         return ElapsedGameTime.getHours()
  317.     endfunction
  318.    
  319.     function GetGameTimeMinutes takes nothing returns integer
  320.         return ElapsedGameTime.getMinutes()
  321.     endfunction
  322.    
  323.     function GetGameTimeSeconds takes nothing returns integer
  324.         return ElapsedGameTime.getSeconds()
  325.     endfunction
  326.    
  327.     function GetGameTimeString takes nothing returns string
  328.         return ElapsedGameTime.getTimeString()
  329.     endfunction
  330.  
  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.
« Last Edit: December 18, 2017, 01:54:39 AM by moyack »



 

Started by cohadar

Replies: 0
Views: 2044
Codes & Snippets

Started by ashujon

Replies: 0
Views: 2976
Warcraft III Spells and Systems

Started by azlier

Replies: 0
Views: 1793
Codes & Snippets

Started by Magtheridon96

Replies: 0
Views: 2026
Codes & Snippets

Started by moyack

Replies: 6
Views: 19003
Codes & Snippets
Vivir aprendiendo.co - A place for learning stuff, in Spanish   Chaos Realm - The world of Game modders and wc3 addicts   Diplo, a gaming community   Power of Corruption, an altered melee featuring Naga and Demon. Play it now!!!   WC3JASS.com - The JASS Vault + vJASS and Zinc   Jetcraft - A Starcraft II mod   WormTastic Clan (wTc)   Warcraft RESOURCES Reforged: Modelos, mapas, proyectos y mas...