CUGL 1.2
Cornell University Game Library
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
cugl::ai::RandomNode Class Reference

#include <CURandomNode.h>

Inheritance diagram for cugl::ai::RandomNode:
cugl::ai::CompositeNode cugl::ai::BehaviorNode

Public Member Functions

 RandomNode ()
 
 ~RandomNode ()
 
bool init (const std::string &name, std::minstd_rand *generator)
 
void dispose () override
 
bool isUniform () const
 
void setUniform (bool uniform)
 
std::string toString (bool verbose=false) const override
 
virtual void query (float dt) override
 
- Public Member Functions inherited from cugl::ai::CompositeNode
 CompositeNode ()
 
 ~CompositeNode ()
 
bool isPreemptive () const
 
void setPreemptive (bool preemptive)
 
const BehaviorNodegetChildByPriorityIndex (Uint32 index) const
 
template<typename T >
const T * getChildByPriorityIndex (Uint32 index) const
 
const BehaviorNodegetActiveChild () const
 
template<typename T >
const T * getActiveChild () const
 
BehaviorNode::State update (float dt) override
 
- Public Member Functions inherited from cugl::ai::BehaviorNode
 BehaviorNode ()
 
 ~BehaviorNode ()
 
bool init (const std::string &name)
 
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)
 
 operator std::string () const
 
const BehaviorNodegetParent () const
 
void setParent (BehaviorNode *parent)
 
void removeFromParent ()
 
int getParentalOffset () const
 
size_t getChildCount () const
 
std::vector< const BehaviorNode * > getChildren () const
 
const BehaviorNodegetChild (Uint32 pos) const
 
template<typename T >
const T * getChild (Uint32 pos) const
 
const BehaviorNodegetNodeByName (const std::string &name) const
 
const BehaviorNodegetNodeByName (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 ()
 
void setPriority (float priority)
 
std::shared_ptr< BehaviorNoderemoveChild (Uint32 pos)
 
void addChild (const std::shared_ptr< BehaviorNode > child)
 

Protected Member Functions

virtual int selectChild () const override
 

Protected Attributes

bool _uniform
 
std::minstd_rand * _generator
 
- Protected Attributes inherited from cugl::ai::CompositeNode
bool _preemptive
 
- Protected Attributes inherited from cugl::ai::BehaviorNode
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
 

Additional Inherited Members

- Public Types inherited from cugl::ai::BehaviorNode
enum  State : unsigned int { State::INACTIVE = 0, State::RUNNING = 1, State::PAUSED = 2, State::FINISHED = 3 }
 
- Static Public Member Functions inherited from cugl::ai::BehaviorNode
static bool compareSiblings (const std::shared_ptr< BehaviorNode > &a, const std::shared_ptr< BehaviorNode > &b)
 

Detailed Description

This class provides a random composite node for a behavior tree.

A random node is a composite node that is designed to run a randomly selected nodes out of its children, based on either a uniform probability or a weighted probability. A random node using a weighted probability will base the weights of the probability of selecting each child on the priority of that child.

If a random node is not given a priority function, it will set its priority as the average of the priorities of its children.

A random node's state is directly based upon the child node currently running or the child node that has finished running. Only one child node will finish running as part of the RandomNode.

Constructor & Destructor Documentation

cugl::ai::RandomNode::RandomNode ( )

Creates an uninitialized random node.

You should never call this constructor directly. Instead, you should allocate a node with the BehaviorManager instance.

cugl::ai::RandomNode::~RandomNode ( )
inline

Deletes this node, disposing all resources.

Member Function Documentation

void cugl::ai::RandomNode::dispose ( )
overridevirtual

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 from cugl::ai::CompositeNode.

bool cugl::ai::RandomNode::init ( const std::string &  name,
std::minstd_rand *  generator 
)

Initializes a random node with the given name and generator.

The generator is provided by BehaviorManager. You should never call this method directly. Instead, you should initialize a node with the BehaviorManager instance.

Parameters
nameThe name of the random node
generatorThe random number generator
Returns
true if initialization was successful.
bool cugl::ai::RandomNode::isUniform ( ) const
inline

Returns true if this node chooses uniformly at random.

If true, then this node chooses its child uniformly at random. Otherwise, this node uses a weighted probability among its children based on each child's priority value.

Returns
true if this node chooses uniformly at random.
virtual void cugl::ai::RandomNode::query ( float  dt)
overridevirtual

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.

Parameters
dtThe elapsed time since the last frame.

Reimplemented from cugl::ai::CompositeNode.

virtual int cugl::ai::RandomNode::selectChild ( ) const
overrideprotectedvirtual

Returns a (possibly new) active child for this node.

This method is subclass dependent, and uses the rules of that subclass to select a child. If no child is selected, this method returns -1.

Returns
a (possibly new) active child for this node.

Implements cugl::ai::CompositeNode.

void cugl::ai::RandomNode::setUniform ( bool  uniform)
inline

Returns true if this node chooses uniformly at random.

If true, then this node chooses its child uniformly at random. Otherwise, this node uses a weighted probability among its children based on each child's priority value.

Returns
true if this node chooses uniformly at random.
std::string cugl::ai::RandomNode::toString ( bool  verbose = false) const
overridevirtual

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.

Parameters
verboseWhether to include class information.
Returns
a string representation of this node for debugging purposes.

Reimplemented from cugl::ai::BehaviorNode.

Member Data Documentation

std::minstd_rand* cugl::ai::RandomNode::_generator
protected

A reference to the behavior tree manager's random generator

bool cugl::ai::RandomNode::_uniform
protected

Whether this node should choose a child uniformly at random.


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