'Behavior Trees' are an A.I. technique used to control the behavior of NPCs.
They became popular after use, for the NPCs in Halo.
They are a special kind of graph called a 'tree' (as the name would suggest). Its structure begins with a single
root and extends out from that, adding branches, nodes and finally end nodes.
There are 3 types of nodes.
Composite, Decorator and Leaf. A Composite node is one that can have "1 or more children nodes". A Decorator
node can have "only 1 child". A Leaf node is a "node at the end of a branch" and corresponds to an actual
action in a game. A group of leaf nodes at the end of a branch may represent a sequence of actions
or a set of actions to select from. How they operate depends on the type of composite node, of which there
are 2 types: Sequences and Selectors. When processed, a Sequence node will ensure that "each child is
visited in order". A Selected node however indicates that "only 1 of its children need to be visited" and
that child can be picked at random.
Let's take a look an example. Imagine you (or your NPC) are hungry.
You have a decision to make about what to eat.
This could be shown using a Selector node in which you can select from just 1 of the children.
You don't need all of them.
So this is when a Selector comes into play, when just 1 leaf node needs to be executed to perform
the behavior of 'eat'.
However, if your NPCs actions were at a lower level than this, such as in The Sims, then you could associate
the eating of each item with a well-defined process.
In this case, you'd need a Sequence node.
For simplicity, let's say such a sequence includes: Gather ingredients; Make pizza; Cook pizza and
Eat pizza.
Notice each leaf node corresponds to an actual action.
It's important that the leaf nodes can be performed with logic or with animation because these
are the actual things you will see the NPC doing on the screen.
They are also self-contained.
This means they can be performed and then the sequence abandoned. Nodes can also represent decisions
that when, in a sequence, can cause a sequence to continue or fail.
In this case, the Behavior Tree will execute completely only if 'Am I Hungry?'
and 'Do I have food?' is satisfied, at which the decision to 'eat burger' or 'eat pizza' will be satisfied.
Behavior Trees can be as simple or as complex as you need, including actions at all levels, joined by different
composite nodes.
So how does all this work in code?
Well, each leaf node must have a matching method that can be executed.
The Behavior Tree itself is a kind of scripting language sitting above your codebase.
You must write a bunch of self-contained modules in a C# file that can be called when a leaf node
is being run.
The method itself will have to report whether or not it has failed or succeeded.
This gives the leaf node a fail or success state, which is reported back to the running of the
Behavior Tree.
In the case of a sequence, this is essential, as if one leaf fails the entire sequence fails. In the remainder
of our investigation into Behavior Trees,
I'll have you follow along with more examples, as it will start to make more sense as you use them.
For this, we will be using the freely available 'Panda BT' plug-in, which allows scripting of Behavior Trees
in simple text files.