-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBehaviourTree.h
More file actions
executable file
·250 lines (216 loc) · 6.86 KB
/
BehaviourTree.h
File metadata and controls
executable file
·250 lines (216 loc) · 6.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#pragma once
/**
* Implement BehaviourTree by C++11
*/
#ifndef _BEHAVIOUR_TREE_H
#define _BEHAVIOUR_TREE_H
#include <vector>
#include <string>
#include <iterator>
#include <functional>
#include <memory>
#include <random>
#include <algorithm>
#include <assert.h>
#define BT_BUILD_WITH_JSON // Need [jsoncpp](https://github.com/open-source-parsers/jsoncpp/releases) for support. (test on 1.8.0)
#define BT_LOG(msg) printf("%s:%d %s\n", __FILE__, __LINE__, msg)
#define BT_ASSERT(expr, msg) assert( (expr) && (msg) )
enum class BT_status {
SUCCESS,
FAILURE,
INVALID,
RUNNING
};
class BT_task {
public:
BT_task(std::function<BT_status(void)> task= [] { return BT_status::SUCCESS; }, const std::string& taskname = "Unknow", const BT_status& status = BT_status::SUCCESS)
: task_func(task), mStatus(status), mName(taskname) {}
const BT_status checkStatus() {
return mStatus;
}
BT_status addChild(BT_task* child) {
try {
mChildren.emplace_back(child);
return BT_status::SUCCESS;
} catch (std::exception) {
return BT_status::FAILURE;
}
}
virtual BT_status run() {
mStatus = BT_status::RUNNING;
mStatus = task_func();
return mStatus;
};
protected:
std::string mName;
BT_status mStatus;
std::vector<std::shared_ptr<BT_task> > mChildren;
std::function<BT_status(void)> task_func;
};
/**
* A Selector will return immediately with a success status code when one of its children runs
* successfully. As long as its children are failing, it will keep on trying. If it runs out of children
* completely, it will return a failure status code.
*/
class BT_selector : public BT_task {
public:
BT_selector(std::function<BT_status(void)> task = [] { return BT_status::SUCCESS; }, const std::string& name = "Unknow")
: BT_task(task, name) {}
virtual BT_status run() {
for (auto &curChild : mChildren) {
BT_status state = curChild->run();
if (state != BT_status::FAILURE) {
return state;
}
}
return BT_status::FAILURE;
}
};
class BT_random_selector : public BT_task {
public:
BT_random_selector(std::function<BT_status(void)> task = [] { return BT_status::SUCCESS; }, const std::string& name = "Unknow")
: BT_task(task, name) {}
virtual BT_status run() {
std::vector<std::shared_ptr<BT_task> > copy_mChildren = mChildren;
std::random_device rd;
std::mt19937 generator(rd());
std::shuffle(copy_mChildren.begin(), copy_mChildren.end(), generator);
for (auto &curChild : copy_mChildren) {
BT_status state = curChild->run();
if (state != BT_status::FAILURE) {
return state;
}
}
return BT_status::FAILURE;
}
};
/**
* A Sequence will return immediately with a failure status code when one of its children fails.
* As long as its children are succeeding, it will keep going. If it runs out of children, it will return in
* success.
*/
class BT_sequence : public BT_task {
public:
BT_sequence(std::function<BT_status(void)> task = [] { return BT_status::SUCCESS; }, const std::string& name = "Unknow")
: BT_task(task, name) {}
virtual BT_status run() {
for (auto &curChild : mChildren) {
BT_status state = curChild->run();
if (state != BT_status::SUCCESS) {
return state;
}
}
return BT_status::SUCCESS;
}
};
class BT_random_sequence : public BT_task {
public:
BT_random_sequence(std::function<BT_status(void)> task = [] { return BT_status::SUCCESS; }, const std::string& name = "Unknow")
: BT_task(task, name) {}
virtual BT_status run() {
std::vector<std::shared_ptr<BT_task> > copy_mChildren = mChildren;
std::random_device rd;
std::mt19937 generator(rd());
std::shuffle(copy_mChildren.begin(), copy_mChildren.end(), generator);
for (auto &curChild : copy_mChildren) {
BT_status state = curChild->run();
if (state != BT_status::SUCCESS) {
return state;
}
}
return BT_status::SUCCESS;
}
};
// Decorators
class BT_DecoratorLimit : public BT_task {
public:
BT_DecoratorLimit(const int limit_times, BT_task* task)
: BT_task(), decorator(task), limit_times(limit_times) {
run_sofar = 0;
}
virtual BT_status run() {
if (run_sofar >= limit_times) {
return BT_status::FAILURE;
} else {
run_sofar++;
return decorator->run();
}
}
protected:
std::shared_ptr<BT_task> decorator;
private:
int run_sofar;
int limit_times;
};
class BT_DecoratorUtilfail : public BT_task {
public:
BT_DecoratorUtilfail(BT_task* task)
: BT_task(), decorator(task) {}
BT_status run() {
while(BT_status::SUCCESS == decorator->run()) {}
return BT_status::SUCCESS;
}
protected:
std::shared_ptr<BT_task> decorator;
};
#ifdef BT_BUILD_WITH_JSON
#include <fstream>
#include "json/json.h"
extern std::string BT_getConfigJson(const std::string& path);
extern BT_task* BT_buildTreeWithJsonValue(const Json::Value& json);
extern std::map<std::string, std::function<BT_status(void)> > g_BTree_registered_tasks;
/**
* Following codes for register task to global table @g_BTree_registered_tasks
*/
// User class instead of inline function
class _BT_REGISTER {
public:
_BT_REGISTER(const std::string& func_str, std::function<BT_status(void)> func) {
BT_ASSERT(g_BTree_registered_tasks.find(func_str) == g_BTree_registered_tasks.end(),
"Register task_func failed because of it exist!");
g_BTree_registered_tasks.emplace(std::make_pair(func_str, func));
}
};
class _BT_UNREGISTER {
public:
_BT_UNREGISTER(const std::string& func_str) {
std::map<std::string, std::function<BT_status(void)> >::iterator it = g_BTree_registered_tasks.find(func_str);
BT_ASSERT(it != g_BTree_registered_tasks.end(),
"Unregister task_func failed because of it not exist!");
g_BTree_registered_tasks.erase(it);
}
};
#define BT_REGISTER_TASK_FUNC(func) \
static _BT_REGISTER g_register_bttask_##func(#func, func)
#define BT_UNREGISTER_TASK_FUNC(func) \
static _BT_UNREGISTER g_unregister_bttask_##func(#func)
// \Deprecated: Replaced by upper #define marocs
inline bool _BT_REGISTER_TASK_FUNC(const std::string& func_str, std::function<BT_status(void)>& func) {
try {
if (g_BTree_registered_tasks.find(func_str) != g_BTree_registered_tasks.end()) {
BT_LOG("Register task_func failed because of it exist!");
return false;
}
g_BTree_registered_tasks.emplace(std::make_pair(func_str, func));
return true;
}
catch (std::exception) {
return false;
}
}
inline bool _BT_REGISTER_TASK_FUNC(const std::string& func_str) {
try {
std::map<std::string, std::function<BT_status(void)> >::iterator it = g_BTree_registered_tasks.find(func_str);
if ( it == g_BTree_registered_tasks.end() ) {
BT_LOG("Unregister task_func failed because of it not exist!");
return false;
}
g_BTree_registered_tasks.erase(it);
return true;
}
catch (std::exception) {
return false;
}
}
#endif
#endif