0% found this document useful (0 votes)
56 views3 pages

AI Toolkit - Behavior Tree

The document describes a behavior tree framework. It contains classes for different node types like sequence, selector, check and task nodes. It also defines an execution state enumeration. An example is given to create a behavior tree with different sequences and tasks to control an agent to destroy enemies or follow waypoints. The tree is evaluated by passing a blackboard object containing the agent and environment state.

Uploaded by

Anna J Bischoff
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views3 pages

AI Toolkit - Behavior Tree

The document describes a behavior tree framework. It contains classes for different node types like sequence, selector, check and task nodes. It also defines an execution state enumeration. An example is given to create a behavior tree with different sequences and tasks to control an agent to destroy enemies or follow waypoints. The tree is evaluated by passing a blackboard object containing the agent and environment state.

Uploaded by

Anna J Bischoff
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Classes | Typedefs | Enumerations

Behavior Tree

Classes

class aitoolkit::bt::node< T >

Base abstract class for all nodes. More...

class aitoolkit::bt::seq< T >

Sequence node, will execute all children in order until one fails. More...

class aitoolkit::bt::sel< T >

Selector node, will execute all children in order until one succeeds. More...

class aitoolkit::bt::neg< T >

Negate node, will return the opposite of the child node. More...

class aitoolkit::bt::check< T >

Check node, will return success if the callback returns true. More...

class aitoolkit::bt::task< T >

Task node, will execute the callback and return the result. More...

Typedefs

template<class T >

using aitoolkit::bt::node_ptr = std::shared_ptr<node<T>>

Heap-allocated pointer to node.

Enumerations

enum class aitoolkit::bt::execution_state { execution_state::success , execution_state::failure , execution_state::running }

Represent the state of a node. More...

Detailed Description

Introduction
A behavior tree is a tree structure where each node represents a behavior. The tree is evaluated from the root node to the leaf nodes. The leaf nodes are
either tasks or checks. A task is a node that performs an action and returns either success or failure. A check is a node that returns either success or
failure based on some condition.

Selector

Sequence Sequence Sequence

Check enemy in attack range Destroy enemy Check enemy in sight Move towards enemy Move towards waypoint Select next waypoint
Usage
First, include the header file:

#include <aitoolkit/behtree.hpp>

Then, create a blackboard class that will hold the state of the tree:

struct blackboard_type {
glm::vec2 agent_position;
glm::vec2 enemy_position;

float attack_range;
float sight_range;

size_t current_waypoint;
std::vector<glm::vec2> waypoints;
};

Next, create the tree:

using namespace aitoolkit::bt;

auto tree = sel<blackboard_type>::make({


seq<blackboard_type>::make({
check<blackboard_type>::make([](const blackboard_type& bb) {
auto distance = glm::distance(bb.agent_position, bb.enemy_position);
return distance <= bb.attack_range;
}),
task<blackboard_type>::make([](blackboard_type& bb) {
// Destroy enemy
return execution_state::success;
})
}),
seq<blackboard_type>::make({
check<blackboard_type>::make([](const blackboard_type& bb) {
auto distance = glm::distance(bb.agent_position, bb.enemy_position);
return distance <= bb.sight_range;
}),
task<blackboard_type>::make([](blackboard_type& bb) {
// Move towards enemy
return execution_state::success;
})
}),
seq<blackboard_type>::make({
task<blackboard_type>::make([](blackboard_type& bb) {
// Move towards waypoint
return execution_state::success;
}),
task<blackboard_type>::make([](blackboard_type& bb) {
// Select next waypoint
return execution_state::success;
})
})
});

Finally, evaluate the tree:

auto bb = blackboard_type{
.agent_position = { 0.0f, 0.0f },
.enemy_position = { 1.0f, 1.0f },
.attack_range = 0.5f,
.sight_range = 1.0f
};

while (true) {
auto state = tree->evaluate(bb);
if (state == execution_state::success) {
break;
}
}
Enumeration Type Documentation

◆ execution_state

enum class aitoolkit::bt::execution_state strong

Represent the state of a node.

Enumerator
success The node was successful

failure The node failed

running The node is still running

You might also like