💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

196 lines
5.3 KiB

  1. /*
  2. * Copyright (C) 2021 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. #ifndef ANTKEEPER_AI_BEHAVIOR_TREE_HPP
  20. #define ANTKEEPER_AI_BEHAVIOR_TREE_HPP
  21. #include <functional>
  22. #include <list>
  23. namespace ai {
  24. /// Behavior tree (BT)
  25. namespace bt {
  26. /// Behavior tree node return status enumerations.
  27. enum class status
  28. {
  29. failure, ///< Indicates a node's execution failed.
  30. success, ///< Indicates a node's execution succeed.
  31. running ///< Indicates a node's execution has not finished.
  32. };
  33. /**
  34. * Abstract base class for behavior tree nodes.
  35. *
  36. * @tparam T Data type on which nodes operate.
  37. */
  38. template <class T>
  39. struct node
  40. {
  41. /// Data type on which nodes operate.
  42. typedef T context_type;
  43. /**
  44. * Executes a node's functionality and returns its status.
  45. *
  46. * @param context Context data on which the node will operate.
  47. */
  48. virtual status execute(context_type& context) const = 0;
  49. };
  50. /// A node with no children.
  51. template <class T>
  52. using leaf_node = node<T>;
  53. /// A node with exactly one child.
  54. template <class T>
  55. struct decorator_node: node<T>
  56. {
  57. node* child;
  58. };
  59. /// A node that can have one or more children.
  60. template <class T>
  61. struct composite_node: node<T>
  62. {
  63. std::list<node*> children;
  64. };
  65. /// Executes a function on a context and returns the status.
  66. template <class T>
  67. struct action: leaf_node<T>
  68. {
  69. virtual status execute(context_type& context) const final;
  70. typedef std::function<status(context_type&)> function_type;
  71. function_type function;
  72. };
  73. /// Evaluates a boolean condition (predicate) and returns either `status::success` or `status::failure`.
  74. template <class T>
  75. struct condition: leaf_node<T>
  76. {
  77. virtual status execute(context_type& context) const final;
  78. typedef std::function<status(const context_type&)> predicate_type;
  79. predicate_type predicate;
  80. };
  81. /// Executes a child node and returns its inverted status. If the child returns `status::success`, then `status::failure` will be returned. Otherwise if the child returns `status::failure`, then `status::success` will be returned.
  82. template <class T>
  83. struct inverter: decorator_node<T>
  84. {
  85. virtual status execute(context_type& context) const final;
  86. };
  87. /// Attempts to execute a child node `n` times or until the child fails.
  88. template <class T>
  89. struct repeater: decorator_node<T>
  90. {
  91. virtual status execute(context_type& context) const final;
  92. int n;
  93. };
  94. /// Executes a child node and returns `status::success` regardless of the child node status.
  95. template <class T>
  96. struct succeeder: decorator_node<T>
  97. {
  98. virtual status execute(context_type& context) const final;
  99. };
  100. /// Attempts to execute each child node sequentially until one fails. If all children are executed successfully, `status::success` will be returned. Otherwise if any children fail, `status::failure` will be returned.
  101. template <class T>
  102. struct sequence: composite_node<T>
  103. {
  104. virtual status execute(context_type& context) const final;
  105. };
  106. /// Attempts to execute each child node sequentially until one succeeds. If a child succeeds, `status::success` will be returned. Otherwise if all children fail, `status::failure` will be returned.
  107. template <class T>
  108. struct selector: composite_node<T>
  109. {
  110. virtual status execute(context_type& context) const final;
  111. };
  112. template <class T>
  113. status action<T>::execute(context_type& context) const
  114. {
  115. return function(context);
  116. }
  117. template <class T>
  118. status condition<T>::execute(context_type& context) const
  119. {
  120. return (predicate(context)) ? status::success : status::failure;
  121. }
  122. template <class T>
  123. status inverter<T>::execute(context_type& context) const
  124. {
  125. status child_status = child->execute(context);
  126. return (child_status == status::success) ? status::failure : (child_status == status::failure) ? status::success : child_status;
  127. }
  128. template <class T>
  129. status repeater<T>::execute(context_type& context) const
  130. {
  131. status child_status;
  132. for (int i = 0; i < n; ++i)
  133. {
  134. child_status = child->execute(context);
  135. if (child_status == status::failure)
  136. break;
  137. }
  138. return child_status;
  139. }
  140. template <class T>
  141. status succeeder<T>::execute(context_type& context) const
  142. {
  143. child->execute(context);
  144. return status::success;
  145. }
  146. template <class T>
  147. status sequence<T>::execute(context_type& context) const
  148. {
  149. for (const node* child: children)
  150. {
  151. status child_status = child->execute(context);
  152. if (child_status != status::success)
  153. return child_status;
  154. }
  155. return status::success;
  156. }
  157. template <class T>
  158. status selector<T>::execute(context_type& context) const
  159. {
  160. for (const node* child: children)
  161. {
  162. status child_status = child->execute(context);
  163. if (child_status != status::failure)
  164. return child_status;
  165. }
  166. return status::failure;
  167. }
  168. } // namespace bt
  169. } // namespace ai
  170. #endif // ANTKEEPER_AI_BEHAVIOR_TREE_HPP