public GetClosestPlayerToPlayer(playerid)
{
new Float:dist = 10.0;
new targetid = -1;
new Float:x1,Float:y1,Float:z1;
new Float:x2,Float:y2,Float:z2;
new Float:tmpdis;
GetPlayerPos(playerid,x1,y1,z1);
for(new i=0;i<MAX_PLAYERS;i++)
{
if(i == INVALID_PLAYER_ID) continue;
if(i == playerid) continue;
if(!IsPlayerConnectedEx(i)) continue;
if(Spectator[i][SpecSpectatingPlayer] != -1) continue;
if(Player[i][AdminDuty] == 1) continue;
GetPlayerPos(i,x2,y2,z2);
tmpdis = floatsqroot(floatpower(floatabs(floatsub(x2,x1)),2)+floatpower(floatabs(floatsub(y2,y1)),2)+floatpower(floatabs(floatsub(z2,z1)),2));
if(tmpdis < dist)
{
dist = tmpdis;
targetid = i;
}
}
return targetid;
}
GetClosestPlayer( toplayer, Float:range )
{
new Float:tpos[ 3 ], targetid = INVALID_PLAYER_ID;
GetPlayerPos( toplayer, tpos[ 0 ], tpos[ 1 ], tpos[ 2 ] );
foreach( Player, i )
{
if( IsPlayerInRangeOfPoint( i, range, tpos[ 0 ], tpos[ 1 ], tpos[ 2 ] ) )
targetid = i;
}
return targetid;
}
enum DISTINFO { Float:cdistance, distpid, }
new pdistance[MAX_PLAYERS][DISTINFO];
GetClosestPlayers(playerid)
{
new Float:x, Float:y, Float:z;
GetPlayerPos(playerid, x, y, z);
for(new i = 0; i < MAX_PLAYERS; i++)
{
if(!IsPlayerConnected(i) || IsPlayerNPC(i) || i == playerid)
{
pdistance[i][cdistance] = -1.0;
pdistance[i][distpid] = i;
}
else
{
pdistance[i][cdistance] = GetPlayerDistanceFromPoint(i, x, y, z);
pdistance[i][distpid] = i;
}
}
SortDeepArray(pdistance, cdistance);
return 1;
}
@Pottus, would there be a way to make that only work within a range of 10.0?
|
#if !defined INFINITY
#define INFINITY (Float:0x7F800000)
#endif
GetClosestPlayerInRange(playerid, Float:range)
{
new Float:x, Float:y, Float:z, Float:dist, Float:closedist = INFINITY, closeid = INVALID_PLAYER_ID;
GetPlayerPos(playerid, x, y, z);
for(new i = 0; i < MAX_PLAYERS; i++)
{
if(!IsPlayerConnected(i) ||
IsPlayerNPC(i) ||
i == playerid)
continue;
else
{
dist = GetPlayerDistanceFromPoint(i, x, y, z);
if(dist <= range)
{
if(dist < closedist)
{
closedist = dist;
closeid = i;
}
}
}
}
return closeid;
}
Well you could just check the list the afterwards basically it just gets every players position then sorts the list it was really meant as just another idea you might be interested in.
It's pretty easy to make a function to do that though. pawn Код:
|