Behavior Tree

http://bt.multi-threading.com/concept/bt/#Introduction

Table of contents

1. Introduction
2. Terminology Definition
3. Behavior Tree Components
3.1 Tree Root Node
3.2 Composite Node
3.2.1 Selector Node
3.2.2 Sequence Node
3.2.3 Parallel Node
3.3 Decorator Node
3.4 Leaf Node
3.4.1 Condition Node
3.4.2 Action Node
4. Behavior Tree Operations
4.1 Execute
4.2 Pause
4.3 Resume
4.4 Stop
4.5 Reset
4.6 Load
4.7 Destroy
Behavior Tree Properties
5.1 Tree Status
5.2 Tree Repeat
5.3 Tree Loops
5.4 Tree Accumulated Loops
5.5 Tree Property Wrapper

1. Introduction
Behavior Tree is a decision making system used in AI games to build the abilities of characters to decide what to do. Behavior Tree has a lot in common with Hierarchy State Machine where the main building block is a state, and the change of states is triggered by events (event-driven), but instead of states, the main building block of a Behavior Tree is a task. Tasks are composed into sub-trees to represent complex actions. In turn, these complex actions can again be composed into higher level behaviors. Since all the tasks have a common interface and are largely self-contained, they can be easily built up into hierarchies, i.e., Behavior Trees, without having to worry about the details of how each sub-task in the hierarchy is implemented. We can consider that the root of the tree performs the top level task, i.e., the overall behavior. This top level task can be decomposed recursively into a hierarchy of sub-tasks (sub-trees) that accomplish simpler behaviors (tasks) that make up the whole. Since the whole tree is registered in a lookup table, the behavior logic can be accessed through out all the nodes in a data-driven way.

2. Terminology Definition
Based on the implementation of the behavior tree system, some terminologies were defined to explain the synchronization and how the system works.

Term Definition
Sub-treesub-tree refers to two cases.
1. The Tree Root Node and all the nodes below the Tree Root Node but no deeper than any Parallel Nodes. (A.k.a. Top Sub-tree.)
2. A child node of a Parallel Node and all the nodes below that child node but no deeper than any Parallel Nodes.
Execute A parent node executes one of its child node. The execution traversal goes down to one of its child node. The child node that is going to be executed will invoke its OnExecute operation.
Report A child node reports a node status to its parent node. The execution traversal goes up to its parent node. The parent node that is going to be reported will invoke its OnReport operation with a node status passed from its child node.
Notify Three cases.
1. When the OnExecute operation of a Decorator Node or the OnStep operation of a Condition Node returned an Executing, it notifies its closest parent Parallel Node if it has one and all the same way up to the Tree Root Node, otherwise, notifies the Tree Root Node directly, to request one more execution. The execution traversal will be suspended at the node that returned an Executing.
2. When an executing sub-tree was interrupted, it notifies its closest parent Parallel Node if it has one and all the same way up to the Tree Root Node, otherwise, notifies the Tree Root Node directly, to indicate that this executing sub-tree has been interrupted.
3. When an executing sub-tree was terminated, it notifies its closest parent Parallel Node that this executing sub-tree has been terminated.
The Tree Root Node that was notified will invoke its OnNotify member function.
Interrupt Interrupt refers to one of these tree operations: PauseStopReset, or Destroy the whole tree. All executing sub-trees will beinterrupted when pausing, stopping, resetting, or destroying the whole tree. Then each executing sub-tree that was interrupted willnotify interruption to its closest parent Parallel Node if it has one and all the same way up to the Tree Root Node, otherwise, notifythe Tree Root Node directly, from the interrupted node, or from the interrupted Parallel Node if the executing sub-tree has anexecuting (non-)determined Parallel Node.
Terminate When the first one of the executing child sub-trees reported a Success to an executing non-determined Parallel Node with the Sequence policy, or reported a Failure to an executing non-determined Parallel Node with the Selector policy in an executing sub-tree, the Parallel Node will terminate all its remaining executing child sub-trees. The executing sub-tree that was terminated willnotify termination to its closest parent Parallel Node from the terminated node, or from the terminated Parallel Node if the executing sub-tree has an executing (non-)determined Parallel Node.
Executing Sub-tree An executing sub-tree refers to a sub-tree that was executed, but
1. hasn’t reported a result node status to, or
2. hasn’t notified an execution request from a Decorator Node or a Condition Node in it that returned an Executing, or
3. hasn’t notified interruption, or
4. hasn’t notified termination
to the sub-tree‘s closest parent Parallel Node if it has one, otherwise, the Tree Root Node, or
5. has an executing (non-)determined Parallel Node in it that hasn’t turned to a non-executing Parallel Node.
Reported Sub-treereported sub-tree refers to an executing sub-tree that finished a complete traversal to report a result node status to the closest parent Parallel Node if it has one, otherwise, the Tree Root Node.
Notified execution request Sub-treenotified execution request sub-tree refers to an executing sub-tree that notified execution request from a Decorator Node or a Condition Node that returned an Executing, in this executing sub-tree, or in an executing child sub-tree of an executing (non-)determined Parallel Node, to the closest parent Parallel Node if it has one, otherwise, the Tree Root Node.
Notified interruption Sub-treenotified interruption sub-tree refers to an executing sub-tree that was interrupted, and then notified interruption from the interrupted node to the closest parent Parallel Node if it has one, otherwise, the Tree Root Node.
Notified termination Sub-treenotified termination sub-tree refers to an executing child sub-tree of a Parallel Node that was terminated, and then notified termination from the terminated node to the closest parent Parallel Node.
Reset Sub-treereset sub-tree refers to a sub-tree whose parent tree was created or resetbut whose closest parent Parallel Node if it has one, otherwise, the Tree Root Node, hasn’t been executed yet.
Non-executing Sub-treenon-executing sub-tree refers to one of these cases.
1. A reported sub-tree.
2. A notified execution request sub-tree that was requested from a Decorator Node or a Condition Node in it that returned an Executing.
3. A notified interruption sub-tree.
4. A notified termination sub-tree.
5. A notified execution request sub-tree that was requested from an executing (non-)determined Parallel Node, where the executing (non-)determined Parallel Node has also turned to a non-executing Parallel Node.
6. A reset sub-tree.

