SA-MP Forums Archive
GetClosestPlayer Bug - 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)
+--- Thread: GetClosestPlayer Bug (/showthread.php?tid=533841)



GetClosestPlayer Bug - Stefand - 26.08.2014

For some reason, even being in spectate mode, or on admin duty, I keep getting messages that I was the closest player.

pawn Код:
public GetClosestPlayerToPlayer(playerid)
{
    new Float:dist = 10.0;
    new targetid = -1;
    new Float:x1,Float:y1,Float:z1;
    new Float:x2,Float:y2,Float:z2;
    new Float:tmpdis;
    GetPlayerPos(playerid,x1,y1,z1);
    for(new i=0;i<MAX_PLAYERS;i++)
    {
        if(i == INVALID_PLAYER_ID) continue;
        if(i == playerid) continue;
        if(!IsPlayerConnectedEx(i)) continue;
        if(Spectator[i][SpecSpectatingPlayer] != -1) continue;
        if(Player[i][AdminDuty] == 1) continue;
        GetPlayerPos(i,x2,y2,z2);
        tmpdis = floatsqroot(floatpower(floatabs(floatsub(x2,x1)),2)+floatpower(floatabs(floatsub(y2,y1)),2)+floatpower(floatabs(floatsub(z2,z1)),2));
        if(tmpdis < dist)
        {
            dist = tmpdis;
            targetid = i;
        }
    }
    return targetid;
}
If ANYONE has a better get closest player script, that actually works. feel free to post it.


Re: GetClosestPlayer Bug - Aerotactics - 26.08.2014

I'm assuming you never tried the search bar...

pawn Код:
GetClosestPlayer( toplayer, Float:range )
{
    new Float:tpos[ 3 ], targetid = INVALID_PLAYER_ID;
    GetPlayerPos( toplayer, tpos[ 0 ], tpos[ 1 ], tpos[ 2 ] );
    foreach( Player, i )
    {
        if( IsPlayerInRangeOfPoint( i, range, tpos[ 0 ], tpos[ 1 ], tpos[ 2 ] ) )
            targetid = i;
    }
    return targetid;
}



Re: GetClosestPlayer Bug - Pottus - 26.08.2014

That would return the last closest player in the specified range lol.

Here is my method it does a little bit more instead of only getting the closest player it will get the distance to all players then sort the list.

https://sampforum.blast.hk/showthread.php?tid=343172 - Deep sort include

pawn Код:
enum DISTINFO { Float:cdistance, distpid, }
new pdistance[MAX_PLAYERS][DISTINFO];
GetClosestPlayers(playerid)
{
    new Float:x, Float:y, Float:z;
    GetPlayerPos(playerid, x, y, z);

    for(new i = 0; i < MAX_PLAYERS; i++)
    {
        if(!IsPlayerConnected(i) || IsPlayerNPC(i) || i == playerid)
        {
            pdistance[i][cdistance] = -1.0;
            pdistance[i][distpid] = i;
        }
        else
        {
            pdistance[i][cdistance] = GetPlayerDistanceFromPoint(i, x, y, z);
            pdistance[i][distpid] = i;
        }
    }
    SortDeepArray(pdistance, cdistance);
    return 1;
}



Re: GetClosestPlayer Bug - Stefand - 26.08.2014

@Pottus, would there be a way to make that only work within a range of 10.0?


Re: GetClosestPlayer Bug - Pottus - 26.08.2014

Quote:
Originally Posted by Stefand
Посмотреть сообщение
@Pottus, would there be a way to make that only work within a range of 10.0?
Well you could just check the list the afterwards basically it just gets every players position then sorts the list it was really meant as just another idea you might be interested in.

It's pretty easy to make a function to do that though.

pawn Код:
#if !defined INFINITY
    #define INFINITY (Float:0x7F800000)
#endif

GetClosestPlayerInRange(playerid, Float:range)
{
    new Float:x, Float:y, Float:z, Float:dist, Float:closedist = INFINITY, closeid = INVALID_PLAYER_ID;
    GetPlayerPos(playerid, x, y, z);

    for(new i = 0; i < MAX_PLAYERS; i++)
    {
        if(!IsPlayerConnected(i) ||
            IsPlayerNPC(i) ||
            i == playerid)
            continue;
        else
        {
            dist = GetPlayerDistanceFromPoint(i, x, y, z);
            if(dist <= range)
            {
                if(dist < closedist)
                {
                    closedist = dist;
                    closeid = i;
                }
            }
        }
    }
    return closeid;
}



Re: GetClosestPlayer Bug - Stefand - 27.08.2014

Quote:
Originally Posted by Pottus
Посмотреть сообщение
Well you could just check the list the afterwards basically it just gets every players position then sorts the list it was really meant as just another idea you might be interested in.

It's pretty easy to make a function to do that though.

pawn Код:
#if !defined INFINITY
    #define INFINITY (Float:0x7F800000)
#endif

GetClosestPlayerInRange(playerid, Float:range)
{
    new Float:x, Float:y, Float:z, Float:dist, Float:closedist = INFINITY, closeid = INVALID_PLAYER_ID;
    GetPlayerPos(playerid, x, y, z);

    for(new i = 0; i < MAX_PLAYERS; i++)
    {
        if(!IsPlayerConnected(i) ||
            IsPlayerNPC(i) ||
            i == playerid)
            continue;
        else
        {
            dist = GetPlayerDistanceFromPoint(i, x, y, z);
            if(dist <= range)
            {
                if(dist < closedist)
                {
                    closedist = dist;
                    closeid = i;
                }
            }
        }
    }
    return closeid;
}
That script still says I was the closest player o.o what?


Re: GetClosestPlayer Bug - MicroD - 27.08.2014

Stefand you did something wrong, everything should work fine.