Bonsai - Behavior Tree
You can serialize the behavior tree using Serde and e.g. Ron.
A Behavior Tree (BT) is a data structure in which we can set the rules of how certain behavior's can occur, and the order in which they would execute. BTs are a very efficient way of creating complex systems that are both modular and reactive. These properties are crucial in many applications, which has led to the spread of BT from computer game programming to many branches of AI and Robotics.
How to use a Behavior tree?
An AI behavior tree is a very generic way of organizing interactive logic.
It has built-in semantics for processes that signals Running, Success or
Failure.
For example, if you have a state A and a state B:
- Move from state
Ato stateBifAsucceeds:Sequence([A, B]) - Try
Afirst and then tryBifAfails:Select([A, B]) - If
conditionsucceedes doA, else doB:If(condition, A, B) - If
Asucceeds, return failure (and vice-versa):Invert(A) - Do
Brepeatedly whileAruns:While(A, [B]) - Do
A,Bforever:While(WaitForever, [A, B]) - Run
AandBin parallell and wait for both to succeed:WhenAll([A, B]) - Run
AandBin parallell and wait for any to succeed:WhenAny([A, B]) - Run
AandBin parallell, butAhas to succeed beforeB:After([A, B])
See the Behavior enum for more information.
Example of use
This is a simple example with two possible Actions: Increment a number, Decrement a number. We
construct a BT where we increment a number twice, one second apart. Then wait 0.5 seconds before we
then decrement the same number again. Additionally we use a Blackboard to store/persist the immediate
state of the number accessed by the key count.
use ;
// Some test actions.
// A test state machine that can increment and decrement.