3. Behavior Tree Components
There are several kinds of nodes in our behavior tree system: Tree Root Node, Composite Node (Selector Node, Sequence Node, Parallel Node), Decorator Node, and Leaf Node (Condition Node, Action Node). The concrete node types that can be instantiated  in our behavior tree system are Tree Root Node, Selector Node, Sequence Node, Parallel Node, Decorator Node, Condition Node, and Action Node. The concrete node types whose behaviors can be customized in Unity Editor through scripting, i.e., whose member functions can be overridden in customized node scripts, are Tree Root Node, Decorator Node, Condition Node, and Action Node. These node types are all classes inherited from the UnityEngine.MonoBehaviour class that can can be updated by the Unity engine, as well as being looked up and invoked on their node operations when executing the behavior trees.

Abstract Node Type Definition Concrete Node Type
Tree Root Node A tree root node has no parent node and exactly one child node. Tree Root Node
Composite Node A composite node has exactly one parent node and at least one child node. Selector NodeSequence NodeParallel Node
Decorator Node A decorator node has exactly one parent node and exactly one child node. Decorator Node
Leaf Node A leaf node has exactly one parent node and no child node. Condition NodeAction Node

3.1. Tree Root NodeTreeRootNode
The tree root node can be considered as the top level task that the whole tree accomplishes. When weexecute a reset tree or a reported tree, the execution thread always starts traversal from the Tree Root Node, and the OnExecute operation of the Tree Root Node always gets invoked accordingly. When the execution thread completes a tree traversal by reporting a node status of Success or Failure from the child node of the Tree Root Node, the OnReport operation of the Tree Root Node always gets invoked and passed a result node status of Success or Failure from its child node as a parameter. When the OnExecute operation of a Decorator Node or the OnStep operation of a Condition Node in the tree returned an Executing to notify the Tree Root Node to request one more execution from the tree, or when the executing sub-tree that was interrupted in the last place notifies the Tree Root Node that the executing tree was interrupted, the OnNotify operation of the Tree Root Node always gets invoked.

Member Function Definition
OnExecute A node operation that is invoked when executing the tree.
OnReport A node operation that is invoked when the Tree Root Node was reported a node status of Success or Failure from its child node.
OnNotify A node operation that is invoked when the Tree Root Node was notified by a Decorator Node or a Condition Node whose OnExecute or OnStep operation returned an Executing, or was notified by an executing sub-tree that was interrupted in the last place.
OnConstruct A node operation that is invoked when loading the tree.
OnDestruct A node operation that is invoked when destroying the tree.
OnPause A node operation that is invoked when pausing the tree.
OnStop A node operation that is invoked when stopping the tree.
OnReset A node operation that is invoked when resetting the tree.

3.2. Composite Node
A Composite Node has at least one child node. There are three types of it: Selector Node, Sequence Node, and Parallel Node.

3.2.1. Selector NodeSelectorNode

Selectors are used to choose the first of a set of possible actions that is successful.
~ Artificial Intelligence For Games, 2nd Edition

Executing a Selector Node

A Selector Node always executes its child nodes starting from the leftmost one. It reports a Success to its parent node when a child node reported a Success to it. It continues to execute the next right child node when a child node reported a Failure to it. It reports a Failure to its parent node when all its child nodes reported a Failure to itself.

3.2.2. Sequence NodeSequenceNode

Sequences represent a series of tasks that need to be undertaken.
~ Artificial Intelligence For Games, 2nd Edition

Executing a Sequence Node

A Sequence Node always executes its child nodes starting from the leftmost one. It reports a Failure to its parent node when a child node reported a Failure to it. It continues to execute the next right child node when a child node reported a Success to it. It reports a Success to its parent node when all its child nodes reported a Success to itself.

3.2.3. Parallel NodeParallelNode

The Parallel task acts in a similar way to the Sequence task. It has a set of child tasks, and it runs them until one of them fails. At that point, the Parallel task as a whole fails. If all of the child tasks complete successfully, the Parallel task returns with success. In this way, it is identical to the Sequence task and its non-deterministic variations.
The difference is the way it runs those tasks. Rather than running them one at a time, it runs them all simultaneously. We can think of it as creating a bunch of new threads, one per child, and setting the child tasks off together.
When one of the child tasks ends in failure, Parallel will terminate all of the other child threads that are still running. Just unilaterally terminating the threads could cause problems, leaving the game inconsistent or failing to free resources (such as acquired semaphores). The termination procedure is usually implemented as a request rather than a direct termination of the thread. In order for this to work, all the tasks in the behavior tree also need to be able to receive a termination request and clean up after themselves accordingly.
~ Artificial Intelligence For Games, 2nd Edition

Based on the implementation of the Parallel Node, some terminologies were defined here to explain the synchronization and how the Parallel Node works in the behavior tree system.

Term Definition
Executing Non-determined Parallel Node An executing non-determined Parallel Node refers to a Parallel Node that was executed, but whose node status hasn’t been determined, and whose executing parent sub-tree hasn’t been interrupted nor terminated, and that has at least one executing child sub-tree that hasn’t turned to a non-executing sub-tree.
Executing Determined Parallel Node An executing determined Parallel Node refers to an executing non-determined Parallel Node whose node status was determined, and that started to terminate all its remaining executing child sub-trees if necessary but hasn’t reported a node status to its parent node.
Reported Parallel Nodereported Parallel Node refers to an executing (non-)determined Parallel Node that reported a node status to its parent node.
Interrupted Parallel Node An interrupted Parallel Node refers to an executing (non-)determined Parallel Node whose executing parent sub-treewas interruptedand that started to interrupt all its remaining executing child sub-trees, but whose executing parent sub-tree hasn’tnotified the closest parent Parallel Node if it has one and all the same way up to the Tree Root Node, otherwise, notify the Tree Root Node directly, of interruption from the interrupted Parallel Node when all of the child sub-trees of the interrupted Parallel Node had turned to non-executing child sub-trees.
Terminated Parallel Nodeterminated Parallel Node refers to an executing (non-)determined Parallel Node whose executing parent sub-treewas terminatedand that started to terminate all its remaining executing child sub-trees, but whose executing parent sub-treehasn’t notified the closest parent Parallel Node of termination from the terminated Parallel Node when all of the child sub-trees of the terminated Parallel Node had turned to non-executing child sub-trees.
Reset Parallel Nodereset Parallel Node refers to a Parallel Node whose parent tree was created or reset, but the Parallel Node itself hasn’t beenexecuted yet.
Non-executing Parallel Nodenon-executing Parallel Node refers to one of these cases.
1. A reported Parallel Node.
2. An interrupted Parallel Node whose executing parent sub-tree has turned to notified interruption sub-tree.
3. A terminated Parallel Node whose executing parent sub-tree has turned to notified termination sub-tree.
4. An executing (non-)determined Parallel Node that all of its child sub-trees have turned to non-executing sub-trees.
5. A reset Parallel Node.

Executing a Parallel Node

