CUGL 1.2
Cornell University Game Library
Public Member Functions | Static Public Member Functions | Protected Attributes | List of all members
cugl::ai::BehaviorManager Class Reference

#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 BehaviorNodegetTree (const std::string &name) const
 
const BehaviorNodegetTree (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< BehaviorManageralloc ()
 
static std::shared_ptr< BehaviorManageralloc (Uint32 seed)
 

Protected Attributes

std::unordered_map< std::string, std::shared_ptr< BehaviorNode > > _trees
 
std::minstd_rand _random
 

Detailed Description

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.

Constructor & Destructor Documentation

cugl::ai::BehaviorManager::BehaviorManager ( )
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.

cugl::ai::BehaviorManager::~BehaviorManager ( )
inline

Deletes this manager, disposing all resources.

This will delete all trees owned by the manager.

Member Function Documentation

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.

Parameters
nameThe name to assign this tree.
treedefThe definition for the root of the behavior tree.
Returns
whether the behaivor tree was successfully created and added.
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.

Parameters
nameThe name to assign this tree.
treedefThe definition for the root of the behavior tree.
Returns
whether the behaivor tree was successfully created and added.
bool cugl::ai::BehaviorManager::addTree ( const char *  name,
const std::shared_ptr< BehaviorNodeDef > &  treedef 
)
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.

Parameters
nameThe name to assign this tree.
treedefThe definition for the root of the behavior tree.
Returns
whether the behaivor tree was successfully created and added.
static std::shared_ptr<BehaviorManager> cugl::ai::BehaviorManager::alloc ( )
inlinestatic

Returns a newly allocated behavior tree manager (with no trees).

This allocator creates a random generator whose seed is the current clock value.

Returns
a newly allocated behavior tree manager (with no trees).
static std::shared_ptr<BehaviorManager> cugl::ai::BehaviorManager::alloc ( Uint32  seed)
inlinestatic

Returns a newly allocated behavior tree manager (with no trees).

This initializer creates a random generator from the given seed.

Parameters
seedThe random generator seed
Returns
a newly allocated behavior tree manager (with no trees).
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.

Parameters
nameAn identifier to find the tree.
Returns
whether this manager contains a tree with the given name.
bool cugl::ai::BehaviorManager::containsTree ( const char *  name) const
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.

Parameters
nameAn identifier to find the tree.
Returns
whether this manager contains a tree with the given name.
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.

Parameters
nameAn identifier to find the tree.
Returns
a (weak) reference to the tree with the given name.
const BehaviorNode* cugl::ai::BehaviorManager::getTree ( const char *  name) const
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.

Parameters
nameAn identifier to find the tree.
Returns
a (weak) reference to the tree with the given name.
BehaviorNode::State cugl::ai::BehaviorManager::getTreeState ( const std::string &  name) const
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.

Parameters
nameAn identifier to find the tree.
Returns
the state of the tree with the given name.
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.

Returns
true if initialization was successful.
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.

Parameters
seedThe random generator seed
Returns
true if initialization was successful.
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.

Parameters
nameAn 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.

Parameters
nameAn 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.

Parameters
nameAn 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.

Parameters
nameAn 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.

Parameters
nameAn 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.

Parameters
dtThe elapsed time since the last frame.

Member Data Documentation

std::minstd_rand cugl::ai::BehaviorManager::_random
protected

The centralized random number generator

std::unordered_map<std::string, std::shared_ptr<BehaviorNode> > cugl::ai::BehaviorManager::_trees
protected

A map of the trees currently being run by the manager.


The documentation for this class was generated from the following file: