FCNPC_GoToPlayerEx
Thanks everyone, with your help I got the NPC to recognize the closest player and chase him.
Now the issue is that the NPC's target doesn't change. They will always chase the one initial player even though other players are closer to them. I've used timer to update the closest player every half second, but the NPC does not change the target. |
Could you post the related code? Otherwise we have to guess what could be the cause
|
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);
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); |
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 }
This code does not determine the closest player.
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. |
foreach(Player, i) { //stuff }
for(new i = 0; i < MAX_PLAYERS; i++) { if(IsPlayerConnected(i)) { //stuff } }