06.05.2012, 16:26
How could i select 3 random players to be part of a mission/etc but i have no idea how to make 3 random player selection?
new first,second,third;
stock ChooseFirst()
{
first = random(MAX_PLAYERS);
if(!IsPlayerConnected(first)) return ChooseFirst();
return first;
}
stock ChooseSecond()
{
second = random(MAX_PLAYERS);
if(!IsPlayerConnected(second) || (second == first)) return ChooseSecond();
return second;
}
stock ChooseThird()
{
third = random(MAX_PLAYERS);
if(!IsPlayerConnected(third) || (third == first) || (second || first)) return ChooseThird();
return third;
}
new
connectedPlayers = 0;
for( new u; u < MAX_PLAYERS; u ++ ) // Get the amount of online players..
{
if( IsPlayerConnected( playerid ) )
{
connectedPlayers++;
}
}
new
firstPlayer = random( connectedPlayers ),
secondPlayer = random( connectedPlayers),
thirdPlayer = random( connectedPlayers );
printf("%s was randomly selected as the first player!\n%s was selected as the second player!\n%s was selected as the third player!", firstPlayer, secondPlayer, thirdPlayer );
stock GetRandomPlayer(bool: init = false) {
static
iCount,
iPlayers[MAX_PLAYERS]
;
if(init) {
for((_: init) = iCount = 0; (_: init) != MAX_PLAYERS; ++init) {
if(IsPlayerConnected(_: init)) {
iPlayers[iCount++] = _: init;
}
}
}
if(iCount != 0) {
new
rand = random(iCount)
;
(_: init) = iPlayers[rand];
iPlayers[rand] = iPlayers[--iCount];
return _: init;
}
return INVALID_PLAYER_ID;
}
new
rand1 = GetRandomPlayer(true),
rand2 = GetRandomPlayer(),
rand3 = GetRandomPlayer()
;