Behavioral trees

This describes what are behavioral trees, and how I have implemented them. Here are also discussed the types of nodes of a behavioral tree.
My implemetation has many custom node types, and they are discussed here aswell
For implementation oriented documentation, navigate here

This is sub-layout for documentation pages


1 Introduction

Behavioral tree is a tree structure with specific types of nodes. It is used to determine, what action should an AI take, with respect to a game situation.

In this real time game, it is called repetitivelly, and I have implemented special modifiers, which allow one tree call to yield in multuiple AI actions.
Note: This can be generally solved, using the Simple paralell node, but that would make the tree quite messy, so I went for my own solution.

2 Mechanics

A behavior tree is assigned to some AI character (called actor). At the leaves of the tree, there are nodes that act as actual commands for the controlled actor. These nodes are called task nodes.

The idea is, that when calling execution of the tree, because of the tree structure and node system, some leaves will get executed. Which task nodes will get executed depends on the actor state, or on the world around him, or some other conditions.
Each node might return one of the success, failure or running flags. According to the result of the node execution, a different branch is / is not executed. This behavior is defined by the node types, discussed below.
Here is an axample of a behavior tree:

3 Node types

The behavior tree may have different types of nodes

3.1 Basic nodes

Basic nodes do not affect the actor himself, but are only used for determining, how / which next tree nodes will be evaluated
In my implementation, they are depicted with grey color, as follows:

3.1.1 Selector


Selector node is used to find and execute the first child that does not fail. The selector node will return immediately with a status code of success or running when one of its children returns success or running. If a child returns failure, the next child is executed, until no child is left. The children are executed from left to right.

3.1.2 Sequence


Sequence nodes are used to find and execute the first child that has not yet succeeded. If an executed child returns success, next child is executed, if possible. If an executed child returns failure or running, the sequence node returns that result, not executing any more children.

3.2 Task nodes

Task nodes represent a command to the actor. They might have attributes, more precisely defining, what to do (for example: Prayer ID)
They can not have any children. The returned value depends on the task execution (whether it suceeded, and so on)
In my implementation, they are depicted with blue color, as follows:

3.2.1 Prayer


Allows to turn on / off some specific prayer

3.2.2 Special attack

Special attack

Uses a special attack for currently wielded weapon (if possible)

3.2.3 Attack character

Attack character

Will find a new character as a combat target, and will attack him (if rules allow that). Attacking that target lasts, until stopped by other action.

4 Decorators

A decorator is a type of a modifier, which can be assigned to any node.
Any decorator returns only 2 values, true or false.

If a node has a decorator assigned to it, then when the node shall be executed, the decorator is executed first, and if it returns false, then the node will not be executed at all, and failure is returned to the parent.
If the decorator returns true, then the node it is assigned to is executed normally, and it's result is returned to a parent (if not said otherwise).