![]() |
CUGL 1.2
Cornell University Game Library
|
#include <CUBehaviorManager.h>
Public Member Functions | |
BehaviorManager () | |
~BehaviorManager () | |
void | dispose () |
bool | init () |
bool | init (Uint32 seed) |
bool | containsTree (const std::string &name) const |
bool | containsTree (const char *name) const |
const BehaviorNode * | getTree (const std::string &name) const |
const BehaviorNode * | getTree (const char *name) const |
bool | addTree (const std::shared_ptr< BehaviorNodeDef > &treedef) |
bool | addTree (const std::string &name, const std::shared_ptr< BehaviorNodeDef > &treedef) |
bool | addTree (const char *name, const std::shared_ptr< BehaviorNodeDef > &treedef) |
BehaviorNode::State | getTreeState (const std::string &name) const |
void | startTree (const std::string &name) |
void | pauseTree (const std::string &name) |
void | resumeTree (const std::string &name) |
void | removeTree (const std::string &name) |
void | resetTree (const std::string &name) |
void | update (float dt) |
Static Public Member Functions | |
static std::shared_ptr< BehaviorManager > | alloc () |
static std::shared_ptr< BehaviorManager > | alloc (Uint32 seed) |
Protected Attributes | |
std::unordered_map< std::string, std::shared_ptr< BehaviorNode > > | _trees |
std::minstd_rand | _random |
A class providing a centralized manager for behavior trees.
An instance of this class owns, runs, and updates all active behavior trees. You should always use a BehaviorManager to create behavior trees, and you should never use a behavior tree not owned by a BehaviorManager.
A behavior manager also has a single, centralized random number generator used for all tree processing. This generator can be given a seed to ensure deterministic behaviors (for testing or networking).
To create a behavior tree, the manager uses a BehaviorNodeDef for the root node, and constructs the behavior tree defined by that definition.
Each update frame, the behavior manager updates all running behavior trees until they are finished. The behavior manager can pause, reset or restart any behavior tree it owns.
|
inline |
Creates an uninitialized behavior manager.
NEVER USE A CONSTRUCTOR WITH NEW. If you want to allocate an object on the heap, use the static constructor instead.
|
inline |
Deletes this manager, disposing all resources.
This will delete all trees owned by the manager.
bool cugl::ai::BehaviorManager::addTree | ( | const std::shared_ptr< BehaviorNodeDef > & | treedef | ) |
Adds the behavior tree described by the provided definition.
All trees must be stored with a unique names in the BehaviorManager. No two trees may have the same name. In this method, the BehaviorManager uses the name of the root node of the behavior tree for the name of the whole tree.
This method ecursively creates a behavior tree from the template provided by the BehaviorNodeDef, and adds it to the BehaviorManager. This method returns false if the BehaviorNodeDef provided does not allow the creation of a valid BehaviorNode, or if the name provided is already in the manager. Otherwise it returns true.
name | The name to assign this tree. |
treedef | The definition for the root of the behavior tree. |
bool cugl::ai::BehaviorManager::addTree | ( | const std::string & | name, |
const std::shared_ptr< BehaviorNodeDef > & | treedef | ||
) |
Adds the behavior tree described by the provided definition.
All trees must be stored with a unique names in the BehaviorManager. No two trees may have the same name. However, the name used to access a tree in the manager does not need to be the same name as in the tree node. This allows the same tree (for navigation purposes) to be used multiple times in the manager.
This method ecursively creates a behavior tree from the template provided by the BehaviorNodeDef, and adds it to the BehaviorManager. This method returns false if the BehaviorNodeDef provided does not allow the creation of a valid BehaviorNode, or if the name provided is already in the manager. Otherwise it returns true.
name | The name to assign this tree. |
treedef | The definition for the root of the behavior tree. |
|
inline |
Adds the behavior tree described by the provided definition.
All trees must be stored with a unique names in the BehaviorManager. No two trees may have the same name. However, the name used to access a tree in the manager does not need to be the same name as in the tree node. This allows the same tree (for navigation purposes) to be used multiple times in the manager.
This method ecursively creates a behavior tree from the template provided by the BehaviorNodeDef, and adds it to the BehaviorManager. This method returns false if the BehaviorNodeDef provided does not allow the creation of a valid BehaviorNode, or if the name provided is already in the manager. Otherwise it returns true.
name | The name to assign this tree. |
treedef | The definition for the root of the behavior tree. |
|
inlinestatic |
Returns a newly allocated behavior tree manager (with no trees).
This allocator creates a random generator whose seed is the current clock value.
|
inlinestatic |
Returns a newly allocated behavior tree manager (with no trees).
This initializer creates a random generator from the given seed.
seed | The random generator seed |
bool cugl::ai::BehaviorManager::containsTree | ( | const std::string & | name | ) | const |
Returns whether this manager contains a tree with the given name.
All trees must be stored with a unique names in the BehaviorManager. No two trees may have the same name.
name | An identifier to find the tree. |
|
inline |
Returns whether this manager contains a tree with the given name.
All trees must be stored with a unique names in the BehaviorManager. No two trees may have the same name.
name | An identifier to find the tree. |
void cugl::ai::BehaviorManager::dispose | ( | ) |
Disposes all of the resources used by this manager.
This will delete all trees owned by the manager. Unfinished actions will not complete their execution.
const BehaviorNode* cugl::ai::BehaviorManager::getTree | ( | const std::string & | name | ) | const |
Returns a (weak) reference to the behavior tree with the given name.
All trees must be stored with a unique names in the BehaviorManager. No two trees may have the same name.
By returning a weak reference, this manager does not pass ownership of the tree.
name | An identifier to find the tree. |
|
inline |
Returns a (weak) reference to the behavior tree with the given name.
All trees must be stored with a unique names in the BehaviorManager. No two trees may have the same name.
By returning a weak reference, this manager does not pass ownership of the tree.
name | An identifier to find the tree. |
|
inline |
Returns the state of the tree with the given name.
All trees must be stored with a unique names in the BehaviorManager. No two trees may have the same name. However, the name used to access a tree in the manager does not need to be the same name as in the tree node. This allows the same tree (for navigation purposes) to be used multiple times in the manager.
name | An identifier to find the tree. |
bool cugl::ai::BehaviorManager::init | ( | ) |
Initializes a behavior tree manager (with no trees).
This initializer creates a random generator whose seed is the current clock value.
bool cugl::ai::BehaviorManager::init | ( | Uint32 | seed | ) |
Initializes a behavior tree manager (with no trees).
This initializer creates a random generator from the given seed.
seed | The random generator seed |
void cugl::ai::BehaviorManager::pauseTree | ( | const std::string & | name | ) |
Pauses the running tree with the given name.
A paused tree will be ignored by the update method.
All trees must be stored with a unique names in the BehaviorManager. No two trees may have the same name. However, the name used to access a tree in the manager does not need to be the same name as in the tree node. This allows the same tree (for navigation purposes) to be used multiple times in the manager.
name | An identifier to find the tree. |
void cugl::ai::BehaviorManager::removeTree | ( | const std::string & | name | ) |
Removes the tree with the given name.
This method only succeeds the tree is not currently running. Otherwise it will cause an error.
All trees must be stored with a unique names in the BehaviorManager. No two trees may have the same name. However, the name used to access a tree in the manager does not need to be the same name as in the tree node. This allows the same tree (for navigation purposes) to be used multiple times in the manager.
name | An identifier to find the tree. |
void cugl::ai::BehaviorManager::resetTree | ( | const std::string & | name | ) |
Resets the tree with the given name.
This method is used to reset a tree back to its initial state once it has been finished. However, it does not restart the tree. The startTree method must be called separately.
All trees must be stored with a unique names in the BehaviorManager. No two trees may have the same name. However, the name used to access a tree in the manager does not need to be the same name as in the tree node. This allows the same tree (for navigation purposes) to be used multiple times in the manager.
name | An identifier to find the tree. |
void cugl::ai::BehaviorManager::resumeTree | ( | const std::string & | name | ) |
Resumes running the paused tree with the given name.
All trees must be stored with a unique names in the BehaviorManager. No two trees may have the same name. However, the name used to access a tree in the manager does not need to be the same name as in the tree node. This allows the same tree (for navigation purposes) to be used multiple times in the manager.
name | An identifier to find the tree. |
void cugl::ai::BehaviorManager::startTree | ( | const std::string & | name | ) |
Starts running the tree with the given name.
Adding a tree with addTree is not enough for the manager to execute it. This method must be called as well.
All trees must be stored with a unique names in the BehaviorManager. No two trees may have the same name. However, the name used to access a tree in the manager does not need to be the same name as in the tree node. This allows the same tree (for navigation purposes) to be used multiple times in the manager.
name | An identifier to find the tree. |
void cugl::ai::BehaviorManager::update | ( | float | dt | ) |
Updates all associated behavior trees.
This function should be called in the main game loop to process the behaviors for each animation frame.
dt | The elapsed time since the last frame. |
|
protected |
The centralized random number generator
|
protected |
A map of the trees currently being run by the manager.