Commit d8d4e234 by Jonathan Thomas

Improvements to Goals and Priorities:

- Added moveConflictingGoals function (to prevent duplicate goal priorities)
- Fixed FollowPlayerGoal to not clear the this.targetPlayer variable when stop() is called. This can be temporarily stopped due to a higher priority goal.
- Updated TALK priority to 2, and FOLLOW priority to 3, for maximum compatability across entities, without overriding their most important survival/attack goals.
parent 6c7acc20
Pipeline #11947 passed with stage
in 24 seconds
......@@ -4,6 +4,7 @@ import net.minecraft.entity.ai.goal.Goal;
import net.minecraft.entity.ai.goal.GoalSelector;
import net.minecraft.entity.mob.MobEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.entity.ai.goal.PrioritizedGoal;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -29,6 +30,9 @@ public class EntityBehaviorManager {
// Use removeGoal to remove any existing goal of the same type
removeGoal(entity, goal.getClass());
// Move any conflicting goals +1 in priority
moveConflictingGoals(entity, priority);
// Now that any existing goal of the same type has been removed, we can add the new goal
List<Goal> goals = entityGoals.computeIfAbsent(entityId, k -> new ArrayList<>());
goals.add(goal);
......@@ -54,4 +58,37 @@ public class EntityBehaviorManager {
});
}
}
private static void moveConflictingGoals(MobEntity entity, GoalPriority newGoalPriority) {
GoalSelector goalSelector = GoalUtils.getGoalSelector(entity);
// Retrieve the existing goals
Set<PrioritizedGoal> existingGoals = new HashSet<>(goalSelector.getGoals());
// Flag to check if there is a goal with the same priority as the new goal
boolean conflictExists = existingGoals.stream()
.anyMatch(goal -> goal.getPriority() == newGoalPriority.getPriority());
if (!conflictExists) {
// If there's no conflict, no need to adjust priorities
return;
}
// If there's a conflict, collect goals that need their priority incremented
List<PrioritizedGoal> goalsToModify = new ArrayList<>();
for (PrioritizedGoal prioritizedGoal : existingGoals) {
if (prioritizedGoal.getPriority() >= newGoalPriority.getPriority()) {
goalsToModify.add(prioritizedGoal);
}
}
// Increment priorities and re-add goals
for (PrioritizedGoal goalToModify : goalsToModify) {
// Remove the original goal
goalSelector.remove(goalToModify.getGoal());
// Increment the priority and re-add the goal
goalSelector.add(goalToModify.getPriority() + 1, goalToModify.getGoal());
}
}
}
......@@ -38,7 +38,6 @@ public class FollowPlayerGoal extends Goal {
@Override
public void stop() {
this.targetPlayer = null;
this.navigation.stop();
}
......
......@@ -6,8 +6,8 @@ package com.owlmaddie.goals;
*/
public enum GoalPriority {
// Enum constants (Goal Types) with their corresponding priority values
TALK_PLAYER(1),
FOLLOW_PLAYER(2);
TALK_PLAYER(2),
FOLLOW_PLAYER(3);
private final int priority;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment