SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 19/06/2015, 11:43 PM   #1
suni
Huge Clucker
 
Join Date: Aug 2014
Location: Locating...
Posts: 280
Reputation: 0
Default nearest player

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?
suni is offline   Reply With Quote
Old 20/06/2015, 12:49 AM   #2
JaydenJason
Gangsta
 
Join Date: Oct 2014
Location: Netherlands
Posts: 685
Reputation: 51
Default Re: nearest player

http://forum.sa-mp.com/showpost.php?...38&postcount=2
JaydenJason is offline   Reply With Quote
Old 20/06/2015, 02:11 AM   #3
suni
Huge Clucker
 
Join Date: Aug 2014
Location: Locating...
Posts: 280
Reputation: 0
Default Re: nearest player

thanks but i really dont know how to start
suni is offline   Reply With Quote
Old 20/06/2015, 02:32 AM   #4
SickAttack
High-roller
 
Join Date: Dec 2013
Posts: 4,768
Reputation: 754
Default Re: nearest player

pawn Code:
// [ 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;
}
SickAttack is offline   Reply With Quote
Old 20/06/2015, 06:34 AM   #5
FarTooBaked
Little Clucker
 
Join Date: Jun 2015
Posts: 35
Reputation: 1
Default Re: nearest player

Here we go.

pawn Code:
#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;
}
FarTooBaked is offline   Reply With Quote
Old 20/06/2015, 09:42 PM   #6
suni
Huge Clucker
 
Join Date: Aug 2014
Location: Locating...
Posts: 280
Reputation: 0
Default Re: nearest player

Quote:
Originally Posted by SickAttack View Post
pawn Code:
// [ 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 View Post
Here we go.

pawn Code:
#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
suni is offline   Reply With Quote
Old 21/06/2015, 03:50 AM   #7
Crayder
Banned
 
Join Date: Sep 2013
Location: Flames of Hell
Posts: 3,329
Reputation: 652
Default Re: nearest player

Quote:
Originally Posted by FarTooBaked View Post
Here we go.

pawn Code:
#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
Crayder is offline   Reply With Quote
Old 21/06/2015, 04:43 AM   #8
SickAttack
High-roller
 
Join Date: Dec 2013
Posts: 4,768
Reputation: 754
Default Re: nearest player

Well, if you want the function to return the closest player solely; then here you go:
pawn Code:
// [ 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;
}
SickAttack is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to get nearest player name Semo Scripting Help 4 13/07/2013 11:37 PM
nearest Player GoldZoroGrab Scripting Help 3 09/12/2012 10:52 AM
Nearest player budelis Help Archive 2 27/07/2011 07:11 AM
Get nearest player austin070 Help Archive 2 04/03/2011 12:07 AM
SetPlayerAngle to nearest player.. wiilweer Help Archive 1 09/01/2010 12:19 PM


All times are GMT. The time now is 05:34 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.