Advertisement
Guest User

a_foreach

a guest
Aug 11th, 2012
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 2.38 KB | None | 0 0
  1. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. //             Foreach (fast loop ir arrays pawn)
  4. //  Author: Bruno da Silva (thanks Allan Jader)
  5. //       www.ips-team.blogspot.com
  6. //
  7. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  8.  
  9. #define newItterArray:%0<%1>        ipsArray%0[%1] = {-1, ...}, ipsInit%0 = -1, ipsFinal%0 = 0
  10.  
  11. #define addItterArray:%0<%1>        intervalChangeItter(ipsArray%0, %1, ipsInit%0, ipsFinal%0)
  12. #define delItterArray:%0<%1>        intervalDeleteItter(ipsArray%0, %1, ipsInit%0, ipsFinal%0)
  13.  
  14. #define getItterArray:%0<%1>        ipsArray%0[%1]
  15. #define foreach(%0,%1)      for(new %1 = ipsInit%0 ; %1 != -1; %1 = ipsArray%0[%1])
  16.  
  17. new newItterArray:Player<MAX_PLAYERS>;
  18.  
  19. intervalChangeItter(array[], valor, &inicio, &ultimoNumeroVar)
  20. {
  21.     if(array[valor] != -1 || valor == -1)
  22.     {
  23.         return false;
  24.     }
  25.    
  26.     if(inicio == -1)
  27.     {
  28.         return inicio = valor, true;
  29.     }
  30.    
  31.     if(valor < inicio)
  32.     {
  33.         return array[valor] = inicio, inicio = valor, true;
  34.     }
  35.    
  36.     if(array[inicio] == -1)
  37.     {
  38.         return array[inicio] = valor, ultimoNumeroVar = valor;
  39.     }
  40.    
  41.     return array[ultimoNumeroVar] = valor, ultimoNumeroVar = valor, true;
  42. }
  43.  
  44. intervalDeleteItter(array[], value, &inicio, &ultimoNumeroVar)
  45. {
  46.     if(inicio == -1 || value == -1)
  47.     {
  48.         return false;
  49.     }
  50.     if(inicio == value)
  51.     {
  52.         return inicio = -1, array[value] = -1, true;
  53.     }
  54.    
  55.     ultimoNumeroVar = 0;
  56.    
  57.     for( ; array[ultimoNumeroVar] != value; ultimoNumeroVar = array[ultimoNumeroVar])
  58.     {
  59.         if(ultimoNumeroVar == -1) return false;
  60.     }
  61.    
  62.     return array[ultimoNumeroVar] = array[value], array[value] = -1, true;
  63.  
  64. }
  65.  
  66. public OnPlayerConnect(playerid)
  67. {
  68.     addItterArray:Player<playerid>;
  69.     return CallLocalFunction("CallOnPlayerConnect", "i", playerid);
  70. }
  71.  
  72. public OnPlayerDisconnect(playerid, reason)
  73. {
  74.     delItterArray:Player<playerid>;
  75.     return CallLocalFunction("CallOnPlayerDisconnect", "ii", playerid, reason);
  76. }
  77.  
  78.  
  79. #if defined HookOnPlayerConnect
  80.     #undef OnPlayerConnect
  81. #else
  82.     #define HookOnPlayerConnect
  83. #endif
  84.  
  85. #define OnPlayerConnect CallOnPlayerConnect
  86.  
  87. #if defined HookOnPlayerDisconnect
  88.     #undef OnPlayerDisconnect
  89. #else
  90.     #define HookOnPlayerDisconnect
  91. #endif
  92.  
  93. #define OnPlayerDisconnect CallOnPlayerDisconnect
  94.  
  95. forward OnPlayerConnect(playerid);
  96. forward OnPlayerDisconnect(playerid, reason);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement