BehaviorTree
Core Library to create and execute Behavior Trees
Loading...
Searching...
No Matches
behavior_tree.h
1/* Copyright (C) 2015-2018 Michele Colledanchise - All Rights Reserved
2 * Copyright (C) 2018-2025 Davide Faconti - All Rights Reserved
3*
4* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
5* to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
6* 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:
7* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8*
9* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
10* 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,
11* 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.
12*/
13
14#pragma once
15
16#include "behaviortree_cpp/action_node.h"
17#include "behaviortree_cpp/actions/always_failure_node.h"
18#include "behaviortree_cpp/actions/always_success_node.h"
19#include "behaviortree_cpp/actions/script_condition.h"
20#include "behaviortree_cpp/actions/script_node.h"
21#include "behaviortree_cpp/actions/set_blackboard_node.h"
22#include "behaviortree_cpp/actions/sleep_node.h"
23#include "behaviortree_cpp/actions/test_node.h"
24#include "behaviortree_cpp/actions/unset_blackboard_node.h"
25#include "behaviortree_cpp/actions/updated_action.h"
26#include "behaviortree_cpp/condition_node.h"
27#include "behaviortree_cpp/controls/fallback_node.h"
28#include "behaviortree_cpp/controls/if_then_else_node.h"
29#include "behaviortree_cpp/controls/parallel_all_node.h"
30#include "behaviortree_cpp/controls/parallel_node.h"
31#include "behaviortree_cpp/controls/reactive_fallback.h"
32#include "behaviortree_cpp/controls/reactive_sequence.h"
33#include "behaviortree_cpp/controls/sequence_node.h"
34#include "behaviortree_cpp/controls/sequence_with_memory_node.h"
35#include "behaviortree_cpp/controls/switch_node.h"
36#include "behaviortree_cpp/controls/try_catch_node.h"
37#include "behaviortree_cpp/controls/while_do_else_node.h"
38#include "behaviortree_cpp/decorators/delay_node.h"
39#include "behaviortree_cpp/decorators/force_failure_node.h"
40#include "behaviortree_cpp/decorators/force_success_node.h"
41#include "behaviortree_cpp/decorators/inverter_node.h"
42#include "behaviortree_cpp/decorators/keep_running_until_failure_node.h"
43#include "behaviortree_cpp/decorators/loop_node.h"
44#include "behaviortree_cpp/decorators/repeat_node.h"
45#include "behaviortree_cpp/decorators/retry_node.h"
46#include "behaviortree_cpp/decorators/run_once_node.h"
47#include "behaviortree_cpp/decorators/script_precondition.h"
48#include "behaviortree_cpp/decorators/subtree_node.h"
49#include "behaviortree_cpp/decorators/timeout_node.h"
50#include "behaviortree_cpp/decorators/updated_decorator.h"
51
52#include <iostream>
53
54namespace BT
55{
56//Call the visitor for each node of the tree, given a root.
57void applyRecursiveVisitor(const TreeNode* root_node,
58 const std::function<void(const TreeNode*)>& visitor);
59
60//Call the visitor for each node of the tree, given a root.
61void applyRecursiveVisitor(TreeNode* root_node,
62 const std::function<void(TreeNode*)>& visitor);
63
64/**
65 * Debug function to print the hierarchy of the tree. Prints to std::cout by default.
66 */
67void printTreeRecursively(const TreeNode* root_node, std::ostream& stream = std::cout);
68
70
71/**
72 * @brief buildSerializedStatusSnapshot can be used to create a buffer that can be stored
73 * (or sent to a client application) to know the status of all the nodes of a tree.
74 * It is not "human readable".
75 *
76 * @param root_node
77 * @param serialized_buffer is the output.
78 */
79void buildSerializedStatusSnapshot(const TreeNode* root_node,
80 SerializedTreeStatus& serialized_buffer);
81
82/// Simple way to extract the type of a TreeNode at COMPILE TIME.
83/// Useful to avoid the cost of dynamic_cast or the virtual method TreeNode::type().
84template <typename T>
85inline NodeType getType()
86{
87 // clang-format off
88 if( std::is_base_of<ActionNodeBase, T>::value ) return NodeType::ACTION;
89 if( std::is_base_of<ConditionNode, T>::value ) return NodeType::CONDITION;
90 if( std::is_base_of<SubTreeNode, T>::value ) return NodeType::SUBTREE;
91 if( std::is_base_of<DecoratorNode, T>::value ) return NodeType::DECORATOR;
92 if( std::is_base_of<ControlNode, T>::value ) return NodeType::CONTROL;
93 return NodeType::UNDEFINED;
94 // clang-format on
95}
96
97const char* LibraryVersionString();
98
99int LibraryVersionNumber();
100
101} // namespace BT
The ActionNodeBase is the base class to use to create any kind of action. A particular derived class ...
Definition: action_node.h:35
The ConditionNode is a leaf node used to check a condition.
Definition: condition_node.h:32
The ControlNode is the base class for nodes that can have multiple children.
Definition: control_node.h:32
The DecoratorNode is the base class for nodes that have exactly one child.
Definition: decorator_node.h:19
The SubTreeNode is a way to wrap an entire Subtree, creating a separated BlackBoard....
Definition: subtree_node.h:53
Abstract base class for Behavior Tree Nodes.
Definition: tree_node.h:154
Definition: action_node.h:24
void printTreeRecursively(const TreeNode *root_node, std::ostream &stream=std::cout)
NodeType
Enumerates the possible types of nodes.
Definition: basic_types.h:21
void buildSerializedStatusSnapshot(const TreeNode *root_node, SerializedTreeStatus &serialized_buffer)
buildSerializedStatusSnapshot can be used to create a buffer that can be stored (or sent to a client ...
NodeType getType()
Definition: behavior_tree.h:85