SimpleAI
 All Classes Namespaces Files Functions Variables Typedefs Macros Groups Pages
TreeNodeImpl.h
Go to the documentation of this file.
1 
4 #pragma once
5 
6 #include "TreeNode.h"
7 #include "AI.h"
8 
9 namespace ai {
10 
11 inline int TreeNode::getId() const {
12  return _id;
13 }
14 
15 inline void TreeNode::setName(const std::string& name) {
16  if (name.empty()) {
17  return;
18  }
19  _name = name;
20 }
21 
22 inline const std::string& TreeNode::getType() const {
23  return _type;
24 }
25 
26 inline const std::string& TreeNode::getName() const {
27  return _name;
28 }
29 
30 inline const ConditionPtr& TreeNode::getCondition() const {
31  return _condition;
32 }
33 
34 inline void TreeNode::setCondition(const ConditionPtr& condition) {
35  _condition = condition;
36 }
37 
38 inline const std::string& TreeNode::getParameters() const {
39  return _parameters;
40 }
41 
42 inline const TreeNodes& TreeNode::getChildren() const {
43  return _children;
44 }
45 
46 inline TreeNodes& TreeNode::getChildren() {
47  return _children;
48 }
49 
50 inline bool TreeNode::addChild(const TreeNodePtr& child) {
51  _children.push_back(child);
52  return true;
53 }
54 
55 inline void TreeNode::resetState(const AIPtr& entity) {
56  for (auto& c : _children) {
57  c->resetState(entity);
58  }
59 }
60 
61 inline void TreeNode::getRunningChildren(const AIPtr& /*entity*/, std::vector<bool>& active) const {
62  const int size = (int)_children.size();
63  for (int i = 0; i < size; ++i) {
64  active.push_back(false);
65  }
66 }
67 
68 inline void TreeNode::setLastExecMillis(const AIPtr& entity) {
69  if (!entity->_debuggingActive) {
70  return;
71  }
72  entity->_lastExecMillis[getId()] = entity->_time;
73 }
74 
75 inline int TreeNode::getSelectorState(const AIPtr& entity) const {
76  AI::SelectorStates::const_iterator i = entity->_selectorStates.find(getId());
77  if (i == entity->_selectorStates.end()) {
78  return AI_NOTHING_SELECTED;
79  }
80  return i->second;
81 }
82 
83 inline void TreeNode::setSelectorState(const AIPtr& entity, int selected) {
84  entity->_selectorStates[getId()] = selected;
85 }
86 
87 inline int TreeNode::getLimitState(const AIPtr& entity) const {
88  AI::LimitStates::const_iterator i = entity->_limitStates.find(getId());
89  if (i == entity->_limitStates.end()) {
90  return 0;
91  }
92  return i->second;
93 }
94 
95 inline void TreeNode::setLimitState(const AIPtr& entity, int amount) {
96  entity->_limitStates[getId()] = amount;
97 }
98 
99 inline TreeNodeStatus TreeNode::state(const AIPtr& entity, TreeNodeStatus treeNodeState) {
100  if (!entity->_debuggingActive) {
101  return treeNodeState;
102  }
103  entity->_lastStatus[getId()] = treeNodeState;
104  return treeNodeState;
105 }
106 
107 inline int64_t TreeNode::getLastExecMillis(const AIPtr& entity) const {
108  if (!entity->_debuggingActive) {
109  return -1L;
110  }
111  AI::LastExecMap::const_iterator i = entity->_lastExecMillis.find(getId());
112  if (i == entity->_lastExecMillis.end()) {
113  return -1L;
114  }
115  return i->second;
116 }
117 
118 inline TreeNodeStatus TreeNode::getLastStatus(const AIPtr& entity) const {
119  if (!entity->_debuggingActive) {
120  return UNKNOWN;
121  }
122  AI::NodeStates::const_iterator i = entity->_lastStatus.find(getId());
123  if (i == entity->_lastStatus.end()) {
124  return UNKNOWN;
125  }
126  return i->second;
127 }
128 
129 inline TreeNodePtr TreeNode::getChild(int id) const {
130  for (auto& child : _children) {
131  if (child->getId() == id) {
132  return child;
133  }
134  const TreeNodePtr& node = child->getChild(id);
135  if (node) {
136  return node;
137  }
138  }
139  return TreeNodePtr();
140 }
141 
142 inline bool TreeNode::replaceChild(int id, const TreeNodePtr& newNode) {
143  auto i = std::find_if(_children.begin(), _children.end(), [id] (const TreeNodePtr& other) { return other->getId() == id; });
144  if (i == _children.end()) {
145  return false;
146  }
147 
148  if (newNode) {
149  *i = newNode;
150  return true;
151  }
152 
153  _children.erase(i);
154  return true;
155 }
156 
157 inline TreeNodePtr TreeNode::getParent_r(const TreeNodePtr& parent, int id) const {
158  for (auto& child : _children) {
159  if (child->getId() == id) {
160  return parent;
161  }
162  const TreeNodePtr& parentPtr = child->getParent_r(child, id);
163  if (parentPtr) {
164  return parentPtr;
165  }
166  }
167  return TreeNodePtr();
168 }
169 
170 inline TreeNodePtr TreeNode::getParent(const TreeNodePtr& self, int id) const {
171  ai_assert(getId() != id, "Root nodes don't have a parent");
172  for (auto& child : _children) {
173  if (child->getId() == id) {
174  return self;
175  }
176  const TreeNodePtr& parent = child->getParent_r(child, id);
177  if (parent) {
178  return parent;
179  }
180  }
181  return TreeNodePtr();
182 }
183 
184 inline TreeNodeStatus TreeNode::execute(const AIPtr& entity, int64_t /*deltaMillis*/) {
185  if (!_condition->evaluate(entity)) {
186  return state(entity, CANNOTEXECUTE);
187  }
188 
189  setLastExecMillis(entity);
190  return state(entity, FINISHED);
191 }
192 
193 }
#define ai_assert(condition,...)
Provide your own assert - this is only executed in DEBUG mode.
Definition: Types.h:75
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
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
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