SHARE
TWEET

Player loop test

ZiGGi Sep 3rd, 2011 (edited) 681 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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 50
  10. #define PLAYER_DISCONNECTING 50
  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.     new
  20.         list_player_IDs[ MAX_PLAYERS ] = { -1, ... },
  21.         maxPlayers = 0;
  22.  
  23.     #define forscan(%1) for( new __idx = 0, %1 = 0; __idx < maxPlayers; %1 = list_player_IDs[ ++__idx ] )
  24.  
  25.     stock forscan_connect( playerid )
  26.     {
  27.         list_player_IDs[ maxPlayers ] = playerid;
  28.         maxPlayers++;
  29.     }
  30.  
  31.     stock forscan_disconnect( playerid, reason )
  32.     {
  33.         for ( new i = 0; i < maxPlayers; i++ )
  34.         {
  35.             if ( list_player_IDs[ i ] == playerid )
  36.             {
  37.                 maxPlayers--;
  38.                 list_player_IDs[ i ] = list_player_IDs[ maxPlayers ];
  39.                 list_player_IDs[ maxPlayers ] = -1;
  40.                 break;
  41.             }
  42.         }
  43.     }  
  44.  
  45. //
  46. #define GetPlayerLastID()       players_lastID
  47.  
  48. new players_lastID = -1;
  49.  
  50. forward plcount_OnPlayerConnect(playerid);
  51. public plcount_OnPlayerConnect(playerid)
  52. {
  53.         if(players_lastID < playerid) players_lastID = playerid;
  54.         return 1;
  55. }
  56.  
  57. forward plcount_OnPlayerDisconnect(playerid,reason);
  58. public plcount_OnPlayerDisconnect(playerid,reason)
  59. {
  60.         #pragma unused reason
  61.         if(players_lastID == playerid && playerid != 0)
  62.         {
  63.                 do players_lastID--;
  64.                 while((IsPlayerNPC(playerid) || (!IsPlayerConnected(players_lastID))) && (players_lastID > 0));
  65.         }
  66.         return 1;
  67. }
  68. //
  69.  
  70. // PLIDs
  71. new PLIDs[MAX_PLAYERS] = {-1,...};
  72. new MaxPlayers = 0;
  73.  
  74. forward plids_OnPlayerConnect(playerid);
  75. public plids_OnPlayerConnect(playerid)
  76. {
  77.         PLIDs[MaxPlayers] = playerid;
  78.         MaxPlayers++;
  79.         return 1;
  80. }
  81.  
  82. forward plids_OnPlayerDisconnect(playerid,reason);
  83. public plids_OnPlayerDisconnect(playerid,reason)
  84. {
  85.         for(new i=0;i<MaxPlayers;i++)
  86.         {
  87.                 if(PLIDs[i] == playerid)
  88.                 {
  89.                         MaxPlayers--;
  90.                         PLIDs[i] = PLIDs[MaxPlayers];
  91.                         PLIDs[MaxPlayers] = -1;
  92.                         break;
  93.                 }
  94.         }
  95.         return 1;
  96. }
  97. //
  98. public OnGameModeInit()
  99. {
  100.         // 200 слотов
  101.        
  102.        
  103.         // IsPlayerConnected
  104.         new count = GetTickCount();
  105.         for(new i=0;i<PLAYER_CONNECTING;i++)
  106.                 _OnPlayerConnect(i*10);
  107.         for(new j=0;j<10000;j++)
  108.         {
  109.                 for( new i; i < 200; i ++ )
  110.                 {
  111.                         if(!IsPlayerConnected(i) || IsPlayerNPC(i)) continue;
  112.                 }
  113.         }
  114.         for(new i=0;i<PLAYER_CONNECTING;i++)
  115.                 _OnPlayerDisconnect(i*10,0);
  116.         printf("IsPlayerConnected: %d", GetTickCount() - count);
  117.        
  118.        
  119.         // GetMaxPlayers
  120.         count = GetTickCount();
  121.         for(new i=0;i<PLAYER_CONNECTING;i++)
  122.                 _OnPlayerConnect(i*10);
  123.         for(new j=0;j<10000;j++)
  124.         {
  125.                 for( new i=GetMaxPlayers()-1 ; i>=0 ; --i )
  126.                 {
  127.                         if(!IsPlayerConnected(i) || IsPlayerNPC(i)) continue;
  128.                 }
  129.         }
  130.         for(new i=0;i<PLAYER_CONNECTING;i++)
  131.                 _OnPlayerDisconnect(i*10,0);
  132.         printf("GetMaxPlayers: %d", GetTickCount() - count);
  133.        
  134.        
  135.         // GetPlayerLastID
  136.         count = GetTickCount();
  137.         for(new i=0;i<PLAYER_CONNECTING;i++)
  138.                 plcount_OnPlayerConnect(i*10);
  139.         for(new j=0;j<10000;j++)
  140.         {
  141.                 for( new i=0 ; i <= GetPlayerLastID() ; i++ )
  142.                 {
  143.                         if(!IsPlayerConnected(i) || IsPlayerNPC(i)) continue;
  144.                 }
  145.         }
  146.         for(new i=0;i<PLAYER_CONNECTING;i++)
  147.                 plcount_OnPlayerDisconnect(i*10,0);
  148.         printf("GetPlayerLastID: %d", GetTickCount() - count);
  149.  
  150.        
  151.         // PLIDs
  152.         count = GetTickCount();
  153.         for(new i=0;i<PLAYER_CONNECTING;i++)
  154.                 plids_OnPlayerConnect(i*10);
  155.         for(new j=0;j<10000;j++)
  156.         {
  157.                 for( new i=0,playerid ; i < MaxPlayers ; i++ )
  158.                 {
  159.                         playerid = PLIDs[i];
  160.                 }
  161.         }
  162.         for(new i=0;i<PLAYER_DISCONNECTING;i++)
  163.                 plids_OnPlayerDisconnect(i*10,0);
  164.         printf("PLID: %d", GetTickCount() - count);
  165.        
  166.        
  167.  
  168.         // foreach
  169.         count = GetTickCount();
  170.         for(new i=0;i<PLAYER_CONNECTING;i++)
  171.                 Iter_Add(Player,i*10);
  172.         for(new j=0;j<10000;j++)
  173.         {
  174.                 foreach(Player, i)
  175.                 {
  176.                         //if(!IsPlayerConnected(i) || IsPlayerNPC(i)) continue;
  177.                 }
  178.         }
  179.         for(new i=0;i<PLAYER_DISCONNECTING;i++)
  180.                 Iter_Remove(Player,i*10);
  181.         printf("foreach: %d", GetTickCount() - count);
  182.        
  183.        
  184.         // foreach by Stepashka
  185.         count = GetTickCount();
  186.         for(new i=0;i<PLAYER_CONNECTING;i++)
  187.                 step_OnPlayerConnect(i*10);
  188.         for(new j=0;j<10000;j++)
  189.         {
  190.                 foreach_step(i)
  191.                 {
  192.                         //if(!IsPlayerConnected(i) || IsPlayerNPC(i)) continue;
  193.                 }
  194.         }
  195.         for(new i=0;i<PLAYER_DISCONNECTING;i++)
  196.                 step_OnPlayerDisconnect(i*10,0);
  197.         printf("foreach_step: %d", GetTickCount() - count);
  198.  
  199.  
  200.  
  201.         // forscan
  202.         count = GetTickCount();
  203.         for(new i=0;i<PLAYER_CONNECTING;i++)
  204.                 forscan_connect( i*10 );
  205.         for(new j=0;j<10000;j++)
  206.         {
  207.                 forscan(i)
  208.                 {
  209.                         //if(!IsPlayerConnected(i) || IsPlayerNPC(i)) continue;
  210.                 }
  211.         }
  212.         for(new i=0;i<PLAYER_DISCONNECTING;i++)
  213.                 forscan_disconnect( i*10, 0 );
  214.         printf("forscan: %d", GetTickCount() - count);
  215.  
  216.         return 1;
  217. }
  218.  
  219. forward _OnPlayerConnect(playerid);
  220. public _OnPlayerConnect(playerid)
  221. {
  222.         return 1;
  223. }
  224.  
  225. forward _OnPlayerDisconnect(playerid,reason);
  226. public _OnPlayerDisconnect(playerid,reason)
  227. {
  228.         return 1;
  229. }
  230.  
  231. forward step_OnPlayerConnect(playerid);
  232. public step_OnPlayerConnect(playerid)
  233. {
  234.         for ( new i; i < MAX_PLAYERS; i++)
  235.         {
  236.                 if ( INVALID_PLAYER_ID == MAX_ONLINE_PLAYERS[i])
  237.                 {
  238.                         MAX_ONLINE_PLAYERS[i] = playerid;
  239.                         break;
  240.                 }
  241.         }
  242.     //...
  243.         return 1;
  244. }
  245.  
  246. forward step_OnPlayerDisconnect(playerid, reason);
  247. public step_OnPlayerDisconnect(playerid, reason)
  248. {
  249.     for ( new i, bool:foundPlayer = false; i < MAX_PLAYERS; i++)
  250.     {
  251.         if ( playerid == MAX_ONLINE_PLAYERS[i] || true == foundPlayer)
  252.         {
  253.             if ( i+1 < MAX_PLAYERS)
  254.             {
  255.                 if ( (MAX_ONLINE_PLAYERS[i] = MAX_ONLINE_PLAYERS[i+1]) == INVALID_PLAYER_ID)
  256.                 {
  257.                     break;
  258.                 }
  259.                 foundPlayer = true;
  260.             }
  261.             else
  262.             {
  263.                 MAX_ONLINE_PLAYERS[i] = INVALID_PLAYER_ID;
  264.             }
  265.         }
  266.     }
  267.     //...
  268.         return 1;
  269. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top