SimpleAI
 All Classes Namespaces Files Functions Variables Typedefs Macros Groups Pages
ITimedNode.h
Go to the documentation of this file.
1 
4 #pragma once
5 
6 #include "tree/TreeNode.h"
7 #include <stdlib.h>
8 
9 namespace ai {
10 
11 #define NOTSTARTED -1
12 #define TIMERNODE_CLASS(NodeName) \
13  NodeName(const std::string& name, const std::string& parameters, const ConditionPtr& condition) : \
14  ITimedNode(name, parameters, condition) { \
15  _type = AI_STRINGIFY(NodeName); \
16  } \
17  virtual ~NodeName() { \
18  } \
19  \
20  NODE_FACTORY(NodeName)
21 
25 class ITimedNode : public TreeNode {
26 protected:
27  int64_t _timerMillis;
28  int64_t _millis;
29 public:
30  ITimedNode(const std::string& name, const std::string& parameters, const ConditionPtr& condition) :
31  TreeNode(name, parameters, condition), _timerMillis(NOTSTARTED) {
32  if (!parameters.empty()) {
33  _millis = ::atol(parameters.c_str());
34  } else {
35  _millis = 1000L;
36  }
37  }
38  virtual ~ITimedNode() {}
39 
40  TreeNodeStatus execute(const AIPtr& entity, int64_t deltaMillis) override {
41  const TreeNodeStatus result = TreeNode::execute(entity, deltaMillis);
42  if (result == CANNOTEXECUTE)
43  return CANNOTEXECUTE;
44 
45  if (_timerMillis == NOTSTARTED) {
46  _timerMillis = _millis;
47  const TreeNodeStatus status = executeStart(entity, deltaMillis);
48  if (status == FINISHED)
49  _timerMillis = NOTSTARTED;
50  return state(entity, status);
51  }
52 
53  if (_timerMillis - deltaMillis > 0) {
54  _timerMillis -= deltaMillis;
55  const TreeNodeStatus status = executeRunning(entity, deltaMillis);
56  if (status == FINISHED)
57  _timerMillis = NOTSTARTED;
58  return state(entity, status);
59  }
60 
61  _timerMillis = NOTSTARTED;
62  return state(entity, executeExpired(entity, deltaMillis));
63  }
64 
69  virtual TreeNodeStatus executeStart(const AIPtr& /*entity*/, int64_t /*deltaMillis*/) {
70  return RUNNING;
71  }
72 
81  virtual TreeNodeStatus executeRunning(const AIPtr& /*entity*/, int64_t /*deltaMillis*/) {
82  return RUNNING;
83  }
84 
89  virtual TreeNodeStatus executeExpired(const AIPtr& /*entity*/, int64_t /*deltaMillis*/) {
90  return FINISHED;
91  }
92 };
93 
94 #undef NOTSTARTED
95 
96 }
TreeNode(const std::string &name, const std::string &parameters, const ConditionPtr &condition)
Definition: TreeNode.h:121
virtual TreeNodeStatus executeExpired(const AIPtr &, int64_t)
Called in the frame where the timer expired.
Definition: ITimedNode.h:89
virtual TreeNodeStatus execute(const AIPtr &entity, int64_t deltaMillis)
Definition: TreeNodeImpl.h:184
A timed node is a TreeNode that is executed until a given time (millis) is elapsed.
Definition: ITimedNode.h:25
The base class for all behaviour tree actions.
Definition: TreeNode.h:88
virtual TreeNodeStatus executeRunning(const AIPtr &, int64_t)
Called whenever the timer is running. Not called in the frame where the timer is started or in the fr...
Definition: ITimedNode.h:81
TreeNodeStatus execute(const AIPtr &entity, int64_t deltaMillis) override
Definition: ITimedNode.h:40
virtual TreeNodeStatus executeStart(const AIPtr &, int64_t)
Called whenever the timer is started or restarted.
Definition: ITimedNode.h:69