When the execution thread of a sub-tree reaches a Parallel Node, the Parallel Node executes all its child sub-trees concurrently in individual threads, blocks the traversal of the execution thread of the parent sub-tree of the Parallel Node, and waits for all the Parallel Node’s child sub-tree threads to complete their executions for synchronization. The execution order of each child sub-tree is arbitrary. Each executing child sub-tree of the Parallel Node can have the the following reactions at any time in an arbitrary order during the execution of the Parallel Node.

(1). report a node status of Success or Failure to the executing (non-)determined Parallel Node when the sub-tree execution completed a traversal

Based on the Parallel Node Policy, once an executing child sub-tree reported a Failure node status to an executing non-determined Parallel Nodewith a Sequence Policy or reported a Success node status to an executing non-determined Parallel Node with a Selector Policy, the executing non-determined Parallel Node‘s status will be determined immediately and has to terminate all the remaining executing child sub-trees. Otherwise, theexecuting non-determined Parallel Node‘s status can be determined only when all the executing child sub-trees have reported a Success to theexecuting non-determined Parallel Node with a Sequence Policy or reported a Failure to the executing non-determined Parallel Node with a Selector Policy. Once the executing non-determined Parallel Node‘s status has been determined, and all its executing child sub-trees have either reported a node status or notified of termination, the executing determined Parallel Node will report its node status of either Success or Failure to its parent node.

(2). notify the executing determined Parallel Node that this executing child sub-tree has been terminated

After all the executing child sub-trees of an executing determined Parallel Node notified of termination, the executing determined Parallel Node willreport its node status to its parent node.

(3). notify the Parallel Node to request one more execution from a Decorator Node or a Condition Node that returned an Executing, in thisexecuting sub-tree or in an executing child sub-tree of an executing (non-)determined Parallel Node

When an executing determined Parallel Node, an interrupted Parallel Node, or a terminated Parallel Node was notified of execution request, the execution request will be ignored. Once an executing non-determined Parallel Node was notified of execution request by the first executing child sub-tree, the executing non-determined Parallel Node will notify its closest parent Parallel Node if it has one and all the same way up to the Tree Root Node, otherwise, notify the Tree Root Node directly, to request one more execution. The executing non-determined Parallel Node won’t notify again if the executing non-determined Parallel Node was notified by other executing child sub-trees for more execution requests before the executing non-determined Parallel Node gets executed by the new execution. When the executing non-determined Parallel Node gets executed again, all its notified execution request child sub-trees will get executed. When an executing determined Parallel Node gets executed again, the executing determined Parallel Node won’t execute its notified execution request child sub-trees nor notified termination sub-trees but will directly report its result node status to its parent node.

(4). notify the interrupted Parallel Node that this executing child sub-tree was interrupted when the executing parent sub-tree of the executing (non-)determined Parallel Node had been interrupted

All the executing parent sub-trees of executing (non-)determined Parallel Nodes will get interrupted when the whole tree was pausedstoppedreset, or destroyed. The executing (non-)determined Parallel Node will be interrupted by its interrupted parent sub-tree. An interrupted Parallel Node will interrupt all its remaining executing child sub-trees. After all the executing child sub-trees notified the interrupted Parallel Node of interruption, the executing parent sub-tree of the interrupted Parallel Node will notify its closest parent Parallel Node if it has one, otherwise, notify the Tree Root Node directly, that the executing sub-tree was interrupted.

(5). notify the terminated Parallel Node that this executing child sub-tree was terminated when the executing parent sub-tree of the executing (non-)determined Parallel Node had been terminated

The executing (non-)determined Parallel Node will be terminated by its terminated parent sub-tree. A terminated Parallel Node will terminate all itsexecuting child sub-trees. After all the executing child sub-trees of the terminated Parallel Node notified the terminated Parallel Node of termination, the executing parent sub-tree of the terminated Parallel Node will notify its closest parent Parallel Node that the executing sub-tree was terminated.

Parallel Node Policy

Parallel Node Policy Definition
Sequence The Parallel Node will report a Success to its parent node only when all its executing child sub-trees reported a Success to it. As long as there is at least one executing child sub-tree that reported a Failure to it, the Parallel Node’s status will be determined as Failure. Once a Parallel Node’s status was determined as Failure, it has to terminate all the remaining executing child sub-trees. After all the Parallel Node’s executing child sub-trees have either reported a node status to it or notified it of termination, the Parallel Node will be ready to report a Failure to its parent node.
Selector The Parallel Node will report a Failure to its parent node only when all its executing child sub-trees reported a Failure to it. As long as there is at least one executing child sub-tree that reported a Success to it, the Parallel Node’s status will be determined as Success. Once a Parallel Node’s status was determined as Success, it has to terminate all the remaining executing child sub-trees. After all the Parallel Node’s executing child sub-trees have either reported a node status to it or notified it of termination, the Parallel Node will be ready to report a Success to its parent node.

