30 inline const ConditionPtr& TreeNode::getCondition()
const {
34 inline void TreeNode::setCondition(
const ConditionPtr& condition) {
35 _condition = condition;
42 inline const TreeNodes& TreeNode::getChildren()
const {
46 inline TreeNodes& TreeNode::getChildren() {
50 inline bool TreeNode::addChild(
const TreeNodePtr& child) {
51 _children.push_back(child);
56 for (
auto& c : _children) {
57 c->resetState(entity);
62 const int size = (int)_children.size();
63 for (
int i = 0; i < size; ++i) {
64 active.push_back(
false);
68 inline void TreeNode::setLastExecMillis(
const AIPtr& entity) {
69 if (!entity->_debuggingActive) {
72 entity->_lastExecMillis[
getId()] = entity->_time;
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;
83 inline void TreeNode::setSelectorState(
const AIPtr& entity,
int selected) {
84 entity->_selectorStates[
getId()] = selected;
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()) {
95 inline void TreeNode::setLimitState(
const AIPtr& entity,
int amount) {
96 entity->_limitStates[
getId()] = amount;
99 inline TreeNodeStatus TreeNode::state(
const AIPtr& entity, TreeNodeStatus treeNodeState) {
100 if (!entity->_debuggingActive) {
101 return treeNodeState;
103 entity->_lastStatus[
getId()] = treeNodeState;
104 return treeNodeState;
108 if (!entity->_debuggingActive) {
111 AI::LastExecMap::const_iterator i = entity->_lastExecMillis.find(
getId());
112 if (i == entity->_lastExecMillis.end()) {
118 inline TreeNodeStatus TreeNode::getLastStatus(
const AIPtr& entity)
const {
119 if (!entity->_debuggingActive) {
122 AI::NodeStates::const_iterator i = entity->_lastStatus.find(
getId());
123 if (i == entity->_lastStatus.end()) {
129 inline TreeNodePtr TreeNode::getChild(
int id)
const {
130 for (
auto& child : _children) {
131 if (child->getId() == id) {
134 const TreeNodePtr& node = child->getChild(
id);
139 return TreeNodePtr();
143 auto i = std::find_if(_children.begin(), _children.end(), [id] (
const TreeNodePtr& other) {
return other->getId() == id; });
144 if (i == _children.end()) {
157 inline TreeNodePtr TreeNode::getParent_r(
const TreeNodePtr& parent,
int id)
const {
158 for (
auto& child : _children) {
159 if (child->getId() == id) {
162 const TreeNodePtr& parentPtr = child->getParent_r(child,
id);
167 return TreeNodePtr();
172 for (
auto& child : _children) {
173 if (child->getId() == id) {
176 const TreeNodePtr& parent = child->getParent_r(child,
id);
181 return TreeNodePtr();
185 if (!_condition->evaluate(entity)) {
186 return state(entity, CANNOTEXECUTE);
189 setLastExecMillis(entity);
190 return state(entity, FINISHED);
#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