?You've already seen the graph
On their controller, the third player — the Intendant — has a little oscilloscope scanning the terrain. It isn't decoration: it is literally the soldiers' pathfinding graph, coloured segment by segment.
Each terrain segment is tinted by its slope-passability. Violet = passable both ways. Blue = climbable only left→right. Red = only right→left. Grey = a wall, impassable. The threshold, CS = 1.35, is exactly the engine's climbSlope: the visual translation of the rule “rise ≤ SU”.
The third player's oscilloscope
violet = both ways, blue = climb →, red = climb ←, grey = wall. The pale dots at the edges are towers; the coloured ones, soldiers.In other words: the developers' debug tool (“Path debug”) has become a gameplay aid. The Intendant who wants to open a road for an army reads their scope to spot the red walls they must flatten or bridge. This annex unpacks what's behind that picture.
1The problem: multi-storey terrain
Tower Duel's terrain is a 1D heightfield: a single ground height per abscissa x. As long as that holds, following the slope is enough. But the Intendant places Lemmings-style works — bridges and stairs — that float above the ground. So at a single abscissa there can be several walkable surfaces: the ground, and one or more planks above it.
A greedy local follow (“walk, and if it's too steep, hug the obstacle”) fails the moment you must step onto a stair that starts far from a wall: the soldier, glued to the ground, can't “see” that ten metres back a ramp would let it clear the cliff. You need a global view of the terrain — a graph, and a search over it.
2The graph model: (column, surface)
We sample the terrain into columns spaced STEP = 8 px apart. At each column we create not one node but one node per walkable surface: the ground, plus every plank passing above it. That's exactly what surfacesAt(x) returns:
// all walkable surfaces at abscissa x function surfacesAt(x) { const t = terrAt(x), out = [t]; // the ground is always a surface for (const p of structures) if (x >= p.x0 && x <= p.x1 && p.y <= t + 2) out.push(p.y); // + every plank above (or flush with) the ground return out; }
A node is therefore the pair (column, surface). An edge links two adjacent columns when the step between their surfaces is passable. The diagram shows both storeys: the ground (violet) and a plank laid over a dip (blue) that creates a second row of nodes.
3The algorithm: an A* / Dijkstra over surfaces
The soldier seeks a path from its position to the abscissa of the enemy tower. We run a shortest-path search (Dijkstra, which becomes A* with a good heuristic). The core is the edge rule and the cost:
for (const y2 of surfacesAt(x2)) { const up = cy - y2; if (up > SU + 1) continue; // climbing more than one SU step → impassable const drop = y2 - cy; let cost = STEP + (drop > SU ? drop*0.6 : 0) // dropping costs (fall penalty); climbing a little costs too + (up > 0 ? up*0.25 : 0); if (density) cost += density[i2]*congW; // decongestion: local soldier density if (jitter) cost += hash(jseed, i2)*jitter*STEP; // per-soldier seeded jitter → alternate routes // relax the edge if this path is better }
A climb of more than SU (≈ one step) is forbidden: it's a wall. A drop is always allowed, but a big fall is penalised in the cost (the soldier gets hurt). We then reconstruct the sequence of waypoints by walking back through predecessors. And if no path reaches the enemy tower? The soldier deserts — it doesn't wander forever.
Two optional terms are added to the cost to decongest routes. First, the local soldier density (density[i2] × congW): a per-column occupancy field, rebuilt once per tick, counting every body — both camps and the horde, because crowding is physical, not a matter of allegiance. Then a per-soldier seeded jitter (hash(jseed, column)), drawn at its birth. The result: a horde fans out across the available routes instead of all queuing at the same bridge. Since both terms are strictly positive, they never remove an edge: feasibility is untouched, only the chosen route changes. The crossing test (crossPath) calls findPath without these options — it stays a pure A\*.
crossPath() tests whether a horde can cross to the ruin. No more false “impassable”: the very function that routes soldiers validates the crossing. The Intendant scores because they actually opened the road.4Following: stick to the real surface
Waypoints give a guideline, not a trajectory to glide along. At each step the soldier advances horizontally along the current segment, computes the planned height on that segment, then snaps to the real surface closest to that height. No soldier floating above the ground:
// planned height on the current segment let ny = plannedY, bd = Infinity; for (const yy of surfacesAt(s.x)) { const d = Math.abs(yy - plannedY); if (d < bd) { bd = d; ny = yy; } // pick the closest real surface } s.y = ny; // the soldier follows the terrain, not the ideal line
This snapping naturally handles climbing and descending (and accumulates fall damage if the drop is harsh). Because path and follow are decoupled, a soldier can backtrack: if a replan hands it a better waypoint behind, it takes it without getting stuck.
5Invalidation & replanning: navVer
The terrain shifts constantly: shell craters, bridges and stairs placed, earth dug or filled. A path computed a second ago may now be wrong. The fix is a simple version counter, navVer, incremented on every edit of terrain or structures. Each soldier remembers the navVer of its last computation and replans as soon as it changes:
// recompute if: no path, terrain changed, end of path, if (!s.path || s.navVer !== this.navVer || s.pi >= s.path.length || (s.rep = (s.rep||0) + dt) > 0.7) { // or the periodic ~0.7 s replan is due s.rep = 0; s.navVer = this.navVer; s.path = this.findPath(s.x, s.y, goalX); // a fresh A* from the current position }
navVer catches the discrete events (a crater, a plank). The periodic (~0.7 s) replan additionally catches continuous edits — digging or flattening frame by frame, which would otherwise bump navVer every tick. Together they keep paths fresh without recomputing everyone every frame.
s.rep timer starts at its birth, so recomputations spread out over time (staggering — see next section). Nobody pays a big spike on the same tick.6Performance: what it costs
The graph's size is on the order of N columns × surfaces per column. Since there's almost never more than one or two surfaces per abscissa, it stays small. The naïve Dijkstra scans the queue to find the minimum at each step — an O(V²) cost, perfectly fine for this node count and a handful of soldiers.
O(E log V)), and share a cache of paths between soldiers of the same camp aiming at the same tower. Premature today: the sim is trivially cheap at the current scale.7The debug tool: seeing the paths
The visual aid exists on both sides of the mirror. On the development side, the lab's “Path debug” button overlays on the terrain:
- the valid paths computed by the real pathfinder, in green (left→right) and blue (right→left), with their waypoints;
- the terrain's impassable slopes, in red (where the step exceeds
climbSlope); - a waypoint count and an “OK / NONE” status for each direction — the crossing test, live.
8Limits & extensions
The model is deliberately 1D + surfaces, not true 2D. That fixes its limits — by choice:
- No covered tunnels. Digging horizontally opens a foot-level corridor (basher), digging down sinks a shaft (digger), but there's no “2D solidity” with a ceiling. Explicitly deferred.
- Buried structures excluded. Construction stops if it would pass under the terrain — no ghost bridge buried that would pollute the graph.
- Future ideas. An explicit A* heuristic (distance to the tower) to prune the search; a per-camp shared path cache. (Decongestion — cost ∝ soldier density + seeded jitter — is already in place: see §3.)
9Your turn: unfold the graph
surfacesAt, findPath). Place a plank, show the nodes and the graph, watch the A* path draw itself — then dig a crater and see navVer increment and the path recompute.It's the same playground as the chapters, but centred on pathfinding: editable terrain, bridge/stair placement, graph and path visualisation, and the third player's oscilloscope up top to close the loop.