SA-MP Forums Archive
nearest Player - 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: nearest Player (/showthread.php?tid=398381)



nearest Player - GoldZoroGrab - 09.12.2012

how to make for example i made rob command

how for example if player did /rob without id it robs the nearest player


just give me the code of nearest player

i have rob command but id must be written

tell me how to make get nearest player if no id written

and if there is no near player it will say no near players\ /rob [id]

the range of point must be 8 (distance)


Re: nearest Player - Konstantinos - 09.12.2012

pawn Код:
#define GetDistanceBetweenCoords(%1,%2,%3,%4,%5,%6) floatsqroot((%4 - %1)*(%4 - %1) + (%5 - %2)*(%5 - %2) + (%6 - %3)*(%6 - %3))

stock GetPlayerClosestPlayer(playerid,bool:NPC = true)
{
    new pid = INVALID_PLAYER_ID,
        Float:distance = 8.0,
        Float:tmp[7];
    GetPlayerPos(playerid,tmp[0],tmp[1],tmp[2]);
    for(new i = 0; i < MAX_PLAYERS; i++)
    {
        if(i == playerid) continue;
        if(!IsPlayerConnected(i)) continue;
        if(NPC == false) if(IsPlayerNPC(i)) continue;
        GetPlayerPos(i,tmp[3],tmp[4],tmp[5]);
        tmp[6] = GetDistanceBetweenCoords(tmp[0],tmp[1],tmp[2],tmp[3],tmp[4],tmp[5]);
        if(distance < tmp[6]) continue;
        distance = tmp[6];
        pid = i;
    }
    return pid;
}

// Usage: GetPlayerClosestPlayer( playerid );



Re: nearest Player - GoldZoroGrab - 09.12.2012

command(rob, playerid, params[])
{
new ID;
return 1;
}
ok i gave u new ID
now for example do it for me in that code the nearest player


Re: nearest Player - Konstantinos - 09.12.2012

Not finished, you should make it. I'll just give you some tips for the beginning.
pawn Код:
#define GetDistanceBetweenCoords(%1,%2,%3,%4,%5,%6) floatsqroot((%4 - %1)*(%4 - %1) + (%5 - %2)*(%5 - %2) + (%6 - %3)*(%6 - %3))

command(rob, playerid, params[])
{
    new
        ID
    ;
    if( !sscanf( params, "r", ID ) )
    {
        // Rob the ID
    }
    else GetPlayerClosestPlayer( playerid );
    return 1;
}

stock GetPlayerClosestPlayer(playerid,bool:NPC = true)
{
    new pid = INVALID_PLAYER_ID,
        Float:distance = 8.0,
        Float:tmp[7];
    GetPlayerPos(playerid,tmp[0],tmp[1],tmp[2]);
    for(new i = 0; i < MAX_PLAYERS; i++)
    {
        if(i == playerid) continue;
        if(!IsPlayerConnected(i)) continue;
        if(NPC == false) if(IsPlayerNPC(i)) continue;
        GetPlayerPos(i,tmp[3],tmp[4],tmp[5]);
        tmp[6] = GetDistanceBetweenCoords(tmp[0],tmp[1],tmp[2],tmp[3],tmp[4],tmp[5]);
        if(distance < tmp[6]) continue;
        distance = tmp[6];
        pid = i;
    }
    return pid;
}