3.3. Decorator NodeDecoratorNode

Decorator is a type of task that has one single child task and modifies its behavior in some way. We could think of it like a composite task with a single child. One simple and very common category of Decorator makes a decision whether to allow their child behavior to run or not (they are sometimes called “filters”). If they allow the child behavior to run, then whatever status code it returns is used as the result of the filter. If they don’t allow the child behavior to run, then they normally return in Failure, so a Selector can choose an alternative action.
~ Artificial Intelligence For Games, 2nd Edition

Executing a Decorator Node

In our behavior tree system, when the parent node of a Decorator Node executes it, the OnExecute operation of the Decorator Node will be invoked; when the child node of a Decorator Node reports a node status to it, the OnReport operation of the Decorator Node will be invoked with a result node status from its child node. In order to enable the capability of a Decorator Node to modify the behavior of its child task, the behaviors of OnExecute and OnReport operations can be changed based on the returned node status.

(1). For the OnExecute operation, if a Success was returned, the Decorator Node should continue to execute its child node; if a Failure was returned, the Decorator Node should report to itself with a Failure node status by invoking the Decorator Node’s OnReport operation with a Failure node status; if an Executing was returned, the Decorator Node should notify the closest parent Parallel Node if it has one and all the same way up to the Tree Root Node, otherwise, notify the Tree Root Node directly, to request one more execution.

(2). For the OnReport operation, if a Success or a Failure was returned, the Decorator Node should continue to report to its parent node with the result node status; if an Executing was returned, the Decorator Node should continue to execute itself by invoking the Decorator Node’s OnExecute operation.

In this way, the child task behavior can be completely customized by the Decorator Node. There are several standard filters that are useful which can be realized with a Decorator Node. For example, we can use a Decorator Node to limit the number of times a task can be run, and we can also keep running a task in a loop until it fails.

Decorator Node Operation and Policy

Based on the Decorator Node Policy, the OnExecute and OnReport operations can be invoked for only once and then executereport, or notify, based on the operation and result node status,  immediately after performing the node task regardless of whatever the result node status was returned. They can also be invoked in every frame to check and wait until a particular node status is returned. If the executing parent sub-tree of the Decorator Node was interrupted or terminated where the Decorator Node is still under execution in every frame to wait for a particular node status, the execution will be suspended by returning the target node status of the Decorator Node Policy anyway. The Decorator Node Policy can be set in the Decorator Node’s Attribute Table Window in BtStudio.

