21.09.2018, 14:41
Quote:
Sorry about that, you are right
![]() Код:
for(new i=0; i < MAX_PLAYERS; i++){ new Float:xp, Float: yp, Float: zp; GetPlayerPos(playerid, xp, yp, zp); for(new iz = 0; iz < WaveZombies; iz++){ if(FCNPC_IsValid(Zombies[iz])){ if(IsPlayerInRangeOfPoint(Zombies[iz], 10, xp, yp, zp)){ FCNPC_GoToPlayer(Zombies[iz], playerid, FCNPC_MOVE_TYPE_RUN, FCNPC_MOVE_SPEED_RUN, false, 0.0, true, 0.0, 1.5, 250); |
It will select everyone in range as target, the highest playerid will then be the final target as it is set last.
To make it actually search for closest player, you can declare two variables before the loop.
One for the target playerid and one for the distance.
Then use GetPlayerDistanceToPoint instead of IsPlayerInRangeOfPoint. If that distance is lower than the current target's distance, update the target variable and the distance.
After the loop, the target variable will contain the closest playerid.
Something like this:
Код:
new target_id = -1, Float:distance = 10.0; // max distance can be configured here for(new i = 0; i < MAX_PLAYERS; i ++) { new Float:x, Float:y, Float:z, Float:tmp_distance; GetPlayerPos(i, x, y, z); tmp_distance = GetPlayerDistanceFromPoint(ZombieID, x, y, z); if(tmp_distance < distance) { distance = tmp_distance; target_id = i; } } if(target_id != -1) { // target_id is the closest player } else { // No target found, do something else }
Also you might want to consider using foreach in combination with the streamer plugin.
foreach will allow for more efficient loops.
With the Streamer Plugin you can create a dynamic area and attach it to the zombies. Then you can just loop through players that are already in those areas instead of checking all zombies vs all players. That will considerably lower the number of distance checks, as it's highly unlikely that all players are always near all zombies.