for(new playerid = 0; playerid < MAX_PLAYERS; playerid++)
{
if(!IsPlayerNPC(playerid) && IsPlayerConnected(playerid))
{
new Float:x, Float:y, Float:z;
FCNPC_GetPosition(npcid, x, y, z);
if(IsPlayerInRangeOfPoint(playerid, GetVictimDetectRange[playerid], x, y, z) && GetPlayerState(playerid) == PLAYER_STATE_ONFOOT && GetPlayerInterior(playerid) == 0 && GetPlayerVirtualWorld(playerid) == 0)
{
return playerid;
}
}
}
new ids[MAX_PLAYERS],i=0;
for(new playerid = 0; playerid < MAX_PLAYERS; playerid++)
{
if(!IsPlayerNPC(playerid) && IsPlayerConnected(playerid))
{
new Float:x, Float:y, Float:z;
FCNPC_GetPosition(npcid, x, y, z);
if(IsPlayerInRangeOfPoint(playerid, GetVictimDetectRange[playerid], x, y, z) && GetPlayerState(playerid) == PLAYER_STATE_ONFOOT && GetPlayerInterior(playerid) == 0 && GetPlayerVirtualWorld(playerid) == 0)
{
ids[i] = playerid;
i++;
}
}
}
if(i != 0)
{
return ids[random(i)];
}
return INVALID_PLAYER_ID;
new Float:x, Float:y, Float:z;
FCNPC_GetPosition(npcid, x, y, z);
new Float:minDist = 9999.9; // I guess you don't have FLOAT_INFINITY
new id = INVALID_PLAYER_ID;
for(new playerid = 0; playerid < MAX_PLAYERS; playerid++)
{
if(!IsPlayerNPC(playerid) && IsPlayerConnected(playerid) && GetPlayerState(playerid) == PLAYER_STATE_ONFOOT && GetPlayerInterior(playerid) == 0 && GetPlayerVirtualWorld(playerid) == 0)
{
new Float:dist = GetPlayerDistanceFromPoint(playerid, x, y, z);
if(dist < minDist && dist < GetVictimDetectRange[playerid])
{
minDist = dist;
id = playerid;
}
}
}
if (id != INVALID_PLAYER_ID) {
// Here's your closest player id, otherwise none found in range
}
Modified version of this: http://forum.sa-mp.com/showpost.php?...50&postcount=2
pawn Code:
|
new ids[MAX_PLAYERS], count = 0;
new Float:x, Float:y, Float:z;
FCNPC_GetPosition(npcid, x, y, z);
for(new playerid = 0; playerid < MAX_PLAYERS; playerid++)
{
if(!IsPlayerNPC(playerid) && IsPlayerConnected(playerid))
{
if(IsPlayerInRangeOfPoint(playerid, GetVictimDetectRange[playerid], x, y, z) && GetPlayerState(playerid) == PLAYER_STATE_ONFOOT && GetPlayerInterior(playerid) == 0 && GetPlayerVirtualWorld(playerid) == 0)
{
ids[count] = playerid;
count++;
}
}
}
if(count != 0)
{
return ids[random(count)];
}
return INVALID_PLAYER_ID;
Hm, can you dump a bunch of prints here and there and posting the results?
|
stock GetTarget(npcid){
new Float:x, Float:y, Float:z, Float:angle, pstate,
pool_players[MAX_PLAYERS], pool_upp = -1;
FCNPC_GetPosition(npcid,x,y,z);
angle = FCNPC_GetAngle(npcid);
Tryg3D::Foreach(i){
pstate = GetPlayerState(i);
if(GetElementsDistance(i,item_player,npcid,item_fcnpc) <= 300.0 && GetPlayerInterior(i) == 0 && GetPlayerVirtualWorld(i) == 0){
if(pstate != PLAYER_STATE_SPECTATING && pstate != PLAYER_STATE_WASTED){
if(IsElementOnFakeScreen(x,y,z,i,item_player,0.0,angle)){ //for vehicle collision: .veh_col=true
pool_upp++;
pool_players[pool_upp] = i;
}
}
}
}
if(pool_upp == -1) return INVALID_PLAYER_ID;
return pool_players[random(pool_upp+1)];
}