Member Function Definition
OnExecute A node operation that is invoked when the Decorator Node was executed by its parent node, or when the OnReport operation of the Decorator Node returned an Executing. If a Success was returned, the Decorator Node will continue to execute its child node. If a Failure was returned, the Decorator Node will report a Failure to itself by invoking its OnReport operation with a Failure node status. If an Executing was returned, the Decorator Node will notify the closest parent Parallel Node if it has one and all the same way up to the Tree Root Node, otherwise, notify the Tree Root Node directly, to request one more execution.
OnReport A node operation that is invoked when the Decorator Node was reported a node status of Success or Failure from its child node, or when the OnExecute operation of the Decorator Node returned a Failure. If a Success or a Failure was returned, the Decorator Node will continue to report the result node status to its parent node. If an Executing was returned, the Decorator Node will continue to execute itself by invoking its OnExecute operation.
OnConstruct A node operation that is invoked when loading the tree.
OnDestruct A node operation that is invoked when destroying the tree.
OnPause A node operation that is invoked when pausing the tree.
OnStop A node operation that is invoked when stopping the tree.
OnReset A node operation that is invoked when resetting the tree.
Decorator Node Policy Definition
Immediately, Immediately Both the OnExecute and the OnReport operations will always be invoked for only once no matter whatever the returned status is.
Immediately, UntilSuccess The OnExecute operation will always be invoked for only once no matter whatever the returned status is. The OnReport operation will always be invoked in every frame until a Success was returned. If the executing parent sub-tree was interrupted or terminated while a Success hasn’t been returned, the OnReport operation will be dropped by automatically returning a Success.
Immediately, UntilFailure The OnExecute operation will always be invoked for only once no matter whatever the returned status is. The OnReport operation will always be invoked in every frame until a Failure was returned. If the executing parent sub-tree was interrupted or terminated while a Failure hasn’t been returned, the OnReport operation will be dropped by automatically returning a Failure.
Immediately, UntilExecuting The OnExecute operation will always be invoked for only once no matter whatever the returned status is. The OnReport operation will always be invoked in every frame until an Executing was returned. If the executing parent sub-tree was interrupted or terminated while an Executing hasn’t been returned, the OnReport operation will be dropped by automatically returning an Executing.
UntilSuccess, Immediately The OnExecute operation will always be invoked in every frame until a Success was returned. If the executing parent sub-tree was interrupted or terminated while a Success hasn’t been returned, the OnExecute operation will be dropped by automatically returning a Success. The OnReport operation will always be invoked for only once no matter whatever the returned status is.
UntilSuccess, UntilSuccess Both the OnExecute and the OnReport operations will always be invoked in every frame until a Success was returned. If the executing parent sub-tree was interrupted or terminated while a Success hasn’t been returned, both the OnExecute and OnReport operations will be dropped by automatically returning a Success.
UntilSuccess, UntilFailure The OnExecute operation will always be invoked in every frame until a Success was returned. If the executing parent sub-tree was interrupted or terminated while a Success hasn’t been returned, the OnExecute operation will be dropped by automatically returning a Success. The OnReport operation will always be invoked in every frame until a Failure was returned. If the executing parent sub-tree was interrupted or terminated while a Failure hasn’t been returned, the OnReport operation will be dropped by automatically returning a Failure.
UntilSuccess, UntilExecuting The OnExecute operation will always be invoked in every frame until a Success was returned. If the executing parent sub-tree was interrupted or terminated while a Success hasn’t been returned, the OnExecute operation will be dropped by automatically returning a Success. The OnReport operation will be invoked in every frame until an Executing was returned. If the executing parent sub-tree was interrupted or terminated while an Executing hasn’t been returned, the OnReport operation will be dropped by automatically returning an Executing.
UntilFailure, Immediately The OnExecute operation will always be invoked in every frame until a Failure was returned. If the executing parent sub-tree was interrupted or terminated while a Failure hasn’t been returned, the OnExecute operation will be dropped by automatically returning a Failure. The OnReport operation will always be invoked for only once no matter whatever the returned status is.
UntilFailure, UntilSuccess The OnExecute operation will always be invoked in every frame until a Failure was returned. If the executing parent sub-tree was interrupted or terminated while a Failure hasn’t been returned, the OnExecute operation will be dropped by automatically returning a Failure. The OnReport operation will always be invoked in every frame until a Success was returned. If the executing parent sub-tree was interrupted or terminated while a Success hasn’t been returned, the OnReport operation will be dropped by automatically returning a Success.
UntilFailure, UntilFailure Both the OnExecute and the OnReport operations will always be invoked in every frame until a Failure was returned. If the executing parent sub-tree was interrupted or terminated while a Failure hasn’t been returned, both the OnExecute and OnReport operations will be dropped by automatically returning a Failure.
UntilFailure, UntilExecuting The OnExecute operation will always be invoked in every frame until a Failure was returned. If the executing parent sub-tree was interrupted or terminated while a Failure hasn’t been returned, the OnExecute operation will be dropped by automatically returning a Failure. The OnReport operation will always be invoked in every frame until an Executing was returned. If the executing parent sub-tree was interrupted or terminated while an Executing hasn’t been returned, the OnReport operation will be dropped by automatically returning an Executing.
UntilExecuting, Immediately The OnExecute operation will always be invoked in every frame until an Executing was returned. If the executing parent sub-tree was interrupted or terminated while an Executing hasn’t been returned, the OnExecute operation will be dropped by automatically returning an Executing. The OnReport operation will always be invoked for only once no matter whatever the returned status is.
UntilExecuting, UntilSuccess The OnExecute operation will always be invoked in every frame until an Executing was returned. If the executing parent sub-tree was interrupted or terminated while an Executing hasn’t been returned, the OnExecute operation will be dropped by automatically returning an Executing. The OnReport operation will always be invoked in every frame until a Success was returned. If the executing parent sub-tree was interrupted or terminated while a Success hasn’t been returned, the OnReport operation will be dropped by automatically returning a Success.
UntilExecuting, UntilFailure The OnExecute operation will always be invoked in every frame until an Executing was returned. If the executing parent sub-tree was interrupted or terminated while an Executing hasn’t been returned, the OnExecute operation will be dropped by automatically returning an Executing. The OnReport operation will always be invoked in every frame until a Failure was returned. If the executing parent sub-tree was interrupted or terminated while a Failure hasn’t been returned, the OnReport operation will be dropped by automatically returning a Failure.
UntilExecuting, UntilExecuting Both the OnExecute and the OnReport operations will always be invoked in every frame until an Executing was returned. If the executing parent sub-tree was interrupted or terminated while an Executing hasn’t been returned, both the OnExecute and OnReport operations will be dropped by automatically returning an Executing.

