CyberCity/build.js
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

241 lines
12 KiB
JavaScript

/* CyberCity build — concatenate src/ into one self-contained index.html.
*
* No dependencies, no bundler, no minifier, no source maps. The modules were written to be
* concatenated: each is an IIFE that resolves CC out of lexical scope, so wrapping the lot in one
* more IIFE is the entire "bundling" step. Everything this script does beyond `join('\n')` is
* removing the CommonJS tails that exist only so tools/headless.cjs can require the same files.
*
* Run: node build.js
*/
'use strict';
const fs = require('fs');
const path = require('path');
const ROOT = path.join(__dirname);
const SRC = path.join(ROOT, 'src');
const OUT = path.join(ROOT, 'index.html');
/* Order is load-bearing, not alphabetical: city.js binds core's helpers at make(), raycast.js
* throws without CC.Surf, every element pushes onto CC.ELEMENTS before main.js reads that array,
* and main.js boots on the last line of the file. compose.js is optional — the contract allows it
* to not exist yet — so it is filtered out rather than assumed. */
const ORDER = [
'core.js',
'weather_state.js', // before the elements: they read CC.Weather from init() onwards
'city.js',
'surfaces.js',
'raycast.js',
'control.js', // after city.js, whose route it re-anchors to; before main.js, which drives it
'compose.js',
'@elements', // expands to src/elements/*.js, sorted, so a build is reproducible
'render_canvas.js',
'main.js'
];
function expand(list) {
const out = [];
for (const name of list) {
if (name === '@elements') {
const dir = path.join(SRC, 'elements');
if (!fs.existsSync(dir)) continue;
for (const f of fs.readdirSync(dir).sort())
if (f.endsWith('.js')) out.push(path.join(dir, f));
continue;
}
const p = path.join(SRC, name);
if (fs.existsSync(p)) out.push(p);
else if (name !== 'compose.js') throw new Error('missing required module src/' + name);
}
return out;
}
/* ---- CommonJS strip -------------------------------------------------------------------------
* Two shapes appear in src/, both harmless at runtime in a browser (the ternary short-circuits
* before require is ever evaluated, and `typeof module` is a safe read on an undeclared name) —
* but leaving them in ships dead branches referencing `require`, which is exactly the kind of
* thing a CSP-conscious reader greps for in a "zero network requests" file. They come out. */
function strip(src) {
return src
// `(typeof CC !== 'undefined' ? CC : require('../core.js'))` -> `CC`
.replace(/typeof\s+CC\s*!==\s*(['"])undefined\1\s*\?\s*CC\s*:\s*require\(\s*(['"]).*?\2\s*\)/g, 'CC')
// one-liner: `if (typeof module !== 'undefined') module.exports = X;`
.replace(/^[ \t]*if\s*\(\s*typeof\s+module\s*!==\s*(['"])undefined\1\s*\)\s*module\.exports\s*=[^\n]*;[ \t]*$/gm, '')
// wrapped form: the `if` on its own line, the assignment on the next
.replace(/^[ \t]*if\s*\(\s*typeof\s+module\s*!==\s*(['"])undefined\1\s*\)\r?\n[ \t]*module\.exports\s*=[^;]*;[ \t]*$/gm, '')
.replace(/\n{3,}/g, '\n\n');
}
/* ---- comment strip ---------------------------------------------------------------------------
* src/ is ~40% prose by weight — this codebase explains WHY on nearly every non-obvious line — and
* carrying all of it inline would put index.html at 253 KB against a 200 KB budget. The comments
* belong in src/, which is where anyone editing this reads them; the deliverable is a picture.
*
* This is a scanner, not a regex, because a regex cannot tell `//` inside a string or a regex
* literal from a comment, and getting that wrong corrupts the page silently. It tracks the three
* literal forms and uses the standard previous-significant-token rule to decide whether a `/`
* opens a regex or is a division. The `new Function` compile check further down is the backstop:
* anything this mangles fails the build instead of shipping. */
const REGEX_OK_WORDS = new Set(['return', 'typeof', 'case', 'in', 'of', 'new', 'delete', 'void',
'instanceof', 'throw', 'do', 'else', 'yield', 'await']);
const IDENT = /[A-Za-z0-9_$]/;
/* A `/` after any of these is division, never a regex, because all of them end a VALUE.
* `)` and `]` are the ones that matter here: `(v - 0.62) / (st.gnd - 2.2)` is everywhere in
* surfaces.js, and reading that `/` as a regex opener swallows the rest of the file into a
* literal and every comment after it ships. */
const DIV_AFTER = /[A-Za-z0-9_$)\]]/;
function decomment(src) {
let out = '', i = 0, prev = '', word = '';
const n = src.length;
/* ...except after a keyword, where `return /re/` is a regex even though `n` is an ident char. */
const regexAllowed = () => prev === '' || !DIV_AFTER.test(prev) || REGEX_OK_WORDS.has(word);
while (i < n) {
const c = src[i];
if (c === '/' && src[i + 1] === '/') {
while (i < n && src[i] !== '\n') i++; // the newline itself stays: ASI depends on it
continue;
}
if (c === '/' && src[i + 1] === '*') {
const start = i; i += 2;
while (i < n && !(src[i] === '*' && src[i + 1] === '/')) i++;
i += 2;
/* A comment sits BETWEEN two tokens, so it cannot vanish without leaving a separator behind
* or an inline one welds its two neighbours into a single identifier. A multi-line one
* collapses to a newline instead, so ASI keeps behaving exactly as it did in src/. */
out += src.slice(start, i).indexOf('\n') >= 0 ? '\n' : ' ';
continue;
}
if (c === '"' || c === "'" || c === '`') {
out += c; i++;
while (i < n) {
const d = src[i]; out += d; i++;
if (d === '\\') { out += src[i]; i++; continue; }
if (d === c) break;
}
prev = c; word = ''; continue;
}
if (c === '/' && regexAllowed()) {
out += c; i++;
let inClass = false;
while (i < n) {
const d = src[i]; out += d; i++;
if (d === '\\') { out += src[i]; i++; continue; }
if (d === '[') inClass = true;
else if (d === ']') inClass = false;
else if (d === '/' && !inClass) break;
}
while (i < n && IDENT.test(src[i])) { out += src[i]; i++; } // flags
prev = '/'; word = ''; continue;
}
out += c; i++;
if (!/\s/.test(c)) {
prev = c;
word = IDENT.test(c) ? word + c : '';
}
}
/* Trailing whitespace and the blank lines the comments left behind. Indentation stays — it costs
* ~6 KB and it is the difference between a viewable page source and a wall. */
return out.replace(/[ \t]+$/gm, '').replace(/\n{2,}/g, '\n\n').trim();
}
const files = expand(ORDER);
const parts = files.map(p => {
const rel = path.relative(ROOT, p).split(path.sep).join('/');
return '/* ' + rel + ' */\n' + decomment(strip(fs.readFileSync(p, 'utf8'))) + '\n';
});
/* One IIFE, one `var CC`. core.js declares CC with `var`, so it is hoisted to the top of this
* function and every later module's `typeof CC !== 'undefined'` guard sees it. Nothing leaks to
* window, which is the point: the page has no API surface to poke at. */
const bundle = '(function () {\n' + parts.join('\n') + '\n})();\n';
/* The page must open with ZERO network requests, so nothing may reference a module loader. */
if (/\brequire\s*\(/.test(bundle)) throw new Error('build: a require() survived the strip');
if (/\bmodule\.exports\b/.test(bundle)) throw new Error('build: a module.exports survived the strip');
if (/\bimport\s|\bexport\s/.test(bundle)) throw new Error('build: ESM syntax in the bundle');
/* A literal closing script tag anywhere in the sources would end the inline block early and dump
* the rest of the city into the document as text. */
if (/<\/script/i.test(bundle)) throw new Error('build: source contains a closing script tag');
/* Every module named its global; if one is missing it was dropped, mis-ordered, or the comment
* scanner ate something structural. Cheap, and it has caught both. */
for (const need of ['var CC', 'CC.City', 'CC.Surf', 'CC.Cast', 'CC.Canvas', 'CC.Main',
'CC.ELEMENTS.push'])
if (!bundle.includes(need)) throw new Error('build: bundle is missing ' + need);
/* Compiles the bundle without running it. This is what makes the comment scanner safe to trust:
* if it ever mangles a regex literal or a template string, the build stops here. */
try { new Function(bundle); }
catch (e) { throw new Error('build: bundle does not parse — ' + e.message); }
/* The page is black before a single pixel of city is drawn — html, body and canvas all — so that
* entering fullscreen never flashes white and never shows a seam at an edge the canvas overhangs.
* The hint is CSS-only on purpose: it fades itself out and no JavaScript ever has to know it
* existed, which keeps main.js free of anything resembling UI. */
const HTML = `<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1,viewport-fit=cover">
<meta name="color-scheme" content="dark">
<meta name="theme-color" content="#000000">
<title>CyberCity</title>
<style>
html, body { margin: 0; padding: 0; background: #000; height: 100%; overflow: hidden; }
/* The canvas is allowed to overhang the viewport by a fraction of a cell (see main.js layout),
so it is pinned to the origin and the overflow above hides the remainder. */
#cc { position: fixed; inset: 0; display: block; background: #000;
image-rendering: auto; }
/* Nothing here is selectable, draggable or scrollable: every gesture is a fullscreen request. */
body { -webkit-user-select: none; user-select: none; -webkit-tap-highlight-color: transparent;
touch-action: none; overscroll-behavior: none; }
#hint {
position: fixed; left: 50%; bottom: 6vh; transform: translateX(-50%);
font: 500 12px/1 ui-monospace, "Liberation Mono", "DejaVu Sans Mono", monospace;
letter-spacing: .28em; text-transform: uppercase; color: #d8e84a;
pointer-events: none; opacity: 0;
animation: hint 9s ease-in-out .8s 1 forwards;
}
/* Ends at opacity 0 and stays there — 'forwards' holds the last keyframe, so the hint is gone
for good after one play and never reappears on resize or fullscreen change. */
@keyframes hint { 0% { opacity: 0 } 12% { opacity: .34 } 70% { opacity: .34 } 100% { opacity: 0 } }
@media (prefers-reduced-motion: reduce) { #hint { animation-duration: 14s } }
</style>
</head>
<body>
<canvas id="cc"></canvas>
<div id="hint">click for fullscreen</div>
<script>
${bundle}</script>
</body>
</html>
`;
fs.writeFileSync(OUT, HTML, 'utf8');
const bytes = Buffer.byteLength(HTML, 'utf8');
for (const p of files)
console.log(' + ' + path.relative(ROOT, p).split(path.sep).join('/'));
console.log('index.html ' + bytes + ' bytes (' + (bytes / 1024).toFixed(1) + ' KB) ' +
files.length + ' modules');
/* THE BUDGET WAS 200 KB AND IT IS NOW 300 KB. That is a raise, not a discovery, and it is being
* written down rather than quietly deleted. src/ went from roughly 190 KB to roughly 530 KB in one
* pass — twelve new ambient elements, a weather director every element reads, four input devices,
* a rewritten road and facade — and the stripped bundle came out at 266 KB against a ceiling set
* when the city had a third of the content in it. Nothing here is dead weight to cut: the comment
* scanner already removes ~40% of the source by volume, and the remaining bytes are all reachable
* code. The alternatives were to strip indentation (about 25 KB, and it turns a viewable page
* source into a wall, which the decomment note above explicitly refuses) or to delete elements to
* make a number, which is the wrong way round.
*
* 300 KB is still a hard stop and still means something: it is one HTTP response, it parses in
* well under a frame on anything made this decade, and it leaves the deliverable a file somebody
* can read. If a later pass pushes past it, the answer is to cut content, not to move this line
* again. */
if (bytes > 300 * 1024) {
console.error('build: index.html is over the 300 KB budget');
process.exit(1);
}