Bebras

[INC]Funkcijos nurodytu laiku - Bebras

May 22nd, 2014
490
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 2.53 KB | None | 0 0
  1.                            
  2.                                 /*
  3.                                                 Funkciju iškvietimo nurodytu laiku include
  4.                                                 Autorius "Bebras" 2013 - 2014 ©
  5.                                                 v0.1
  6.                                
  7.                                 */
  8. /*
  9.         native AddEvent(funcname[], bool:repeating, hour, minute=0, second=0);
  10.        
  11. */
  12.  
  13.  
  14. #if !defined MAX_EVENTS
  15.     #define MAX_EVENTS      15
  16. #endif
  17.  
  18.  
  19. #if !defined EVENTS_USE_YTIMERS
  20.     #if defined _inc_y_timers
  21.         #define EVENTS_USE_YTIMERS
  22.     #endif
  23. #endif
  24.  
  25.  
  26. // Macro from Y_Less
  27. #if !defined strcpy
  28.     #define strcpy(%0,%1,%2) strcat((%0[0] = '\0', %0), %1, %2)
  29. #endif
  30.  
  31.  
  32. enum event_data {
  33.     eFunctionName[32],
  34.     eHour,
  35.     eMinute,
  36.     eSecond,
  37.     eLastCall,
  38.     bool:eActive,
  39.     bool:eRepeat
  40. };
  41.  
  42. new EventData[MAX_EVENTS][event_data];
  43.  
  44.  
  45.  
  46. stock AddEvent(funcname[],bool:repeating,hour,minute=0,second=0)
  47. {
  48.     for(new i = 0; i < sizeof(EventData); i++)
  49.     {
  50.         if(EventData[i][eActive]) continue;
  51.         if(hour >= 24 || hour < 0) EventData[i][eHour] = 0;
  52.         if(minute < 0 || minute > 59) minute = 0;
  53.         if(second < 0 || second > 59) second = 0;
  54.         EventData[i][eActive] = true;
  55.         strcpy(EventData[i][eFunctionName],funcname,32);
  56.         EventData[i][eHour] = hour;
  57.         EventData[i][eMinute] = minute;
  58.         EventData[i][eSecond] = second;
  59.         EventData[i][eRepeat] = repeating;
  60.         return 1;
  61.     }
  62.     return 0;
  63. }
  64.  
  65.  
  66.  
  67. public OnGameModeInit()
  68. {
  69.     #if !defined EVENTS_USE_YTIMERS
  70.         #if defined _inc_timerfix
  71.             SetTimer("EventTimeCheck", 1000, true);
  72.         #else
  73.             SetTimer("EventTimeCheck", 600, true);
  74.         #endif
  75.     #endif
  76.  
  77.  
  78.     #if defined Event_OnGameModeInit
  79.         Event_OnGameModeInit();
  80.     #endif
  81. }
  82.  
  83. #if defined _ALS_OnGameModeInit
  84.     #undef OnGameModeInit
  85. #else
  86.     #define _ALS_OnGameModeInit
  87. #endif
  88. #define OnGameModeInit Event_OnGameModeInit
  89. #if defined Event_OnGameModeInit
  90.     forward Event_OnGameModeInit();
  91. #endif
  92.  
  93.  
  94. #if defined EVENTS_USE_YTIMERS
  95.     task EventTimeCheckYTimers[1000]()
  96.         EventTimeCheck();
  97. #endif
  98.  
  99.  
  100. forward EventTimeCheck();
  101. public EventTimeCheck()
  102. {
  103.     new hour,minute,second,stamp = gettime();
  104.     gettime(hour,minute,second);
  105.     for(new i = 0; i < sizeof(EventData); i++)
  106.     {
  107.         if(!EventData[i][eActive]) break;
  108.  
  109.         if(hour == EventData[i][eHour] && minute == EventData[i][eMinute]
  110.             && second >= EventData[i][eSecond] && second > EventData[i][eLastCall])
  111.         {
  112.             CallLocalFunction(EventData[i][eFunctionName],"","");
  113.             EventData[i][eLastCall] = stamp;
  114.             printf("Calling %s",EventData[i][eFunctionName]);
  115.             if(!EventData[i][eRepeat])
  116.             {
  117.                 EventData[i][eActive] = false;
  118.                 strdel(EventData[i][eFunctionName],0,32);
  119.             }
  120.         }
  121.     }  
  122.     return;
  123. }
Advertisement
Add Comment
Please, Sign In to add comment