SimpleAI
 All Classes Namespaces Files Functions Variables Typedefs Macros Groups Pages
ITask.h
Go to the documentation of this file.
1 
4 #pragma once
5 
6 #include "tree/TreeNode.h"
7 #include "common/Types.h"
8 
9 namespace ai {
10 
14 #define TASK_CLASS_CTOR(TaskName) \
15  TaskName(const std::string& name, const std::string& parameters, const ::ai::ConditionPtr& condition) : \
16  ::ai::ITask(name, parameters, condition)
17 
20 #define TASK_CLASS_DTOR(TaskName) virtual ~TaskName()
21 
25 #define TASK_CLASS(TaskName) \
26  TASK_CLASS_CTOR(TaskName) {}\
27  TASK_CLASS_DTOR(TaskName) {}
28 
33 class ITask: public TreeNode {
34 protected:
35  TreeNodeStatus execute(const AIPtr& entity, int64_t deltaMillis) override {
36  if (TreeNode::execute(entity, deltaMillis) == CANNOTEXECUTE) {
37  return CANNOTEXECUTE;
38  }
39 
40 #if AI_EXCEPTIONS
41  try {
42 #endif
43  return state(entity, doAction(entity, deltaMillis));
44 #if AI_EXCEPTIONS
45  } catch (...) {
46  ai_log_error("Exception while running task %s of type %s", _name.c_str(), _type.c_str());
47  }
48  return state(entity, EXCEPTION);
49 #endif
50  }
51 public:
52  ITask(const std::string& name, const std::string& parameters, const ConditionPtr& condition) :
53  TreeNode(name, parameters, condition) {
54  }
55 
56  virtual ~ITask() {
57  }
58 
63  virtual TreeNodeStatus doAction(const AIPtr& entity, int64_t deltaMillis) = 0;
64 
65  bool addChild(const TreeNodePtr& /*child*/) override {
66  return false;
67  }
68 };
69 
70 }
A node for your real actions in the behaviour tree.
Definition: ITask.h:33
TreeNodeStatus execute(const AIPtr &entity, int64_t deltaMillis) override
Definition: ITask.h:35
#define ai_log_error(...)
Logging macro to provide your own loggers.
Definition: Types.h:23
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
virtual TreeNodeStatus doAction(const AIPtr &entity, int64_t deltaMillis)=0