Advertisement
Guest User

Goldkiller - TimeFactory

a guest
Dec 28th, 2014
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 6.36 KB | None | 0 0
  1. /*
  2.     TimeFactory.inc
  3.         Autor: Daniel "Goldkiller", 2014
  4.         Voraussetzung: CTime Plugin, RyDeR`
  5. */
  6.  
  7. #include <CTime>
  8.  
  9. enum {
  10.     INGAME_TO_REAL,
  11.     REAL_TO_INGAME
  12. }
  13.  
  14. enum e_TimeFactory {
  15.     TF_iRealDate, // Tick zu dem Script beginnt zu zählen
  16.     TF_iIngameDate, // Tick bei dem die Zeit beginnt
  17.     TF_iSpeed // Geschwindigkeit (zb 2x)
  18. }
  19.  
  20. new g_TimeFactory[e_TimeFactory];
  21. /*
  22.     Funktion zum Starten der Berechnung. Muss als erstes gemacht werden bevor
  23.     alle anderen Funktionen genutzt werden können.
  24.    
  25.     realDate
  26.         UnixTimestamp bei dem in der ECHTEN Welt die Zeit beginnt zu rechnen
  27.         Sinnvoll wäre ein UnixTimestamp zB bei Server Eröffnung um ab da die Zeitrechnung
  28.         laufen zu lassen
  29.  
  30.     ingameDate
  31.         UnixTimestamp bei dem die Zeitrechnung Ingame beginnt.
  32.        
  33.    
  34.     speed
  35.         Integer um die Zeitrechnung zu beschleunigen (2 - 16)
  36. */
  37.  
  38. stock TimeFactory(realdate = 0, ingamedate = 0 , speed = 4) {
  39.     if(!realdate) {
  40.         g_TimeFactory[TF_iRealDate] = gettime();
  41.     }
  42.     else {
  43.         g_TimeFactory[TF_iRealDate] = realdate;
  44.     }
  45.     if(!ingamedate) {
  46.         g_TimeFactory[TF_iIngameDate] = gettime();
  47.     }
  48.     else {
  49.         g_TimeFactory[TF_iIngameDate] = ingamedate;
  50.     }
  51.     if( speed < 2 || speed > 16 ) {
  52.         printf("TimeFactory: speed darf nur zwischen 2 und 16 liegen!");
  53.         return 0;
  54.     }
  55.     else if( (speed % 2) != 0 ) {
  56.         printf("TimeFactory: speed darf nur vielfaches von 2 sein (%d)!",speed);
  57.         return 0;
  58.     }
  59.     else {
  60.         g_TimeFactory[TF_iSpeed] = speed;
  61.     }
  62.     /*
  63.     printf("g_TimeFactory[TF_iRealDate] = %d", g_TimeFactory[TF_iRealDate] );
  64.     printf("g_TimeFactory[TF_iIngameDate] = %d", g_TimeFactory[TF_iIngameDate] );
  65.     printf("g_TimeFactory[TF_iSpeed] = %d", g_TimeFactory[TF_iSpeed] );
  66.     */
  67.     return 1;
  68. }
  69.  
  70. /*
  71.     TimeFactory_RealDate
  72.         Gibt den Unix Timestamp von Initialwert realDate an
  73.  
  74. */
  75. stock TimeFactory_RealDate() {
  76.     return g_TimeFactory[TF_iRealDate];
  77. }
  78.  
  79. /*
  80.     TimeFactory_IngameInitDate
  81.         Gibt den Unix Timestamp von Initialwert ingameDate an
  82.  
  83. */
  84. stock TimeFactory_IngameInitDate() {
  85.     return g_TimeFactory[TF_iIngameDate];
  86. }
  87.  
  88. /*
  89.  TimeFactory_Speed
  90.         Gibt den Zeitfaktor an, der IngameInitDate beschleunigt
  91.  
  92. */
  93. stock TimeFactory_Speed() {
  94.     return g_TimeFactory[TF_iSpeed];
  95. }
  96.  
  97. /*
  98.     TimeFactory_GetElapsedSec
  99.         Zeit in Sec die Vergangen ist bis <jetzt>
  100. */
  101. stock TimeFactory_GetElapsedSec( since = 0 ) {
  102.     new
  103.         fake_sec;
  104.     if(!since) {
  105.         since = gettime();
  106.     }
  107.     fake_sec = ( since - TimeFactory_RealDate() ) * TimeFactory_Speed();
  108.     return fake_sec;
  109. }
  110.  
  111. /*
  112.     TimeFactory_InGameUnix
  113.         Gibt den UnixTimestamp der InGame aktuell ist.
  114.         Ist immer größer/gleich dem IngameDate bzw "ingamedate" aus TimeFactory
  115.  
  116. */
  117. stock TimeFactory_InGameUnix() {
  118.     return ( TimeFactory_IngameInitDate() + TimeFactory_GetElapsedSec() );
  119. }
  120.  
  121. /*
  122.     tm:TimeFactory_InGameDate
  123.     Gibt eine Variable zurück, die basierend auf TimeFactory_InGameUnix
  124.     ein Array zurück gibt mit folgender Struktur:
  125.     var {
  126.         tm_sec, //sekunde
  127.         tm_min, // Minute
  128.         tm_hour, // Stunde
  129.         tm_mday, // Monatstag
  130.         tm_mon, // 0 Jan,1 Feb, 2 Mär, 3 Apr ...
  131.         tm_year, // X - 1900
  132.         tm_wday, // 0 SO,1 MO,2 TU,3 WE,4 TH,5 FR, 6 SA
  133.         tm_yday, // Tag des Jahres
  134.         tm_isdst // DayLightSaving
  135.     }
  136.     Mit dieser Struktur wird das Datum und Zeit genau beschrieben.
  137. */
  138. stock tm:TimeFactory_InGameDate() {
  139.     new
  140.         tm <tTime>;
  141.     localtime( Time:TimeFactory_InGameUnix(), tTime);
  142.     return tTime;
  143. }
  144.  
  145. stock TimeFactory_PrintDate( tm<tTime>) {
  146.     printf("tm_sec %d",tTime[tm_sec]);
  147.     printf("tm_min %d",tTime[tm_min]);
  148.     printf("tm_hour %d",tTime[tm_hour]);
  149.     printf("tm_mday %d",tTime[tm_mday]);
  150.     printf("tm_mon %d",tTime[tm_mon]);
  151.     printf("tm_year %d",tTime[tm_year]);
  152.     printf("tm_wday %d",tTime[tm_wday]);
  153.     printf("tm_yday %d",tTime[tm_yday]);
  154.     printf("tm_isdst %d",tTime[tm_isdst]);
  155. }
  156. /*
  157.     Konvertiert ein Datum von:
  158.         InGame zur Realen Zeiten
  159.         Realen Zeite zu InGame
  160.        
  161. */
  162. stock tm:TimeFactory_Convert(type,second,minute,hour,dayofmonth,month,year) {
  163.     new
  164.         tm <tTime>
  165.     ;
  166.     localtime(time(), tTime);
  167.     if( type != INGAME_TO_REAL && type != REAL_TO_INGAME ) {
  168.         printf("TimeFacoty_Convert: Falscher Wert für \"type\" (%d)",type);
  169.         return tTime;
  170.     }
  171.     //
  172.     tTime[tm_sec] = second;
  173.     tTime[tm_min] = minute;
  174.     tTime[tm_hour] = hour;
  175.     tTime[tm_mday] = dayofmonth;
  176.     tTime[tm_mon] = month - 1;
  177.     tTime[tm_year] = year - 1900;
  178.     //
  179.     switch(type) {
  180.         case INGAME_TO_REAL: {
  181.             localtime( Time:( TimeFactory_RealDate() + ( mktime(tTime) - TimeFactory_RealDate() ) / TimeFactory_Speed()), tTime );
  182.         }
  183.         case REAL_TO_INGAME: {
  184.             localtime( Time:( TimeFactory_IngameInitDate() + ( mktime(tTime) - TimeFactory_RealDate() ) * TimeFactory_Speed()), tTime );
  185.         }
  186.     }
  187.     return tTime;
  188. }
  189. /*
  190.     InGame_Second
  191.         - Selbsterklärend
  192. */
  193. stock InGame_Second() {
  194.     new
  195.         sec,
  196.         tm <tTime>
  197.     ;
  198.     tTime = TimeFactory_InGameDate();
  199.     sec = tTime[tm_sec];
  200.     return sec;
  201. }
  202. /*
  203.     InGame_Hour
  204.         - Selbsterklärend
  205. */
  206.  
  207. stock InGame_Hour() {
  208.     new
  209.         hour,
  210.         tm <tTime>
  211.     ;
  212.     tTime = TimeFactory_InGameDate();
  213.     hour = tTime[tm_hour];
  214.     return hour;
  215. }
  216. /*
  217.     InGame_Minute
  218.         - Selbsterklärend
  219. */
  220.  
  221. stock InGame_Minute() {
  222.     new
  223.         minute,
  224.         tm <tTime>
  225.     ;
  226.     tTime = TimeFactory_InGameDate();
  227.     minute = tTime[tm_min];
  228.     return minute;
  229. }
  230. /*
  231.     InGame_DayOfMonth
  232.         - Selbsterklärend
  233. */
  234.  
  235. stock InGame_DayOfMonth() {
  236.     new
  237.         dayofmonth,
  238.         tm <tTime>
  239.     ;
  240.     tTime = TimeFactory_InGameDate();
  241.     dayofmonth = tTime[tm_mday];
  242.     return dayofmonth;
  243. }
  244. /*
  245.     InGame_Month
  246.         - Selbsterklärend
  247. */
  248.  
  249. stock InGame_Month() {
  250.     new
  251.         month,
  252.         tm <tTime>
  253.     ;
  254.     tTime = TimeFactory_InGameDate();
  255.     month = tTime[tm_mon];
  256.     return month;
  257. }
  258. /*
  259.     InGame_Year
  260.         - Selbsterklärend
  261. */
  262.  
  263. stock InGame_Year() {
  264.     new
  265.         year,
  266.         tm <tTime>
  267.     ;
  268.     tTime = TimeFactory_InGameDate();
  269.     year = tTime[tm_year] + 1900;
  270.     return year;
  271. }
  272. /*
  273.     InGame_WeekDay
  274.         Wochentag
  275.         0 ist Montag
  276.         ...
  277.         6 ist Sonntag
  278. */
  279.  
  280. stock InGame_WeekDay() {
  281.     new
  282.         weekday,
  283.         tm <tTime>
  284.     ;
  285.     tTime = TimeFactory_InGameDate();
  286.     weekday = tTime[tm_wday] + 6 ;
  287.     weekday %= 7;
  288.     return weekday;
  289. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement