FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
btsk/BehaviorTreeEvent.cpp at master · aigamedev/btsk · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
aigamedev
/
btsk
Public
Notifications
You must be signed in to change notification settings
Fork
127
Star
486
Code
Issues
2
Pull requests
1
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
btsk
/
BehaviorTreeEvent.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
356 lines (291 loc) · 7.28 KB
Breadcrumbs
btsk
/
BehaviorTreeEvent.cpp
Copy path
File metadata and controls
356 lines (291 loc) · 7.28 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/*
*****************************************************************************
* This file is part of the Behavior Tree Starter Kit.
*
* Copyright (c) 2012, AiGameDev.com
*
* Credits: Alex J. Champandard
****************************************************************************
*/
#
include
<
functional
>
#
include
<
vector
>
#
include
<
deque
>
#
include
"
Shared.h
"
#
include
"
Test.h
"
namespace
bt4
{
// ============================================================================
enum
Status
{
BH_INVALID
,
BH_SUCCESS
,
BH_FAILURE
,
BH_RUNNING
,
BH_SUSPENDED
,
};
typedef
std::function<
void
(Status)> BehaviorObserver;
class
Behavior
{
public:
Behavior
()
: m_eStatus(
BH_INVALID
)
{
}
virtual
~Behavior
()
{
}
Status
tick
()
{
if
(m_eStatus ==
BH_INVALID
)
{
onInitialize
();
}
m_eStatus =
update
();
if
(m_eStatus !=
BH_RUNNING
)
{
onTerminate
(m_eStatus);
}
return
m_eStatus;
}
virtual
Status
update
() = 0;
virtual
void
onInitialize
() {}
virtual
void
onTerminate
(Status) {}
Status m_eStatus;
BehaviorObserver m_Observer;
};
class
BehaviorTree
{
public:
void
start
(Behavior& bh, BehaviorObserver* observer =
NULL
)
{
if
(observer !=
NULL
)
{
bh.
m_Observer
= *observer;
}
m_Behaviors.
push_front
(&bh);
}
void
stop
(Behavior& bh, Status result)
{
ASSERT
(result !=
BH_RUNNING
);
bh.
m_eStatus
= result;
if
(bh.
m_Observer
)
{
bh.
m_Observer
(result);
}
}
void
tick
()
{
//
Insert an end-of-update marker into the list of tasks.
m_Behaviors.
push_back
(
NULL
);
//
Keep going updating tasks until we encounter the marker.
while
(
step
())
{
continue
;
}
}
bool
step
()
{
Behavior* current = m_Behaviors.
front
();
m_Behaviors.
pop_front
();
//
If this is the end-of-update marker, stop processing.
if
(current ==
NULL
)
{
return
false
;
}
//
Perform the update on this individual task.
current->
tick
();
//
Process the observer if the task terminated.
if
(current->
m_eStatus
!=
BH_RUNNING
&& current->
m_Observer
)
{
current->
m_Observer
(current->
m_eStatus
);
}
else
//
Otherwise drop it into the queue for the next tick().
{
m_Behaviors.
push_back
(current);
}
return
true
;
}
protected:
std::deque<Behavior*> m_Behaviors;
};
//
----------------------------------------------------------------------------
struct
MockBehavior
:
public
Behavior
{
int
m_iInitializeCalled;
int
m_iTerminateCalled;
int
m_iUpdateCalled;
Status m_eReturnStatus;
Status m_eTerminateStatus;
MockBehavior
()
: m_iInitializeCalled(
0
)
, m_iTerminateCalled(
0
)
, m_iUpdateCalled(
0
)
, m_eReturnStatus(
BH_RUNNING
)
, m_eTerminateStatus(
BH_INVALID
)
{
}
virtual
~MockBehavior
()
{
}
virtual
void
onInitialize
()
{
++m_iInitializeCalled;
}
virtual
void
onTerminate
(Status s)
{
++m_iTerminateCalled;
m_eTerminateStatus = s;
}
virtual
Status
update
()
{
++m_iUpdateCalled;
return
m_eReturnStatus;
}
};
TEST
(StarterKit4, TaskInitialize)
{
MockBehavior t;
BehaviorTree bt;
bt.
start
(t);
CHECK_EQUAL
(
0
, t.
m_iInitializeCalled
);
bt.
tick
();
CHECK_EQUAL
(
1
, t.
m_iInitializeCalled
);
};
TEST
(StarterKit4, TaskUpdate)
{
MockBehavior t;
BehaviorTree bt;
bt.
start
(t);
bt.
tick
();
CHECK_EQUAL
(
1
, t.
m_iUpdateCalled
);
t.
m_eReturnStatus
=
BH_SUCCESS
;
bt.
tick
();
CHECK_EQUAL
(
2
, t.
m_iUpdateCalled
);
};
TEST
(StarterKit4, TaskTerminate)
{
MockBehavior t;
BehaviorTree bt;
bt.
start
(t);
bt.
tick
();
CHECK_EQUAL
(
0
, t.
m_iTerminateCalled
);
t.
m_eReturnStatus
=
BH_SUCCESS
;
bt.
tick
();
CHECK_EQUAL
(
1
, t.
m_iTerminateCalled
);
};
// ============================================================================
class
Composite
:
public
Behavior
{
public:
typedef
std::vector<Behavior*> Behaviors;
Behaviors m_Children;
};
class
Sequence
:
public
Composite
{
public:
Sequence
(BehaviorTree& bt)
: m_pBehaviorTree(&bt)
{
}
protected:
BehaviorTree* m_pBehaviorTree;
virtual
void
onInitialize
()
{
m_Current = m_Children.
begin
();
BehaviorObserver observer =
std::bind
(&Sequence::onChildComplete,
this
, std::placeholders::_1);
m_pBehaviorTree->
start
(**m_Current, &observer);
}
void
onChildComplete
(Status)
{
Behavior& child = **m_Current;
if
(child.
m_eStatus
==
BH_FAILURE
)
{
m_pBehaviorTree->
stop
(*
this
,
BH_FAILURE
);
return
;
}
ASSERT
(child.
m_eStatus
==
BH_SUCCESS
);
if
(++m_Current == m_Children.
end
())
{
m_pBehaviorTree->
stop
(*
this
,
BH_SUCCESS
);
}
else
{
BehaviorObserver observer =
std::bind
(&Sequence::onChildComplete,
this
, std::placeholders::_1);
m_pBehaviorTree->
start
(**m_Current, &observer);
}
}
virtual
Status
update
()
{
return
BH_RUNNING
;
}
Behaviors::iterator m_Current;
};
//
----------------------------------------------------------------------------
template
<
class
COMPOSITE
>
class
MockComposite
:
public
COMPOSITE
{
public:
MockComposite
(BehaviorTree& bt,
size_t
size)
:
COMPOSITE
(bt)
{
for
(
size_t
i=
0
; i<size; ++i)
{
COMPOSITE
::m_Children.
push_back
(
new
MockBehavior);
}
}
~MockComposite
()
{
for
(
size_t
i=
0
; i<
COMPOSITE
::m_Children.
size
(); ++i)
{
delete
COMPOSITE
::m_Children[i];
}
}
MockBehavior&
operator
[](
size_t
index)
{
ASSERT
(index <
COMPOSITE
::m_Children.
size
());
return
*
static_cast
<MockBehavior*>(
COMPOSITE
::m_Children[index]);
}
};
typedef
MockComposite<Sequence> MockSequence;
TEST
(StarterKit4, SequenceOnePassThrough)
{
Status status[
2
] = {
BH_SUCCESS
,
BH_FAILURE
};
for
(
int
i=
0
; i<
2
; ++i)
{
BehaviorTree bt;
MockSequence
seq
(bt,
1
);
bt.
start
(seq);
bt.
tick
();
CHECK_EQUAL
(seq.
m_eStatus
,
BH_RUNNING
);
CHECK_EQUAL
(
0
, seq[
0
].
m_iTerminateCalled
);
seq[
0
].
m_eReturnStatus
= status[i];
bt.
tick
();
CHECK_EQUAL
(seq.
m_eStatus
, status[i]);
CHECK_EQUAL
(
1
, seq[
0
].
m_iTerminateCalled
);
}
}
TEST
(StarterKit4, SequenceTwoFails)
{
BehaviorTree bt;
MockSequence
seq
(bt,
2
);
bt.
start
(seq);
bt.
tick
();
CHECK_EQUAL
(seq.
m_eStatus
,
BH_RUNNING
);
CHECK_EQUAL
(
0
, seq[
0
].
m_iTerminateCalled
);
seq[
0
].
m_eReturnStatus
=
BH_FAILURE
;
bt.
tick
();
CHECK_EQUAL
(seq.
m_eStatus
,
BH_FAILURE
);
CHECK_EQUAL
(
1
, seq[
0
].
m_iTerminateCalled
);
}
TEST
(StarterKit4, SequenceTwoContinues)
{
BehaviorTree bt;
MockSequence
seq
(bt,
2
);
bt.
start
(seq);
bt.
tick
();
CHECK_EQUAL
(seq.
m_eStatus
,
BH_RUNNING
);
CHECK_EQUAL
(
0
, seq[
0
].
m_iTerminateCalled
);
seq[
0
].
m_eReturnStatus
=
BH_SUCCESS
;
bt.
tick
();
CHECK_EQUAL
(seq.
m_eStatus
,
BH_RUNNING
);
CHECK_EQUAL
(
1
, seq[
0
].
m_iTerminateCalled
);
}
}
//
namespace bt4
Back
|
FazBrowse Home
|
New Git URL