Skip to content

AI Behavior Trees: Their Functioning Mechanisms

Guideline on Building and Optimizing Behavior Trees: Examples, Descriptions, and Tips for Creating Effective and Impressive Trees.

AI Functioning through Behavior Trees: An Explanation
AI Functioning through Behavior Trees: An Explanation

AI Behavior Trees: Their Functioning Mechanisms

Project Zomboid, the open-world, zombie-survival sandbox game, uses a behavior tree system to manage AI for both zombies and survivors. Although the game's base code is not open-sourced, and its behavior tree node system is not fully documented, we can infer logical examples of behavior tree (BT) nodes based on in-game behaviour, modding, and broader game AI practices.

## Common Behavior Tree Node Types in Project Zomboid

The behavior tree in Project Zomboid comprises several types of nodes, including Selector Nodes, Sequence Nodes, Leaf Nodes (Actions and Conditions), Decorator Nodes, and Composite Nodes.

### Selector Nodes These nodes run child nodes in sequence until one succeeds. For example, a zombie’s high-level selector might first check for a nearby sound, then visible players, and finally wander if nothing else is detected.

### Sequence Nodes These execute child nodes in order, stopping if any fail. For instance, a zombie might “see player → move toward player → attack player” as a sequence.

### Leaf Nodes (Actions and Conditions) These are the end-point actions or checks. Examples include: - **MoveToSound:** The zombie moves toward the last heard noise. - **AttackPlayer:** The zombie attacks a detected player in melee range. - **FindPath:** The zombie calculates a path to its target. - **IsPlayerVisible:** Checks if a player is within line of sight. - **IsHungry:** (For survivors or animals) Checks if the agent needs to eat. - **Wander:** The agent moves randomly within a radius.

### Decorator Nodes These modify the behaviour of other nodes, such as: - **Loop:** Makes a zombie repeatedly check for players. - **Cooldown:** Prevents a zombie from attacking too frequently. - **Inverter:** Inverts the result of a child node (e.g., “IsNotSafe” instead of “IsSafe”).

### Composite Nodes Composite nodes can have one or more children and process them in either a first-to-last sequence or random order, depending on the particular composite node.

## Modding Context and Practical Examples

While the search results do not provide explicit Project Zomboid BT node examples, modding communities for similar sandbox games (like RimWorld, which shares a spirit of emergent, AI-driven gameplay) often implement nodes such as: - **NomadMove:** Moves the group between biomes based on resources. - **Forage:** Searches the environment for food or materials. - **Flee:** Runs away from a threat when health is low.

These concepts are directly transferable to Project Zomboid, where modders might create nodes for: - **BarricadeDoor:** A survivor boards up a door if zombies are approaching. - **LootNearestHouse:** Searches the nearest building for supplies. - **Hide:** Finds a hiding spot when spotted by zombies. - **CallForHelp:** Alerts other survivors to danger.

## Zombie-Specific AI

Zombies in Project Zomboid exhibit behaviours such as: - **Idle/Wander:** Moves randomly when no threat is present. - **Chase:** Pursues a player or sound source. - **Attack:** Engages with a player in melee range. - **BreakDoor:** Attempts to break down barriers to reach a target.

These behaviours map directly to BT nodes like **Wander**, **Chase**, **Attack**, and **BreakObject**. A logical structure might be: ```lua Selector ├─ Sequence (Player visible?) │ ├─ MoveToPlayer │ └─ AttackPlayer ├─ Sequence (Sound heard?) │ ├─ MoveToSound │ └─ WanderNearSound └─ Wander ```

## The EnsureItemInInventory Behavior

The EnsureItemInInventory behavior in Project Zomboid can layer failsafes and alternate courses of action for every possible situation. If the item is not found in the building, the behaviour iterates a list of crafting recipes that result in the item they desire, and for each of these recipes it iterates through each ingredient item, and recursively calls the EnsureItemInInventory behavior for each of those items in turn. If the item is found in the building, the character travels to the location of the container holding the item and takes it from the container, and the behaviour succeeds.

If the item is not in the character's inventory, it checks the contents of any bags or backpacks the character is carrying. If the item is not found, it checks the building the character is currently residing in. If these failsafes fail, the behaviour will fail, and the NPC will add that item to a list of desired items to look out for during looting missions and live without the item.

The behaviour tree engine should store any currently processing nodes so they can be ticked directly within the behavior tree engine rather than per-tick traversal of the entire tree. This is crucial, as particular nodes or branches in the tree may take many ticks of the game to complete.

Decorator nodes can have a child node and can specifically only have a single child. They can transform the result they receive from their child node's status, terminate the child, or repeat processing of the child.

The behaviour can be used liberally throughout many trees, whenever we need an NPC to ensure they have an item in their inventory.

## Conclusion

Behavior Trees are a powerful tool for managing AI in Project Zomboid, providing a hierarchical structure that allows for complex, context-sensitive, and emergent behaviours. The use of selectors, sequences, leaf nodes, decorator nodes, and composite nodes enables developers and modders to create AI entities that can adapt to a wide range of situations, ensuring an engaging and immersive gameplay experience for players.

In Project Zomboid, artificial-intelligence (AI) behavior for both zombies and survivors relies on a behavior tree system with common types of nodes such as Selector Nodes, Sequence Nodes, Leaf Nodes, Decorator Nodes, and Composite Nodes. For example, a zombie's high-level behavior could be managed by a Selector Node running child nodes in sequence until one succeeds, like checking for a nearby sound, visible players, and finally wandering if nothing else is detected (Selector Node > Sequence Node > MoveToSound, IsPlayerVisible, Wander).

Technology advances and the integration of artificial-intelligence in games have led to the development of various gadgets that contribute to a more immersive gaming experience. In Project Zomboid, the EnsureItemInInventory behavior stores currently processing nodes in the behavior tree engine for efficient execution, reflecting how technology continues to shape AI in games.

Read also:

    Latest