CPTP:ClosestPlayerToPoint(x,y,z) -
BroZeus - 15.03.2014
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;
}
Re: CPTP:ClosestPlayerToPoint(x,y,z) - Emmet_ - 15.03.2014
That's not an include... that's a single function, and there's a separate
thread designed for that purpose.
Re: CPTP:ClosestPlayerToPoint(x,y,z) -
Marricio - 15.03.2014
It's not even going to work properly, you need to update the "clos" variable when you update "id".
Re: CPTP:ClosestPlayerToPoint(x,y,z) -
BroZeus - 16.03.2014
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
Re: CPTP:ClosestPlayerToPoint(x,y,z) -
MP2 - 16.03.2014
And have you ever even
heard of indentation..?
Re: CPTP:ClosestPlayerToPoint(x,y,z) -
BroZeus - 16.03.2014
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
Re: CPTP:ClosestPlayerToPoint(x,y,z) -
Kaperstone - 16.03.2014
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)
Re: CPTP:ClosestPlayerToPoint(x,y,z) -
BroZeus - 16.03.2014
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.
Re: CPTP:ClosestPlayerToPoint(x,y,z) -
Kaperstone - 16.03.2014
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.
Re: CPTP:ClosestPlayerToPoint(x,y,z) -
Vinnyy - 16.03.2014
Fucking noobs....
PHP код:
stock ClosestPlayerToPoint(Float:_x, Float:_y, Float:_z)
{
new bIsClosestChoosed = 0;
new dwClosestID;
new Float:fClosestDistance;
new Float:px, Float:py, Float:pz;
foreach (new i : Player)
{
GetPlayerPos(i, px, py, pz);
new Float:fDistance = Floatsqroot(Floatpower(px - _x, 2) + Floatpower(py - _y, 2) + Floatpower(pz - _z, 2));
if (!bIsClosestChoosed || fClosestDistance > fDistance)
{
dwClosestID = i;
fClosestDistance = fDistance;
}
if (!bIsClosestChoosed)
{
bIsClosestChoosed = 1;
}
}
return iClosest;
}