![]() |
CUGL 1.3
Cornell University Game Library
|
#include <CUBehaviorNode.h>
Public Types | |
enum | State : unsigned int { State::INACTIVE = 0, State::RUNNING = 1, State::PAUSED = 2, State::FINISHED = 3 } |
Public Member Functions | |
BehaviorNode () | |
~BehaviorNode () | |
bool | init (const std::string &name) |
virtual void | dispose () |
const std::string & | getName () const |
float | getPriority () const |
BehaviorNode::State | getState () const |
virtual void | setState (BehaviorNode::State state) |
std::function< float()> | getPrioritizer () const |
void | setPrioritizer (const std::function< float()> &func) |
virtual std::string | toString (bool verbose=false) const |
operator std::string () const | |
const BehaviorNode * | getParent () const |
void | setParent (BehaviorNode *parent) |
void | removeFromParent () |
int | getParentalOffset () const |
size_t | getChildCount () const |
std::vector< const BehaviorNode * > | getChildren () const |
const BehaviorNode * | getChild (Uint32 pos) const |
template<typename T > | |
const T * | getChild (Uint32 pos) const |
const BehaviorNode * | getNodeByName (const std::string &name) const |
const BehaviorNode * | getNodeByName (const char *name) const |
template<typename T > | |
const T * | getNodeByName (const std::string &name) const |
template<typename T > | |
const T * | getNodeByName (const char *name) const |
virtual void | reset () |
virtual void | pause () |
virtual void | resume () |
virtual void | preempt () |
virtual void | start () |
virtual void | query (float dt)=0 |
virtual BehaviorNode::State | update (float dt)=0 |
void | setPriority (float priority) |
std::shared_ptr< BehaviorNode > | removeChild (Uint32 pos) |
void | addChild (const std::shared_ptr< BehaviorNode > child) |
Static Public Member Functions | |
static bool | compareSiblings (const std::shared_ptr< BehaviorNode > &a, const std::shared_ptr< BehaviorNode > &b) |
Protected Attributes | |
std::string | _name |
std::string | _classname |
BehaviorNode * | _parent |
BehaviorNode::State | _state |
float | _priority |
std::function< float()> | _prioritizer |
std::vector< std::shared_ptr< BehaviorNode > > | _children |
int | _activeChild |
int | _childOffset |
An abstract class for a behavior tree node.
This class is a base class for the individual nodes of the behavior tree. Behavior tree nodes are either composite, decorator, or leaf nodes. A leaf node has no children, a decorator has only one, and a composite has one or more. Only leaf nodes have actions attached.
A behavior tree is a construction of behavior nodes. The top node without a parent is the the root of the tree. The tree chooses the action to run based on the priority value of each of the root's descendents. The tree must use an update function to run on each tick, updating the state of each node. The root node of a behavior tree returns the state of the selected leaf node to run.
This class has abstract methods for calculating the priority and updating, which are implemented by the subclasses.
Behavior trees should be managed by a BehaviorManager, which creates each BehaviorNode from a BehaviorNodeDef and runs and updates the behavior trees. While in the BehaviorManager, a behavior tree cannot be modified by any outside methods and any references to the nodes of the behavior tree will be constant.
|
strong |
An enumeration indicating the current state of the tree node.
Behaviors are long running, across multiple animation frames. Therefore, we need to track them in the same way that we would track an audio asset.
cugl::ai::BehaviorNode::BehaviorNode | ( | ) |
Creates an uninitialized behavior tree node.
You should never call this constructor directly. Instead, you should allocate a node with the BehaviorManager instance.
|
inline |
Deletes this node, disposing all resources.
void cugl::ai::BehaviorNode::addChild | ( | const std::shared_ptr< BehaviorNode > | child | ) |
Adds the child at the end of the child list of this node.
child | The child to add. |
|
static |
Returns true if sibling a has a larger priority than sibling b.
This method is used by std::sort to sort the children. Ties are broken from the offset of the children.
a | The first child |
b | The second child |
|
virtual |
Disposes all of the resources used by this node, including any descendants.
A disposed node can be safely reinitialized. Any children owned by this node will be released. They will be deleted if no other object owns them. This method should only be called by BehaviorManager.
Reimplemented in cugl::ai::RandomNode, cugl::ai::CompositeNode, cugl::ai::TimerNode, and cugl::ai::LeafNode.
const BehaviorNode* cugl::ai::BehaviorNode::getChild | ( | Uint32 | pos | ) | const |
Returns a (weak) pointer to the child node at the given position.
The purpose of this pointer is to allow access to the subtree of a behavior tree. It does not grant ownership, as ownership is confined to BehaviorManager.
pos | The child position |
|
inline |
Returns a (weak) pointer to the child node at the given position.
The purpose of this pointer is to allow access to the subtree of a behavior tree. It does not grant ownership, as ownership is confined to BehaviorManager.
This version of the method performs a dynamic typecast to the correct type.
pos | The child position |
|
inline |
Returns the number of children of this composite node.
std::vector<const BehaviorNode*> cugl::ai::BehaviorNode::getChildren | ( | ) | const |
Returns the list of (weak) references to the node's children.
The purpose of this collection is to allow access to the subtree of a behavior tree. It does not grant ownership, as ownership is confined to BehaviorManager.
|
inline |
Returns a string that is used to identify the node.
This name is used to identify nodes in a behavior tree. It is used by the BehaviorManager to access this node.
|
inline |
Returns the (first) node with the given name.
This method performs a recursive search down the behavior tree. If there is more than one node with the given name, it returns the first one that is found in an unspecified search order. As a result, names should be unique for best results.
The purpose of this pointer is to allow access to the subtree of a behavior tree. It does not grant ownership, as ownership is confined to BehaviorManager.
name | An identifier to find the node. |
|
inline |
Returns the (first) node with the given name.
This method performs a recursive search down the behavior tree. If there is more than one node with the given name, it returns the first one that is found in an unspecified search order. As a result, names should be unique for best results.
The purpose of this pointer is to allow access to the subtree of a behavior tree. It does not grant ownership, as ownership is confined to BehaviorManager.
This version of the method performs a dynamic typecast to the correct type.
name | An identifier to find the node. |
const BehaviorNode* cugl::ai::BehaviorNode::getNodeByName | ( | const std::string & | name | ) | const |
Returns the (first) node with the given name.
This method performs a recursive search down the behavior tree. If there is more than one node with the given name, it returns the first one that is found in an unspecified search order. As a result, names should be unique for best results.
The purpose of this pointer is to allow access to the subtree of a behavior tree. It does not grant ownership, as ownership is confined to BehaviorManager.
name | An identifier to find the node. |
|
inline |
Returns the (first) node with the given name.
This method performs a recursive search down the behavior tree. If there is more than one node with the given name, it returns the first one that is found in an unspecified search order. As a result, names should be unique for best results.
The purpose of this pointer is to allow access to the subtree of a behavior tree. It does not grant ownership, as ownership is confined to BehaviorManager.
This version of the method performs a dynamic typecast to the correct type.
name | An identifier to find the node. |
|
inline |
Returns a (weak) pointer to the parent node.
The purpose of this pointer is to climb back up the behavior tree. No child asserts ownership of its parent.
|
inline |
Returns the offset of this behavior tree node within its parent node.
If this node is a root node, it will return -1.
|
inline |
Returns the priority function for this node.
This function should return a value between 0 and 1 representing the priority. When this function is defined, it overrides the rules that this node uses for defining its priority in query.
|
inline |
Returns the current priority of this node.
This priority value is used to determine the relevance of a node in comparison to other nodes. This value is between 0 and 1. Higher priority nodes are more likely to be selected. It will be updated each time query is called.
|
inline |
Returns the state of this node.
If this node has no parent, then this is the state of the behavior tree.
bool cugl::ai::BehaviorNode::init | ( | const std::string & | name | ) |
Initializes a behavior tree node with the given name.
You should never call this method directly. Instead, you should initialize a node with the BehaviorManager instance.
name | The name of the behavior node |
|
inline |
Cast from a BehaviorNode to a string.
|
virtual |
Pauses this running node and all running nodes below it in the tree.
A paused node can be resumed later. This method has no effect on values stored within nodes, and values (such as priority or timer delay) will not be updated while nodes are paused.
Reimplemented in cugl::ai::LeafNode.
|
virtual |
Stops this node from running.
This method also stops any running nodes under this one if they exist.
Reimplemented in cugl::ai::TimerNode, and cugl::ai::LeafNode.
|
pure virtual |
Updates the priority value(s) for this node.
This method recursively determines the priority of this node and all of its children. The priority may be determined by a user-provided priority function or by the default priority function of the class.
When this method is complete, it will chose a child node to run, but will not run it. Unlike update, this method is guaranteed to run every time step in BehaviorManager, provided that the root node is running.
dt | The elapsed time since the last frame. |
Implemented in cugl::ai::TimerNode, cugl::ai::CompositeNode, cugl::ai::RandomNode, cugl::ai::LeafNode, cugl::ai::DecoratorNode, and cugl::ai::InverterNode.
std::shared_ptr<BehaviorNode> cugl::ai::BehaviorNode::removeChild | ( | Uint32 | pos | ) |
Removes the child at the given position from this node.
pos | The position of the child node that will be removed. |
|
inline |
Removes this node from its parent.
If this node has no parent, nothing happens.
|
virtual |
Resets this node and all nodes below it to an uninitialized state.
This method also resets any class values to those set at the start of the tree. This method allows the node to be started again, as if it had not been run before.
Reimplemented in cugl::ai::TimerNode, and cugl::ai::LeafNode.
|
virtual |
Resumes a paused node and all paused nodes below it in the tree.
Values such as priority or timer delay will not have been updated while the node was paused.
Reimplemented in cugl::ai::LeafNode.
|
inline |
Sets the parent of this node.
The purpose of this pointer is to climb back up the behavior tree. No child asserts ownership of its parent.
parent | The parent of this node. |
|
inline |
Sets the priority function for this node.
This function should return a value between 0 and 1 representing the priority. When this function is defined, it overrides the rules that this node uses for defining its priority in query.
func | The priority function for this node. |
void cugl::ai::BehaviorNode::setPriority | ( | float | priority | ) |
Sets the priority of this node.
priority | The priority of this node. |
|
virtual |
Sets the state of this node.
If this node has no parent, then this is the state of the behavior tree.
state | The state of this node. |
Reimplemented in cugl::ai::TimerNode.
|
virtual |
Initializes this node for execution.
When called this node moves from an uninitialized state to one where the update() function is safe to be called.
|
virtual |
Returns a string representation of this node for debugging purposes.
If verbose is true, the string will include class information. This allows us to unambiguously identify the class.
verbose | Whether to include class information. |
Reimplemented in cugl::ai::RandomNode, cugl::ai::TimerNode, and cugl::ai::LeafNode.
|
pure virtual |
Updates this node and any active children.
This method runs the update function, which executes to active child (if not a leaf) or the associated action (if a leaf). This method is not guaranteed to execute every time step; only if the node is the root of the tree or is selected as part of the active path.
If a node is not a leaf node and it has no active children, then the method will return INACTIVE.
dt | The elapsed time since the last frame. |
Implemented in cugl::ai::TimerNode, cugl::ai::CompositeNode, cugl::ai::LeafNode, and cugl::ai::DecoratorNode.
|
protected |
The index of the child running (-1 if no child is currently running).
|
protected |
The (current) child offset of this node (-1 if root)
|
protected |
The array of children for this composite node.
|
protected |
The name of this class (for debugging polymorphism).
|
protected |
The descriptive, identifying name of the node.
|
protected |
A weaker pointer to the parent (or null if root).
|
protected |
The current priority function for this behavior node.
|
protected |
The current priority, or relevance of this node.
|
protected |
The current state of this node.