Skip to content

Commit 2428a4b

Browse files
committed
Few potential tweaks to reduce chance of AttackPassive retaining targets for too long
1 parent fe92cb5 commit 2428a4b

1 file changed

Lines changed: 33 additions & 9 deletions

File tree

craftbook-bukkit/src/main/java/org/enginehub/craftbook/bukkit/mechanics/betterai/AttackPassiveGoal.java

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,19 @@ class AttackPassiveGoal implements Goal<Monster> {
3434
new NamespacedKey("craftbook", "attack_passive")
3535
);
3636

37+
/**
38+
* Distance within which the target is considered active and will not be forgotten or overridden.
39+
*/
40+
private final static int ACTIVE_DISTANCE = 15;
41+
/**
42+
* Distance within which to search for passive mobs to attack.
43+
*/
44+
private final static double SEARCH_DISTANCE = 10d;
45+
/**
46+
* Distance beyond which the target will always be forgotten.
47+
*/
48+
private final static int FORGET_DISTANCE = 60;
49+
3750
private final Monster monster;
3851
private final boolean attackPassiveIgnoreHostileMounts;
3952
private @Nullable LivingEntity target;
@@ -61,21 +74,32 @@ public void stop() {
6174

6275
@Override
6376
public void tick() {
64-
if (target != null && !target.isDead()) {
65-
if (target.getWorld().equals(monster.getWorld())
66-
&& target.getLocation().distanceSquared(monster.getLocation()) < 15 * 15) {
77+
if (target != null) {
78+
var targetValid = target.isValid() && !target.isDead() && target.getWorld().equals(monster.getWorld());
79+
var distanceSquared = target.getLocation().distanceSquared(monster.getLocation());
80+
81+
if (targetValid && distanceSquared < ACTIVE_DISTANCE * ACTIVE_DISTANCE) {
82+
// Not outside of active distance, keep target & skip the search.
6783
return;
6884
}
85+
86+
if (!targetValid || distanceSquared > FORGET_DISTANCE * FORGET_DISTANCE) {
87+
// Not a valid target, or beyond the forget distance - remove it and allow another search
88+
this.target = null;
89+
monster.setTarget(null);
90+
}
91+
92+
// If it's valid & inside forget distance, but outside active distance, we continue to search for a closer target.
6993
}
7094

7195
Animals closest = null;
7296
double closestDist = Double.MAX_VALUE;
7397

74-
for (Entity ent : monster.getNearbyEntities(10D, 10D, 10D)) {
75-
if (ent instanceof Animals && monster.hasLineOfSight(ent)) {
76-
if (attackPassiveIgnoreHostileMounts && !ent.getPassengers().isEmpty()) {
98+
for (Entity ent : monster.getNearbyEntities(SEARCH_DISTANCE, SEARCH_DISTANCE, SEARCH_DISTANCE)) {
99+
if (ent instanceof Animals animal && monster.hasLineOfSight(animal)) {
100+
if (attackPassiveIgnoreHostileMounts && !animal.getPassengers().isEmpty()) {
77101
boolean foundAny = false;
78-
for (Entity passenger : ent.getPassengers()) {
102+
for (Entity passenger : animal.getPassengers()) {
79103
if (passenger instanceof Monster) {
80104
foundAny = true;
81105
break;
@@ -85,9 +109,9 @@ public void tick() {
85109
continue;
86110
}
87111
}
88-
double dist = ent.getLocation().distanceSquared(monster.getLocation());
112+
double dist = animal.getLocation().distanceSquared(monster.getLocation());
89113
if (dist < closestDist) {
90-
closest = (Animals) ent;
114+
closest = animal;
91115
closestDist = dist;
92116
}
93117
}

0 commit comments

Comments
 (0)