Advertisement
Guest User

trafficlights.inc

a guest
May 1st, 2014
1,240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 4.88 KB | None | 0 0
  1. #include <a_samp>
  2. #include <core>
  3. #include <float>
  4.  
  5. new globalik, whatislight = 5, lighttimer = -1, howmanyplayer;
  6.  
  7. new lightstartdelay = 5000;
  8. new tl_green = 10400;
  9. new tl_red = 20400;
  10. new tl_yellow = 2000;
  11.  
  12. forward LightUpdate(lighttobe);
  13. public LightUpdate(lighttobe)
  14. {
  15.     new sure, zamanzaman = GetTickCount();
  16.     whatislight = lighttobe;
  17.     switch(lighttobe)
  18.     {
  19.         case 1:
  20.         {
  21.             sure = globalik + tl_red;
  22.             #if defined DONT_CALL_TL_CB_IF_DELAYED
  23.             if(zamanzaman <= (sure + 500))
  24.             {
  25.             #endif
  26.                 CallLocalFunction("OnTrafficLightStateChange","i",lighttobe);
  27.             #if defined DONT_CALL_TL_CB_IF_DELAYED
  28.             }
  29.             #endif
  30.             sure = sure + tl_green - zamanzaman;
  31.             if(sure <= 0)
  32.             {
  33.                 LightUpdate(2);
  34.                 lighttimer = -1;
  35.                 return 1;
  36.             }
  37.             else lighttimer = SetTimerEx("LightUpdate",sure,0,"i",2);
  38.             return 1;
  39.         }
  40.         case 2:
  41.         {
  42.             sure = globalik + tl_red + tl_green;
  43.             #if defined DONT_CALL_TL_CB_IF_DELAYED
  44.             if(zamanzaman <= (sure + 500))
  45.             {
  46.             #endif
  47.                 CallLocalFunction("OnTrafficLightStateChange","i",lighttobe);
  48.             #if defined DONT_CALL_TL_CB_IF_DELAYED
  49.             }
  50.             #endif
  51.             sure = sure + tl_yellow - zamanzaman;
  52.             if(sure <= 0)
  53.             {
  54.                 LightUpdate(3);
  55.                 lighttimer = -1;
  56.                 return 1;
  57.             }
  58.             else lighttimer = SetTimerEx("LightUpdate",sure,0,"i",3);
  59.             return 1;
  60.         }
  61.         case 3:
  62.         {
  63.             sure = globalik + tl_red + tl_green + tl_yellow;
  64.             #if defined DONT_CALL_TL_CB_IF_DELAYED
  65.             if(zamanzaman <= (sure + 500))
  66.             {
  67.             #endif
  68.                 CallLocalFunction("OnTrafficLightStateChange","i",lighttobe);
  69.             #if defined DONT_CALL_TL_CB_IF_DELAYED
  70.             }
  71.             #endif
  72.             sure = sure + tl_green - zamanzaman;
  73.             if(sure <= 0)
  74.             {
  75.                 LightUpdate(4);
  76.                 lighttimer = -1;
  77.                 return 1;
  78.             }
  79.             else lighttimer = SetTimerEx("LightUpdate",sure,0,"i",4);
  80.             return 1;
  81.         }
  82.         case 4:
  83.         {
  84.             sure = globalik + tl_red + tl_green + tl_yellow + tl_green;
  85.             #if defined DONT_CALL_TL_CB_IF_DELAYED
  86.             if(zamanzaman <= (sure + 500))
  87.             {
  88.             #endif
  89.                 CallLocalFunction("OnTrafficLightStateChange","i",lighttobe);
  90.             #if defined DONT_CALL_TL_CB_IF_DELAYED
  91.             }
  92.             #endif
  93.             sure = sure + tl_yellow - zamanzaman;
  94.             if(sure <= 0)
  95.             {
  96.                 LightUpdate(5);
  97.                 lighttimer = -1;
  98.                 return 1;
  99.             }
  100.             else lighttimer = SetTimerEx("LightUpdate",sure,0,"i",5);
  101.             return 1;
  102.         }
  103.         case 5:
  104.         {
  105.             sure = globalik + tl_red + tl_green + tl_yellow + tl_green + tl_yellow;
  106.             #if defined DONT_CALL_TL_CB_IF_DELAYED
  107.             if(zamanzaman <= (sure + 500))
  108.             {
  109.             #endif
  110.                 CallLocalFunction("OnTrafficLightStateChange","i",lighttobe);
  111.             #if defined DONT_CALL_TL_CB_IF_DELAYED
  112.             }
  113.             #endif
  114.             sure = sure + tl_green - tl_yellow - zamanzaman;
  115.             globalik = globalik + tl_green + tl_red + tl_yellow;
  116.             if(sure <= 0)
  117.             {
  118.                 LightUpdate(1);
  119.                 lighttimer = -1;
  120.                 return 1;
  121.             }
  122.             else lighttimer = SetTimerEx("LightUpdate",sure,0,"i",1);
  123.             return 1;
  124.         }
  125.     }
  126.     return 1;
  127. }
  128.  
  129. stock GetTrafficLightState()
  130. {
  131.     return whatislight;
  132. }
  133.  
  134. forward CalibrateTrafficLightCounter(lightstate);
  135. public CalibrateTrafficLightCounter(lightstate)
  136. {
  137.     switch(lightstate)
  138.     {
  139.         case 1:
  140.         {
  141.             globalik = GetTickCount() - tl_red;
  142.         }
  143.         case 2:
  144.         {
  145.             globalik = GetTickCount() - tl_red - tl_green;
  146.         }
  147.         case 3:
  148.         {
  149.             globalik = GetTickCount() - tl_red - tl_green - tl_yellow;
  150.         }
  151.         case 4:
  152.         {
  153.             globalik = GetTickCount() - tl_red - tl_green - tl_yellow - tl_green;
  154.         }
  155.         case 5:
  156.         {
  157.             globalik = GetTickCount();
  158.         }
  159.     }
  160.     if(lighttimer != -1)
  161.     {
  162.         KillTimer(lighttimer);
  163.     }
  164.     LightUpdate(lightstate);
  165.     return 1;
  166. }
  167.  
  168. forward OnTrafficLightStateChange(lightstate);
  169.  
  170. public OnPlayerConnect(playerid)
  171. {
  172.     if(!IsPlayerNPC(playerid))
  173.     {
  174.         if(!howmanyplayer)
  175.         {
  176.             lighttimer = SetTimerEx("CalibrateTrafficLightCounter",tl_red - lightstartdelay,0,"i",1);
  177.         }
  178.         howmanyplayer ++;
  179.     }
  180.     #if defined TRAFFIC_OnPlayerConnect
  181.         return TRAFFIC_OnPlayerConnect(playerid);
  182.     #else
  183.         return 1;
  184.     #endif
  185. }
  186.  
  187. #if defined _ALS_OnPlayerConnect
  188.     #undef OnPlayerConnect
  189. #else
  190.     #define _ALS_OnPlayerConnect
  191. #endif
  192. #define OnPlayerConnect TRAFFIC_OnPlayerConnect
  193. #if defined TRAFFIC_OnPlayerConnect
  194.     forward TRAFFIC_OnPlayerConnect(playerid);
  195. #endif
  196.  
  197. public OnPlayerDisconnect(playerid, reason)
  198. {
  199.     if(!IsPlayerNPC(playerid))
  200.     {
  201.         howmanyplayer --;
  202.         if(!howmanyplayer)
  203.         {
  204.             if(lighttimer != -1)
  205.             {
  206.                 KillTimer(lighttimer);
  207.             }
  208.         }
  209.     }
  210.     #if defined TRAFFIC_OnPlayerDisconnect
  211.         return TRAFFIC_OnPlayerDisconnect(playerid,reason);
  212.     #else
  213.         return 1;
  214.     #endif
  215. }
  216.  
  217. #if defined _ALS_OnPlayerDisconnect
  218.     #undef OnPlayerDisconnect
  219. #else
  220.     #define _ALS_OnPlayerDisconnect
  221. #endif
  222. #define OnPlayerDisconnect TRAFFIC_OnPlayerDisconnect
  223. #if defined TRAFFIC_OnPlayerDisconnect
  224.     forward TRAFFIC_OnPlayerDisconnect(playerid,reason);
  225. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement