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]) - Wait for both
AandBto complete:WhenAll([A, B]) - Wait for either
AorBto complete:WhenAny([A, B]) - Wait for either
AorBto complete:WhenAny([A, B])See theBehaviorenum for more information.
Example of use
This is a enemy NPC (non-player-character) behavior mock-up which decides if the AI should shoot while running for nearby cover, rush in to attack the player up close or stand its ground while firing at the player.
use ;
// Some test actions.
// A test state machine that can increment and decrement.