SimpleAI
 All Classes Namespaces Files Functions Variables Typedefs Macros Groups Pages
TreeNode.h
Go to the documentation of this file.
1 
4 #pragma once
5 
6 #include "common/Types.h"
9 #include "conditions/True.h"
10 #include <vector>
11 #include <string>
12 #include <memory>
13 #include <algorithm>
14 
15 namespace ai {
16 
17 class TreeNode;
18 typedef std::shared_ptr<TreeNode> TreeNodePtr;
19 typedef std::vector<TreeNodePtr> TreeNodes;
20 
24 enum TreeNodeStatus {
25  UNKNOWN,
30  CANNOTEXECUTE,
34  RUNNING,
38  FINISHED,
42  FAILED,
46  EXCEPTION,
47 
48  MAX_TREENODESTATUS
49 };
50 
54 #define NODE_FACTORY(NodeName) \
55  class Factory: public ::ai::ITreeNodeFactory { \
56  public: \
57  ::ai::TreeNodePtr create (const ::ai::TreeNodeFactoryContext *ctx) const override { \
58  return std::make_shared<NodeName>(ctx->name, ctx->parameters, ctx->condition); \
59  } \
60  }; \
61  static const Factory& getFactory() { \
62  static Factory FACTORY; \
63  return FACTORY; \
64  }
65 
69 #define NODE_CLASS(NodeName) \
70  NodeName(const std::string& name, const std::string& parameters, const ::ai::ConditionPtr& condition) : \
71  ::ai::TreeNode(name, parameters, condition) { \
72  _type = AI_STRINGIFY(NodeName); \
73  } \
74  virtual ~NodeName() { \
75  } \
76  \
77  NODE_FACTORY(NodeName)
78 
88 class TreeNode : public MemObject {
89 protected:
90  static int getNextId() {
91  static int _nextId;
92  const int nextId = _nextId++;
93  return nextId;
94  }
98  int _id;
99  TreeNodes _children;
100  std::string _name;
101  std::string _type;
102  std::string _parameters;
103  ConditionPtr _condition;
104 
105  TreeNodeStatus state(const AIPtr& entity, TreeNodeStatus treeNodeState);
106  int getSelectorState(const AIPtr& entity) const;
107  void setSelectorState(const AIPtr& entity, int selected);
108  int getLimitState(const AIPtr& entity) const;
109  void setLimitState(const AIPtr& entity, int amount);
110  void setLastExecMillis(const AIPtr& entity);
111 
112  TreeNodePtr getParent_r(const TreeNodePtr& parent, int id) const;
113 
114 public:
121  TreeNode(const std::string& name, const std::string& parameters, const ConditionPtr& condition) :
122  _id(getNextId()), _name(name), _parameters(parameters), _condition(condition) {
123  }
124 
125  virtual ~TreeNode() {}
130  int getId() const;
131 
135  const std::string& getName() const;
136 
140  const std::string& getParameters() const;
141 
147  void setName(const std::string& name);
151  const std::string& getType() const;
152  const ConditionPtr& getCondition() const;
153  void setCondition(const ConditionPtr& condition);
154  const TreeNodes& getChildren() const;
155  TreeNodes& getChildren();
156 
161  virtual void getRunningChildren(const AIPtr& entity, std::vector<bool>& active) const;
165  int64_t getLastExecMillis(const AIPtr& ai) const;
166  TreeNodeStatus getLastStatus(const AIPtr& ai) const;
167 
173  virtual TreeNodeStatus execute(const AIPtr& entity, int64_t deltaMillis);
174 
178  virtual void resetState(const AIPtr& entity);
179 
180  virtual bool addChild(const TreeNodePtr& child);
181  TreeNodePtr getChild(int id) const;
189  bool replaceChild(int id, const TreeNodePtr& newNode);
198  TreeNodePtr getParent(const TreeNodePtr& self, int id) const;
199 };
200 
201 }
TreeNode(const std::string &name, const std::string &parameters, const ConditionPtr &condition)
Definition: TreeNode.h:121
void setName(const std::string &name)
Updates the custom name of this TreeNode.
Definition: TreeNodeImpl.h:15
int64_t getLastExecMillis(const AIPtr &ai) const
Returns the time in milliseconds when this node was last run. This is only updated if execute() was c...
Definition: TreeNodeImpl.h:107
virtual TreeNodeStatus execute(const AIPtr &entity, int64_t deltaMillis)
Definition: TreeNodeImpl.h:184
Condition related stuff.
Definition: MemoryAllocator.h:26
int getId() const
Return the unique id for this node.
Definition: TreeNodeImpl.h:11
const std::string & getType() const
The node type - this usually matches the class name of the TreeNode.
Definition: TreeNodeImpl.h:22
The base class for all behaviour tree actions.
Definition: TreeNode.h:88
virtual void getRunningChildren(const AIPtr &entity, std::vector< bool > &active) const
Get the state of all child nodes for the given entity.
Definition: TreeNodeImpl.h:61
const std::string & getName() const
Each node can have a user defines name that can be retrieved with this method.
Definition: TreeNodeImpl.h:26
bool replaceChild(int id, const TreeNodePtr &newNode)
Replace the given child node with a new one (or removes it)
Definition: TreeNodeImpl.h:142
TreeNodePtr getParent(const TreeNodePtr &self, int id) const
Get the parent node for a given TreeNode id - This should only be called on the root node of the beha...
Definition: TreeNodeImpl.h:170
virtual void resetState(const AIPtr &entity)
Reset the states in the node and also in the entity.
Definition: TreeNodeImpl.h:55
const std::string & getParameters() const
Return the raw parameters for this node.
Definition: TreeNodeImpl.h:38
int _id
Every node has an id to identify it. It's unique per type.
Definition: TreeNode.h:98