Advertisement
Kwarde

AdvancedTime 0.0.23-RC

Oct 29th, 2014
583
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 5.51 KB | None | 0 0
  1. #define FILTERSCRIPT
  2.  
  3. #include <a_samp>
  4. #include <TimestampToDate>
  5.  
  6. #define FS_NAME         "Advanced Time"
  7. #define FS_VERS_MAJ     0
  8. #define FS_VERS_MIN     0
  9. #define FS_VERS_BLD     32
  10. #define FS_VERS_TAG     "RC"
  11.  
  12. //==== You may edit this part ====//
  13. #define DEFAULT         TIME_NORMAL //Default time type (see below)
  14. #define D_YEAR          2014 //Default year when first opening the filterscript
  15. #define D_MON           10 //Default month when first opening the filterscript
  16. #define D_DAY           30 //Default day
  17. #define D_HOUR          12 //Default hour
  18. #define D_MIN           0 //Default minute
  19. #define D_SEC           0 //Default second
  20. #define GMT             1 //The GMT's timezone of your server!
  21. //===== End of this part.  =====//
  22. //Seriously, it's not necesarry to edit stuff below here. Get some coffee instead =D
  23.  
  24. #define TIME_DEFAULT    0 //GTA:SA time - 1m = 1s
  25. #define TIME_NORMAL     1 //Normal time rate, can be changed at any time - 1m = 1m
  26. #define TIME_SYNC       2 //Server synchronised time
  27.  
  28. #define CB:%0(%1)       forward %0(%1); public %0(%1) //TIP: Use it for your own scripts. Really handy :)
  29. #define ploop(%0)       for (new %0 = 0; %0 < MAX_PLAYERS; %0++) if (IsPlayerConnected(%0) && !IsPlayerNPC(%0))
  30.  
  31. new TimeType,
  32.     TimeTimer = (-2), //Not sure what the max ID/limit for timers is, and not sure if (-1) would give problems or not
  33.     DT[2][3];
  34.  
  35. public OnFilterScriptInit()
  36. {
  37.     new File:timeFile,
  38.         readStr[50],
  39.         fCount = (-1);
  40.        
  41.     print(" ");
  42.     print("|----- Loading Kwarde's Advanced Time FS -----|");
  43.    
  44.     if (!fexist("time.conf"))
  45.     {
  46.         print("* Creating the configuration file with the standard time settings");
  47.        
  48.         timeFile = fopen("time.conf", io_append);
  49.         format(readStr, 50, "%d\n%d\n%d\n%d\n%d\n%d\n%d", DEFAULT, D_YEAR, D_MON, D_DAY, D_HOUR, D_MIN, D_SEC);
  50.         fwrite(timeFile, readStr);
  51.         fclose(timeFile);
  52.     }
  53.  
  54.     timeFile = fopen("time.conf", io_read);
  55.     while (fread(timeFile, readStr))
  56.     {
  57.         fCount++;
  58.         if (fCount == 0) TimeType = strval(readStr);
  59.         if (fCount == 1) DT[0][0] = strval(readStr);
  60.         if (fCount == 2) DT[0][1] = strval(readStr);
  61.         if (fCount == 3) DT[0][2] = strval(readStr);
  62.         if (fCount == 4) DT[1][0] = strval(readStr);
  63.         if (fCount == 5) DT[1][1] = strval(readStr);
  64.         if (fCount == 6) DT[1][2] = strval(readStr);
  65.     }
  66.     fclose(timeFile);
  67.    
  68.     TimeTimer = SetTimer("UpdateTime", 1000, true);
  69.    
  70.     print("* Configuration loaded and timer started");
  71.     print("* Filterscript "#FS_NAME" v"#FS_VERS_MAJ"."#FS_VERS_MIN"."#FS_VERS_BLD"-"#FS_VERS_TAG" loaded");
  72.     print("|----------------------------------------|");
  73.     print(" ");
  74.     return 1;
  75. }
  76.  
  77. public OnFilterScriptExit()
  78. {
  79.     SaveData();
  80.    
  81.     print(" ");
  82.     print("-- CREDITS OF AdvancedTime.amx --");
  83.     print("* Kwarde - Script creator");
  84.     print("* Jochemd - TimestampToDate.inc");
  85.     print("-- Thanks for using my script! --");
  86.     print(" ");
  87.     return 1;
  88. }
  89.  
  90. CB:UpdateTime()
  91. {
  92.     switch (TimeType)
  93.     {
  94.         case TIME_DEFAULT:
  95.         {
  96.             DT[1][1]++;
  97.             if (DT[1][1] > 59)
  98.             {
  99.                 DT[1][0]++;
  100.                 DT[1][1] = 0;
  101.             }
  102.             if (DT[1][0] > 23)
  103.             {
  104.                 NextDay();
  105.                 DT[1][0] = 0;
  106.             }
  107.         }
  108.         case TIME_NORMAL:
  109.         {
  110.             DT[1][2]++;
  111.             if (DT[1][2] > 59)
  112.             {
  113.                 DT[1][1]++;
  114.                 DT[1][2] = 0;
  115.             }
  116.             if (DT[1][1] > 59)
  117.             {
  118.                 DT[1][0]++;
  119.                 DT[1][1] = 0;
  120.             }
  121.             if (DT[1][0] > 23)
  122.             {
  123.                 NextDay();
  124.                 DT[1][0] = 0;
  125.             }
  126.         }
  127.         case TIME_SYNC: TimestampToDate(gettime(), DT[0][0], DT[0][1], DT[0][2], DT[1][0], DT[1][1], DT[1][2], GMT);
  128.     }
  129.     ploop(i) SetPlayerTime(i, DT[1][0], DT[1][1]);
  130. }
  131.  
  132. stock bool:LeapYear(year)
  133.     return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
  134.  
  135. stock bool:IsLastDay(day, month, year)
  136. {
  137.     if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
  138.     {
  139.         if (day == 31) return true;
  140.         return false;
  141.     }
  142.     else if (month == 2)
  143.     {
  144.         if (LeapYear(year) && day == 29) return true;
  145.         else if (!LeapYear(year) && day == 28) return true;
  146.         return false;
  147.     }
  148.     else if (month == 4 || month == 6 || month == 9 || month == 11)
  149.     {
  150.         if (day == 30) return true;
  151.         return false;
  152.     }
  153.     return false;
  154. }
  155.  
  156. stock NextDay()
  157. {
  158.     if (IsLastDay(DT[0][2], DT[0][1], DT[0][0]))
  159.     {
  160.         DT[0][2] = 1;
  161.         DT[0][1]++;
  162.     }
  163.     if (DT[0][1] > 12)
  164.     {
  165.         DT[0][1] = 1;
  166.         DT[0][0]++;
  167.     }
  168. }
  169.  
  170. stock SaveData()
  171. {
  172.     if (fexist("time.conf")) fremove("time.conf");
  173.     new File:timeFile = fopen("time.conf", io_append),
  174.         str[50];
  175.        
  176.     format(str, 50, "%d\n%d\n%d\n%d\n%d\n%d\n%d", TimeType, DT[0][0], DT[0][1], DT[0][2], DT[1][0], DT[1][1], DT[1][2]);
  177.     fwrite(timeFile, str);
  178.     fclose(timeFile);
  179. }
  180.  
  181. //===== FUNCTIONS =====//
  182. stock SetTime(hour, minute, second)
  183. {
  184.     if (TimeType == TIME_SYNC) return false;
  185.    
  186.     DT[1][0] = hour;
  187.     DT[1][1] = minute;
  188.     DT[1][2] = second;
  189.     SaveData();
  190.     return true;
  191. }
  192.  
  193. stock SetDate(day, month, year)
  194. {
  195.     if (TimeType == TIME_SYNC) return false;
  196.    
  197.     DT[0][0] = year;
  198.     DT[0][1] = month;
  199.     DT[0][2] = day;
  200.     SaveData();
  201.     return true;
  202. }
  203.  
  204. stock SetTimeType(type = DEFAULT)
  205. {
  206.     TimeType = type;
  207.     SaveData();
  208.     return true;
  209. }
  210.  
  211. stock ToggleTime(bool:toggle = true) //Free/unfreeze time (only works on TIME_DEFAULT and TIME_NORMAL !). toggle=true: freeze time. toggle=false: unfreeze time
  212. {
  213.     if (TimeType == TIME_SYNC) return false;
  214.    
  215.     if (TimeTimer > (-2))
  216.     {
  217.         KillTimer(TimeTimer);
  218.         TimeTimer = (-2);
  219.     }
  220.     else TimeTimer = SetTimer("UpdateTimer", 1000, true);
  221.     return true;
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement