nearest player
#1

i wanted to create a command /np to get the nearest player who close to you with metres like this:
Quote:

** The nearest player to you is %s (ID:%d) at %i metres...

can someone so me a example on how to create this?
Reply
#2

http://forum.sa-mp.com/showpost.php?...38&postcount=2
Reply
#3

thanks but i really dont know how to start
Reply
#4

pawn Код:
// [ DEVELOPMENT GAMEMODE ]

// INCLUDES:

#include <a_samp>
#include <foreach>
#include <zcmd>

// MAIN:

main()
{
    print("Development Mode: closest_player_to_player.amx");
}

// CALLBACKS:

public OnGameModeInit()
{
    return 1;
}

public OnGameModeExit()
{
    return 1;
}

// COMMANDS:

CMD:closestplayer(playerid, params[])
{
    GetClosestPlayerToPlayer(playerid);
    return 1;
}

// FUNCTIONS:

stock GetClosestPlayerToPlayer(playerid)
{
    new Float:x, Float:y, Float:z, closest_player = -1, Float:distance, Float:temp;
    GetPlayerPos(playerid, x, y, z);

    foreach(new i: Player)
    {
        if(i == playerid) continue;

        if(closest_player != -1)
        {
            temp = GetPlayerDistanceFromPoint(i, x, y, z);

            if(temp < distance)
            {
                closest_player = i;
                distance = temp;
            }
        }
        else
        {
            closest_player = i;
            distance = GetPlayerDistanceFromPoint(i, x, y, z);
        }
    }

    new string[144];
    if(closest_player == -1) strcat(string, "No players have been found.");
    else format(string, sizeof(string), "%s (%d) is the closest player to you (%0.2f meters).", PlayerName(closest_player), closest_player, distance);
    return SendClientMessage(playerid, -1, string);
}

stock PlayerName(playerid)
{
    new name[MAX_PLAYER_NAME];
    GetPlayerName(playerid, name, sizeof(name));
    return name;
}
Reply
#5

Here we go.

pawn Код:
#define COLOR_WHITE 0xFFFFFFAA

new Float:DistanceFromPlayer[MAX_PLAYERS];
new ThePlayer[MAX_PLAYERS];
new GotFirstDistance[MAX_PLAYERS];

/yourcommandhere

for(new i = 0; i < MAX_PLAYERS; i++)
{
    if(IsPlayerConnected(i))
    {
        new Float:pX, Float:pY, Float:pZ;
        GetPlayerPos(i, pX, pY, pZ);
       
        new Distance = GetPlayerDistanceFromPoint(playerid, pX, pY, pZ);
       
        if(GotFirstDistance[playerid] != 1)
        {
            DistanceFromPlayer[playerid] = Distance;
            ThePlayer[playerid] = i;
            GotFirstDistance[playerid] = 1;
        }
        else
        {
            if(Distance < DistanceFromPlayer[playerid])
            {
                DistanceFromPlayer[playerid] = Distance;
                ThePlayer[playerid] = i;
            }
            if(i == MAX_PLAYERS)
            {
                if(Distance < DistanceFromPlayer[playerid])
                {
                    new string[128];
                   
                    DistanceFromPlayer[playerid] = Distance;
                    ThePlayer[playerid] = i;
                   
                    new Player = ThePlayer[playerid];
                   
                    format(string, sizeof(string), "You are %0.2fm away from %s", DistanceFromPlayer[playerid], GetPlayerNameEx(Player));
                    SendClientMessage(playerid, COLOR_WHITE, string);
                }
                else
                {
                    new string[128];
                   
                    format(string, sizeof(string), "You are %0.2fm away from %s", DistanceFromPlayer[playerid], GetPlayerNameEx(Player));
                    SendClientMessage(playerid, COLOR_WHITE, string);
                }
            }
        }
    }
}
                   
//GET PLAYER NAME
stock GetPlayerNameEx(playerid)
{
    new PlayerName[24];
    GetPlayerName(playerid, PlayerName, 24);
    new string[24];
    strmid(string, PlayerName, 0, strlen(PlayerName), 24);
    return string;
}
Reply
#6

Quote:
Originally Posted by SickAttack
Посмотреть сообщение
pawn Код:
// [ DEVELOPMENT GAMEMODE ]

// INCLUDES:

#include <a_samp>
#include <foreach>
#include <zcmd>

// MAIN:

main()
{
    print("Development Mode: closest_player_to_player.amx");
}

// CALLBACKS:

public OnGameModeInit()
{
    return 1;
}

public OnGameModeExit()
{
    return 1;
}

// COMMANDS:

CMD:closestplayer(playerid, params[])
{
    GetClosestPlayerToPlayer(playerid);
    return 1;
}

// FUNCTIONS:

stock GetClosestPlayerToPlayer(playerid)
{
    new Float:x, Float:y, Float:z, closest_player = -1, Float:distance, Float:temp;
    GetPlayerPos(playerid, x, y, z);

    foreach(new i: Player)
    {
        if(i == playerid) continue;

        if(closest_player != -1)
        {
            temp = GetPlayerDistanceFromPoint(i, x, y, z);

            if(temp < distance)
            {
                closest_player = i;
                distance = temp;
            }
        }
        else
        {
            closest_player = i;
            distance = GetPlayerDistanceFromPoint(i, x, y, z);
        }
    }

    new string[144];
    if(closest_player == -1) strcat(string, "No players have been found.");
    else format(string, sizeof(string), "%s (%d) is the closest player to you (%0.2f meters).", PlayerName(closest_player), closest_player, distance);
    return SendClientMessage(playerid, -1, string);
}

stock PlayerName(playerid)
{
    new name[MAX_PLAYER_NAME];
    GetPlayerName(playerid, name, sizeof(name));
    return name;
}
thanks sir +rep code work

Quote:
Originally Posted by FarTooBaked
Посмотреть сообщение
Here we go.

pawn Код:
#define COLOR_WHITE 0xFFFFFFAA

new Float:DistanceFromPlayer[MAX_PLAYERS];
new ThePlayer[MAX_PLAYERS];
new GotFirstDistance[MAX_PLAYERS];

/yourcommandhere

for(new i = 0; i < MAX_PLAYERS; i++)
{
    if(IsPlayerConnected(i))
    {
        new Float:pX, Float:pY, Float:pZ;
        GetPlayerPos(i, pX, pY, pZ);
       
        new Distance = GetPlayerDistanceFromPoint(playerid, pX, pY, pZ);
       
        if(GotFirstDistance[playerid] != 1)
        {
            DistanceFromPlayer[playerid] = Distance;
            ThePlayer[playerid] = i;
            GotFirstDistance[playerid] = 1;
        }
        else
        {
            if(Distance < DistanceFromPlayer[playerid])
            {
                DistanceFromPlayer[playerid] = Distance;
                ThePlayer[playerid] = i;
            }
            if(i == MAX_PLAYERS)
            {
                if(Distance < DistanceFromPlayer[playerid])
                {
                    new string[128];
                   
                    DistanceFromPlayer[playerid] = Distance;
                    ThePlayer[playerid] = i;
                   
                    new Player = ThePlayer[playerid];
                   
                    format(string, sizeof(string), "You are %0.2fm away from %s", DistanceFromPlayer[playerid], GetPlayerNameEx(Player));
                    SendClientMessage(playerid, COLOR_WHITE, string);
                }
                else
                {
                    new string[128];
                   
                    format(string, sizeof(string), "You are %0.2fm away from %s", DistanceFromPlayer[playerid], GetPlayerNameEx(Player));
                    SendClientMessage(playerid, COLOR_WHITE, string);
                }
            }
        }
    }
}
                   
//GET PLAYER NAME
stock GetPlayerNameEx(playerid)
{
    new PlayerName[24];
    GetPlayerName(playerid, PlayerName, 24);
    new string[24];
    strmid(string, PlayerName, 0, strlen(PlayerName), 24);
    return string;
}
thanks for helping anyway
Reply
#7

Quote:
Originally Posted by FarTooBaked
Посмотреть сообщение
Here we go.

pawn Код:
#define COLOR_WHITE 0xFFFFFFAA

new Float:DistanceFromPlayer[MAX_PLAYERS];
new ThePlayer[MAX_PLAYERS];
new GotFirstDistance[MAX_PLAYERS];

/yourcommandhere

for(new i = 0; i < MAX_PLAYERS; i++)
{
    if(IsPlayerConnected(i))
    {
        new Float:pX, Float:pY, Float:pZ;
        GetPlayerPos(i, pX, pY, pZ);
       
        new Distance = GetPlayerDistanceFromPoint(playerid, pX, pY, pZ);
       
        if(GotFirstDistance[playerid] != 1)
        {
            DistanceFromPlayer[playerid] = Distance;
            ThePlayer[playerid] = i;
            GotFirstDistance[playerid] = 1;
        }
        else
        {
            if(Distance < DistanceFromPlayer[playerid])
            {
                DistanceFromPlayer[playerid] = Distance;
                ThePlayer[playerid] = i;
            }
            if(i == MAX_PLAYERS)
            {
                if(Distance < DistanceFromPlayer[playerid])
                {
                    new string[128];
                   
                    DistanceFromPlayer[playerid] = Distance;
                    ThePlayer[playerid] = i;
                   
                    new Player = ThePlayer[playerid];
                   
                    format(string, sizeof(string), "You are %0.2fm away from %s", DistanceFromPlayer[playerid], GetPlayerNameEx(Player));
                    SendClientMessage(playerid, COLOR_WHITE, string);
                }
                else
                {
                    new string[128];
                   
                    format(string, sizeof(string), "You are %0.2fm away from %s", DistanceFromPlayer[playerid], GetPlayerNameEx(Player));
                    SendClientMessage(playerid, COLOR_WHITE, string);
                }
            }
        }
    }
}
                   
//GET PLAYER NAME
stock GetPlayerNameEx(playerid)
{
    new PlayerName[24];
    GetPlayerName(playerid, PlayerName, 24);
    new string[24];
    strmid(string, PlayerName, 0, strlen(PlayerName), 24);
    return string;
}
Just a hint, STOP USING MAX_PLAYERS FOR LOOPS! GetPlayerPoolSize
Reply
#8

Well, if you want the function to return the closest player solely; then here you go:
pawn Код:
// [ DEVELOPMENT GAMEMODE ]

// INCLUDES:

#include <a_samp>
#include <foreach>
#include <zcmd>

// MAIN:

main()
{
    print("Development Mode: closest_player_to_player.amx");
}

// CALLBACKS:

public OnGameModeInit()
{
    return 1;
}

public OnGameModeExit()
{
    return 1;
}

// COMMANDS:

CMD:closestplayer(playerid, params[])
{
    new string[144], closest_player = GetClosestPlayerToPlayer(playerid);
    if(closest_player == -1)
    {
        strcat(string, "No players have been found.");
        SendClientMessage(playerid, -1, string);
    }
    else
    {
        new Float:x, Float:y, Float:z, Float:distance;
        GetPlayerPos(playerid, x, y, z);
        distance = GetPlayerDistanceFromPoint(closest_player, x, y, z);

        format(string, sizeof(string), "%s (%d) is the closest player to you (%0.2f meters).", PlayerName(closest_player), closest_player, distance);
        SendClientMessage(playerid, -1, string);
    }
    return 1;
}

// FUNCTIONS:

stock GetClosestPlayerToPlayer(playerid)
{
    new Float:x, Float:y, Float:z, closest_player = -1, Float:distance, Float:temp;
    GetPlayerPos(playerid, x, y, z);

    foreach(new i: Player)
    {
        if(i == playerid) continue;

        if(closest_player != -1)
        {
            temp = GetPlayerDistanceFromPoint(i, x, y, z);

            if(temp < distance)
            {
                closest_player = i;
                distance = temp;
            }
        }
        else
        {
            closest_player = i;
            distance = GetPlayerDistanceFromPoint(i, x, y, z);
        }
    }
    return closest_player;
}

stock PlayerName(playerid)
{
    new name[MAX_PLAYER_NAME];
    GetPlayerName(playerid, name, sizeof(name));
    return name;
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)