Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

behavior tree added #4

Merged
merged 1 commit into from
Oct 3, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions gdx-ai/src/com/badlogic/gdx/ai/btree/BehaviorTree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright 2014 See AUTHORS file.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.badlogic.gdx.ai.btree;

import com.badlogic.gdx.ai.btree.parser.TreeParser;
import com.badlogic.gdx.ai.btree.parser.TreeLineProcessor;

/**
* The behavior tree itself
*
* @param <E> type of the blackboard nodes use to read or modify game state
*
* @author implicit-invocation
*/
public class BehaviorTree<E> extends Node<E> {

private final Node<E> rootNode;

/**
* Create a behavior tree with a root node and a blackboard object
*
* @param rootNode the root node of this tree
* @param object the blackboard
*/
public BehaviorTree(Node<E> rootNode, E object) {
this.rootNode = rootNode;
this.object = object;
}

/**
* Create a behavior tree with a text format definition and a blackboard
* object
*
* @param data the text tree
* @param object the blackboard
*/
public BehaviorTree(String data, E object) {
this.rootNode = TreeParser.parse(data, new TreeLineProcessor());
this.object = object;
}

/**
* Change the blackboard object
*
* @param object the new blackboard
*/
public void setObject(E object) {
this.object = object;
}

/**
* This method should be called when game entity needs to make decisions: call
* this in game loop or after a fixed time slice if the game is realtime, or
* on entity's turn if the game is turn-based
*/
public void step() {
if (runningNode != null) {
runningNode.run(object);
} else {
rootNode.setControl(this);
rootNode.object = object;
rootNode.start(object);
rootNode.run(object);
}
}

@Override
public void run(E object) {
}

}
72 changes: 72 additions & 0 deletions gdx-ai/src/com/badlogic/gdx/ai/btree/BranchNode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright 2014 See AUTHORS file.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.badlogic.gdx.ai.btree;

import java.util.List;

/**
* A branch node defines a behavior tree branch, contains logic of starting or
* running sub-branches and leaves
*
* @param <E> type of the blackboard nodes use to read or modify game state
*
* @author implicit-invocation
*/
public abstract class BranchNode<E> extends Node<E> {

protected int actualTask;
protected Node<E> nodeRunning;

/**
* Create a branch node with a list of children
*
* @param nodes list of this node's children, can be empty
*/
public BranchNode(List<Node<E>> nodes) {
this.children = nodes;
}

@Override
public void childRunning(Node<E> node, Node<E> reporter) {
runningNode = node;
control.childRunning(node, this);
}

@Override
public void run(E object) {
if (runningNode != null) {
runningNode.run(object);
} else {
this.object = object;
if (actualTask < children.size()) {
runningNode = children.get(actualTask);
runningNode.setControl(this);
runningNode.object = object;
runningNode.start(object);
run(object);
} else {
end(object);
}
}
}

@Override
public void start(E object) {
this.actualTask = 0;
runningNode = null;
}

}
76 changes: 76 additions & 0 deletions gdx-ai/src/com/badlogic/gdx/ai/btree/Decorator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright 2014 See AUTHORS file.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.badlogic.gdx.ai.btree;

/**
* Decorators are wrappers that provide custom behaviors for nodes of any kind
* (branch node and task)
*
* @param <E> type of the blackboard nodes use to read or modify game state
*
* @author implicit-invocation
*/
public abstract class Decorator<E> extends Node<E> {

private Node<E> node;

/**
* Create a decorator that wraps a tree node
*
* @param node the node that will be wrapped
*/
public Decorator(Node<E> node) {
this.node = node;
}

@Override
public void addChild(Node<E> node) {
this.node = node;
}

@Override
public void run(E object) {
this.object = object;
node.run(object);
}

@Override
public void end(E object) {
node.end(object);
}

@Override
public void start(E object) {
node.setControl(this);
node.start(object);
}

@Override
public void childRunning(Node<E> runningNode, Node<E> reporter) {
control.childRunning(runningNode, this);
}

@Override
public void childFail(Node<E> runningNode) {
control.childFail(this);
}

@Override
public void childSuccess(Node<E> runningNode) {
control.childSuccess(this);
}

}
131 changes: 131 additions & 0 deletions gdx-ai/src/com/badlogic/gdx/ai/btree/Node.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
* Copyright 2014 See AUTHORS file.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.badlogic.gdx.ai.btree;

import java.util.List;

/**
* Node of a behavior tree, has one control and a list of children
*
* @param <E> type of the blackboard nodes use to read or modify game state
*
* @author implicit-invocation
*/
public abstract class Node<E> {

protected Node<E> control;
protected Node<E> runningNode;
protected List<Node<E>> children;
protected E object;

/**
* This method will add a child to the list of this node's children
*
* @param node the child node which will be added
*/
public void addChild(Node<E> node) {
children.add(node);
}

/**
* This method will set a node as this node's control (parent)
*
* @param control the parent node
*/
public void setControl(Node<E> control) {
this.control = control;
}

/**
* This method will be called once before this node's first run
*
* @param object the blackboard
*/
public void start(E object) {

}

/**
* This method will be called when this node succeeds or fails
*
* @param object blackboard
*/
public void end(E object) {

}

/**
* This method contains update logic of this node
*
* @param object blackboard
*/
public abstract void run(E object);

/**
* This method will be called in {@link #run(E)} to inform control that this
* node needs to run again
*/
public final void running() {
control.childRunning(this, this);
}

/**
* This method will be called in {@link #run(E)} to inform control that this
* node has finished running with a success result
*/
public void success() {
end(object);
control.childSuccess(this);
}

/**
* This method will be called in {@link #run(E)} to inform control that this
* node has finished running with a failure result
*/
public void fail() {
end(object);
control.childFail(this);
}

/**
* This method will be called when one of the children of this node succeeds
*
* @param node the node that succeeded
*/
public void childSuccess(Node<E> node) {
this.runningNode = null;
}

/**
* This method will be called when one of the children of this node fails
*
* @param node the node that failed
*/
public void childFail(Node<E> node) {
this.runningNode = null;
}

/**
* This method will be called when one of the ancestors of this node needs to
* run again
*
* @param runningNode the node that needs to run again
* @param reporter the node that reports, usually one of this node's children
*/
public void childRunning(Node<E> runningNode, Node<E> reporter) {
this.runningNode = runningNode;
}
}
Loading