SA-MP Forums Archive
How can I? - 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)
+---- Forum: Help Archive (https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: How can I? (/showthread.php?tid=151995)



How can I? - ViruZZzZ_ChiLLL - 01.06.2010

How can I get the closest player in a certain object?

Example :

ViruZZzZ_ChiLLL (ID : 3) is the nearest player from the tree.


Re: How can I? - Antonio [G-RP] - 01.06.2010

Use the Function 'GetClosestPlayerToPlayer' but replace player2 with an object.


Re: How can I? - ViruZZzZ_ChiLLL - 01.06.2010

Quote:
Originally Posted by Antonio (eternalrp.webatu.com)
Use the Function 'GetClosestPlayerToPlayer' but replace player2 with an object.
Is there a GetClosestPlayerToPlayer function?


Re: How can I? - Antonio [G-RP] - 01.06.2010

Well its a function I made ..


Re: How can I? - [NYRP]Mike. - 01.06.2010

pawn Код:
forward GetClosestPlayer(p1);
forward Float:GetDistanceBetweenPlayers(p1,p2);

public GetClosestPlayer(p1)
{
    new x,Float:dis,Float:dis2,player;
    player = -1;
    dis = 99999.99;
    for (x=0;x<MAX_PLAYERS;x++)
    {
        if(IsPlayerConnected(x))
        {
            if(x != p1)
            {
                dis2 = GetDistanceBetweenPlayers(x,p1);
                if(dis2 < dis && dis2 != -1.00)
                {
                    dis = dis2;
                    player = x;
                }
            }
        }
    }
    return player;
}

public Float:GetDistanceBetweenPlayers(p1,p2)
{
    new Float:x1,Float:y1,Float:z1,Float:x2,Float:y2,Float:z2;
    if(!IsPlayerConnected(p1) || !IsPlayerConnected(p2))
    {
        return -1.00;
    }
    GetPlayerPos(p1,x1,y1,z1);
    GetPlayerPos(p2,x2,y2,z2);
    return floatsqroot(floatpower(floatabs(floatsub(x2,x1)),2)+floatpower(floatabs(floatsub(y2,y1)),2)+floatpower(floatabs(floatsub(z2,z1)),2));
}