forked from bitbrain/beehave
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbeehave_tree.cpp
More file actions
228 lines (185 loc) Β· 7.62 KB
/
beehave_tree.cpp
File metadata and controls
228 lines (185 loc) Β· 7.62 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
/**************************************************************************/
/* beehave_tree.cpp */
/**************************************************************************/
/* This file is part of: */
/* BEEHAVE */
/* https://bitbra.in/beehave */
/**************************************************************************/
/* Copyright (c) 2024-present Beehave Contributors. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "beehave_tree.h"
#include "beehave_blackboard.h"
#include <core/class_db.hpp>
#include "variant/utility_functions.hpp"
using namespace godot;
BeehaveTree::BeehaveTree() :
context(Ref<BeehaveContext>(memnew(BeehaveContext))),
tick_status(BeehaveTickStatus::PENDING),
tick_rate(1),
blackboard(nullptr),
actor(nullptr) {
}
BeehaveTree::~BeehaveTree() {
_internal_blackboard = nullptr;
blackboard = nullptr;
actor = nullptr;
}
void BeehaveTree::_bind_methods() {
// signals
ADD_SIGNAL(MethodInfo("enabled"));
ADD_SIGNAL(MethodInfo("disabled"));
// enums
BIND_ENUM_CONSTANT(IDLE);
BIND_ENUM_CONSTANT(PHYSICS);
// methods
ClassDB::bind_method(D_METHOD("tick"), &BeehaveTree::tick);
ClassDB::bind_method(D_METHOD("enable"), &BeehaveTree::enable);
ClassDB::bind_method(D_METHOD("disable"), &BeehaveTree::disable);
ClassDB::bind_method(D_METHOD("set_enabled", "enabled"), &BeehaveTree::set_enabled);
ClassDB::bind_method(D_METHOD("is_enabled"), &BeehaveTree::is_enabled);
ClassDB::bind_method(D_METHOD("get_tick_status"), &BeehaveTree::get_tick_status);
ClassDB::bind_method(D_METHOD("set_tick_rate", "tick_rate"), &BeehaveTree::set_tick_rate);
ClassDB::bind_method(D_METHOD("get_tick_rate"), &BeehaveTree::get_tick_rate);
ClassDB::bind_method(D_METHOD("set_process_thread", "thread"), &BeehaveTree::set_process_thread);
ClassDB::bind_method(D_METHOD("get_process_thread"), &BeehaveTree::get_process_thread);
ClassDB::bind_method(D_METHOD("set_actor", "actor"), &BeehaveTree::set_actor);
ClassDB::bind_method(D_METHOD("get_actor"), &BeehaveTree::get_actor);
ClassDB::bind_method(D_METHOD("set_blackboard", "blackboard"), &BeehaveTree::set_blackboard);
ClassDB::bind_method(D_METHOD("get_blackboard"), &BeehaveTree::get_blackboard);
// exports
ADD_PROPERTY(PropertyInfo(Variant::INT, "process_thread", PROPERTY_HINT_ENUM, "Idle,Physics"), "set_process_thread", "get_process_thread");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_enabled", "is_enabled");
ADD_PROPERTY(PropertyInfo(Variant::INT, "tick_rate"), "set_tick_rate", "get_tick_rate");
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "actor", PROPERTY_HINT_NODE_TYPE, "Node"), "set_actor", "get_actor");
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "blackboard", PROPERTY_HINT_NODE_TYPE, "BeehaveBlackboard"), "set_blackboard", "get_blackboard");
}
void BeehaveTree::_ready() {
this->_internal_blackboard = memnew(BeehaveBlackboard);
add_child(_internal_blackboard, false, Node::INTERNAL_MODE_FRONT);
set_physics_process(enabled && process_thread == ProcessThread::PHYSICS);
set_process(enabled && process_thread == ProcessThread::IDLE);
// Randomize at what frames tick() will happen to avoid stutters
_last_tick = rand() % tick_rate;
}
void BeehaveTree::_process(double delta) {
if (process_thread == BeehaveTree::ProcessThread::IDLE) {
process_internally(delta);
}
}
void BeehaveTree::_physics_process(double delta) {
if (process_thread == BeehaveTree::ProcessThread::PHYSICS) {
process_internally(delta);
}
}
void BeehaveTree::set_actor(Node* actor) {
this->actor = actor;
}
Node* BeehaveTree::get_actor() const {
return actor;
}
void BeehaveTree::set_blackboard(BeehaveBlackboard *blackboard) {
this->blackboard = blackboard;
}
BeehaveBlackboard *BeehaveTree::get_blackboard() const {
return blackboard;
}
void BeehaveTree::enable() {
enabled = true;
set_physics_process(enabled && process_thread == ProcessThread::PHYSICS);
set_process(enabled && process_thread == ProcessThread::IDLE);
}
void BeehaveTree::disable() {
enabled = false;
set_physics_process(enabled && process_thread == ProcessThread::PHYSICS);
set_process(enabled && process_thread == ProcessThread::IDLE);
interrupt();
}
void BeehaveTree::set_enabled(bool enabled) {
this->enabled = enabled;
if(enabled) {
enable();
}
else {
disable();
}
}
bool BeehaveTree::is_enabled() const {
return enabled;
}
void BeehaveTree::set_tick_rate(int tick_rate) {
this->tick_rate = tick_rate;
}
int BeehaveTree::get_tick_rate() const {
return tick_rate;
}
BeehaveTickStatus BeehaveTree::get_tick_status() const {
return tick_status;
}
void BeehaveTree::set_process_thread(BeehaveTree::ProcessThread thread) {
process_thread = thread;
set_physics_process(enabled && process_thread == ProcessThread::PHYSICS);
set_process(enabled && process_thread == ProcessThread::IDLE);
}
BeehaveTree::ProcessThread BeehaveTree::get_process_thread() const {
return process_thread;
}
void BeehaveTree::process_internally(double delta) {
// ensure that we consider the current tick rate of the tree
if (_last_tick < tick_rate - 1) {
_last_tick += 1;
return;
}
_last_tick = 0;
context->set_delta(delta);
tick();
}
BeehaveTickStatus BeehaveTree::tick() {
context->set_tree(this);
context->set_actor(actor ? actor : get_parent());
context->set_blackboard(blackboard ? blackboard : _internal_blackboard);
if (get_child_count() == 0) {
tick_status = BeehaveTickStatus::FAILURE;
return tick_status;
}
for (int i = 0; i < get_child_count(); i++) {
Node *child = get_child(i);
if (!child) {
continue;
}
BeehaveTreeNode *tree_node = cast_to<BeehaveTreeNode>(child);
if (tree_node == nullptr) {
// Skip nodes that aren't valid Beehave nodes
continue;
}
if (tick_status != BeehaveTickStatus::RUNNING) {
tree_node->before_run(context);
}
tick_status = tree_node->tick(context);
if (tick_status != BeehaveTickStatus::RUNNING) {
tree_node->after_run(context);
}
}
return tick_status;
}
void BeehaveTree::interrupt() {
// TODO: interrupt currently running child, if it exists
}