Skip to content

Commit

Permalink
Add new helper methods to GoalController to addBehaviors
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Dec 19, 2013
1 parent 33c0c1c commit 8d29243
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
20 changes: 20 additions & 0 deletions src/main/java/net/citizensnpcs/api/ai/GoalController.java
@@ -1,6 +1,7 @@
package net.citizensnpcs.api.ai;

import net.citizensnpcs.api.ai.GoalController.GoalEntry;
import net.citizensnpcs.api.ai.tree.Behavior;

/**
* Represents a collection of goals that are prioritised and executed, allowing
Expand All @@ -11,6 +12,17 @@
* a lower priority are replaced via {@link Goal#reset()}.
*/
public interface GoalController extends Runnable, Iterable<GoalEntry> {
/**
* Registers a {@link Behavior} with a given priority.
*
* @see #addGoal(Goal, int)
* @param behavior
* The behavior
* @param priority
* The priority
*/
void addBehavior(Behavior behavior, int priority);

/**
* Registers a {@link Goal} with a given priority. Priority must be greater
* than 0.
Expand Down Expand Up @@ -53,6 +65,14 @@ public interface GoalController extends Runnable, Iterable<GoalEntry> {
*/
boolean isPaused();

/**
* Removes the given {@link Behavior} from rotation.
*
* @param behavior
* The behavior to remove
*/
void removeBehavior(Behavior behavior);

/**
* Removes a {@link Goal} from rotation.
*
Expand Down
26 changes: 25 additions & 1 deletion src/main/java/net/citizensnpcs/api/ai/SimpleGoalController.java
Expand Up @@ -5,6 +5,8 @@
import java.util.List;

import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.ai.tree.Behavior;
import net.citizensnpcs.api.ai.tree.BehaviorGoalAdapter;

import org.bukkit.Bukkit;
import org.bukkit.event.HandlerList;
Expand All @@ -21,6 +23,15 @@ public class SimpleGoalController implements GoalController {
private final List<GoalEntry> possibleGoals = Lists.newArrayList();
private final GoalSelector selector = new SimpleGoalSelector();

@Override
public void addBehavior(Behavior behavior, int priority) {
if (behavior instanceof Goal) {
addGoal((Goal) behavior, priority);
return;
}
addGoal(BehaviorGoalAdapter.create(behavior), priority);
}

@Override
public void addGoal(Goal goal, int priority) {
Preconditions.checkNotNull(goal, "goal cannot be null");
Expand Down Expand Up @@ -117,6 +128,18 @@ public void remove() {
};
}

@Override
public void removeBehavior(Behavior behavior) {
for (int i = 0; i < possibleGoals.size(); ++i) {
Goal test = possibleGoals.get(i).getGoal();
if (test.equals(behavior)) {
possibleGoals.remove(i--);
if (test == executingRootGoal)
finishCurrentGoalExecution();
}
}
}

@Override
public void removeGoal(Goal goal) {
Preconditions.checkNotNull(goal, "goal cannot be null");
Expand All @@ -125,8 +148,9 @@ public void removeGoal(Goal goal) {
if (!test.equals(goal))
continue;
possibleGoals.remove(j--);
if (test == executingRootGoal)
if (test == executingRootGoal) {
finishCurrentGoalExecution();
}
}
if (goal instanceof PrioritisableGoal) {
boolean foundOther = false;
Expand Down
Expand Up @@ -22,4 +22,8 @@ public void run(GoalSelector selector) {
public boolean shouldExecute(GoalSelector selector) {
return shouldExecute();
}

public static Goal create(Behavior behavior) {
return new ForwardingBehaviorGoalAdapter(behavior);
}
}

0 comments on commit 8d29243

Please sign in to comment.