SimpleAI
 All Classes Namespaces Files Functions Variables Typedefs Macros Groups Pages
Limit.h
Go to the documentation of this file.
1 
4 #pragma once
5 
6 #include "tree/TreeNode.h"
7 
8 namespace ai {
9 
17 class Limit: public TreeNode {
18 private:
19  int _amount;
20 public:
22 
23  Limit(const std::string& name, const std::string& parameters, const ConditionPtr& condition) :
24  TreeNode(name, parameters, condition) {
25  _type = "Limit";
26  if (!parameters.empty()) {
27  _amount = ::atoi(parameters.c_str());
28  } else {
29  _amount = 1;
30  }
31  }
32 
33  TreeNodeStatus execute(const AIPtr& entity, int64_t deltaMillis) override {
34  ai_assert(_children.size() == 1, "Limit must have exactly one node");
35 
36  if (TreeNode::execute(entity, deltaMillis) == CANNOTEXECUTE) {
37  return CANNOTEXECUTE;
38  }
39 
40  const int alreadyExecuted = getLimitState(entity);
41  if (alreadyExecuted >= _amount) {
42  return state(entity, FINISHED);
43  }
44 
45  const TreeNodePtr& treeNode = *_children.begin();
46  const TreeNodeStatus status = treeNode->execute(entity, deltaMillis);
47  setLimitState(entity, alreadyExecuted + 1);
48  if (status == RUNNING) {
49  return state(entity, RUNNING);
50  }
51  return state(entity, FAILED);
52  }
53 };
54 
55 }
#define ai_assert(condition,...)
Provide your own assert - this is only executed in DEBUG mode.
Definition: Types.h:75
TreeNode(const std::string &name, const std::string &parameters, const ConditionPtr &condition)
Definition: TreeNode.h:121
A decorator node which limits the execution of the attached child to a specified amount of runs...
Definition: Limit.h:17
TreeNodeStatus execute(const AIPtr &entity, int64_t deltaMillis) override
Definition: Limit.h:33
virtual TreeNodeStatus execute(const AIPtr &entity, int64_t deltaMillis)
Definition: TreeNodeImpl.h:184
The base class for all behaviour tree actions.
Definition: TreeNode.h:88
#define NODE_FACTORY(NodeName)
A node factory macro to ease and unify the registration at AIRegistry.
Definition: TreeNode.h:54