AI Toolkit
Loading...
Searching...
No Matches
behtree.hpp
1#pragma once
2
133#include <functional>
134#include <memory>
135#include <vector>
136
137#include <type_traits>
138#include <concepts>
139
140namespace aitoolkit::bt {
146 enum class execution_state {
147 success,
148 failure,
149 running
150 };
151
157 template <class T>
158 class node {
159 public:
160 node() = default;
161 node(const node&) = delete;
162 node(node&& other) {
163 m_children = std::move(other.m_children);
164 }
165
166 virtual ~node() = default;
167
168 virtual execution_state evaluate(T& blackboard) const = 0;
169
170 protected:
171 std::vector<std::unique_ptr<node<T>>> m_children;
172 };
173
178 template <class T>
179 using node_ptr = std::unique_ptr<node<T>>;
180
181 template <typename N, class T>
182 concept node_trait = std::derived_from<N, node<T>>;
183
188 template <typename T, node_trait<T> ...Children>
189 std::vector<node_ptr<T>> node_list(Children&&... children) {
190 auto nodes = std::vector<node_ptr<T>>{};
191 nodes.reserve(sizeof...(children));
192 (nodes.push_back(std::make_unique<Children>(std::move(children))), ...);
193 return nodes;
194 }
195
201 template <class T>
202 class seq final : public node<T> {
203 public:
204 seq(std::vector<node_ptr<T>> children) {
205 this->m_children = std::move(children);
206 }
207
208 virtual execution_state evaluate(T& blackboard) const override {
209 for (auto& child : this->m_children) {
210 auto state = child->evaluate(blackboard);
211 if (state != execution_state::success) {
212 return state;
213 }
214 }
215
217 }
218 };
219
225 template <class T>
226 class sel final : public node<T> {
227 public:
228 sel(std::vector<node_ptr<T>> children) {
229 this->m_children = std::move(children);
230 }
231
232 virtual execution_state evaluate(T& blackboard) const override {
233 for (auto& child : this->m_children) {
234 auto state = child->evaluate(blackboard);
235 if (state != execution_state::failure) {
236 return state;
237 }
238 }
239
241 }
242 };
243
249 template <class T>
250 class neg final : public node<T> {
251 public:
252 template <node_trait<T> N>
253 neg(N&& child) {
254 this->m_children.reserve(1);
255 this->m_children.push_back(std::make_unique<N>(std::move(child)));
256 }
257
258 virtual execution_state evaluate(T& blackboard) const override {
259 if (this->m_children.size() != 1) {
261 }
262
263 auto& child = this->m_children.front();
264 auto state = child->evaluate(blackboard);
265 if (state == execution_state::success) {
267 } else if (state == execution_state::failure) {
269 }
270
271 return state;
272 }
273 };
274
280 template <class T>
281 class check final : public node<T> {
282 public:
283 using callback_type = std::function<bool(const T&)>;
284
285 public:
286 check(callback_type fn) : m_fn(fn) {}
287
288 virtual execution_state evaluate(T& blackboard) const override {
289 if (m_fn(blackboard)) {
291 }
292
294 }
295
296 private:
297 callback_type m_fn;
298 };
299
305 template <class T>
306 class task final : public node<T> {
307 public:
308 using callback_type = std::function<execution_state(T&)>;
309
310 public:
311 task(callback_type fn) : m_fn(fn) {}
312
313 virtual execution_state evaluate(T& blackboard) const override {
314 return m_fn(blackboard);
315 }
316
317 private:
318 callback_type m_fn;
319 };
320}
Check node, will return success if the callback returns true.
Definition behtree.hpp:281
Negate node, will return the opposite of the child node.
Definition behtree.hpp:250
Base abstract class for all nodes.
Definition behtree.hpp:158
Selector node, will execute all children in order until one succeeds.
Definition behtree.hpp:226
Sequence node, will execute all children in order until one fails.
Definition behtree.hpp:202
Task node, will execute the callback and return the result.
Definition behtree.hpp:306
Definition behtree.hpp:182
std::unique_ptr< node< T > > node_ptr
Heap-allocated pointer to node.
Definition behtree.hpp:179
execution_state
Represent the state of a node.
Definition behtree.hpp:146
std::vector< node_ptr< T > > node_list(Children &&... children)
Helper function to create a list of nodes.
Definition behtree.hpp:189