CUGL 1.3
Cornell University Game Library
|
#include <CUBehaviorParser.h>
Public Member Functions | |
BehaviorParser () | |
~BehaviorParser () | |
void | dispose () |
bool | init () |
void | addPrioritizer (const std::string &name, std::function< float()> prioritizer) |
void | addPrioritizer (const char *name, std::function< float()> prioritizer) |
std::function< float()> | getPrioritizer (const std::string &name) const |
std::function< float()> | getPrioritizer (const char *name) const |
std::function< float()> | removePrioritizer (const std::string &name) |
std::function< float()> | removePrioritizer (const char *name) |
void | addAction (std::shared_ptr< BehaviorActionDef > actiondef) |
void | addAction (const std::string &name, std::shared_ptr< BehaviorActionDef > actiondef) |
void | addAction (const char *name, std::shared_ptr< BehaviorActionDef > actiondef) |
std::shared_ptr< BehaviorActionDef > | getAction (const std::string &name) const |
std::shared_ptr< BehaviorActionDef > | getAction (const char *name) const |
std::shared_ptr< BehaviorActionDef > | removeAction (const std::string &name) |
std::shared_ptr< BehaviorActionDef > | removeAction (const char *name) |
std::unordered_map< std::string, std::shared_ptr< BehaviorNodeDef > > | parseFile (const std::string &file) |
std::unordered_map< std::string, std::shared_ptr< BehaviorNodeDef > > | parseFile (const char *file) |
Static Public Member Functions | |
static std::shared_ptr< BehaviorParser > | alloc () |
A class designed to parse a JSON file describing a behavior tree.
An instance of this class is used to parse a JSON file into a collection of behavior trees. When finished, this class produces a mapping from the names of each behavior tree to the BehaviorNodeDef of that root of that tree.
The JSON file cannot contain the definitions of actions or priority functions, as these are function definitions. However, they can contain the names of these functions. When this parser is created, these functions can be added manually to the parser (before parsing) to support this feature.
When parsing a JSON file, each named, top-level object will be considered a BehaviorNodeDef. The node name is the name of the object, and is not an attribute of the JSON object. The supported attributes are:
type: A string representing the type of the node definition prioritizer: A string naming a priority function background: A boolean indicating whether this node processed in the background preemptive: A boolean indicating whether this node can be prempted uniform: A boolean indicating whether any random selection is uniform delay: A number, giving the time delay in seconds children: A list of named objects defining behavior nodes action: A string naming a possible action
With the exception of type, all attributes are optional and have default values.
|
inline |
Creates an uninitialized parser.
NEVER USE A CONSTRUCTOR WITH NEW. If you want to allocate an object on the heap, use one of the static constructors instead.
|
inline |
Deletes this parser, disposing all resources.
|
inline |
Adds an action definition for the given name.
This method will fail if the name is already in use.
name | The name to associate with the action definition |
actiondef | The action definition |
void cugl::ai::BehaviorParser::addAction | ( | const std::string & | name, |
std::shared_ptr< BehaviorActionDef > | actiondef | ||
) |
Adds an action definition for the given name.
This method will fail if the name is already in use.
name | The name to associate with the action definition |
actiondef | The action definition |
|
inline |
Adds an action definition to the parser.
This method assigned the action name to the one in the definition. This method will fail if the name is already in use.
actiondef | The action definition |
|
inline |
Adds a prioritizer for the given name.
This function should return a value between 0 and 1 representing the priority of a node or action. This method will fail if the name is already in use.
name | The name to associate with the priority function |
prioritizer | The priority function |
void cugl::ai::BehaviorParser::addPrioritizer | ( | const std::string & | name, |
std::function< float()> | prioritizer | ||
) |
Adds a prioritizer for the given name.
This function should return a value between 0 and 1 representing the priority of a node or action. This method will fail if the name is already in use.
name | The name to associate with the priority function |
prioritizer | The priority function |
|
inlinestatic |
Returns a newly allocated behavior parser.
This parser starts with no predefined prioritizers or actions. They should be added (if needed) before parsing.
void cugl::ai::BehaviorParser::dispose | ( | ) |
Disposes all of the resources used by this parser.
A disposed action can be safely reinitialized.
It is unsafe to call this on an action that is still currently inside of a running behavior tree.
|
inline |
Returns the action definition for the given name.
This function returns nullptr if there is no definition for that name.
name | The name associated with the action definition |
std::shared_ptr<BehaviorActionDef> cugl::ai::BehaviorParser::getAction | ( | const std::string & | name | ) | const |
Returns the action definition for the given name.
This function returns nullptr if there is no definition for that name.
name | The name associated with the action definition |
|
inline |
Returns the prioritizer for the given name.
This function returns nullptr if there is no prioritizer for that name.
name | The name associated with the priority function |
std::function<float()> cugl::ai::BehaviorParser::getPrioritizer | ( | const std::string & | name | ) | const |
Returns the prioritizer for the given name.
This function returns nullptr if there is no prioritizer for that name.
name | The name associated with the priority function |
|
inline |
Initializes a behavior parser.
This parser starts with no predefined prioritizers or actions. They should be added (if needed) before parsing.
std::unordered_map<std::string, std::shared_ptr<BehaviorNodeDef> > cugl::ai::BehaviorParser::parseFile | ( | const char * | file | ) |
Parses the JSON file provided to create behavior node definitions.
This method adds the BehaviorNodeDef objects to a map, which the user can extract using the name of the root as the key. Hence all root nodes should have unique names.
This function assumes that the file name is a relative path. It will search the application asset directory {
file | The relative path to the file. |
|
inline |
Parses the JSON file provided to create behavior node definitions.
This method adds the BehaviorNodeDef objects to a map, which the user can extract using the name of the root as the key. Hence all root nodes should have unique names.
This function assumes that the file name is a relative path. It will search the application asset directory {
file | The relative path to the file. |
|
inline |
Removes the action definition for the given name.
This function returns the definition removed. It returns nullptr if there is no definition for that name.
name | The name associated with the action definition |
std::shared_ptr<BehaviorActionDef> cugl::ai::BehaviorParser::removeAction | ( | const std::string & | name | ) |
Removes the action definition for the given name.
This function returns the definition removed. It returns nullptr if there is no definition for that name.
name | The name associated with the action definition |
|
inline |
Removes the prioritizer for the given name.
This function returns the prioritizer removed. It returns nullptr if there is no prioritizer for that name.
name | The name associated with the priority function |
std::function<float()> cugl::ai::BehaviorParser::removePrioritizer | ( | const std::string & | name | ) |
Removes the prioritizer for the given name.
This function returns the prioritizer removed. It returns nullptr if there is no prioritizer for that name.
name | The name associated with the priority function |