Enemy brains and pathfinding

Play this lesson

Aliens that fly straight at you

In Kai and Remy's first Hyper Shards, every alien flew in a straight line toward the ship. After one wave, players knew all their tricks.

Game AI is the set of rules that decides what enemies and other characters do. It is not the same as the AI tools our studio uses. Claude Opus 5.5 is a large language model, trained on huge amounts of text, that helps us write code. Game AI is usually a few dozen lines of ordinary code we write ourselves, running every frame on the player's device.

Simple behaviours

Most enemies are built from a few simple behaviours. In Pac-Man (1980), the red ghost aims at Pac-Man's own tile, while the pink one aims four tiles ahead to cut you off.

Each behaviour is a state, and an enemy is in one state at a time. A finite state machine is the rule list that says when to switch: "patrol until the ship is closer than 200 pixels, then chase". The Game loops and state machines lesson goes deeper.

Bug hunt

diver.js
// one state at a time: patrol, chase or fleefunction think(d, ship) {  const dist = Math.hypot(ship.x - d.x, ship.y - d.y);  if (d.state === "patrol" && dist > 200) {    d.state = "chase";  }  if (d.state === "chase" && d.health <= 1) {    d.state = "flee";  }}

Divers ignore the ship when it flies close, but start chasing when it is far away. Which line is the bug?

  1. Line 3
  2. Line 4
  3. Line 7
  4. Line 8
Show the answer

Line 4

Line 4 switches to chase when the distance is more than 200 pixels. It should be dist < 200, so divers react only when the ship comes close. Math.hypot is the distance formula from the Game math lesson.

Telegraph every attack

A telegraph is a warning before an attack: a wind-up, a glow, a sound, or a line showing where a shot will go. It turns "that came from nowhere" into "I should have dodged".

The Hyper Shards boss laser glows red and draws a thin aim line for one second before it fires, so players who watch can always get out of the way. In Garden Defense, the soil shakes before a mole pops up.

Bigger attacks get longer, louder warnings. And never let other effects cover a warning, or players cannot see it.

Quick check

The one-second glow before the boss laser fires makes the fight too easy, so it should be removed.

Show the answer

False

The warning makes the attack fair, not easy. Players still have to notice it and move in time. Without it, getting hit feels like the game cheated.

Finding the way: breadth-first search

A Garden Defense map is a grid of tiles, and some are blocked by hedges. How does a beetle find its way to Sprig?

Breadth-first search (BFS) spreads out from the start like ripples in a pond. It visits every open tile 1 step away, then every tile 2 steps away, then 3, and notes for each tile which tile it came from. When it reaches Sprig, it follows those notes backwards, and that is the path. Because it goes ring by ring, the first path it finds is a shortest one.

Bolt says: Tower defense games often run BFS once, outward from the goal. Then every tile knows its next step, and 100 beetles share one map. This is sometimes called a flow field.

A*: search with a good guess

BFS searches in every direction, even away from Sprig. A* (say "A star") adds a guess. For each tile, it adds the steps taken so far to a guess of the steps left, and explores the lowest total first.

On a grid, a common guess is the Manhattan distance: steps across plus steps down, ignoring walls, like walking city blocks. As long as the guess is never more than the real distance, A* still finds a shortest path, usually after checking far fewer tiles.

Peter Hart, Nils Nilsson and Bertram Raphael published A* in 1968, to plan routes for Shakey, an early robot.

Count the steps

map3.txt
// S = snail, P = Sprig// # = hedge (blocked), . = grass// moves: up, down, left or right S . . . .# # # # .P . . . .

Sprig is 2 tiles below the snail, so the Manhattan distance is 2. How many steps is the shortest real path?

  1. 2
  2. 6
  3. 10
  4. 12
Show the answer

10

The hedges block the way down, so the snail goes 4 right, 2 down and 4 left: 10 steps. Manhattan distance ignores walls, so it can be too low but never too high. That is exactly the kind of guess A* needs.

Steering and flocking

Pathfinding says where to go. Steering says how to move there smoothly: turn a little each frame toward the target, never faster than a top turning speed, and slow down when close.

In 1986, Craig Reynolds made boids, simulated birds that flock with three rules. Each bird only looks at its neighbours:

A swarm of Hyper Shards drones with these rules moves like a real flock, with no leader.

Fun, not perfect

A perfect enemy is easy to write: an alien that always knows where you are and never misses. Playing against it is miserable. The goal is an enemy that is fun to beat.

Keep these numbers in balance.json, so playtests can tune them.

Bolt says: Next lesson: procedural generation, where rules and randomness build levels for us.

Fact-checked 9 October 2026