27.09.2017, 15:36
Well, I've this function to get the player within the range of zombie. Today I was with my friend and all the zombies were after him and ignored me, so I thought that the loop is taking the player which comes in it on first that means if there are more than 1 player near the zombies, it won't get the other players. So I'm thinking something like to get the ids of the players near the zombies and return a random id from those player ids for the zombie. But I got no idea how would I make that.
EDIT: At the moment, I made this, if I look it at it, the code looks pretty bad and unoptimized, so I gotta find a better way, help me.
PHP Code:
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;
}
}
}
PHP Code:
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;