Most game enemies do not "think" — they run a finite-state machine (FSM), a tiny program with a handful of named states and hard rules for switching between them. At any instant the guard below is in exactly one of four states, and every frame it re-checks the same simple conditions to decide whether to stay or switch.
PATROL → CHASE — target enters the detection radius.CHASE → ATTACK — distance to target drops below the attack range.ATTACK → CHASE — target steps back outside the attack range.CHASE → SEARCH — target leaves the detection radius (line of sight lost).SEARCH → CHASE — target re-enters the detection radius before the search patience timer runs out.SEARCH → PATROL — the search patience timer expires with no target found.FSMs power enemy AI in everything from the original Pac-Man ghosts (scatter, chase, frightened) to modern stealth games. Their appeal is predictability: designers and players alike can reason about exactly why an enemy behaves the way it does.
A robot guard patrols a fixed loop and switches between patrol, chase, attack and search states purely by re-checking simple distance rules against a moving target — the same logic pattern that drives enemy behaviour in countless games.
Every frame the guard is in exactly one named state; transitions fire only when a hard condition (distance to target vs. detection radius or attack range, or a search timer) is crossed — no hidden memory, no learning, just rules.
Adjust detection radius, attack range, guard speed and search patience, then watch the state diagram and the guard's eye colour update live. Turn off autopilot to click the ground and drive the target yourself.
FSMs are deliberately simple by design — that predictability is what let designers hand-tune Pac-Man's ghosts in 1980 and still lets modern stealth games telegraph "the guard heard something" today.