// The neighborhood as data: the Embarcadero from Telegraph Hill to the Bay Bridge, with the // Financial District, North Beach, the slopes of Nob Hill and SoMa behind it. // // Pure and deterministic: no DOM, no three.js. The renderer draws it and the rules ride on it. // Meters. x runs east (the bay is at x > 0), z runs south (the north shore is at z = -680), // y is up. The Ferry Building's clock tower stands near the origin. // // Streets form a grid of bands. Where two streets cross, the intersection is flat; between // intersections a street slopes in a straight line. That is San Francisco's shape: steep // blocks and flat crossings, so a fast scooter leaves the ground at every crest. import { BRICKS, CHADAI, CLAMCODE, CREAMS, GLASS, PASTELS, STONES, WHITE_TRIM } from "./palette.js"; import { between, hash, pick, rng } from "./rng.js"; export const QUAY = 3.2; // the Embarcadero, its promenade and the piers stand this far above the bay export const CURB = 0.15; // sidewalks stand this far above the road export const NORTH_SHORE = -680; const smooth = (e0, e1, v) => { const t = Math.min(1, Math.max(0, (v - e0) / (e1 - e0))); return t * t * (3 - 2 * t); }; const lerp = (a, b, t) => a + (b - a) * t; // --------------------------------------------------------------------------------------------- // Streets. Each is a band: road from r0 to r1, sidewalks out to b0 and b1. function street(name, c, o = {}) { const road = o.road ?? 7; const walk = o.walk ?? 4; return { name, c, r0: c - road, r1: c + road, b0: c - road - walk, b1: c + road + walk, kind: o.kind ?? "street", terminal: null }; } /** Streets that run north–south (constant x), west to east. */ export const XS = [ street("Franklin St", -1475), street("Van Ness Ave", -1385, { road: 12, kind: "avenue" }), street("Polk St", -1295), street("Larkin St", -1205), street("Hyde St", -1115), street("Leavenworth St", -1025), street("Jones St", -935), street("Taylor St", -845), street("Mason St", -755), street("Powell St", -665), street("Stockton St", -575), street("Grant Ave", -485, { road: 6 }), street("Kearny St", -395), street("Montgomery St", -305), street("Sansome St", -215), street("Battery St", -125), { name: "The Embarcadero", c: -30, r0: -50, r1: -10, b0: -56, b1: 0, kind: "embarcadero", terminal: "east" }, ]; /** Streets that run east–west (constant z), north to south. */ export const ZS = [ { name: "The Embarcadero", c: -655, r0: -670, r1: -640, b0: -680, b1: -634, kind: "waterfront", terminal: "north" }, street("Greenwich St", -565), street("Union St", -485), street("Broadway", -405, { road: 8 }), street("Jackson St", -325), street("Clay St", -245), street("California St", -165, { road: 9, kind: "cable" }), street("Pine St", -85), street("Market St", 0, { road: 16, walk: 7, kind: "market" }), street("Mission St", 105, { road: 8 }), street("Howard St", 215), street("Folsom St", 325), street("Harrison St", 435), street("Bryant St", 545), street("Brannan St", 655), street("Townsend St", 765), street("King St", 875, { road: 9 }), street("Berry St", 985), street("Channel St", 1095), ]; const xIndex = (name) => XS.findIndex((s) => s.name === name); const zIndex = (name) => ZS.findIndex((s) => s.name === name); export const EMBARCADERO = XS[XS.length - 1]; export const MARKET = ZS[zIndex("Market St")]; export const CALIFORNIA = ZS[zIndex("California St")]; /** Where Claudia can ride: Powell St to the bay, the north shore to Bryant St, plus the piers. */ export const RIDE = { x0: XS[xIndex("Powell St")].b0, x1: 0, z0: NORTH_SHORE, z1: ZS[zIndex("Bryant St")].b1 }; // The Embarcadero's lanes, west to east. export const LANES = { walkWest: [-56, -50], bikeSouth: -48.5, carSouth: -44, trackSouth: -33.5, median: [-36, -24], planter: [-31.2, -28.8], trackNorth: -26.5, carNorth: -16.5, bikeNorth: -11.5, promenade: [-10, 0], marketBikeEast: 14.5, marketCarEast: 9, }; // --------------------------------------------------------------------------------------------- // Terrain: the hills before the streets flatten them into steps. // Soft cones: height h, radius r (rN and rS shorten the north and south sides), rounded over // a at the top. const HILLS = [ { name: "Telegraph Hill", x: -355, z: -425, h: 54, r: 290, rS: 200, a: 60 }, { name: "Nob Hill", x: -880, z: -220, h: 95, r: 430, rN: 300, rS: 230, a: 110 }, { name: "North Beach", x: -560, z: -500, h: -26, r: 210, a: 60 }, { name: "Russian Hill", x: -1150, z: -560, h: 85, r: 430, rN: 300, a: 80 }, { name: "Rincon Hill", x: -150, z: 470, h: 16, r: 200, a: 50 }, { name: "Pacific Heights", x: -2300, z: -380, h: 100, r: 900, a: 200 }, { name: "Twin Peaks", x: -4740, z: 4560, h: 270, r: 2200, a: 300 }, { name: "Potrero Hill", x: -1250, z: 1950, h: 80, r: 700, a: 150 }, ]; export const TELEGRAPH = HILLS[0]; export const MAX_GRADE = 0.28; /** Ground height before streets: soft cones for the hills, flat fill along the sea walls. */ export function terrain(x, z) { let h = 0; for (const k of HILLS) { const dz = z > k.z ? ((z - k.z) * k.r) / (k.rS ?? k.r) : ((z - k.z) * k.r) / (k.rN ?? k.r); const d = Math.hypot(x - k.x, dz); const f = 1 - (Math.sqrt(d * d + k.a * k.a) - k.a) / k.r; if (f > 0) h += k.h * f; } return QUAY + Math.max(0, h) * smooth(20, 130, -x) * smooth(10, 110, z - NORTH_SHORE); } // Node heights: one per intersection. The waterfront streets stay at quay height, and no // block between two intersections climbs more than MAX_GRADE: hills are lowered, never raised, // until every street is steep but rideable. export const H = XS.map((xs) => ZS.map((zs) => (xs.kind === "embarcadero" || zs.kind === "waterfront" ? QUAY : terrain(xs.c, zs.c))), ); { let changed = true; while (changed) { changed = false; for (let i = 0; i < XS.length; i++) for (let j = 0; j < ZS.length; j++) { const pairs = []; if (i + 1 < XS.length) pairs.push([i + 1, j, XS[i + 1].b0 - XS[i].b1]); if (j + 1 < ZS.length) pairs.push([i, j + 1, ZS[j + 1].b0 - ZS[j].b1]); for (const [a, b, len] of pairs) { const most = MAX_GRADE * len; if (H[i][j] > H[a][b] + most + 1e-9) { H[i][j] = H[a][b] + most; changed = true; } else if (H[a][b] > H[i][j] + most + 1e-9) { H[a][b] = H[i][j] + most; changed = true; } } } } } /** Which band holds v: { k, inside } or the gap after band k with its fraction t; out if off the grid. */ function findBand(bands, v) { let lo = 0; let hi = bands.length - 1; let k = -1; while (lo <= hi) { const m = (lo + hi) >> 1; if (bands[m].b0 <= v) { k = m; lo = m + 1; } else hi = m - 1; } if (k < 0) return { k: -1, inside: false, out: true, t: 0 }; if (v <= bands[k].b1) return { k, inside: true, out: false, t: 0 }; if (k === bands.length - 1) return { k, inside: false, out: true, t: 0 }; return { k, inside: false, out: false, t: (v - bands[k].b1) / (bands[k + 1].b0 - bands[k].b1) }; } /** The street surface height (roads, sidewalks before curbs, block grounds); terrain off the grid. */ export function heightAt(x, z) { const fx = findBand(XS, x); const fz = findBand(ZS, z); if (fx.out || fz.out) return terrain(x, z); const i = fx.k; const j = fz.k; if (fx.inside && fz.inside) return H[i][j]; if (fx.inside) return lerp(H[i][j], H[i][j + 1], fz.t); if (fz.inside) return lerp(H[i][j], H[i + 1][j], fx.t); return lerp(lerp(H[i][j], H[i + 1][j], fx.t), lerp(H[i][j + 1], H[i + 1][j + 1], fx.t), fz.t); } /** 'road', 'walk' (sidewalk or promenade), 'block' (between streets) or 'out' (off the grid). */ export function surfaceAt(x, z) { const fx = findBand(XS, x); const fz = findBand(ZS, z); if (fx.out || fz.out) return "out"; if (!fx.inside && !fz.inside) return "block"; const xb = fx.inside ? XS[fx.k] : null; const zb = fz.inside ? ZS[fz.k] : null; const inXr = xb && x >= xb.r0 && x <= xb.r1; const inZr = zb && z >= zb.r0 && z <= zb.r1; if (xb && zb) { const xRoadReaches = zb.terminal === "north" ? z >= zb.r0 : true; const zRoadReaches = xb.terminal === "east" ? x <= xb.r1 : true; return (inXr && xRoadReaches) || (inZr && zRoadReaches) ? "road" : "walk"; } return inXr || inZr ? "road" : "walk"; } /** The street (or streets) at a point, for the HUD. */ export function streetAt(x, z) { const fx = findBand(XS, x); const fz = findBand(ZS, z); const a = fx.inside ? XS[fx.k].name : null; const b = fz.inside ? ZS[fz.k].name : null; if (a && b) return a === b ? a : `${a} & ${b}`; return a ?? b ?? null; } // --------------------------------------------------------------------------------------------- // The waterfront: the Ferry Building and the piers. export const FERRY = { x0: 5, x1: 35, z0: -105, z1: 105, tower: { x: 20, z: 0 } }; export const PIERS = [ { name: "Pier 1", z: -150, w: 46, len: 190, shed: true }, { name: "Pier 3", z: -225, w: 40, len: 240, shed: true }, { name: "Pier 5", z: -290, w: 28, len: 210, shed: true }, { name: "Pier 7", z: -350, w: 12, len: 270, shed: false }, { name: "Pier 9", z: -420, w: 44, len: 260, shed: true }, { name: "Pier 15", z: -505, w: 56, len: 300, shed: true }, { name: "Pier 19", z: -585, w: 40, len: 250, shed: true }, { name: "Pier 14", z: 150, w: 10, len: 200, shed: false }, { name: "Pier 22½", z: 215, w: 22, len: 52, shed: "fireboat" }, { name: "Pier 24", z: 320, w: 40, len: 210, shed: true }, { name: "Pier 26", z: 390, w: 42, len: 230, shed: true }, { name: "Pier 28", z: 535, w: 34, len: 200, shed: true }, ].map((p) => { const z0 = p.z - p.w / 2; const z1 = p.z + p.w / 2; const deck = { x0: 0, x1: p.len, z0, z1 }; if (p.shed === true) { const shed = { x0: 7, x1: p.len - 16, z0: z0 + 6, z1: z1 - 4 }; const rides = [ { x0: 0, x1: p.len, z0, z1: shed.z0 }, { x0: 0, x1: p.len, z0: shed.z1, z1 }, { x0: shed.x1, x1: p.len, z0, z1 }, { x0: 0, x1: shed.x0, z0, z1 }, ]; return { ...p, kind: "shed", deck, shed, rides }; } if (p.shed === "fireboat") { const shed = { x0: 14, x1: 40, z0: z0 + 5, z1: z1 - 5 }; return { ...p, kind: "fireboat", deck, shed, rides: [{ x0: 0, x1: shed.x0, z0, z1 }, { x0: 0, x1: p.len, z0, z1: shed.z0 }, { x0: 0, x1: p.len, z0: shed.z1, z1 }, { x0: shed.x1, x1: p.len, z0, z1 }] }; } return { ...p, kind: "open", deck, shed: null, rides: [deck] }; }); function onPier(x, z) { if (x < 0) return null; for (const p of PIERS) { if (x > p.deck.x1 || z < p.deck.z0 || z > p.deck.z1) continue; for (const r of p.rides) if (x >= r.x0 && x <= r.x1 && z >= r.z0 && z <= r.z1) return p; } return null; } // --------------------------------------------------------------------------------------------- // The Bay Bridge: from its anchorage at the waterfront, northeast across the bay to Yerba Buena // Island, and southwest over SoMa as an elevated approach. export const BRIDGE = (() => { const angle = (42 * Math.PI) / 180; const ax = 40; const az = 470; const dx = Math.cos(angle); const dz = -Math.sin(angle); const towers = [350, 1050, 1750, 2450]; const deckY = (s) => { if (s < -520) return 24; if (s < 0) return 24 + 28 * smooth(-520, 0, s); if (s < 350) return 52 + (10 * s) / 350; if (s < 2450) return 62 + 5 * Math.sin((Math.PI * (s - 350)) / 2100); return 62 - 12 * smooth(2450, 2850, s); }; const at = (s) => ({ x: ax + dx * s, z: az + dz * s, y: deckY(s) }); return { ax, az, dx, dz, angle, towers, center: 1400, island: 2850, start: -1250, width: 24, deckY, at, towerTop: 158 }; })(); /** Distance in plan from (x, z) to the bridge's line, and the s along it. */ export function bridgeNear(x, z) { const s = (x - BRIDGE.ax) * BRIDGE.dx + (z - BRIDGE.az) * BRIDGE.dz; const px = BRIDGE.ax + BRIDGE.dx * s; const pz = BRIDGE.az + BRIDGE.dz * s; return { s, d: Math.hypot(x - px, z - pz) }; } // --------------------------------------------------------------------------------------------- // Neighborhoods. const SPECIAL = [ { kind: "coit", x: -350, z: -445 }, { kind: "square", x: -530, z: -445 }, { kind: "pyramid", x: -350, z: -205 }, { kind: "plaza", x: -85, z: -50 }, { kind: "chadai", x: -260, z: 160 }, { kind: "ec", x: -85, z: -205 }, { kind: "ec", x: -85, z: -125 }, { kind: "ec", x: -170, z: -205 }, { kind: "ec", x: -170, z: -125 }, { kind: "clamcode", x: -440, z: 1040 }, ]; export function districtAt(x, z) { if (x > RIDE.x1 - 60 && z < RIDE.z1 + 40) return "The Embarcadero"; const dt = Math.hypot(x - TELEGRAPH.x, z - TELEGRAPH.z); if (z < -330) { if (x < -520 && dt > 150) return "North Beach"; return "Telegraph Hill"; } if (z < -23) { if (x > -330 && z < -250) return "Jackson Square"; if (x > -440) return "Financial District"; if (x > -530) return "Chinatown"; return "Nob Hill"; } if (z < 23) return "Market Street"; if (z > 300 && x > -250) return "Rincon Hill"; return "SoMa"; } function blockKind(b) { const cx = (b.x0 + b.x1) / 2; const cz = (b.z0 + b.z1) / 2; for (const s of SPECIAL) if (s.x > b.x0 && s.x < b.x1 && s.z > b.z0 && s.z < b.z1) return s.kind; if (cz > RIDE.z1) return cz > 900 ? "missionbay" : "warehouse"; if (cx < RIDE.x0) { if (cz < -330) return "houses"; if (cz < 23) return "flats"; return "warehouse"; } const dt = Math.hypot(cx - TELEGRAPH.x, cz - TELEGRAPH.z); if (cz < -330) { if (cx > -136) return "brick"; if (dt < 230 || cx > -440) return "houses"; return "flats"; } if (cz < -250) return cx > -440 ? "brick" : "flats"; if (cz < -23) { if (cx > -440) return "fidi"; return "flats"; } if (cz < 220) return cx > -440 ? "transbay" : "warehouse"; if (cx > -240 && cz > 300) return "rincon"; return "warehouse"; } // --------------------------------------------------------------------------------------------- // Buildings. Each is an axis-aligned box: footprint center (x, z), size w (x) by d (z), facade // bottom y0, height h above it, sunk `sink` below so slopes never show a gap. `win` is the // window grid: [story, column spacing, window width, window height] as fractions of a cell. const WIN = { house: [3.1, 2.3, 0.5, 0.56], flat: [3.0, 2.8, 0.5, 0.56], brick: [3.8, 3.2, 0.52, 0.6], office: [3.7, 2.1, 0.62, 0.62], tower: [3.8, 1.7, 0.7, 0.7], glass: [3.6, 1.5, 0.86, 0.84], slab: [3.6, 1.2, 0.42, 0.78], granite: [3.8, 2.2, 0.6, 0.7], fins: [3.6, 1.05, 0.4, 0.86], }; // The skyline's families: stone towers that step back, dark granite, green-blue glass, and // white slabs ribbed with fins. const TOWER_STYLES = [ { kind: "tower", colors: [0xe3d9c6, 0xd8ccb6, 0xeae2d2, 0xd2c2a6], tiers: 3, weight: 0.36 }, { kind: "granite", colors: [0x5b4a45, 0x4e4442, 0x6a5650], tiers: 2, weight: 0.14 }, { kind: "glass", colors: [0x8fb3b5, 0x86a6bd, 0x9cb8c6, 0x7d98ab, 0xa9c0c2], tiers: 2, weight: 0.32 }, { kind: "fins", colors: [0xeeebe4, 0xe6e2d8], tiers: 1, weight: 0.18 }, ]; function towerStyle(random) { let v = random(); for (const s of TOWER_STYLES) { if (v < s.weight) return s; v -= s.weight; } return TOWER_STYLES[0]; } function towerHeight(random, tall = 1) { const r = random(); return (r < 0.5 ? between(random, 52, 92) : r < 0.85 ? between(random, 92, 138) : between(random, 138, 182)) * tall; } function edgesOf(b) { return [ { side: "n", axis: "x", u0: b.x0, u1: b.x1, edge: b.z0, inward: 1 }, { side: "s", axis: "x", u0: b.x0, u1: b.x1, edge: b.z1, inward: -1 }, { side: "w", axis: "z", u0: b.z0, u1: b.z1, edge: b.x0, inward: 1 }, { side: "e", axis: "z", u0: b.z0, u1: b.z1, edge: b.x1, inward: -1 }, ]; } /** The sidewalk height just outside an edge, at position u along it. */ function walkAt(e, u) { return e.axis === "x" ? heightAt(u, e.edge - e.inward * 1.5) + CURB : heightAt(e.edge - e.inward * 1.5, u) + CURB; } function footprint(e, u, w, d, setback = 0) { const uc = u + w / 2; const vc = e.edge + e.inward * (setback + d / 2); return e.axis === "x" ? { x: uc, z: vc, w, d } : { x: vc, z: uc, w: d, d: w }; } function lowestUnder(f) { let lo = Infinity; for (const [sx, sz] of [[-1, -1], [1, -1], [-1, 1], [1, 1], [0, 0]]) lo = Math.min(lo, heightAt(f.x + (sx * f.w) / 2, f.z + (sz * f.d) / 2)); return lo; } function building(f, y0, h, kind, color, extra = {}) { const sink = Math.max(1.5, y0 - lowestUnder(f) + 1.5); return { x: f.x, z: f.z, w: f.w, d: f.d, y0, h, sink, kind, color, win: WIN[kind] ?? null, gf: extra.gf ?? 3.2, shop: extra.shop ?? 0, face: extra.face ?? null, bay: extra.bay ?? false, cornice: extra.cornice ?? false, trim: extra.trim ?? WHITE_TRIM, roof: extra.roof ?? null }; } /** Buildings along the four edges of a block, with corner buildings, leaving a yard inside. */ function perimeter(b, random, o) { const out = []; const c = o.corner; for (const e of edgesOf(b)) { let u = e.u0 + c; const end = e.u1 - c; while (end - u > o.minW * 0.75) { let w = between(random, o.minW, o.maxW); if (end - u - w < o.minW) w = end - u; const d = Math.min(c, between(random, o.minD, o.maxD)); const f = footprint(e, u, w, d); const y0 = walkAt(e, u + w / 2); const stories = Math.round(between(random, o.minS, o.maxS + 0.99) - 0.5); const kind = o.kind; const h = stories * WIN[kind][0] + o.gf + 0.6; out.push(building(f, y0, h, kind, o.color(random), { face: e.side, bay: o.bay && w > 5.8 && random() < 0.85, cornice: o.cornice, gf: o.gf, shop: o.shop ? (random() < o.shop ? 1 : 0) : 0 })); u += w; } } // Corners: one building each, facing whichever street is lower. for (const [cx, cz] of [[b.x0, b.z0], [b.x1, b.z0], [b.x0, b.z1], [b.x1, b.z1]]) { const f = { x: cx + (cx === b.x0 ? c / 2 : -c / 2), z: cz + (cz === b.z0 ? c / 2 : -c / 2), w: c, d: c }; const y0 = heightAt(cx + (cx === b.x0 ? -1.5 : 1.5), cz + (cz === b.z0 ? -1.5 : 1.5)) + CURB; const stories = Math.round(between(random, o.minS + 0.5, o.maxS + 1.49) - 0.5); const kind = o.cornerKind ?? o.kind; const h = stories * WIN[kind][0] + o.gf + 0.6; out.push(building(f, y0, h, kind, o.cornerColor ? o.cornerColor(random) : o.color(random), { face: cz === b.z0 ? "n" : "s", cornice: o.cornice, gf: o.gf, shop: o.shop ? 1 : 0 })); } return out; } /** Split a block into a grid of lots, every lot built to the edge. */ function lots(b, random, nx, nz, make) { const out = []; const xs = [b.x0]; const zs = [b.z0]; for (let i = 1; i < nx; i++) xs.push(lerp(b.x0, b.x1, i / nx + (random() - 0.5) * 0.18)); for (let j = 1; j < nz; j++) zs.push(lerp(b.z0, b.z1, j / nz + (random() - 0.5) * 0.18)); xs.push(b.x1); zs.push(b.z1); for (let i = 0; i < nx; i++) for (let j = 0; j < nz; j++) { const f = { x: (xs[i] + xs[i + 1]) / 2, z: (zs[j] + zs[j + 1]) / 2, w: xs[i + 1] - xs[i] - 0.6, d: zs[j + 1] - zs[j] - 0.6 }; const face = j === 0 ? "n" : j === nz - 1 ? "s" : i === 0 ? "w" : "e"; const y0 = Math.min(heightAt(f.x, j === 0 ? b.z0 - 1.5 : b.z1 + 1.5), heightAt(i === 0 ? b.x0 - 1.5 : b.x1 + 1.5, f.z)) + CURB; for (const bld of make(f, y0, face, i, j)) out.push(bld); } return out; } /** A tower with setbacks: each tier is its own box. */ function tower(f, y0, height, kind, color, random, tiers = 3) { const out = []; let base = y0; let w = f.w; let d = f.d; const shares = tiers === 1 ? [1] : tiers === 2 ? [0.7, 0.3] : [0.55, 0.3, 0.15]; for (let t = 0; t < tiers; t++) { const h = height * shares[t]; out.push(building({ x: f.x, z: f.z, w, d }, base, h, kind, color, { gf: t === 0 ? 6 : 0, shop: t === 0 ? 1 : 0, cornice: t === tiers - 1 })); out[out.length - 1].sink = t === 0 ? out[out.length - 1].sink : 0.2; out[out.length - 1].tier = t; base += h; const k = between(random, 0.72, 0.86); w *= k; d *= k; } return out; } const pastelPair = (random) => { const a = Math.floor(random() * PASTELS.length); const b = (a + 1 + Math.floor(random() * (PASTELS.length - 1))) % PASTELS.length; return (r) => { const v = r(); return v < 0.4 ? PASTELS[a] : v < 0.8 ? PASTELS[b] : pick(r, CREAMS); }; }; function buildBlock(b, random) { switch (b.kind) { case "houses": { const color = pastelPair(random); return perimeter(b, random, { kind: "house", corner: 15, minW: 6.2, maxW: 8.4, minD: 11, maxD: 15, minS: 2, maxS: 3, gf: 3.0, bay: true, cornice: true, color, cornerKind: "flat", cornerColor: (r) => pick(r, CREAMS) }); } case "flats": { const color = (r) => (r() < 0.75 ? pick(r, CREAMS) : pick(r, PASTELS)); return perimeter(b, random, { kind: "flat", corner: 17, minW: 11, maxW: 18, minD: 14, maxD: 17, minS: 3, maxS: 6, gf: 3.6, bay: true, cornice: true, shop: 0.5, color }); } case "brick": return lots(b, random, 2, 2, (f, y0, face) => [building(f, y0, Math.round(between(random, 2, 5.99)) * 3.8 + 4.2, "brick", pick(random, BRICKS), { face, gf: 4.2, shop: 1, cornice: true })]); case "warehouse": return lots(b, random, 2, 2, (f, y0, face) => { const brick = random() < 0.55; return [building(f, y0, Math.round(between(random, 2, 6.99)) * 3.8 + 4.2, brick ? "brick" : "office", brick ? pick(random, BRICKS) : pick(random, STONES), { face, gf: 4.2, shop: 1, cornice: brick })]; }); case "missionbay": return lots(b, random, 2, 2, (f, y0, face) => [building(f, y0, Math.round(between(random, 4, 9.99)) * 3.7 + 5, random() < 0.5 ? "glass" : "office", random() < 0.5 ? pick(random, GLASS) : pick(random, CREAMS), { face, gf: 5, shop: 1 })]); case "fidi": { const out = []; const split = b.x1 - b.x0 > 60 && random() < 0.6; const halves = split ? [[b.x0, (b.x0 + b.x1) / 2], [(b.x0 + b.x1) / 2, b.x1]] : [[b.x0, b.x1]]; for (const [x0, x1] of halves) { const s = Math.min(x1 - x0 - 6, b.z1 - b.z0 - 8, between(random, 26, 38)); const f = { x: (x0 + x1) / 2, z: (b.z0 + b.z1) / 2 + between(random, -4, 4), w: s, d: s * between(random, 0.85, 1.15) }; const y0 = heightAt(f.x, b.z0 - 1.5) + CURB; const style = towerStyle(random); out.push(...tower(f, y0, towerHeight(random), style.kind, pick(random, style.colors), random, style.tiers)); } return out; } case "transbay": { const s = Math.min(b.x1 - b.x0 - 10, between(random, 28, 36)); const f = { x: (b.x0 + b.x1) / 2 + between(random, -5, 5), z: (b.z0 + b.z1) / 2, w: s, d: s * between(random, 0.9, 1.4) }; const y0 = heightAt(f.x, b.z0 - 1.5) + CURB; const style = towerStyle(random); return tower(f, y0, towerHeight(random, 1.1), style.kind, pick(random, style.colors), random, Math.min(2, style.tiers)); } case "rincon": { const out = []; const f = { x: (b.x0 + b.x1) / 2, z: (b.z0 + b.z1) / 2, w: 22, d: 22 }; const y0 = heightAt(f.x, b.z0 - 1.5) + CURB; out.push(...tower(f, y0, between(random, 110, 170), "glass", pick(random, GLASS), random, 1)); out.push(building({ x: f.x, z: b.z1 - 9, w: b.x1 - b.x0 - 4, d: 16 }, heightAt(f.x, b.z1 + 1.5) + CURB, 9, "office", pick(random, CREAMS), { face: "s", gf: 4, shop: 1 })); return out; } case "ec": { const f = { x: (b.x0 + b.x1) / 2, z: (b.z0 + b.z1) / 2, w: 22, d: Math.min(46, b.z1 - b.z0 - 8) }; const y0 = heightAt(f.x, b.z0 - 1.5) + CURB; const out = [building({ x: f.x, z: f.z, w: b.x1 - b.x0 - 2, d: b.z1 - b.z0 - 2 }, y0, 7, "office", 0xd8d2c6, { gf: 5, shop: 1 })]; out.push({ ...building(f, y0 + 7, between(random, 120, 165), "slab", 0xe4ded2, { gf: 0, cornice: false }), sink: 0.2 }); return out; } case "chadai": { // The tower itself is drawn as a landmark; around it, a podium. const y0 = heightAt(b.x0 - 1.5, b.z0 - 1.5) + CURB; return [ building({ x: (b.x0 + b.x1) / 2, z: b.z0 + 8, w: b.x1 - b.x0 - 2, d: 14 }, y0, 8, "glass", 0xb9c2cc, { gf: 5, shop: 1, face: "n" }), building({ x: (b.x0 + b.x1) / 2, z: b.z1 - 8, w: b.x1 - b.x0 - 2, d: 14 }, y0, 8, "glass", 0xb9c2cc, { gf: 5, shop: 1, face: "s" }), ]; } case "clamcode": { const f = { x: (b.x0 + b.x1) / 2, z: (b.z0 + b.z1) / 2, w: b.x1 - b.x0 - 10, d: b.z1 - b.z0 - 10 }; const y0 = heightAt(f.x, b.z0 - 1.5) + CURB; return [building(f, y0, 44, "glass", CLAMCODE, { gf: 5, shop: 1 })]; } case "pyramid": case "coit": case "square": case "plaza": default: return []; } } // --------------------------------------------------------------------------------------------- // Assemble the city once: blocks, buildings, piers, props, obstacles. function build() { const blocks = []; const buildings = []; for (let i = 0; i < XS.length - 1; i++) for (let j = 0; j < ZS.length - 1; j++) { const b = { i, j, x0: XS[i].b1, x1: XS[i + 1].b0, z0: ZS[j].b1, z1: ZS[j + 1].b0 }; b.cx = (b.x0 + b.x1) / 2; b.cz = (b.z0 + b.z1) / 2; b.ride = b.cx > RIDE.x0 && b.cx < RIDE.x1 && b.cz > RIDE.z0 && b.cz < RIDE.z1; b.kind = blockKind(b); b.heights = [H[i][j], H[i + 1][j], H[i][j + 1], H[i + 1][j + 1]]; const random = rng(Math.floor(hash(i, j, 1906) * 2 ** 32)); const list = buildBlock(b, random).filter((bl) => { // Nothing under the bridge's approach but its own piers. const near = bridgeNear(bl.x, bl.z); if (near.s > -1300 && near.s < 60 && near.d < Math.max(bl.w, bl.d) / 2 + 16) return bl.y0 + bl.h < BRIDGE.deckY(near.s) - 9; return true; }); b.top = list.reduce((m, bl) => Math.max(m, bl.y0 + bl.h), heightAt(b.cx, b.cz) + 2); b.buildings = list; for (const bl of list) { bl.block = blocks.length; buildings.push(bl); } blocks.push(b); } // Landmarks. const blockAt = (x, z) => blocks.find((b) => x > b.x0 && x < b.x1 && z > b.z0 && z < b.z1); const coitBlock = blockAt(-350, -445); const coit = { x: coitBlock.x0 + 24, z: coitBlock.z1 - 22, mound: 9 }; coit.y = heightAt(coit.x, coit.z) + coit.mound; const pyramidBlock = blockAt(-350, -205); const pyramid = { x: pyramidBlock.cx + 6, z: pyramidBlock.cz, y: heightAt(pyramidBlock.cx, pyramidBlock.cz) + CURB, base: 40, height: 256 }; const chadaiBlock = blockAt(-260, 160); const chadai = { x: chadaiBlock.cx, z: chadaiBlock.cz, y: heightAt(chadaiBlock.cx, chadaiBlock.cz) + CURB, w: 36, height: 300 }; const clamBlock = blockAt(-440, 1040); const clamcode = { x: clamBlock.cx, z: clamBlock.cz, y: heightAt(clamBlock.cx, clamBlock.cz) + CURB }; for (const b of [coitBlock, pyramidBlock, chadaiBlock]) b.top = Math.max(b.top, b === coitBlock ? coit.y + 64 : b === pyramidBlock ? pyramid.y + pyramid.height : chadai.y + chadai.height); // Billboards: the labs talk to the street from the rooftops. const billboards = []; const roofOf = (x, z) => { const b = blockAt(x, z); return b.buildings.filter((bl) => Math.abs(bl.x - x) < bl.w / 2 + 6 && Math.abs(bl.z - z) < bl.d / 2 + 6).reduce((m, bl) => Math.max(m, bl.y0 + bl.h), heightAt(x, z)); }; // `facing` is the way the face points, as a heading: 0 north, π/2 east, π south. billboards.push({ lab: "chadai", x: -80, z: -280, y: roofOf(-80, -280) + 1, w: 16, h: 6.5, facing: 2.6, lines: ["AGI BY FRIDAY.", "ChadAI"] }); billboards.push({ lab: "clamcode", x: -80, z: -365, y: roofOf(-80, -365) + 1, w: 17, h: 6.5, facing: 2.6, lines: ["Our model is 40% more aligned*", "ClamCode"] }); billboards.push({ lab: "clamcode", x: -80, z: -445, y: roofOf(-80, -445) + 1, w: 12, h: 4.2, facing: 2.6, lines: ["*than our last model", "ClamCode"] }); billboards.push({ lab: "chadai", x: -80, z: 250, y: roofOf(-80, 250) + 1, w: 16, h: 6.5, facing: 0.5, lines: ["Scale is all you need.", "ChadAI"] }); billboards.push({ lab: "clamcode", x: -440, z: -110, y: roofOf(-440, -110) + 1, w: 18, h: 6.5, facing: -Math.PI / 2, lines: ["Responsibly scaling, irresponsibly fast.", "ClamCode"] }); // Props and obstacles. Obstacles are what the scooter bumps into: circles and boxes. const random = rng(415); const palms = []; const lamps = []; const trees = []; const cars = []; const planters = []; const obstacles = []; const circle = (x, z, r, list, extra = {}) => { const o = { type: "circle", x, z, r, ...extra }; list.push(o); if (x >= RIDE.x0 - 2 && x <= RIDE.x1 + 1 && z >= RIDE.z0 - 2 && z <= RIDE.z1 + 2) obstacles.push(o); return o; }; // The Embarcadero median: a planted strip of palms between the streetcar tracks. const emb = XS.length - 1; for (let j = 0; j < ZS.length - 1; j++) { const z0 = ZS[j].b1 + 4; const z1 = ZS[j + 1].b0 - 4; if (z0 > RIDE.z1) break; const p = { type: "box", x0: LANES.planter[0], x1: LANES.planter[1], z0, z1, h: 0.25 }; planters.push(p); obstacles.push(p); for (let z = z0 + 4; z < z1 - 2; z += 17) { palms.push({ x: -30, z, h: between(random, 9, 13), s: between(random, 0.9, 1.15), r: random() * Math.PI * 2 }); obstacles.push({ type: "circle", x: -30, z, r: 0.5 }); } } // Promenade lamps along the sea wall, leaving pier mouths clear. for (let z = NORTH_SHORE + 30; z < RIDE.z1 - 5; z += 30) { if (PIERS.some((p) => z > p.deck.z0 - 3 && z < p.deck.z1 + 3)) continue; if (z > FERRY.z0 - 2 && z < FERRY.z1 + 2 && z % 60 !== 0) continue; circle(-1.4, z, 0.25, lamps, { style: "harbor" }); } for (let x = -40; x > RIDE.x0; x -= 30) circle(x, NORTH_SHORE + 1.4, 0.25, lamps, { style: "harbor" }); // Street lamps, trees and parked cars along every block face. const treeDistricts = new Set(["fidi", "transbay", "warehouse", "rincon", "flats", "brick"]); for (const b of blocks) { const ride = b.ride; // Street props only where someone can see them up close. const away = Math.max(RIDE.x0 - b.cx, b.cz - RIDE.z1, 0); if (away > 300) continue; const leafy = treeDistricts.has(b.kind) || b.kind === "plaza" || b.kind === "ec"; for (const e of edgesOf(b)) { const len = e.u1 - e.u0; // a point on the sidewalk, `off` meters from the block edge toward the curb const pt = (u, off) => (e.axis === "x" ? [u, e.edge - e.inward * off] : [e.edge - e.inward * off, u]); const bandWalk = e.axis === "x" ? (e.side === "n" ? ZS[b.j] : ZS[b.j + 1]) : e.side === "w" ? XS[b.i] : XS[b.i + 1]; if (bandWalk.kind === "embarcadero" || bandWalk.kind === "waterfront") { // the Embarcadero's own sidewalk gets lamps only for (let u = e.u0 + 12; u < e.u1 - 6; u += 34) { const [x, z] = pt(u, 1.0); circle(x, z, 0.22, lamps, { style: "street" }); } continue; } const walkWidth = e.inward > 0 ? (e.axis === "x" ? bandWalk.b1 - bandWalk.r1 : bandWalk.b1 - bandWalk.r1) : bandWalk.r0 - bandWalk.b0; const curbOff = walkWidth - 0.6; for (let u = e.u0 + 8 + hash(b.i, b.j, e.side.charCodeAt(0)) * 10; u < e.u1 - 6; u += 33) { const [x, z] = pt(u, curbOff); circle(x, z, 0.2, lamps, { style: "street" }); } if (leafy || bandWalk.kind === "market") { for (let u = e.u0 + 5; u < e.u1 - 4; u += between(random, 8.5, 11)) { if (random() < 0.15) continue; const [x, z] = pt(u, curbOff - 0.4); circle(x, z, 0.32, trees, { h: between(random, 5.5, 8), s: between(random, 0.8, 1.2), kind: "plane" }); } } // Parked cars along the curb, not on the arterials. if (bandWalk.kind === "market" || bandWalk.kind === "avenue") continue; const p = { houses: 0.62, flats: 0.6, brick: 0.45, warehouse: 0.5, fidi: 0.2, transbay: 0.2, rincon: 0.35, missionbay: 0.4 }[b.kind] ?? 0.3; for (let u = e.u0 + 6; u < e.u1 - 10; u += 5.6) { if (random() > p) continue; const roadEdge = e.inward > 0 ? (e.axis === "x" ? bandWalk.r1 : bandWalk.r1) : bandWalk.r0; const off = e.inward > 0 ? -1.05 : 1.05; // a car sits a meter into the road from the curb // Cars face the way traffic runs on their side of the street. const c = e.axis === "x" ? { x: u + 2.3, z: roadEdge + off, w: 4.4, d: 1.8, heading: e.side === "n" ? Math.PI / 2 : -Math.PI / 2 } : { x: roadEdge + off, z: u + 2.3, w: 1.8, d: 4.4, heading: e.side === "w" ? 0 : Math.PI }; c.color = pick(random, [0xd9d9d6, 0x2f3238, 0x8b9096, 0x4a5a6e, 0x6e2f33, 0xe8e4da, 0x3e4a3d, 0xb9a88a, 0x5d6d7e]); c.ride = ride; cars.push(c); if (c.x > RIDE.x0 - 3 && c.x < RIDE.x1 && c.z > RIDE.z0 && c.z < RIDE.z1 + 3) obstacles.push({ type: "box", x0: c.x - c.w / 2, x1: c.x + c.w / 2, z0: c.z - c.d / 2, z1: c.z + c.d / 2, h: 1.5 }); u += 1.2; } } } // Park trees. for (const b of blocks) { if (!["coit", "square", "plaza", "pyramid"].includes(b.kind)) continue; const n = b.kind === "plaza" ? 14 : b.kind === "pyramid" ? 16 : 30; for (let k = 0; k < n; k++) { const x = between(random, b.x0 + 4, b.x1 - 4); const z = between(random, b.z0 + 4, b.z1 - 4); if (b.kind === "coit" && Math.hypot(x - coit.x, z - coit.z) < 16) continue; if (b.kind === "pyramid" && Math.hypot(x - pyramid.x, z - pyramid.z) < 34) continue; trees.push({ type: "circle", x, z, r: 0.5, h: between(random, 7, 14), s: between(random, 1, 1.6), kind: b.kind === "pyramid" ? "redwood" : b.kind === "coit" ? (random() < 0.5 ? "pine" : "plane") : "plane" }); } } // Traffic signals: two at every crossing in the ride, on opposite corners, arms over the road. const signals = []; for (let i = 0; i < XS.length; i++) for (let j = 0; j < ZS.length; j++) { const xs = XS[i]; const zs = ZS[j]; if (xs.b0 < RIDE.x0 || xs.b1 > RIDE.x1 + 0.1 || zs.b0 < RIDE.z0 - 0.1 || zs.b1 > RIDE.z1) continue; if (zs.kind === "waterfront") continue; const corners = [ { x: xs.b0 + 1.1, z: zs.b0 + 1.1, arm: 1, face: 0 }, { x: xs.b1 - 1.1, z: zs.b1 - 1.1, arm: -1, face: Math.PI }, ]; for (const c of corners) { if (surfaceAt(c.x, c.z) !== "walk") continue; const s = { ...c, y: heightAt(c.x, c.z) + CURB, reach: Math.min(6, (xs.r1 - xs.r0) * 0.45), green: (i + j) % 2 === (c.arm > 0 ? 0 : 1) }; signals.push(s); obstacles.push({ type: "circle", x: c.x, z: c.z, r: 0.16 }); } } // Barriers where the ride ends: across every street that leaves it. const barriers = []; for (const zs of ZS) if (zs.b0 >= RIDE.z0 && zs.b1 <= RIDE.z1) barriers.push({ x0: RIDE.x0 - 1.2, z0: zs.b0, x1: RIDE.x0 - 1.2, z1: zs.b1 }); for (const xs of XS) if (xs.b0 >= RIDE.x0 && xs.b1 <= RIDE.x1) barriers.push({ x0: xs.b0, z0: RIDE.z1 + 1.2, x1: xs.b1, z1: RIDE.z1 + 1.2 }); // A spatial hash of obstacles, 16 m cells. const grid = new Map(); const key = (cx, cz) => cx * 100003 + cz; for (const o of obstacles) { const x0 = o.type === "circle" ? o.x - o.r : o.x0; const x1 = o.type === "circle" ? o.x + o.r : o.x1; const z0 = o.type === "circle" ? o.z - o.r : o.z0; const z1 = o.type === "circle" ? o.z + o.r : o.z1; for (let cx = Math.floor(x0 / 16); cx <= Math.floor(x1 / 16); cx++) for (let cz = Math.floor(z0 / 16); cz <= Math.floor(z1 / 16); cz++) { const k = key(cx, cz); if (!grid.has(k)) grid.set(k, []); grid.get(k).push(o); } } const near = (x, z) => grid.get(key(Math.floor(x / 16), Math.floor(z / 16))) ?? []; return { blocks, buildings, coit, pyramid, chadai, clamcode, billboards, palms, lamps, trees, cars, planters, signals, barriers, obstacles, near, blockAt }; } export const CITY = build(); /** The height of a block's lawn at (x, z): a curb above the street grid, and on Telegraph Hill * the mound Coit Tower stands on. The mound tapers to nothing at the block's edges, so the * lawn always meets the sidewalks around it. */ export function lawnAt(x, z) { const base = heightAt(x, z) + CURB; const c = CITY.coit; const b = blockAt(x, z); if (!b || b.kind !== "coit") return base; const d = Math.hypot(x - c.x, z - c.z); if (d < 9) return c.y; const edge = Math.min(x - b.x0, b.x1 - x, z - b.z0, b.z1 - z); const fade = smooth(1, 14, edge); return base + c.mound * (1 - Math.min(1, (d - 9) / 26)) ** 1.6 * fade; } // --------------------------------------------------------------------------------------------- // Questions the rules ask. /** Is (x, z) somewhere a scooter can be: a street, a sidewalk, the promenade or a pier? */ export function rideable(x, z) { if (x >= RIDE.x0 && x <= RIDE.x1 && z >= RIDE.z0 && z <= RIDE.z1) { const k = surfaceAt(x, z); return k === "road" || k === "walk"; } return onPier(x, z) !== null; } /** The height a wheel rests on at (x, z): roads, curbs, the promenade and the piers. */ export function groundAt(x, z) { if (x > 0) return onPier(x, z) ? QUAY + CURB : x < FERRY.x1 + 2 && z > FERRY.z0 && z < FERRY.z1 ? QUAY : 0; if (z < NORTH_SHORE) return 0; const k = surfaceAt(x, z); const y = heightAt(x, z); return k === "walk" ? y + CURB : y; } /** The block (the land between streets) holding (x, z), if any. */ export function blockAt(x, z) { const fx = findBand(XS, x); const fz = findBand(ZS, z); if (fx.out || fz.out || fx.inside || fz.inside) return null; return CITY.blocks[fx.k * (ZS.length - 1) + fz.k]; } /** The first obstacle a circle at (x, z) of radius r overlaps, with how to push out of it. * Obstacles lower than `above` (how high the wheels are off the ground) are jumped over. */ export function obstacleHit(x, z, r, above = 0) { for (const o of CITY.near(x, z)) { if ((o.h ?? 99) < above) continue; if (o.type === "circle") { const dx = x - o.x; const dz = z - o.z; const d = Math.hypot(dx, dz); if (d < o.r + r) return { nx: d > 1e-6 ? dx / d : 1, nz: d > 1e-6 ? dz / d : 0, depth: o.r + r - d }; } else { const cx = Math.max(o.x0, Math.min(o.x1, x)); const cz = Math.max(o.z0, Math.min(o.z1, z)); const dx = x - cx; const dz = z - cz; const d = Math.hypot(dx, dz); if (d < r) { if (d > 1e-6) return { nx: dx / d, nz: dz / d, depth: r - d }; // inside the box: push out the shortest way const opts = [ { nx: -1, nz: 0, depth: x - o.x0 + r }, { nx: 1, nz: 0, depth: o.x1 - x + r }, { nx: 0, nz: -1, depth: z - o.z0 + r }, { nx: 0, nz: 1, depth: o.z1 - z + r }, ]; return opts.reduce((a, b) => (b.depth < a.depth ? b : a)); } } } return null; } /** Where a segment from a to b first runs into a building block, as a fraction (1 if clear). */ export function sightline(ax, ay, az, bx, by, bz) { let tMin = 1; const steps = Math.ceil(Math.hypot(bx - ax, bz - az) / 3) + 1; for (let k = 1; k <= steps; k++) { const t = k / steps; const x = ax + (bx - ax) * t; const z = az + (bz - az) * t; const y = ay + (by - ay) * t; const s = surfaceAt(x, z); if (s === "block") { const b = blockAt(x, z); if (!b || y < b.top) { tMin = (k - 1) / steps; break; } } if (y < groundAt(x, z) + 0.3) { tMin = (k - 1) / steps; break; } } return tMin; } /** Which pier, if any, is at (x, z). */ export function pierAt(x, z) { return onPier(x, z); } /** A name for where Claudia is: the neighborhood and the street. */ export function placeAt(x, z) { const pier = onPier(x, z); if (pier) return { district: "The Embarcadero", street: pier.name }; return { district: districtAt(x, z), street: streetAt(x, z) }; } export const PALETTE_IN_USE = { CHADAI, CLAMCODE };