26.10.2012, 12:27
pawn Код:
// Returns the nearest player to point x/y/z
// ignoreplayer will be ignored, if set
// (useful when you want to find the closest player to another player)
stock GetNearestPlayer(Float:x, Float:y, Float:z, ignoreplayer=-1) {
// Store the currently nearest player and currently shortest distance in these:
new Float:dist=9999999.0, player = -1;
new Float:px, Float:py, Float:pz; // Position of the currently looped player
new Float:curdist;
// Loop through all players
for (new i = 0; i < MAX_PLAYERS; i++) {
// Skip not connected players, and the player to ignore, if set
if (!IsPlayerConnected(i) || i == ignoreplayer) continue;
// Calculate quadratic distance from x/y/z to player
GetPlayerPos(i, px, py, pz);
curdist = (px - x) * (px - x) + (py - y) * (py - y) + (pz - z) * (pz - z);
// if it is closer than the current minimum, update it
if (curdist < dist) {
dist = curdist;
player = i;
}
}
return player;
}
You could also have searched btw.