-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomposite.cpp
More file actions
422 lines (318 loc) · 9.29 KB
/
Copy pathcomposite.cpp
File metadata and controls
422 lines (318 loc) · 9.29 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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#include <iostream>
using std::cout;
using std::endl;
#include <vector>
#include <memory>
// https://refactoring.guru/design-patterns/composite
//
// ----------------------------------
// Base class for Behavior Tree nodes
// ----------------------------------
class IBehaviorTreeNode
{
public:
// Node status
enum class e_status
{
SUCCESS,
FAILURE,
// Running is returned if the action is asynchronous
// and it needs more time to complete its operations
// RUNNING,
UNKNOWN = -1
};
// Node type
enum class e_nodeType
{
// Control nodes
SEQUENCE,
FALLBACK,
// PARALLEL,
// DECORATOR,
// Execution nodes
ACTION,
CONDITION
};
// Raw pointer to node
using t_nodeRawPtr = IBehaviorTreeNode*;
public:
// ctor
IBehaviorTreeNode(e_nodeType type);
// dtor
virtual ~IBehaviorTreeNode() = default;
// Virtual functions to override:
virtual auto update(float dt) -> e_status = 0;
// Getters:
auto getNodeType() const noexcept -> e_nodeType;
auto getParent() const noexcept -> t_nodeRawPtr;
auto hasChildren() const noexcept -> bool;
auto getChildren() const -> const std::vector<t_nodeRawPtr>&;
// Add children
void addChildren(t_nodeRawPtr node);
protected:
// Node type
e_nodeType m_type;
// Parent node
t_nodeRawPtr m_parent;
// Children nodes
std::vector<t_nodeRawPtr> m_children;
};
// cpp
// ctor
IBehaviorTreeNode::IBehaviorTreeNode(e_nodeType type)
: m_type(type),
m_parent(nullptr)
{
}
// Getters:
auto IBehaviorTreeNode::getNodeType() const noexcept -> e_nodeType
{
return m_type;
}
auto IBehaviorTreeNode::getParent() const noexcept -> t_nodeRawPtr
{
return m_parent;
}
auto IBehaviorTreeNode::hasChildren() const noexcept -> bool
{
return !m_children.empty();
}
auto IBehaviorTreeNode::getChildren() const -> const std::vector<t_nodeRawPtr>&
{
return m_children;
}
// Add children
void IBehaviorTreeNode::addChildren(t_nodeRawPtr node)
{
m_children.push_back(node);
}
// Utility
auto nodeStatusToString(IBehaviorTreeNode::e_status status) -> std::string_view
{
switch(status)
{
case IBehaviorTreeNode::e_status::FAILURE:
return "FAILURE";
break;
case IBehaviorTreeNode::e_status::SUCCESS:
return "SUCCESS";
break;
case IBehaviorTreeNode::e_status::UNKNOWN:
return "UNKNOWN!";
break;
}
return {};
}
// ----------------------------
// Example nodes implementation
// ----------------------------
// Control nodes:
class Fallback final : public IBehaviorTreeNode
{
public:
// ctor
Fallback()
: IBehaviorTreeNode(IBehaviorTreeNode::e_nodeType::FALLBACK)
{}
// Virtual functions to override:
auto update(float dt) -> e_status override
{
cout << "Running FALLBACK" << endl;
// Iterate over all of the children nodes
for (const auto child : this->m_children)
{
// As soon as one child is successful, the entire fallback/strategy is successful
if (child->update(dt) == IBehaviorTreeNode::e_status::SUCCESS)
return IBehaviorTreeNode::e_status::SUCCESS;
}
// Return failure!
return IBehaviorTreeNode::e_status::FAILURE;
}
};
//
class Sequence final : public IBehaviorTreeNode
{
public:
// ctor
Sequence()
: IBehaviorTreeNode(IBehaviorTreeNode::e_nodeType::SEQUENCE)
{}
// Virtual functions to override:
auto update(float dt) -> e_status override
{
cout << "Running SEQUENCE" << endl;
// Iterate over all of the children nodes
for (const auto child : this->m_children)
{
// One child node fails, the entire sequence fails too
if (child->update(dt) == IBehaviorTreeNode::e_status::FAILURE)
return IBehaviorTreeNode::e_status::FAILURE;
}
// Return success!
return IBehaviorTreeNode::e_status::SUCCESS;
}
};
// Execution nodes:
//
class Esto final : public IBehaviorTreeNode
{
public:
// ctor
Esto()
: IBehaviorTreeNode(IBehaviorTreeNode::e_nodeType::ACTION)
{}
// Virtual functions to override:
auto update(float dt) -> e_status override
{
cout << "Esto -> " << nodeStatusToString(IBehaviorTreeNode::e_status::FAILURE) << endl;
return IBehaviorTreeNode::e_status::FAILURE;
}
};
//
class Aquello final : public IBehaviorTreeNode
{
public:
// ctor
Aquello()
: IBehaviorTreeNode(IBehaviorTreeNode::e_nodeType::ACTION)
{}
// Virtual functions to override:
auto update(float dt) -> e_status override
{
cout << "Aquello -> " << nodeStatusToString(IBehaviorTreeNode::e_status::FAILURE) << endl;
return IBehaviorTreeNode::e_status::FAILURE;
}
};
//
class Uno final : public IBehaviorTreeNode
{
public:
// ctor
Uno()
: IBehaviorTreeNode(IBehaviorTreeNode::e_nodeType::ACTION)
{}
// Virtual functions to override:
auto update(float dt) -> e_status override
{
cout << "Uno -> " << nodeStatusToString(IBehaviorTreeNode::e_status::SUCCESS) << endl;
return IBehaviorTreeNode::e_status::SUCCESS;
}
};
//
class Dos final : public IBehaviorTreeNode
{
public:
// ctor
Dos()
: IBehaviorTreeNode(IBehaviorTreeNode::e_nodeType::ACTION)
{}
// Virtual functions to override:
auto update(float dt) -> e_status override
{
cout << "Dos -> " << nodeStatusToString(IBehaviorTreeNode::e_status::SUCCESS) << endl;
return IBehaviorTreeNode::e_status::SUCCESS;
}
};
//
class Tres final : public IBehaviorTreeNode
{
public:
// ctor
Tres()
: IBehaviorTreeNode(IBehaviorTreeNode::e_nodeType::ACTION)
{}
// Virtual functions to override:
auto update(float dt) -> e_status override
{
cout << "Tres -> " << nodeStatusToString(IBehaviorTreeNode::e_status::SUCCESS) << endl;
return IBehaviorTreeNode::e_status::SUCCESS;
}
};
// -----------------------------
// Main class for behavior trees
// -----------------------------
class BehaviorTree
{
public:
// Creates a new node
template <typename NodeClass, typename... Args>
auto create(Args... args) -> IBehaviorTreeNode::t_nodeRawPtr;
// Iterates over the entire tree
void run(float dt) const;
// Set the root node
void setRoot(IBehaviorTreeNode::t_nodeRawPtr node);
// Get the root node of the tree
auto getRoot() const noexcept -> IBehaviorTreeNode::t_nodeRawPtr;
private:
// Root node
IBehaviorTreeNode::t_nodeRawPtr m_root;
// Node storage
std::vector<std::unique_ptr<IBehaviorTreeNode>> m_nodes;
};
// --- Template functions implementation ---
// Creates a new node
template <typename NodeClass, typename... Args>
auto BehaviorTree::create(Args... args) -> IBehaviorTreeNode::t_nodeRawPtr
{
// Must be a valid derived class
static_assert(std::is_base_of_v<IBehaviorTreeNode, NodeClass>,
"[C++] BehaviorTree::create(): <NodeClass> class must be derived from <IBehaviorTreeNode> class!");
// Make a new object in place and return a reference to it (std::unique_ptr<NodeClass>)
auto& reference = m_nodes.emplace_back(std::make_unique<NodeClass>(std::forward<Args>(args)...));
// Return a reference to
// the underlying raw pointer
return reference.get();
}
// cpp
// Set the root node
void BehaviorTree::setRoot(IBehaviorTreeNode::t_nodeRawPtr node)
{
m_root = node;
}
//
void BehaviorTree::run(float dt) const
{
m_root->update(dt);
}
// Get the root node of the tree
auto BehaviorTree::getRoot() const noexcept -> IBehaviorTreeNode::t_nodeRawPtr
{
return m_root;
}
/*
Main entry point
Output:
Running FALLBACK
Esto -> FAILURE
Aquello -> FAILURE
Running SEQUENCE
Uno -> SUCCESS
Dos -> SUCCESS
Tres -> SUCCESS
*/
int main()
{
// Create a behavior tree
BehaviorTree bt;
// Create some nodes
auto* sequence1 = bt.create<Sequence>();
auto* fallback1 = bt.create<Fallback>();
auto* esto = bt.create<Esto>();
auto* aquello = bt.create<Aquello>();
auto* uno = bt.create<Uno>();
auto* dos = bt.create<Dos>();
auto* tres = bt.create<Tres>();
// Add nodes to fallback 1
fallback1->addChildren(esto);
fallback1->addChildren(aquello);
fallback1->addChildren(sequence1);
// Add nodes to sequence 1
sequence1->addChildren(uno);
sequence1->addChildren(dos);
sequence1->addChildren(tres);
// The root node will be the first fallback in this case
bt.setRoot(fallback1);
// Run from the root node
bt.run(1.0f / 60.0f);
return 0;
}