Skip to content

Instantly share code, notes, and snippets.

@scooterman
Created February 14, 2016 13:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scooterman/b03dfe1560cfc3b977a3 to your computer and use it in GitHub Desktop.
Save scooterman/b03dfe1560cfc3b977a3 to your computer and use it in GitHub Desktop.
package fabrica.game.ai;
import com.badlogic.gdx.ai.btree.SingleRunningChildBranch;
import com.badlogic.gdx.ai.btree.Task;
import com.badlogic.gdx.utils.Array;
public class ExhaustLoop<E> extends SingleRunningChildBranch<E> {
boolean childrenSuccess;
private Task<E> checker;
public ExhaustLoop() {
}
public ExhaustLoop (Task<E>... tasks) {
super(new Array<>(tasks));
}
@Override
public void start() {
super.start();
if (children.size <= 1) {
throw new IllegalStateException("The exhaust loop needs at leasy two children");
}
checker = children.get(0);
}
@Override
public void run() {
if (runningChild != null) {
runningChild.run();
} else {
childrenSuccess = false;
do {
currentChildIndex = 1;
checker.setControl(this);
checker.start();
checker.run();
if (checker.getStatus() == Status.RUNNING) {
runningChild = null;
super.run();
if (runningChild != null) {
running();
return;
}
} else {
fail();
return;
}
} while (!childrenSuccess);
}
}
@Override
public void childFail (Task<E> runningTask) {
super.childFail(runningTask);
if (++currentChildIndex < children.size) {
run(); // Run next child
}
}
@Override
public void childSuccess (Task<E> runningTask) {
childrenSuccess = true;
super.childSuccess(runningTask);
success(); // Return success status when a child says it succeeded
}
}
root
selector
# the exhaust branch is a special branch verifier. It checks the first task, and until it
# runs the next tasks are checked. This is useful when you have to apply different checks
# on a list of items. The first item will select a valid entity in the current entity range
# and set the "target" variable so the next tasks can use them to do AI stuff.
# if any of the other children succeeds then the exhaust branch will succeed, otherwise
# it will iter while "entitiesInRange" returns a RUNNING state. If it returns a FAILURE
# then the exhaust branch will fail.
# in other words: iter everything, if nothing succeeds, fail.
exhaust
# this is the exhaust task verifier, until it runs, the next tasks are called
entitiesInRange
# if any of the remaining tasks returns a SUCCESS the exhaust loop succeeded
selector
tryAttack
tryChase
randomSelector
wander
wait seconds:"uniform,1,5"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment