Avarax's NearestPlayer function, with a bit more functionality. This was taken from MMod v1.1
//team -> -1 to -5: Player is not of that team. 0: Team doesn't matter. 1 to 5: Player is of that team
//dead -> 0: Player must be alive. 1: Player must be dead. >1: Doesn't matter.
//bot -> -1: Doesn't matter. 0: Player is no bot. 1: Player is a bot.
function NearestPlayer(const X1,Y1: single; const team,dead,bot: integer; maxrange: single): byte;
var dist: single;
i: byte;
begin
result:=0;
for i:=1 to MaxID do
If (GetPlayerStat(i,'Active')=true) and ((bot = -1) or (GetPlayerStat(i,'Human') = true) and (bot = 0) or (GetPlayerStat(i,'Human') = false) and (bot = 1)) and (((GetPlayerStat(i,'Alive')=true) and (dead = 0)) or ((GetPlayerStat(i,'Alive')=false) and (dead = 1)) or (dead>1)) and (((GetPlayerStat(i,'Team')<>-team) and (team<0)) or ((GetPlayerStat(i,'Team')=team) and (team>0)) or (team=0)) then begin
dist:=Distance(X1,Y1,GetPlayerStat(i,'X'),GetPlayerStat(i,'Y'));
If (dist < maxrange) and (dist > 0.001) then
begin
maxrange:=dist;
result:=i;
end;
end;
end;