View Single Post
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 05-10-2009 , 10:58   Re: Random player/s.
Reply With Quote #19

Here's a way you could do it. This will only return a player that has not yet been selected. You could modify the code to make it automatically reset selected players when all players have been selected. If you are unsure how to do this just ask.

PHP Code:
new g_Selected;
new 
g_MaxPlayers;

g_MaxPlayers get_maxplayers();

ResetSelected()
{
    
g_Selected 0;
}

GetRandomPlayer()
{
    new 
iPlayerBits;
    new 
i;
    
    
//Make a bitsum of connected player indexes. 
    //You can add additional checks here (bot,team,admin, etc).
    
for ( <= g_MaxPlayers i++ )
        if ( 
is_user_connected) )
            
iPlayerBits |= ( << ( ) );
    
    
//No players are connected.
    
if ( !iPlayerBits )
        return 
0;

    
//Keep looping until there is no players that haven't been selected.
    
while( iPlayerBits != g_Selected )
    {
        
//Get random player id
        
= ( random_numg_MaxPlayers ) - );
        
        
//If player is connected and has not yet been selected
        
if ( ( iPlayerBits & ( << ) ) && !( g_Selected & ( << ) ) )
        {
            
//Set bit in g_Selected bit-field so they will be ignored on the next call
            
g_Selected |= ( << );
            return ( 
);
        }
    }
    
    
//All players have already been selected.
    
return 0;

__________________

Last edited by Bugsy; 05-10-2009 at 11:26.
Bugsy is offline