Working with behavior trees
The last pattern for this chapter is a little specific but no less useful than the others. Behavior trees define decision trees for AI brains to run. These trees define how information stored about the situation the AI thinks it is in will affect the actions it takes. Instead of compartmentalizing code into discrete blocks like other AI brain solutions, the behavior tree favors fragmentation with the aim of reuse. This flows back to the idea from the beginning of the chapter about the reuse of code for efficiency. A tree may look complex with many branches and leaves (yes, that is what we call the composites and tasks that make up a behavior tree), but on closer inspection, you will see that there are only a few building blocks and their arrangement is what creates the illusion of complex decision making.
The flow through a simple tree starts at the root and evaluates each node in the layer below in ascending order from 0 (order matters here) until it...