Antkeeper  0.0.1
node.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 Christopher J. Howard
3  *
4  * This file is part of Antkeeper source code.
5  *
6  * Antkeeper source code is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Antkeeper source code is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef ANTKEEPER_AI_BT_NODE_HPP
21 #define ANTKEEPER_AI_BT_NODE_HPP
22 
23 #include <engine/ai/bt/status.hpp>
24 
25 namespace ai {
26 namespace bt {
27 
33 template <class T>
34 struct node
35 {
37  typedef T context_type;
38 
44  virtual status execute(context_type& context) const = 0;
45 };
46 
48 template <class T>
50 
51 
53 template <class T>
54 struct decorator_node: public node<T>
55 {
57 };
58 
60 template <class T>
61 struct composite_node: public node<T>
62 {
63  std::list<node<T>*> children;
64 };
65 
67 template <class T>
68 struct action: public leaf_node<T>
69 {
70  virtual status execute(node<T>::context_type& context) const final;
71  typedef std::function<status(node<T>::context_type&)> function_type;
72  function_type function;
73 };
74 
76 template <class T>
77 struct condition: public leaf_node<T>
78 {
79  virtual status execute(node<T>::context_type& context) const final;
80  typedef std::function<status(const node<T>::context_type&)> predicate_type;
82 };
83 
85 template <class T>
86 struct inverter: public decorator_node<T>
87 {
88  virtual status execute(node<T>::context_type& context) const final;
89 };
90 
92 template <class T>
93 struct repeater: public decorator_node<T>
94 {
95  virtual status execute(node<T>::context_type& context) const final;
96  int n;
97 };
98 
100 template <class T>
101 struct succeeder: public decorator_node<T>
102 {
103  virtual status execute(node<T>::context_type& context) const final;
104 };
105 
107 template <class T>
108 struct sequence: public composite_node<T>
109 {
110  virtual status execute(node<T>::context_type& context) const final;
111 };
112 
114 template <class T>
115 struct selector: public composite_node<T>
116 {
117  virtual status execute(node<T>::context_type& context) const final;
118 };
119 
120 template <class T>
122 {
123  return function(context);
124 }
125 
126 template <class T>
128 {
129  return (predicate(context)) ? status::success : status::failure;
130 }
131 
132 template <class T>
134 {
135  status child_status = decorator_node<T>::child->execute(context);
136  return (child_status == status::success) ? status::failure : (child_status == status::failure) ? status::success : child_status;
137 }
138 
139 template <class T>
141 {
142  status child_status;
143  for (int i = 0; i < n; ++i)
144  {
145  child_status = decorator_node<T>::child->execute(context);
146  if (child_status == status::failure)
147  break;
148  }
149  return child_status;
150 }
151 
152 template <class T>
154 {
156  return status::success;
157 }
158 
159 template <class T>
161 {
162  for (const node<T>* child: composite_node<T>::children)
163  {
164  status child_status = child->execute(context);
165  if (child_status != status::success)
166  return child_status;
167  }
168  return status::success;
169 }
170 
171 template <class T>
173 {
174  for (const node<T>* child: composite_node<T>::children)
175  {
176  status child_status = child->execute(context);
177  if (child_status != status::failure)
178  return child_status;
179  }
180  return status::failure;
181 }
182 
183 } // namespace bt
184 } // namespace ai
185 
186 #endif // ANTKEEPER_AI_BT_NODE_HPP
status
Behavior tree node return status enumerations.
Definition: status.hpp:28
@ success
Indicates a node's execution has succeeded.
@ failure
Indicates a node's execution has failed.
Artificial intelligence (AI)
Definition: ai.hpp:24
Executes a function on a context and returns the status.
Definition: node.hpp:69
virtual status execute(node< T >::context_type &context) const final
Definition: node.hpp:121
std::function< status(node< T >::context_type &)> function_type
Definition: node.hpp:71
A node that can have one or more children.
Definition: node.hpp:62
std::list< node< T > * > children
Definition: node.hpp:63
Evaluates a boolean condition (predicate) and returns either status::success or status::failure.
Definition: node.hpp:78
predicate_type predicate
Definition: node.hpp:81
virtual status execute(node< T >::context_type &context) const final
Definition: node.hpp:127
std::function< status(const node< T >::context_type &)> predicate_type
Definition: node.hpp:80
A node with exactly one child.
Definition: node.hpp:55
node< T > * child
Definition: node.hpp:56
Executes a child node and returns its inverted status. If the child returns status::success,...
Definition: node.hpp:87
virtual status execute(node< T >::context_type &context) const final
Definition: node.hpp:133
Abstract base class for behavior tree nodes.
Definition: node.hpp:35
virtual status execute(context_type &context) const =0
Executes a node's function and returns its status.
T context_type
Data type on which nodes operate.
Definition: node.hpp:37
Attempts to execute a child node n times or until the child fails.
Definition: node.hpp:94
virtual status execute(node< T >::context_type &context) const final
Definition: node.hpp:140
Attempts to execute each child node sequentially until one succeeds. If a child succeeds,...
Definition: node.hpp:116
virtual status execute(node< T >::context_type &context) const final
Definition: node.hpp:172
Attempts to execute each child node sequentially until one fails. If all children are executed succes...
Definition: node.hpp:109
virtual status execute(node< T >::context_type &context) const final
Definition: node.hpp:160
Executes a child node and returns status::success regardless of the child node status.
Definition: node.hpp:102
virtual status execute(node< T >::context_type &context) const final
Definition: node.hpp:153