CyberCity/CONTRACT.md
MiguelCarino 574d4113e6 CyberCity: a walkable ASCII cyberpunk city in one HTML file
A first-person street canyon rendered at character resolution, fullscreen,
self-contained: index.html has zero external references and is built from
src/ by `node build.js`. Never edit index.html by hand.

Renderer is voxel-space (heightmap) raycasting, not a wall grid: each screen
COLUMN marches front-to-back keeping a running silhouette row, which gives
occlusion, varying building heights, cross-streets and the slot of sky
between rooftops for free.

- src/city.js       endless deterministic heightmap; districts, lots, signs,
                    the walk, and a 192-candidate search for a start point
                    that opens inside a canyon on every seed
- src/weather_state.js  one weather director: six presets on a seeded schedule
                    with weighted transitions. Every element reads rel(), which
                    is 1.0 under the preset the build was tuned as
- src/control.js    arbitration, not replacement -- the autopilot never stops
                    running, the viewer's pose is cross-faded over it, and
                    letting go re-anchors to the nearest point on the route.
                    Keyboard, mouse-look, touch, gamepad; pitch slides the
                    horizon because at this resolution that IS pitch
- src/surfaces.js   facade/floor/sky texture, puddles, wet reflection
- src/raycast.js    the column march
- src/elements/     38 ambient elements: rain, steam, fog, lightning, stars,
                    moon, aircraft, blimps, drones, searchlights, beacons,
                    crowds with clothing archetypes, traffic, a cat, food
                    stalls, litter, props, skybridges, cables, neon, holo,
                    signals, shop spill, and the optics passes
- tools/            headless.cjs renders a frame with no browser; topng.py
                    prints it; metrics.py measures it; domshim.cjs boots the
                    BUILT page under a fake DOM; flicker-rate.cjs and
                    lightning-rate.cjs are the photosensitivity gates

Verified: deterministic (byte-identical renders across processes), 252/252
domshim checks, flicker-rate PASS under all six skies, lightning-rate PASS
on the four skies that carry storms and NOT_APPLICABLE on the two that do not.

Not yet verified in a real browser: glyph legibility, bloom appearance,
on-screen colour, frame budget, and touch/gamepad on hardware.
2026-08-16 02:20:18 -06:00

3.5 KiB
Raw Blame History

CyberCity — module contract

Deliverable: one self-contained index.html, fullscreen, pure ambient, no game loop. Sources live in src/; node build.js inlines them into index.html. Never edit index.html by hand.

Renderer model — voxel-space (heightmap) raycasting at character resolution

Not a wall-grid raycaster. Each map cell carries a height, and each screen column is marched front-to-back keeping a running silhouette row. This gives correct occlusion, varying building heights, cross-streets, alleys and — critically — the slot of sky between rooftops, all for free.

Per column x:

  1. ray dir from yaw + fov*(x/cols-0.5)
  2. march t from T_NEAR to T_FAR, step growing with t
  3. sample h = height(gx,gz); project topRow = horizon - (h - eyeY)*scale/t
  4. if topRow < silhouette[x], paint rows topRow .. min(silhouette[x], baseRow) as façade, then silhouette[x] = topRow
  5. below the horizon, the floor is painted by inverse projection: t = eyeY*scale/(row-horizon)

Frame — parallel typed arrays, length cols*rows, index i = row*cols + col

array type meaning
ch Uint8Array index into GLYPHS
col Uint8Array index into PALETTE
lum Uint8Array 0..255 brightness multiplier applied to the palette colour
dist Float32Array depth in world units; Infinity for sky. Elements MUST depth-test against this
kind Uint8Array 0 sky 1 façade 2 floor 3 element

Modules

Every module is a plain script defining one global, no imports, no bundler syntax — build.js concatenates them in order and the headless harness requires them through a shim.

file global responsibility
src/core.js CC Frame alloc, PALETTE, GLYPHS, rng/noise, math, put()
src/city.js CC.City make(seed) → heightmap + per-cell district/style/sign metadata
src/raycast.js CC.Cast render(frame, cam, city) — fills every cell: façades, floor, sky
src/surfaces.js CC.Surf texture fns: facade(u,v,cell,dist), floorTex(wx,wz,dist), sky(x,y,t)
src/elements/*.js pushes to CC.ELEMENTS ambient systems, see below
src/compose.js CC.Compose fog, element pass ordering, reduced-motion damping
src/render_canvas.js CC.Canvas glyph atlas, drawImage per cell, bloom. Only DOM-touching module
src/main.js — loop, resize, fullscreen, seed in hash, prefers-reduced-motion

Ambient element interface

CC.ELEMENTS.push({
  name: 'rain',
  layer: 10,              // lower draws first; elements draw after the world pass
  init(city, rng) {},     // allocate persistent state
  update(dt, t, cam) {},  // advance simulation; no drawing
  draw(frame, cam, t) {}, // use CC.put(frame, x, y, glyph, colour, lum, dist) — it depth-tests for you
});

Rules for elements:

  • Never write to the typed arrays directly; always go through CC.put, which honours frame.dist.
  • Must be allocation-free in update/draw (pre-allocate in init) — this runs 60×/sec.
  • Must read CC.reducedMotion and damp or freeze accordingly.
  • Must be deterministic given the seeded rng handed to init.

Verification (this is not optional)

node tools/headless.mjs <seed> <frame> <cols> <rows> > f.txt renders one frame with no browser and dumps glyph,colourIdx,lum per cell. python3 tools/topng.py f.txt out.png renders that to an image with the same bloom the canvas does. Every change gets looked at this way before it ships.