Advertisement
ZiGGi

Player loop test

Sep 3rd, 2011
1,292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 4.94 KB | None | 0 0
  1. #include <..\compiler\includes\a_samp>
  2. #include "foreach"
  3.  
  4. main(){}
  5.  
  6. #undef MAX_PLAYERS
  7. #define MAX_PLAYERS 200
  8.  
  9. #define PLAYER_CONNECTING 10
  10. #define PLAYER_DISCONNECTING 10
  11.  
  12. //
  13. #define foreach_step(%0) for(new p_i,%0 = MAX_ONLINE_PLAYERS[p_i]; (%0 = MAX_ONLINE_PLAYERS[p_i]) != INVALID_PLAYER_ID; p_i++)
  14.  
  15. new MAX_ONLINE_PLAYERS[MAX_PLAYERS] = {INVALID_PLAYER_ID, ...};
  16.  
  17.  
  18. //
  19. #define GetPlayerLastID()   players_lastID
  20.  
  21. new players_lastID = 0;
  22.  
  23. forward plcount_OnPlayerConnect(playerid);
  24. public plcount_OnPlayerConnect(playerid)
  25. {
  26.     if(players_lastID < playerid) players_lastID = playerid;
  27.     return 1;
  28. }
  29.  
  30. forward plcount_OnPlayerDisconnect(playerid,reason);
  31. public plcount_OnPlayerDisconnect(playerid,reason)
  32. {
  33.     #pragma unused reason
  34.     if(players_lastID == playerid && playerid != 0)
  35.     {
  36.         do players_lastID--;
  37.         while((IsPlayerNPC(playerid) || (!IsPlayerConnected(players_lastID))) && (players_lastID > 0));
  38.     }
  39.     return 1;
  40. }
  41. //
  42.  
  43. // PLIDs
  44. new PLIDs[MAX_PLAYERS] = {-1,...};
  45. new MaxPlayers = 0;
  46.  
  47. forward plids_OnPlayerConnect(playerid);
  48. public plids_OnPlayerConnect(playerid)
  49. {
  50.     PLIDs[MaxPlayers] = playerid;
  51.     MaxPlayers++;
  52.     return 1;
  53. }
  54.  
  55. forward plids_OnPlayerDisconnect(playerid,reason);
  56. public plids_OnPlayerDisconnect(playerid,reason)
  57. {
  58.     for(new i=0;i<MaxPlayers;i++)
  59.     {
  60.         if(PLIDs[i] == playerid)
  61.         {
  62.             MaxPlayers--;
  63.             PLIDs[i] = PLIDs[MaxPlayers];
  64.             PLIDs[MaxPlayers] = -1;
  65.             break;
  66.         }
  67.     }
  68.     return 1;
  69. }
  70. //
  71. public OnGameModeInit()
  72. {
  73.     // 200 слотов
  74.    
  75.    
  76.     // IsPlayerConnected
  77.     new count = GetTickCount();
  78.     for(new j=0;j<10000;j++)
  79.     {
  80.         for(new i=0;i<PLAYER_CONNECTING;i++)
  81.             _OnPlayerConnect(i*10);
  82.         for( new i; i < 200; i ++ )
  83.         {
  84.             if(!IsPlayerConnected(i) || IsPlayerNPC(i)) continue;
  85.         }
  86.         for(new i=0;i<PLAYER_CONNECTING;i++)
  87.             _OnPlayerDisconnect(i*10,0);
  88.     }
  89.     printf("IsPlayerConnected: %d", GetTickCount() - count);
  90.    
  91.    
  92.     // GetMaxPlayers
  93.     count = GetTickCount();
  94.     for(new j=0;j<10000;j++)
  95.     {
  96.         for(new i=0;i<PLAYER_CONNECTING;i++)
  97.             _OnPlayerConnect(i*10);
  98.         for( new i=GetMaxPlayers()-1 ; i>=0 ; --i )
  99.         {
  100.             if(!IsPlayerConnected(i) || IsPlayerNPC(i)) continue;
  101.         }
  102.         for(new i=0;i<PLAYER_CONNECTING;i++)
  103.             _OnPlayerDisconnect(i*10,0);
  104.     }
  105.     printf("GetMaxPlayers: %d", GetTickCount() - count);
  106.    
  107.    
  108.     // GetPlayerLastID
  109.     count = GetTickCount();
  110.     for(new j=0;j<10000;j++)
  111.     {
  112.         for(new i=0;i<PLAYER_CONNECTING;i++)
  113.             plcount_OnPlayerConnect(i*10);
  114.         for( new i=0 ; i <= GetPlayerLastID() ; i++ )
  115.         {
  116.             if(!IsPlayerConnected(i) || IsPlayerNPC(i)) continue;
  117.         }
  118.         for(new i=0;i<PLAYER_CONNECTING;i++)
  119.             plcount_OnPlayerDisconnect(i*10,0);
  120.     }
  121.     printf("GetPlayerLastID: %d", GetTickCount() - count);
  122.  
  123.    
  124.     // PLIDs
  125.     count = GetTickCount();
  126.     for(new j=0;j<10000;j++)
  127.     {
  128.         for(new i=0;i<PLAYER_CONNECTING;i++)
  129.             plids_OnPlayerConnect(i*10);
  130.         for( new i=0 ; i < MaxPlayers ; i++ )
  131.         {
  132.             if(!IsPlayerConnected(PLIDs[i]) || IsPlayerNPC(PLIDs[i])) continue;
  133.         }
  134.         for(new i=0;i<PLAYER_DISCONNECTING;i++)
  135.             plids_OnPlayerDisconnect(i*10,0);
  136.     }
  137.     printf("PLID: %d", GetTickCount() - count);
  138.    
  139.    
  140.  
  141.     // foreach
  142.     count = GetTickCount();
  143.     for(new j=0;j<10000;j++)
  144.     {
  145.         for(new i=0;i<PLAYER_CONNECTING;i++)
  146.             Iter_Add(Player,i*10);
  147.            
  148.         foreach(Player, i)
  149.         {
  150.             if(!IsPlayerConnected(i) || IsPlayerNPC(i)) continue;
  151.         }
  152.        
  153.         for(new i=0;i<PLAYER_DISCONNECTING;i++)
  154.             Iter_Remove(Player,i*10);
  155.     }
  156.     printf("foreach: %d", GetTickCount() - count);
  157.    
  158.    
  159.     // foreach by Stepashka
  160.     count = GetTickCount();
  161.     for(new j=0;j<10000;j++)
  162.     {
  163.         for(new i=0;i<PLAYER_CONNECTING;i++)
  164.             step_OnPlayerConnect(i*10);
  165.         foreach_step(i)
  166.         {
  167.             if(!IsPlayerConnected(i) || IsPlayerNPC(i)) continue;
  168.         }
  169.         for(new i=0;i<PLAYER_DISCONNECTING;i++)
  170.             step_OnPlayerDisconnect(i*10,0);
  171.     }
  172.     printf("foreach_step: %d", GetTickCount() - count);
  173.     return 1;
  174. }
  175.  
  176. forward _OnPlayerConnect(playerid);
  177. public _OnPlayerConnect(playerid)
  178. {
  179.     return 1;
  180. }
  181.  
  182. forward _OnPlayerDisconnect(playerid,reason);
  183. public _OnPlayerDisconnect(playerid,reason)
  184. {
  185.     return 1;
  186. }
  187.  
  188. forward step_OnPlayerConnect(playerid);
  189. public step_OnPlayerConnect(playerid)
  190. {
  191.     for ( new i; i < MAX_PLAYERS; i++)
  192.     {
  193.         if ( INVALID_PLAYER_ID == MAX_ONLINE_PLAYERS[i])
  194.         {
  195.             MAX_ONLINE_PLAYERS[i] = playerid;
  196.             break;
  197.         }
  198.     }
  199.     //...
  200.     return 1;
  201. }
  202.  
  203. forward step_OnPlayerDisconnect(playerid, reason);
  204. public step_OnPlayerDisconnect(playerid, reason)
  205. {
  206.     for ( new i, bool:foundPlayer = false; i < MAX_PLAYERS; i++)
  207.     {
  208.         if ( playerid == MAX_ONLINE_PLAYERS[i] || true == foundPlayer)
  209.         {
  210.             if ( i+1 < MAX_PLAYERS)
  211.             {
  212.                 if ( (MAX_ONLINE_PLAYERS[i] = MAX_ONLINE_PLAYERS[i+1]) == INVALID_PLAYER_ID)
  213.                 {
  214.                     break;
  215.                 }
  216.                 foundPlayer = true;
  217.             }
  218.             else
  219.             {
  220.                 MAX_ONLINE_PLAYERS[i] = INVALID_PLAYER_ID;
  221.             }
  222.         }
  223.     }
  224.     //...
  225.     return 1;
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement