05.09.2013, 04:48
It's all about your steps.
1) Get all players into a list that are in range of point
2) Was any players found?
No - Return INVALID_PLAYER_ID;
Yes - Proceed to step 3
3) How many players?
One player - return ID
Two players - return random ID
That gives a basic algorithm of steps to perform.
I didn't test pretty sure I got it right based on the above steps
1) Get all players into a list that are in range of point
2) Was any players found?
No - Return INVALID_PLAYER_ID;
Yes - Proceed to step 3
3) How many players?
One player - return ID
Two players - return random ID
That gives a basic algorithm of steps to perform.
I didn't test pretty sure I got it right based on the above steps
pawn Код:
stock GetRandomPlayerInRange(Float:X,Float:Y,Float:Z,Float:Range)
{
// List of in range players
new InRangeList[MAX_PLAYERS] = { INVALID_PLAYER_ID, ... };
// How many in range players there is
new InRangeCount;
// Gather in range players (Change this to foreach()
for(new i = 0; i < MAX_PLAYERS; i++)
{
if(IsPlayerConnected(i))
{
if(IsPlayerInRangeOfPoint(i, range, X, Y, Z)) InRangeList[InRangeCount++] = i;
}
}
// There are players in range
if(InRangeCount)
{
// Only one? Return that ID
if(InRangeCount == 1) return InRangeList[0];
// More than one return a random ID
else
{
new r = Random(InRangeCount);
return InRangeList[r];
}
}
// No one in range return INVALID_PLAYER_ID
return INVALID_PLAYER_ID;
}