Text & documents
Numbers & maths
Data & formats
Security
Development & DevOps
Artificial Intelligence
Finance
Health & Wellness
Productivity
Games & Entertainment
Multimedia & design
Business
How to use
The pet: a state machine you look after

The pet has a mood (the state) and it changes for two reasons: what you do (the buttons are events) and what happens to it (its needs cross a threshold). The same button does different things depending on the mood: playing cheers up a bored pet and angers a sleeping one, and feeding it when it is not hungry ends in overeating. The diagram highlights the current mood, its exits and the last arrow that fired.

The dungeon: planning is searching

A hero with a mission: take the key, open the door, take the treasure and leave. It plans with GOAP: it searches for the cheapest chain of actions, and if something fails (a guard sees it) it plans again. The guards have a state machine: they patrol, become suspicious if they hear something, chase if they see the hero (field of view, a wall blocks it) and search the last place they saw it. Raise the cost of fighting and watch the plan change. In You play, click a cell and the hero walks there.

The house: how it decides what to do

A character with hunger, sleepiness and hygiene (and fun, if you add it) lives in a house, as in The Sims. Each object advertises what it gives (the fridge lowers hunger) and the brain chooses. Above the character, a thought bubble with what it is thinking; below, the bars with their marks and the timeline (click on it to go back to that moment). All four at once shows the same day with the four brains, and the challenges check themselves. The link keeps the brain, the seed, the parameters and the tick.

How to use it

A character with hunger and sleepiness lives on a map with food, a bed, shelters and enemies that patrol and chase it if they see it. Choose which brain decides and press play. You can switch brains or change their parameters without stopping: the world carries on and only the way it decides changes. Below, the trace: why it did what it did in this tick.

The four brains

State machine: one state per action and arrows with a condition; simple, but each new action needs arrows to and from the others. Behaviour tree: priorities; every tick it is walked from the top and the first branch that can run wins. Utility: each action is scored with a curve of its need and the highest score wins, as in The Sims. GOAP: planning is searching; an A* over world states (not cells) finds the chain of actions that meets the most urgent goal.

Going back and forth

If a decision depends on a threshold and the need hovers around it, the character goes back and forth between two actions. Each brain has its brake: hysteresis in the state machine and in GOAP (enter with one threshold and leave with a lower one), remembering the current task in the tree, and inertia in utility (extra points for what it was already doing). Remove them and watch the «back-and-forths» go up.

Adding an action: drinking

Turn thirst on and look at the table «What you have to add»: in the state machine, one state and three arrows; in the tree, a branch of three nodes; in utility, one curve; in GOAP, one action and one goal, and the planner does the rest. With many actions, the state machine is the one that grows worst.

Enemies: where they patrol and how they see

With Place enemies, click on the map where a patrol starts and where it turns back (up to 6); clicking an end removes it. See around them is the simplest perception: whoever is a few steps away. See in a cone is the one stealth games use: only what is in front, up to a certain distance and with line of sight (a wall blocks it). You can walk past behind them unseen. Compare the deaths with one and the other.

Taking the tree to a game

With the behavior tree selected, Copy for BehaviorTree.CPP gives the tree in the XML of BehaviorTree.CPP v4, the C++ (and ROS 2) library many games and robots use; it opens in its editor, Groot2. Conditions go as ScriptCondition on the blackboard (hunger, fear…) and actions as custom nodes the game implements.

Game AIFour brains for one character: states, tree, utility and GOAP
Game AIThe same character with four brains: state machine, behaviour tree, utility and GOAP
Hunger35
Sleep25
Hygiene20

Marks: green, where it lets go (threshold − hysteresis); black, where it starts on it (threshold); red, urgent (interrupts whatever it is doing).

wandereatsleepshower
Tick 0
Accidents 0Ticks in distress 0Back-and-forths 0

The four brains at once

The same character in the same house, with the same seed and the same parameters: only the way it decides changes.

Tick 0 / 500
State machine
Accidents 0In distress 0Back-and-forths 0
Behaviour tree
Accidents 0In distress 0Back-and-forths 0
Utility
Accidents 0In distress 0Back-and-forths 0
GOAP (planning)
Accidents 0In distress 0Back-and-forths 0

Challenges

With the state machine and hysteresis at 0, how many back-and-forths are there in 500 ticks?

"Set it up" gets it ready: press Play until tick 500 and look at the counter.

Add fun: how many NEW arrows does the state machine need?

Count the ones leaving each state and think about the ones arriving at the new one.

With no brakes (hysteresis 0, no finishing what it started, inertia 0), which brain spends the fewest ticks in distress in 500?

"Set it up" removes the brakes; then "All four at once".

Brain

It is in wander. Its arrows, in order (the first one that holds fires):

  • → eat if Hunger > 50 (35)
  • → sleep if Sleep > 50 (25)
  • → shower if Hygiene > 50 (20)
Its parameters (applied without stopping)
What each brain needs to add for fun
State machine12 → 20+8
Behaviour tree11 → 14+3
Utility4 → 5+1
GOAP (planning)6 → 8+2