[Include] CPTP:ClosestPlayerToPoint(x,y,z)
#1

CPTP:ClosestPlayerToPoint(x,y,z)
So this is a very simple and a samll include but very useful

What it does?
It returns the id of closet player to a point.

Usage
Its usage is like this--
pawn Код:
ClosestPlayerToPoint(x,y,z)
Waht will you need to use it?
You will need foreach.inc for its use.

Code
A very short code so i will not provide any link and here is the code--
pawn Код:
/*This include is by Zeus and whole credit goes to him*/
/*===================DO Not Remove This credit========*/

#include <a_samp>
#include <foreach>

stock ClosestPlayerToPoint(Float:x,Float:y,Float:z)
{
new Float:clos=20000.0,m,Float:distance,id;

foreach(m:Player)
{
distance=GetPlayerDistanceFromPoint(m,x,y,z);
if(distance < clos)
{
id=m;
clos=distance;
}
}
return id;
}
Reply
#2

That's not an include... that's a single function, and there's a separate thread designed for that purpose.
Reply
#3

It's not even going to work properly, you need to update the "clos" variable when you update "id".
Reply
#4

Quote:
Originally Posted by Marricio
Посмотреть сообщение
It's not even going to work properly, you need to update the "clos" variable when you update "id".
oops my bad corrected that thanks for telling
Reply
#5

And have you ever even heard of indentation..?
Reply
#6

Quote:
Originally Posted by MP2
Посмотреть сообщение
And have you ever even heard of indentation..?
yes of course i do but whats the use of in indentation in this script
Reply
#7

would be nicer if it had some math in it.
pawn Код:
// Too lazy for 3d,so ... 2d will be enough I guess ...
stock ClosestPlayerToPoint(Float:x,Float:y) {
    new iClosest,d[MAX_PLAYERS],Float:x,Float:y,Float:z;
    foreach(new i : Player) {
        GetPlayerPos(i,x,y,z);
        d[i]=Floatsqroot(Floatpower(x,2) + Floatpower(y,2));
        if(d[i] < d[i-1]) {
                iClosest=i;
        }
    }
    return iClosest;
}
All what's left is Z
(Height for 3d distance,in my code it's 2d,because it calculates x and y distance)
Reply
#8

Quote:
Originally Posted by xkirill
Посмотреть сообщение
would be nicer if it had some math in it.
pawn Код:
// Too lazy for 3d,so ... 2d will be enough I guess ...
stock ClosestPlayerToPoint(Float:x,Float:y) {
    new iClosest,d[MAX_PLAYERS],Float:x,Float:y,Float:z;
    foreach(new i : Player) {
        GetPlayerPos(i,x,y,z);
        d[i]=Floatsqroot(Floatpower(x,2) + Floatpower(y,2));
        if(d[i] < d[i-1]) {
                iClosest=i;
        }
    }
    return iClosest;
}
All what's left is Z ;)
(Height for 3d distance,in my code it's 2d,because it calculates x and y distance)
that code has some errors for your in formation u have declared x and y twice one in stock ClosestPlayerToPoint(Float:x,Float:y)
and one in new iClosest,d[MAX_PLAYERS],Float:x,Float:y,Float:z;
so the declaration of x and y lines should be removed.
Reply
#9

Quote:
Originally Posted by BroZeus
Посмотреть сообщение
that code has some errors for your in formation u have declared x and y twice one in stock ClosestPlayerToPoint(Float,Float:y)
and one in new iClosest,d[MAX_PLAYERS],Float,Float:y,Float:z;
so the declaration of x and y lines should be removed.
Oh yeah,completely forgot about it,lol.
Anyhow,it's only an example;I think you got my point.
Reply
#10

Fucking noobs....

PHP код:
stock ClosestPlayerToPoint(Float:_xFloat:_yFloat:_z
{
    new 
bIsClosestChoosed 0;
    new 
dwClosestID;
    new 
Float:fClosestDistance;
    new 
Float:pxFloat:pyFloat:pz;
    foreach (new 
Player
    {
        
GetPlayerPos(ipxpypz);
        new 
Float:fDistance Floatsqroot(Floatpower(px _x2) + Floatpower(py _y2) + Floatpower(pz _z2));
        if (!
bIsClosestChoosed || fClosestDistance fDistance)
        {
                
dwClosestID i;
                
fClosestDistance fDistance;
        }
        if (!
bIsClosestChoosed)
        {
                
bIsClosestChoosed 1;
        }
    }
    return 
iClosest;

Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)