View Single Post
JusTGo
Veteran Member
Join Date: Mar 2013
Old 12-27-2015 , 15:39   Re: [HOW TO] Retrieve random values from an array without retreiving the same twice
Reply With Quote #64

Quote:
Originally Posted by Bugsy View Post
This will only return a player that has not yet been selected. Once all players have been selected you must call ResetPlayers() to be able to select players again; 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();

public 
client_disconnect(id)
{
    
g_Selected &= ~( << (id-1) ) )
}

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;

plz make it automatically reset selected players when all players have been selected.
__________________
JusTGo is offline