Road-graph map pipeline (author roads, generate the rest)
level-designprocedural-generationmap-editortilemapjsondata-format
A city map for a top-down driving game can be authored as a small road graph and "baked" at load time into everything the game needs. This keeps the authored file tiny and diffable, and makes the road graph the single source of truth. This is the design used by Spacebar's campaign mode (vanilla JS, 2026-08).
Authored truth: the road graph JSON
- Nodes
{id, x, y, z}and segments{a, b, w, oneway}that reference stable node ids, in tile coordinates, plus aformatversion field. - Landmarks (hub, spawn): anything with persistent identity lives here. Generated filler may never own identity.
- An
overrides: []escape hatch for cosmetic tiles only, never roads. - One record per line through a shared serializer, so the generator and the editor produce byte-stable, diff-friendly output.
- Reserve fields you do not support yet (
z,oneway) and reject non-default values at bake time. Cheap now, and the migration later is trivial.
Bake at load, never stored
About 11 ms for a 512 x 512 map. It emits three artifacts:
1. A tile grid (Uint8Array).
2. Merged collision rectangles plus a per-tile index (Tile collision with merged rectangles).
3. A routing graph (Float64Array x/y plus neighbor lists) for traffic AI (Traffic AI path following on a road graph).
Filler is position-hashed, never drawn from a sequential RNG stream. With a sequential stream, any edit shifts every later random draw and reshuffles the whole city; hashing by position keeps edits local.
Validation that hard-errors
Wrong format version, unsupported z/oneway, diagonal segments, nodes off the map, and road width below a minimum (2 tiles: a 38 px garbage truck cannot fit a 32 px tile). An editor should draw these problems on the map: orphan nodes, nodes unreachable from spawn (flood fill), dangling or narrow segments, missing spawn.
Stamping roads of width w
Use lo = (w - 1) >> 1 and hi = w - 1 - lo on either side of the centerline. Using w >> 1 on both
sides makes every road odd-width.
In-game editor lessons
- Editing is a mode of viewing the same world, not a third world type. Tab bakes and drops you into driving at the junction you were viewing.
- Undo as whole-state snapshots (40 deep), not inverse operations: far simpler and correct.
- Draw the overview from the vector graph, never from scaled-down tiles.
- Load the editor via dynamic import so normal players fetch nothing (verified via the access log); cache-bust the import (Caching static sites and unbundled ES modules (?v= stamps, no-cache, prefetch)).
- Do not re-run a generator over a hand-edited map: once edited, the JSON is the truth.
Curves via stair-step barriers (approved design)
Segments gain curve: 1|2 (quadratic Bezier, control point at the derived elbow so it is tangent to the
axes). The bake disc-stamps tiles along samples, so the barrier becomes a staircase and collision and
rendering need no changes, while virtual nodes every ~3 tiles go into the routing graph so traffic follows
a smooth path with zero code changes. Minimum curve radius 4 tiles, plus a post-stamp clearance check.
Generated seed district tips
Irregular block gaps of 16 to 22 tiles (about half a screen) read as a city; 34-tile spacing read as corridors. Order the palette by brightness: buildings darkest, road middle, sidewalk lightest.
See also
- Tile collision with merged rectangles
- Rendering a tile map without a chunk cache
- Traffic AI path following on a road graph
- Deterministic seeds for procedural worlds