Get a random player in range of position using loops
#6

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

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;
}
Reply


Messages In This Thread
Get a random player in range of position using loops - by iJumbo - 04.09.2013, 20:34
Re: Good way to - by Vince - 04.09.2013, 22:32
Re: Good way to - by iJumbo - 04.09.2013, 23:00
Re: Good way to - by Kar - 05.09.2013, 02:00
Re: Good way to - by CyNiC - 05.09.2013, 03:48
Re: Get a random player in range of position using loops - by Pottus - 05.09.2013, 04:48
Re: Get a random player in range of position using loops - by iJumbo - 05.09.2013, 10:01
Re: Get a random player in range of position using loops - by CaveDweller - 05.09.2013, 11:24
Re: Get a random player in range of position using loops - by iJumbo - 05.09.2013, 11:30
Re: Get a random player in range of position using loops - by CaveDweller - 05.09.2013, 11:31

Forum Jump:


Users browsing this thread: 1 Guest(s)