Behavior trees

A Npc Think tank is called a forest.

A behavior tree is made up of hierarchical nodes that control the flow of decision making of an AI entity. The branches are various types of utility nodes that control what they AI choose to do and in what order. The leaves are the actual commands that control the AI entity. To update a tree start at the at root node, visit each node until one is found running.


Node

A node is the basic building block in a behavior tree. There are four main types of nodes; actions, conditions, composites and decorators. The composite nodes are the ones that can have children and they are the ones that turn the whole thing into a tree. A node have an Update function that does it's work and to flag the result back to it's parent node it sets a status code on itself. A parent node can check the status and select what to do next depending on it's own type.

  • Running: The node is currently running.
  • Success: The node is done and succeed.
  • Failure: The node is done and failed.

Each node can also take parameters that depend on the node. For example a PlayInRange can take a distance parameter.

Tree

The tree part is made up by creating the nodes and linking them as children to each other. One easy way is to use an XML format where one can specify a node, it's parameters and children. It also possible to reuse trees by including a tree into another with an Include node.

Idle.bt

BT that runs idle on a agent. If phone is charged it surfs the web, if not raining it smokes and if nothing else is possible it stands around and yawns.

<BTree>
    <Selector>
        <Sequence>
            <selfstate state="iphone_charged" value="true">
            <idle name="surfweb">
        </Sequence>

        <Sequence>
            <worldstate state="raining" value="false">
            <idle name="smoke">
        </Sequence>

        <idle name="yawn">
    <Sequence>
</BTree>

Guard.bt

Guard BT that hunt down target and attacks if it has one and if not it use the idle.bt. 

<BTree>
    <Selector>
        <Sequence>
            <HaveTarget/>

            <Sequence>
                <IsCloseTarget>
                <FaceTarget/>
                <AttackTarget name="stab" />
            </Sequence>

            <Sequence>
                <MoveToTarget />
            </Sequence>
        </<Sequence>

        <Include name="idle.bt />
    </Selector>

</BTree>


Reference