SA-MP Forums Archive
Randomly select players? - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Randomly select players? (/showthread.php?tid=522901)



Randomly select players? - MythicalMarauder - 29.06.2014

So what I want is to select 6 players each map only if there are 20 players online, so, the less players are online should select less players. Any ideas how to do that? I really need this urgently!


Re: Randomly select players? - Jefff - 29.06.2014

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);