CUGL 1.3
Cornell University Game Library
CUBehaviorNode.h
1 //
2 // CUBehaviorNode.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This module provides support for a behavior node as part of a behavior tree.
6 // The behavior tree node chooses an action by setting a priority for each node
7 // and then traverses down the tree to select an action.
8 //
9 // BehaviorNode objects are managed by BehaviorManager, and should never
10 // be allocated directly. Instead, you create a behavior node definition
11 // and pass it to a factor method in BehaviorManager.
12 //
13 // EXPERIMENTAL: This module is experimental. The API may change significantly
14 // in future CUGL releases.
15 //
16 // CUGL MIT License:
17 // This software is provided 'as-is', without any express or implied
18 // warranty. In no event will the authors be held liable for any damages
19 // arising from the use of this software.
20 //
21 // Permission is granted to anyone to use this software for any purpose,
22 // including commercial applications, and to alter it and redistribute it
23 // freely, subject to the following restrictions:
24 //
25 // 1. The origin of this software must not be misrepresented; you must not
26 // claim that you wrote the original software. If you use this software
27 // in a product, an acknowledgment in the product documentation would be
28 // appreciated but is not required.
29 //
30 // 2. Altered source versions must be plainly marked as such, and must not
31 // be misrepresented as being the original software.
32 //
33 // 3. This notice may not be removed or altered from any source distribution.
34 //
35 // Author: Apurv Sethi and Andrew Matsumoto (with Walker White)
36 // Version: 5/22/2018
37 //
38 #ifndef __CU_BEHAVIOR_NODE_H__
39 #define __CU_BEHAVIOR_NODE_H__
40 #include <cugl/ai/behavior/CUBehaviorAction.h>
41 #include <functional>
42 #include <string>
43 #include <vector>
44 
45 namespace cugl {
46  namespace ai {
47 
48 #pragma mark Behavior Node Defintion
49 
57 class BehaviorNodeDef : public std::enable_shared_from_this<BehaviorNodeDef> {
58 public:
69  enum class Type : int {
99  TIMER_NODE,
107  LEAF_NODE
108  };
109 
111  std::string name;
112 
115 
125  std::function<float()> prioritizer;
126 
138 
151 
161  bool uniform;
162 
177  float delay;
178 
186  std::vector<std::shared_ptr<BehaviorNodeDef>> children;
187 
193  std::shared_ptr<BehaviorActionDef> action;
194 
195 #pragma mark Methods
196 
204  BehaviorNodeDef();
205 
213  static std::shared_ptr<BehaviorNodeDef> alloc() {
214  return std::make_shared<BehaviorNodeDef>();
215  }
216 
231  std::shared_ptr<BehaviorNodeDef> getNodeByName(const std::string& name);
232 
247  std::shared_ptr<BehaviorNodeDef> getNodeByName(const char* name) {
248  return getNodeByName(std::string(name));
249  }
250 
251 };
252 
253 
254 #pragma mark -
255 #pragma mark Behavior Node
256 
281 #pragma mark Values
282 public:
290  enum class State : unsigned int {
292  INACTIVE = 0,
294  RUNNING = 1,
296  PAUSED = 2,
298  FINISHED = 3
299  };
300 
301 protected:
303  std::string _name;
304 
306  std::string _classname;
307 
310 
313 
315  float _priority;
316 
318  std::function<float()> _prioritizer;
319 
321  std::vector<std::shared_ptr<BehaviorNode>> _children;
322 
325 
328 
329 public:
330 #pragma mark Constructors
331 
337  BehaviorNode();
338 
343 
354  bool init(const std::string& name);
355 
363  virtual void dispose();
364 
365 #pragma mark Attributes
366 
374  const std::string& getName() const { return _name; }
375 
386  float getPriority() const { return _priority; }
387 
395  BehaviorNode::State getState() const { return _state; }
396 
404  virtual void setState(BehaviorNode::State state);
405 
415  std::function<float()> getPrioritizer() const { return _prioritizer; }
416 
426  void setPrioritizer(const std::function<float()>& func) {
427  _prioritizer = func;
428  }
429 
440  virtual std::string toString(bool verbose = false) const;
441 
445  operator std::string() const { return toString(); }
446 
447 #pragma mark Tree Access
448 
456  const BehaviorNode* getParent() const { return _parent; }
457 
466  void setParent(BehaviorNode* parent) { _parent = parent; }
467 
474 
482  int getParentalOffset() const { return _childOffset; }
483 
489  size_t getChildCount() const { return _children.size(); }
490 
500  std::vector<const BehaviorNode*> getChildren() const;
501 
513  const BehaviorNode* getChild(Uint32 pos) const;
514 
528  template <typename T>
529  const T* getChild(Uint32 pos) const {
530  return dynamic_cast<const T*>(getChild(pos));
531  }
532 
549  const BehaviorNode* getNodeByName(const std::string& name) const;
550 
567  const BehaviorNode* getNodeByName(const char* name) const {
568  return getNodeByName(std::string(name));
569  }
570 
589  template <typename T>
590  const T* getNodeByName(const std::string& name) const {
591  return dynamic_cast<const T*>(getNodeByName(name));
592  }
593 
612  template <typename T>
613  const T* getNodeByName(const char* name) const {
614  return dynamic_cast<const T*>(getNodeByName(name));
615  }
616 
617 #pragma mark Behavior Management
618 
625  virtual void reset();
626 
634  virtual void pause();
635 
642  virtual void resume();
643 
649  virtual void preempt();
650 
657  virtual void start();
658 
673  virtual void query(float dt) = 0;
674 
690  virtual BehaviorNode::State update(float dt) = 0;
691 
697  void setPriority(float priority);
698 
706  std::shared_ptr<BehaviorNode> removeChild(Uint32 pos);
707 
713  void addChild(const std::shared_ptr<BehaviorNode> child);
714 
726  static bool compareSiblings(const std::shared_ptr<BehaviorNode>& a,
727  const std::shared_ptr<BehaviorNode>& b);
728 };
729  }
730 }
731 #endif /* __CU_BEHAVIOR_NODE_H__ */
cugl::ai::BehaviorNodeDef::background
bool background
Definition: CUBehaviorNode.h:137
cugl::ai::BehaviorNode::compareSiblings
static bool compareSiblings(const std::shared_ptr< BehaviorNode > &a, const std::shared_ptr< BehaviorNode > &b)
cugl::ai::BehaviorNode::State::RUNNING
cugl::ai::BehaviorNode::State::PAUSED
cugl::ai::BehaviorNodeDef::prioritizer
std::function< float()> prioritizer
Definition: CUBehaviorNode.h:125
cugl::ai::BehaviorNode::getPriority
float getPriority() const
Definition: CUBehaviorNode.h:386
cugl::ai::BehaviorNode::toString
virtual std::string toString(bool verbose=false) const
cugl::ai::BehaviorNodeDef::Type::TIMER_NODE
cugl::ai::BehaviorNodeDef::uniform
bool uniform
Definition: CUBehaviorNode.h:161
cugl::ai::BehaviorNode::getParent
const BehaviorNode * getParent() const
Definition: CUBehaviorNode.h:456
cugl::ai::BehaviorNode::_classname
std::string _classname
Definition: CUBehaviorNode.h:306
cugl::ai::BehaviorNodeDef::type
Type type
Definition: CUBehaviorNode.h:114
cugl::ai::BehaviorNode::_prioritizer
std::function< float()> _prioritizer
Definition: CUBehaviorNode.h:318
cugl::ai::BehaviorNode::getNodeByName
const BehaviorNode * getNodeByName(const std::string &name) const
cugl::ai::BehaviorNode::getChildren
std::vector< const BehaviorNode * > getChildren() const
cugl::ai::BehaviorNodeDef::getNodeByName
std::shared_ptr< BehaviorNodeDef > getNodeByName(const std::string &name)
cugl::ai::BehaviorNodeDef::Type
Type
Definition: CUBehaviorNode.h:69
cugl::ai::BehaviorNodeDef::children
std::vector< std::shared_ptr< BehaviorNodeDef > > children
Definition: CUBehaviorNode.h:186
cugl::ai::BehaviorNode::getNodeByName
const BehaviorNode * getNodeByName(const char *name) const
Definition: CUBehaviorNode.h:567
cugl::ai::BehaviorNode::setPrioritizer
void setPrioritizer(const std::function< float()> &func)
Definition: CUBehaviorNode.h:426
cugl::ai::BehaviorNodeDef::delay
float delay
Definition: CUBehaviorNode.h:177
cugl::ai::BehaviorNodeDef::name
std::string name
Definition: CUBehaviorNode.h:111
cugl::ai::BehaviorNode::_activeChild
int _activeChild
Definition: CUBehaviorNode.h:324
cugl::ai::BehaviorNode::setPriority
void setPriority(float priority)
cugl::ai::BehaviorNode::State::INACTIVE
cugl::ai::BehaviorNode::setParent
void setParent(BehaviorNode *parent)
Definition: CUBehaviorNode.h:466
cugl::ai::BehaviorNode
Definition: CUBehaviorNode.h:280
cugl::ai::BehaviorNodeDef::BehaviorNodeDef
BehaviorNodeDef()
cugl::ai::BehaviorNode::_state
BehaviorNode::State _state
Definition: CUBehaviorNode.h:312
cugl::ai::BehaviorNode::_parent
BehaviorNode * _parent
Definition: CUBehaviorNode.h:309
cugl::ai::BehaviorNode::query
virtual void query(float dt)=0
cugl::ai::BehaviorNode::removeChild
std::shared_ptr< BehaviorNode > removeChild(Uint32 pos)
cugl::ai::BehaviorNode::dispose
virtual void dispose()
cugl::ai::BehaviorNodeDef::getNodeByName
std::shared_ptr< BehaviorNodeDef > getNodeByName(const char *name)
Definition: CUBehaviorNode.h:247
cugl::ai::BehaviorNode::getChild
const BehaviorNode * getChild(Uint32 pos) const
cugl::ai::BehaviorNode::resume
virtual void resume()
cugl::ai::BehaviorNode::getPrioritizer
std::function< float()> getPrioritizer() const
Definition: CUBehaviorNode.h:415
cugl::ai::BehaviorNodeDef::Type::RANDOM_NODE
cugl::ai::BehaviorNodeDef
Definition: CUBehaviorNode.h:57
cugl::ai::BehaviorNode::State::FINISHED
cugl::ai::BehaviorNodeDef::Type::INVERTER_NODE
cugl::ai::BehaviorNode::_priority
float _priority
Definition: CUBehaviorNode.h:315
cugl::ai::BehaviorNode::_name
std::string _name
Definition: CUBehaviorNode.h:303
cugl::ai::BehaviorNodeDef::alloc
static std::shared_ptr< BehaviorNodeDef > alloc()
Definition: CUBehaviorNode.h:213
cugl::ai::BehaviorNode::_childOffset
int _childOffset
Definition: CUBehaviorNode.h:327
cugl::ai::BehaviorNode::update
virtual BehaviorNode::State update(float dt)=0
cugl::ai::BehaviorNode::reset
virtual void reset()
cugl::ai::BehaviorNode::start
virtual void start()
cugl::ai::BehaviorNode::~BehaviorNode
~BehaviorNode()
Definition: CUBehaviorNode.h:342
cugl::ai::BehaviorNode::setState
virtual void setState(BehaviorNode::State state)
cugl::ai::BehaviorNode::init
bool init(const std::string &name)
cugl::ai::BehaviorNode::getChildCount
size_t getChildCount() const
Definition: CUBehaviorNode.h:489
cugl::ai::BehaviorNode::State
State
Definition: CUBehaviorNode.h:290
cugl::ai::BehaviorNodeDef::Type::SELECTOR_NODE
cugl::ai::BehaviorNodeDef::action
std::shared_ptr< BehaviorActionDef > action
Definition: CUBehaviorNode.h:193
cugl::ai::BehaviorNode::getParentalOffset
int getParentalOffset() const
Definition: CUBehaviorNode.h:482
cugl::ai::BehaviorNode::preempt
virtual void preempt()
cugl::ai::BehaviorNode::getState
BehaviorNode::State getState() const
Definition: CUBehaviorNode.h:395
cugl::ai::BehaviorNodeDef::preemptive
bool preemptive
Definition: CUBehaviorNode.h:150
cugl::ai::BehaviorNode::BehaviorNode
BehaviorNode()
cugl::ai::BehaviorNode::removeFromParent
void removeFromParent()
Definition: CUBehaviorNode.h:473
cugl::ai::BehaviorNodeDef::Type::PRIORITY_NODE
cugl::ai::BehaviorNode::addChild
void addChild(const std::shared_ptr< BehaviorNode > child)
cugl::ai::BehaviorNode::getName
const std::string & getName() const
Definition: CUBehaviorNode.h:374
cugl::ai::BehaviorNodeDef::Type::LEAF_NODE
cugl::ai::BehaviorNode::pause
virtual void pause()
cugl::ai::BehaviorNode::_children
std::vector< std::shared_ptr< BehaviorNode > > _children
Definition: CUBehaviorNode.h:321