A cut through an ant nest, like an ant farm behind glass: carriers take crumbs from the entrance to the store, nurses carry the queen's eggs to the nursery, and you dig and fill the tunnels. Every ant takes the shortest way at once, and the toolbox's new world/pathfinding is how.
Play it · Source: collection-spielwerk/31-ameisenbau · Toolbox 6.1.0 or later
The game
Ants from the colony outside drop crumbs at the entrance. 24 carriers (red) take them to the store ("Vorrat"), 4 nurses (orange) carry each egg from the queen to the nursery. The start nest works on its own, but its tunnel to the store goes the long way round: dig a shortcut and the carriers take it at once. Fill a tunnel and they turn round; an ant with no way left stops with a question mark. After three minutes the round ends with the crumbs stored. There is nothing to lose.
- Mouse or touch: tap earth to dig a cell, tap a tunnel to fill it; hold and drag to do a row.
- Keyboard or pad: the arrows (or the cross) move a cursor, Space (or A) digs or fills.
- "Wege zeigen" or V: the carriers' flow field as arrows, the nurses' paths as lines, and a cross on every cell with no way.
This is the inside of the nest. The outside is the Ant Colony, where the ants have no map: each one leaves scent and follows the scent of others, and trails grow without any ant knowing the way. Inside, the tunnels are known, so every ant can simply be told the shortest way.
New in the toolbox: pathfinding
The problem. Steering straight at the store gets stuck in the first dead end, and a hand-written search per ant per frame is slow and easy to get subtly wrong: a diagonal step that slips between two blocks of earth, a path that isn't the cheapest after all.
The idea. Two searches for two situations, both plain functions on a Grid2D (and so on a TileMap layer). One walker going to its own place gets A*: search outwards from the walker, always continuing at the cell whose cost so far plus a guess of what is left is lowest. Many walkers going to the same place get a flow field: search once outwards from the goal, and every cell learns which neighbour leads home. Then a hundred carriers cost one search, not a hundred.
The only thing a game must say is which cells can be walked:
const WALKING = { passable: (kind: CellKind) => kind === 'tunnel', diagonal: true };
find_path(grid, from, to, { passable, cost?, diagonal?, max_steps? }): GridPosition[] | null
flow_field(grid, targets, { passable, cost?, diagonal? }): FlowField
// field.next_cell(x, y), direction_at(x, y), distance_at(x, y), is_reachable(x, y)
smooth_path(grid, path, passable): GridPosition[]
Costs per cell are optional (mud at 10 makes the long dry way the cheap one), and a diagonal step is allowed only when both cells beside it are open, so an ant never squeezes past a corner of earth. Both searches share a binary heap of cell indices; the heart of the flow field (src/world/pathfinding/FlowField.ts) is a Dijkstra pass from all targets at once:
while (open.size > 0) {
const current = open.pop(); // the cheapest cell not yet settled
if (closed[current] === 1) {
continue;
}
closed[current] = 1;
const cost = cells.of(current); // what a neighbour pays to step onto it
for (let step = 0; step < steps; step++) {
const neighbour = cells.neighbour(x, y, step); // -1 outside or past a blocked corner
if (neighbour < 0 || cells.of(neighbour) < 0) {
continue;
}
const total = distances[current] + (step >= 4 ? cost * Math.SQRT2 : cost);
if (total < distances[neighbour]) {
distances[neighbour] = total;
next[neighbour] = current; // "from here, go there"
open.push(neighbour, total, 0);
}
}
}
How the nest uses it. After every dig or fill, the game builds two fields (to the store, to the entrance) and plans a new A* path for each nurse on her way. A carrier asks its field each frame where to step; a nurse walks her path, smoothed by smooth_path, which skips cells while the next one is still in straight sight:
const next = field.next_cell(cell.x, cell.y);
if (next === null) {
ant.is_stuck = !field.is_target(cell.x, cell.y); // the question mark
return;
}
walk_towards(ant, cell_center(next));
Measured (npm run measure, Node 22):
| Nest | Open cells | One flow field | One A* path | Field cheaper from |
|---|---|---|---|---|
| start nest | 137 | 27 µs | 9 µs | 4 walkers |
| all dug open | 763 | 145 µs | 11 µs | 15 walkers |
A* stays cheap because its guess leads it almost straight to the goal; a field pays for every open cell. A whole rebuild after a dig (two fields and the nurses' paths) takes 65 µs on the start nest and 0.33 ms on the open one, far inside a frame. The line test of smooth_path walks the grid cell by cell, 3.5 times faster than testing the line against every blocked cell's box.
A test worth copying. The two searches check each other: on 30 random grids with mud and walls, every A* path is walkable, never cuts a corner, and costs exactly the flow field's distance for its start cell. One trap: the start cell is never tested, so a walker standing on a cell that was just closed still gets a way out instead of null.
From earlier steps
Grid2Dfrom step 03: the nest is one, and the searches take any.TileMapfrom step 13: its layers areGrid2Ds, so the same functions find ways across them.SeededRandomfrom step 01: digs the start nest and sends the colony's crumbs.- The
ActionMapfrom step 30: the cursor for keys and pads, with no extra code per device. - The earth comes from the asset library by id (
flat/tile/dirt), as in step 29; the ants, crumbs and eggs are drawn in code, red and green as in the colony. The scaffold, start screen, intro and scaling are explained in Spielwerk 00: The workbench.
Embed it
<script src="https://www.game-object.de/tce-elements/tobidot/dll-tce-assets"></script>
<script src="https://www.game-object.de/tce-elements/tobidot/tce-ameisenbau"></script>
<tce-ameisenbau init-seed="4711"></tce-ameisenbau>
| Attribute | Values | Default |
|---|---|---|
init-seed |
whole number | random |
init-intro |
true / false |
true |
Next
Previous: Spielwerk 30: Steuerknüppel · Next: Spielwerk 32: Daumenkino · All steps
Comments