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

[Snippet] Game Clock

nel · 563

0 Members and 1 Guest are viewing this topic.

Rating

Average Score - 5 / 5

« Created: December 22, 2018, 10:12:05 AM by moyack »

[Snippet] Game Clock
on: December 21, 2018, 03:09:29 AM
Category: Variables
Language: ZINC

Game Clock

It allows you format the time and get the elapsed time.

Code: jass
  1. //! zinc
  2. library GameClock { /* v1.0.0.7
  3. *************************************************************************************
  4. *
  5. *    Game Clock
  6. *        by nel
  7. *
  8. *       It allows you format the time and get the elapsed time.
  9. *
  10. ************************************************************************************
  11. *
  12. *   SETTING
  13. *
  14. */
  15.     private constant string DELIMITER = " : "; /* It used to seperate the hour,
  16.                                                   minute, and second */
  17. /*
  18. ************************************************************************************
  19. *
  20. *    struct GameClock extends array
  21. *
  22. *        static method FormatTime takes real seconds return string
  23. *            - It returns time as hh/mm/ss format.
  24. *
  25. *        static method operator ElapsedTime takes nothing returns real
  26. *            - It returns elapsed time.
  27. *
  28. *        static method operator ElapsedTimeEx takes nothing returns string
  29. *            - It returns elapsed time as hh/mm/ss format.
  30. *
  31. *        static method operator Delimiter takes nothing returns string
  32. *            - It returns GameClock's delimiter.
  33. *
  34. *************************************************************************************
  35. *
  36. *    Issue:
  37. *
  38. *       This library will not give you an exact current elapsed time.
  39. *
  40. ************************************************************************************/
  41.  
  42.     //! textmacro GAMECLOCK_TIMEFORMAT takes Time, delimiter
  43.         if( $Time$ < 10 ) {
  44.             format = format + "0" + I2S( $Time$ ) $delimiter$;
  45.         } else {
  46.             format = format + I2S( $Time$ ) $delimiter$;
  47.         }
  48.     //! endtextmacro
  49.  
  50.     private constant timer TIMER = CreateTimer();
  51.  
  52.     public { struct GameClock [] {
  53.         public {
  54.             static method FormatTime( real seconds ) -> string {
  55.                 integer s = R2I( ModuloReal( seconds, 60 ) );
  56.                 integer m = R2I( ModuloReal( seconds, 3600 ) / 60 );
  57.                 integer h = R2I( seconds / 3600 );
  58.                 string format = "";
  59.  
  60.                 //! runtextmacro GAMECLOCK_TIMEFORMAT( "h", "+ DELIMITER" )
  61.                 //! runtextmacro GAMECLOCK_TIMEFORMAT( "m", "+ DELIMITER" )
  62.                 //! runtextmacro GAMECLOCK_TIMEFORMAT( "s", "" )
  63.  
  64.                 return format;
  65.             }
  66.  
  67.             static method operator ElapsedTime() -> real {
  68.                 return TimerGetElapsed( TIMER );
  69.             }
  70.  
  71.             static method operator ElapsedTimeEx() -> string {
  72.                 return thistype.FormatTime( TimerGetElapsed(TIMER) );
  73.             }
  74.  
  75.             static method operator Delimiter() -> string {
  76.                 return DELIMITER;
  77.             }
  78.         }
  79.  
  80.         private static method onInit() {
  81.             TimerStart( TIMER, 100000000, true, null );
  82.         }
  83.     }}
  84. }
  85. //! endzinc
« Last Edit: June 23, 2019, 06:52:49 AM by nel »



Re: [Snippet] Game Clock
Reply #1 on: December 22, 2018, 10:11:24 AM

Nice code, clean and to the point :)

I think adding a demo map showing this code working could be a good thing. I did one for you


Re: [Snippet] Game Clock
Reply #2 on: December 22, 2018, 03:10:35 PM

Nice code, clean and to the point :)

I think adding a demo map showing this code working could be a good thing. I did one for you

I think you don't need a demo map for this, because the demo code is enough to understand how this snippet works. :v

*meh meh*



 

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)