3.4. Leaf Node
A Leaf Node has no child node under it. There are two types of a Leaf Node: Condition Node and Action Node. A Condition Node is used to check some conditions for decision making and branching in the behavior tree execution traversal. As a consequence, a Condition Node is able to report a node status of either Success or Failure to its parent node to indicate the result of performing the node task. On the other hand, an Action Node is used to perform some certain task where the result is not important so that the reported node status won’t affect branching in the behavior tree execution traversal.

3.4.1. Condition NodeConditionNode

Conditions test some property of the game.
~ Artificial Intelligence For Games, 2nd Edition

Executing a Condition Node

In our behavior tree system, when the parent node of a Condition Node executes it, the OnStep operation of the Condition Node will be invoked. When the OnStep operation returns a Success or a Failure, the Condition Node will then report the result node status to its parent node; when the OnStep operation returns an Executing, the Condition Node will then notify the closest parent Parallel Node if it has one and all the same way up to the Tree Root Node, otherwise, notify the Tree Root Node directly, to request one more execution and get executed again. Based on the Condition Node Policy, the OnStep operation can be invoked for only once and then report or notify, based on the returned node status, immediately after performing the node task regardless of whatever the result node status was returned. The OnStep operation can also be invoked in every frame to check and wait until a certain node status is returned. If the executing parent sub-tree of the Condition Node was interrupted or terminated where the Condition Node is still under execution in every frame to wait for a particular node status, the execution will be suspended by returning the target node status of the Condition Node Policy anyway. The Condition Node Policy can be set in the Condition Node’s Attribute Table Window in BtStudio.

Member Function Definition
OnStep A node operation that is invoked when the Condition Node was executed by its parent node. If a Success or a Failure was returned, the Condition Node will then report the result node status to its parent node. If an Executing was returned, the Condition Node will then notify the closest parent Parallel Node if it has one and all the same way up to the Tree Root Node, otherwise, notify the Tree Root Node directly, to request one more execution.
OnConstruct A node operation that is invoked when loading the tree.
OnDestruct A node operation that is invoked when destroying the tree.
OnPause A node operation that is invoked when pausing the tree.
OnStop A node operation that is invoked when stopping the tree.
OnReset A node operation that is invoked when resetting the tree.
Condition Node Policy Definition
ReturnImmediately The OnStep operation will always be invoked for only once no matter whatever the node status was returned.
NotReturnUntilSuccess The OnStep operation will always be invoked in every frame until a Success was returned. If the executing parent sub-tree was interrupted or terminated while a Success hasn’t been returned, the OnStep operation will be dropped by automatically returning a Success.
NotReturnUntilFailure The OnStep operation will always be invoked in every frame until a Failure was returned. If the executing parent sub-tree was interrupted or terminated while a Failure hasn’t been returned, the OnStep operation will be dropped by automatically returning a Failure.
NotReturnUntilExecuting The OnStep operation will always be invoked in every frame until an Executing was returned. If the executing parent sub-tree was interrupted or terminated while an Executing hasn’t been returned, the OnStep operation will be dropped by automatically returning an Executing.

3.4.2. Action NodeActionNode

Actions alter the state of the game.
~ Artificial Intelligence For Games, 2nd Edition

Executing an Action Node

In our behavior tree system, when the parent node of an Action Node executes it, the OnStep operation of the Action Node will be invoked. The OnStep operation is always invoked for only once and always returns a fixed node status immediately after performing the node task to report to its parent node. Based on the Action Node Policy, the OnStep operation can always return either a Success or a Failure. The Action Node Policy can be set in the Action Node’s Attribute Table Window in BtStudio.

