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

How a character chooses what to do at each moment: state machines, behaviour trees, utility, GOAP and schedules.

See what the AI is thinking:

The house. Kitchen, bedroom, bathroom and living room: the map from the article.

Needs
Hunger35
Sleep25
Hygiene20
wandereatsleepshower
Tick 0
Accidents 0Ticks in distress 0Back-and-forths 0
Brain

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

eat → sleep: Sleep > 85eat → shower: Hygiene > 85sleep → eat: Hunger > 85sleep → shower: Hygiene > 85shower → eat: Hunger > 85shower → sleep: Sleep > 85wander → eat: Hunger > 50wander → sleep: Sleep > 50wander → shower: Hygiene > 50eat → wander: Hunger < 10sleep → wander: Sleep < 10shower → wander: Hygiene < 10wandereatsleepshower

12 arrows. In orange, the one that has just fired.

  • → 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