Artificial Intelligence Through Behavior Trees: An Explanation of Their Functionality
Behaviour Trees, a popular AI decision-making system, are a key component in the game development of Project Zomboid. These hierarchical nodes control the flow of decision-making for AI entities, providing a sequence of events and different execution paths to ensure the AI behaves as desired.
Core Components of Behaviour Trees
Behaviour Trees consist of various node types, each with a unique function. Some common nodes you might find or implement using JBT, the Java Behavior Tree library used in Project Zomboid, include:
- Sequence Node: Executes child nodes in order until one fails. Useful for tasks that require steps to complete sequentially, such as search then attack.
- Selector Node: Tries child nodes in order and succeeds on the first successful child. Used for choosing actions based on priority, like if an enemy is nearby, attack, else patrol.
- Action Nodes: Leaf nodes that perform an action like moving to a location, attacking, or interacting with objects.
- Condition Nodes: Leaf nodes that check if a game condition is true, like "Is enemy in sight?" or "Is health below 50%?"
- Decorator Nodes: Modify the behavior of child nodes, like repeating an action until a condition is met or inverting success/failure results.
Complex Behaviours in Project Zomboid
In Project Zomboid, Behaviour Trees are used to create complex and convincing AI behavior. For example, the behaviour checks if the item is already in the character's inventory, checks the contents of any bags or backpacks, checks the items in the building the character is currently residing in, iterates a list of crafting recipes, and recursively calls the behaviour for each of those items in turn.
If the item is not found in any of these methods, the behaviour will fail, and the NPC will add that item to a list of desired items to look out for during looting missions. Additionally, Behaviour Trees can be used to retrieve and try to enter every single door into a building, and return success if the character succeeded in getting in any of the doors, and it will return failure if they did not.
Leaf Nodes and Utility Nodes
Leaf nodes are the most powerful of node types, as they will be defined and implemented by your game to do the game-specific or character-specific tests or actions required to make your tree actually do useful stuff. Utility nodes, such as , , and , allow for managing a stack of objects within Behaviour Trees, which can be used to allow for iterating through a stack of objects.
Development and Modularization
Development is highly iterable, where you can start by forming a basic behavior, then create new branches to deal with alternate methods of achieving goals, with branches ordered by their desirability, allowing for the AI to have fallback tactics should a particular behavior fail. An integral type of Leaf node is one that calls another behavior tree, passing the existing tree's data context through to the called tree. This allows for modularization of the trees to create behavior trees that can be reused in countless places.
Failure as a Part of the Decision-Making Process
Failure in a Behaviour Tree is no longer a critical full stop, but a natural and expected part of the decision-making process. SetVariable and IsNull nodes allow for setting arbitrary variables throughout the behaviour tree in circumstances where the composites and decorators don't allow enough granularity to get information up the tree.
In summary, Behaviour Trees in Java game development include action, condition, sequence, selector, and decorator nodes managing movement, detection, attack, and decision logic. While examples typically follow general AI design patterns rather than framework-specific implementations, repositories or tutorials on Java behavior trees could provide exact node implementations for those seeking concrete Java implementations.
- Artificial intelligence in Project Zomboid utilizes Behaviour Trees to create complex and intricate decision-making processes, employing nodes such as Sequence, Selector, Action, Condition, and Decorator for managing actions, detection, attacks, and logic.
- To attain game-specific and character-specific actions, Leaf nodes, the most powerful node type, are defined and implemented within the game, while Utility nodes serve to manage stacks of objects within Behaviour Trees, enabling iteration through them.