Member Function Definition
OnStep A node operation that is invoked when the Action Node was executed by its parent node. The OnStep operation of the Action Node always returns a fixed node status of either Success or Failure immediately after performing the node task to report to its parent node.
OnConstruct A node operation that is invoked when loading the tree.
OnDestruct A node operation that is invoked when destroying the tree.
OnPause A node operation that is invoked when pausing the tree.
OnStop A node operation that is invoked when stopping the tree.
OnReset A node operation that is invoked when resetting the tree.
Action Node Policy Definition
ReturnSuccessImmediately The OnStep operation will always be invoked for only once and always return a Success.
ReturnFailureImmediately The OnStep operation will always be invoked for only once and always return a Failure.

4. Behavior Tree Operations

Behavior Tree operations can be invoked through BtEngine’s group tree operations at engine level or forest level, or BtEngine’s single tree operations.

Based on the implementation, some terminologies were defined to explain the synchronization and how the system works.

Term Definition
Executing Tree An executing tree refers to a tree whose top sub-tree is an executing sub-tree.
Notified Execution Request Treenotified execution request tree refers to a tree whose top sub-tree is a notified execution request sub-tree.
Notified Interruption Treenotified interruption tree refers to a tree whose top sub-tree is a notified interruption sub-tree.
Reported Treereported tree refers to a tree whose top sub-tree is a reported sub-tree.
Reset Treereset tree refers to a tree whose top sub-tree is a reset sub-tree.
Non-executing Treenon-executing tree refers to a tree whose top sub-tree is a non-executing sub-tree.

4.1 Execute

Execute the tree. The BtEngine always executes a non-executing tree and increments the accumulated loop number accordingly. The execution traversal will start from the Tree Root Node when executing a reset tree or a reported tree. It will start from the node(s) that returned an Executing when executing a notified execution request tree. It will start from the node(s) that were interrupted when (resuming) executing a notified interruption tree.

4.2 Pause

Pause the tree. Pausing an executing tree will interrupt all the remaining executing sub-tree(s)After all the remaining executing sub-tree(s) turned tonotified interruption sub-tree(s), the executing tree will turn to a notified interruption tree. Pausing a non-executing tree won’t interrupt anything. All node’s OnPause operations will then be invoked in the depth-first traversal order.

4.3 Resume

Resume executing the tree. The execution traversal will start from the interrupted node(s).

4.4 Stop

Stop the tree. Stopping an executing tree will interrupt all the remaining executing sub-trees. After all the remaining executing sub-tree(s) turned to notified interruption sub-tree(s), the executing tree will turn to a notified interruption tree. Stopping a non-executing tree won’t interrupt anything. All node’s OnStop operations will then be invoked in the depth-first traversal order.

4.5 Reset

Reset the tree. Resetting an executing tree will interrupt all the remaining executing sub-trees. After all the remaining executing sub-tree(s) turned to notified interruption sub-tree(s), the executing tree will turn to a notified interruption tree. Resetting a non-executing tree won’t interrupt anything. All node’s OnReset operations will then be invoked in the depth-first traversal order. The notified interruption tree will then be reset to a reset tree.

4.6 Load

Instantiate the tree(s) from the corresponding forest xml and tree xml files. Existing trees won’t be instantiated again.

4.7 Destroy

Destroy the tree instance(s).

5. Behavior Tree Properties

5.1 Tree Status

Based on the tree operations, the behavior tree can have these statuses: Unconstructed, Executing, Paused, Stopped, Reset.

Tree Status Definition
Unconstructed The tree hasn’t been loaded yet, or the loaded tree was destroyed.
Executing The loaded tree was executed, and hasn’t completed the target loop number or hasn’t been pausedstopped, nor reset yet.
Paused The loaded tree was paused.
Stopped The loaded tree was stopped, or the loaded tree was executed and has completed the target loop number.
Reset The loaded tree hasn’t been executed yet, or the loaded tree was reset.

5.2 Tree Repeat

Two repeat options are available when executing a tree: once and loop.

5.3 Tree Loops

A target loop number can be specified when executing a tree with a repeat option of loop.

5.4 Tree Accumulated Loops

The accumulated loop number of tree executions.

5.5 Tree Property Wrapper

The properties of a tree instance can be queried from a tree property wrapper. The tree property wrapper is a wrapper that contains the properties from the tree level down to the node level. The detail level of the tree property wrapper can also be specified when acquiring the property wrapper from the BtEngine’s tree property wrapper API. The tree property wrapper enable users to iterate through all properties of those node instances in the underlying tree.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值