Randomly select players?
#2

pawn Код:
#define Loop(%0,%1) \
    for(new d=0,%1=%0[0]; %0[d] != INVALID_PLAYER_ID; d++,%1=%0[d])

SelectRandomPlayers(dest[], how_many_players = 6, s_size = sizeof(dest))
{
    new ids[MAX_PLAYERS], count = 0;
    for(new i = GetMaxPlayers() - 1; i != -1; i--)
        if(IsPlayerConnected(i))
            ids[count++] = i;

    if(how_many_players > s_size)
        how_many_players = s_size;

    if(count < how_many_players)
        how_many_players = count;

    while(how_many_players > 0)
    {
        new rand = random(count);
        if(ids[rand] != INVALID_PLAYER_ID)
        {
            dest[--how_many_players] = ids[rand];
            ids[rand] = INVALID_PLAYER_ID;
        }
    }
}

new Players[10] = {INVALID_PLAYER_ID, ...}; // we want random 10 players but we must set array to INVALID_PLAYER_ID before start
SelectRandomPlayers(Players, sizeof(Players)); // sizeof(Players) means 10 slots in array Players[]

Loop(Players,i)
    printf("Connected_ID: %d",i);
Reply


Messages In This Thread
Randomly select players? - by MythicalMarauder - 29.06.2014, 19:03
Re: Randomly select players? - by Jefff - 29.06.2014, 20:26

Forum Jump:


Users browsing this thread: 1 Guest(s)