14.07.2011, 19:24
Hey, i want to make something that takes a random player, but, how can i make this AND check if the playerid is connected?
stock RandomID()
{
next:
new
playerid;
playerid = random(GetMaxPlayers());
if(!IsPlayerConnected(playerid))
goto next;
return playerid;
}
//or this
stock players() {
new c;
for(new i = 0; i < MAX_PLAYERS; ++i) {
if(IsPlayerConnected(i))
{
c++ ;
}
}
return c;
}
//using
//random(players())
GetRandomPlayer()
{
new
pCount,
rPlayers[MAX_PLAYERS] = {INVALID_PLAYER_ID, ...};
for(new playerid; playerid < MAX_PLAYERS; playerid++)
{
if(IsPlayerConnected(playerid))
{
rPlayers[pCount] = playerid;
pCount++;
}
}
if(pCount)
return rPlayers[random(pCount)];
else
return INVALID_PLAYER_ID;
}
pawn Code:
|
Here's my version. Returns a random player or INVALID_PLAYER_ID if no players are connected. Also its worth noting that foreach has an inbuilt random player function if you already use that library.
pawn Code:
|
#include <IRC>
#include <a_samp>
new RandomList:PlayersRandomList;
public OnPlayerConnect(playerid)
{
AddItemsToRandomList(PlayersRandomList, playerid);
return 1;
}
public OnPlayerDisconnect(playerid)
{
RemoveItemsFromRandomList(PlayersRandomList, playerid);
return 1;
}
public OnPlayerSpawn(playerid)
{
new msg[128], randomhitforplayer = RandomNumberFromList(PlayersRandomList);
if(randomlistforplayer != INVALID_RANDOM_NUMBER)
{
GetPlayerName(randomhitforplayer, msg, MAX_PLAYER_NAME);
format(msg, sizeof msg, "You have a new hit! Kill {FF4040}%s{FFFFFF}!", msg);
SendClientMessage(playerid, -1, msg);
Hit[playerid] = randomhitforplayer;
}
return 1;
}
public OnPlayerDeath(playerid, killerid, reason)
{
if(IsPlayerConnected(killerid))
{
if(Hit[killerid] == playerid)
{
new msg[128], randomhitforplayer = RandomNumberFromList(PlayersRandomList);
Hit[killerid] = randomhitforplayer;
GetPlayerName(playerid, msg, MAX_PLAYER_NAME);
format(msg, sizeof msg, "You killed %s and earned 500$", msg);
SendClientMessage(killerid, -1, msg);
format(msg, sizeof msg, "You have a new hit to do! Kill {FF4040}%s{FFFFFF}!", msg);
SendClientMessage(killerid, -1, msg);
}
}
}