Make FollowTargetGoal query for targets when necessary

This commit is contained in:
TheMode 2021-08-18 22:11:15 +02:00
parent 5cf358ac3c
commit 8c6778983b
2 changed files with 32 additions and 21 deletions

View File

@ -1,6 +1,7 @@
package net.minestom.server.entity.ai.goal; package net.minestom.server.entity.ai.goal;
import net.minestom.server.coordinate.Point; import net.minestom.server.coordinate.Point;
import net.minestom.server.coordinate.Pos;
import net.minestom.server.entity.Entity; import net.minestom.server.entity.Entity;
import net.minestom.server.entity.EntityCreature; import net.minestom.server.entity.EntityCreature;
import net.minestom.server.entity.ai.GoalSelector; import net.minestom.server.entity.ai.GoalSelector;
@ -15,6 +16,8 @@ public class FollowTargetGoal extends GoalSelector {
private boolean forceEnd = false; private boolean forceEnd = false;
private Point lastTargetPos; private Point lastTargetPos;
private Entity target;
/** /**
* Creates a follow target goal object. * Creates a follow target goal object.
* *
@ -28,8 +31,14 @@ public class FollowTargetGoal extends GoalSelector {
@Override @Override
public boolean shouldStart() { public boolean shouldStart() {
return entityCreature.getTarget() != null && Entity target = entityCreature.getTarget();
entityCreature.getTarget().getPosition().distance(entityCreature.getPosition()) >= 2; if (target == null) target = findTarget();
if (target == null) return false;
final boolean result = target.getPosition().distance(entityCreature.getPosition()) >= 2;
if (result) {
this.target = target;
}
return result;
} }
@Override @Override
@ -37,23 +46,23 @@ public class FollowTargetGoal extends GoalSelector {
lastUpdateTime = 0; lastUpdateTime = 0;
forceEnd = false; forceEnd = false;
lastTargetPos = null; lastTargetPos = null;
final Entity target = entityCreature.getTarget(); if (target == null) {
if (target != null) { // No defined target
Navigator navigator = entityCreature.getNavigator(); this.forceEnd = true;
return;
lastTargetPos = target.getPosition(); }
if (lastTargetPos.distance(entityCreature.getPosition()) < 2) { this.entityCreature.setTarget(target);
forceEnd = true; Navigator navigator = entityCreature.getNavigator();
navigator.setPathTo(null); this.lastTargetPos = target.getPosition();
return; if (lastTargetPos.distance(entityCreature.getPosition()) < 2) {
} // Target is too far
this.forceEnd = true;
if (navigator.getPathPosition() == null || navigator.setPathTo(null);
(!navigator.getPathPosition().samePoint(lastTargetPos))) { return;
navigator.setPathTo(lastTargetPos); }
} else { if (navigator.getPathPosition() == null ||
forceEnd = true; (!navigator.getPathPosition().samePoint(lastTargetPos))) {
} navigator.setPathTo(lastTargetPos);
} else { } else {
forceEnd = true; forceEnd = true;
} }
@ -66,7 +75,7 @@ public class FollowTargetGoal extends GoalSelector {
pathDuration.toMillis() + lastUpdateTime > time) { pathDuration.toMillis() + lastUpdateTime > time) {
return; return;
} }
final var targetPos = entityCreature.getTarget() != null ? entityCreature.getTarget().getPosition() : null; final Pos targetPos = entityCreature.getTarget() != null ? entityCreature.getTarget().getPosition() : null;
if (targetPos != null && !targetPos.samePoint(lastTargetPos)) { if (targetPos != null && !targetPos.samePoint(lastTargetPos)) {
this.lastUpdateTime = time; this.lastUpdateTime = time;
this.lastTargetPos = targetPos; this.lastTargetPos = targetPos;
@ -79,11 +88,12 @@ public class FollowTargetGoal extends GoalSelector {
final Entity target = entityCreature.getTarget(); final Entity target = entityCreature.getTarget();
return forceEnd || return forceEnd ||
target == null || target == null ||
target.isRemoved() ||
target.getPosition().distance(entityCreature.getPosition()) < 2; target.getPosition().distance(entityCreature.getPosition()) < 2;
} }
@Override @Override
public void end() { public void end() {
entityCreature.getNavigator().setPathTo(null); this.entityCreature.getNavigator().setPathTo(null);
} }
} }

View File

@ -21,6 +21,7 @@ public class ClosestEntityTarget extends TargetSelector {
private final float range; private final float range;
private final Class<? extends LivingEntity>[] entitiesTarget; private final Class<? extends LivingEntity>[] entitiesTarget;
@SafeVarargs
public ClosestEntityTarget(@NotNull EntityCreature entityCreature, float range, public ClosestEntityTarget(@NotNull EntityCreature entityCreature, float range,
@NotNull Class<? extends LivingEntity>... entitiesTarget) { @NotNull Class<? extends LivingEntity>... entitiesTarget) {
super(entityCreature); super(entityCreature);