import assert from "node:assert/strict"; import { test } from "node:test"; import { autopilot } from "../src/bot.js"; import { H, MAX_GRADE, QUAY, XS, ZS, blockAt, heightAt, rideable } from "../src/city.js"; import { RIDE_FEEL, createGame, simulate, start, step } from "../src/game.js"; import { rng } from "../src/rng.js"; import { posesAt } from "../src/traffic.js"; const band = (list, name) => list.find((s) => s.name === name); const play = (seed = 1, opts) => start(createGame(seed, opts)); test("the same seed rides the same ride", () => { const a = simulate(play(9), 45, autopilot); const b = simulate(play(9), 45, autopilot); assert.deepEqual([a.p.x, a.p.z, a.p.y, a.p.speed, a.stats.meters, a.cam.x, a.cam.z], [b.p.x, b.p.z, b.p.y, b.p.speed, b.stats.meters, b.cam.x, b.cam.z]); }); test("crossings are flat, and no block of street is steeper than MAX_GRADE", () => { for (const xs of XS) for (const zs of ZS) assert.equal(heightAt(xs.b0 + 0.3, zs.b0 + 0.3), heightAt(xs.b1 - 0.3, zs.b1 - 0.3), `${xs.name} & ${zs.name}`); for (let i = 0; i < XS.length; i++) for (let j = 0; j < ZS.length; j++) { if (j + 1 < ZS.length) assert.ok(Math.abs(H[i][j + 1] - H[i][j]) / (ZS[j + 1].b0 - ZS[j].b1) <= MAX_GRADE + 1e-9); if (i + 1 < XS.length) assert.ok(Math.abs(H[i + 1][j] - H[i][j]) / (XS[i + 1].b0 - XS[i].b1) <= MAX_GRADE + 1e-9); } }); test("the Embarcadero is flat at quay height", () => { for (let z = -670; z < 550; z += 37) assert.equal(heightAt(-30, z), QUAY); }); test("she reaches top speed in about a second on the flat", () => { const g = play(); simulate(g, 1, () => ({ throttle: 1 })); const atOne = g.p.speed; simulate(g, 3, () => ({ throttle: 1 })); assert.ok(atOne > 0.85 * g.p.speed, `${atOne.toFixed(1)} m/s after 1 s, ${g.p.speed.toFixed(1)} m/s after 4 s`); assert.ok(g.p.speed > 13); }); test("climbing slows her down", () => { const flat = play(); simulate(flat, 3, () => ({ throttle: 1 })); // Montgomery St, heading north from Jackson St up toward Broadway: one of the steepest blocks. const x = band(XS, "Montgomery St").c + 3.2; const z = band(ZS, "Jackson St").b0 + 2; assert.ok(heightAt(x, z - 30) > heightAt(x, z) + 6, "the test street climbs"); const hill = play(1, { x, z, heading: 0 }); simulate(hill, 3, () => ({ throttle: 1 })); assert.ok(hill.p.speed < 0.85 * flat.p.speed, `uphill ${hill.p.speed.toFixed(1)} vs flat ${flat.p.speed.toFixed(1)} m/s`); }); test("going over a crest fast throws her into the air", () => { // Montgomery St at Broadway, heading south, about to drop toward Jackson St. const x = band(XS, "Montgomery St").c - 3.2; const z = band(ZS, "Broadway").b1 - 4; assert.ok(heightAt(x, z + 20) < heightAt(x, z) - 4, "the street falls away"); const g = play(1, { x, z, heading: Math.PI }); g.p.speed = 15; let flew = false; let landed = false; for (let i = 0; i < 120; i++) { step(g, { throttle: 1 }); if (i < 30) flew ||= g.p.air; if (flew && !g.p.air) landed = true; } assert.ok(flew, "she leaves the ground"); assert.ok(landed, "and lands"); assert.ok(g.stats.jumps >= 1); }); test("she never rides into a building, a block or the bay", () => { for (const seed of [1, 2, 3]) { const g = play(seed); const random = rng(seed * 31); let steer = 0; for (let i = 0; i < 60 * 60; i++) { if (i % 40 === 0) steer = random() * 2 - 1; step(g, { throttle: random() < 0.9 ? 1 : 0, steer, hop: random() < 0.01 }); assert.ok(rideable(g.p.x, g.p.z), `seed ${seed} at ${g.p.x.toFixed(1)}, ${g.p.z.toFixed(1)}`); } } }); test("a hard crash throws her off, and she is back up in about a second", () => { // On the promenade, aimed at the sea wall. const g = play(1, { x: -4, z: -118, heading: Math.PI / 2 }); g.p.speed = 12; let fell = false; for (let i = 0; i < 30 && !fell; i++) { step(g, { throttle: 1 }); fell = g.p.tumble > 0; } assert.ok(fell, "she tumbles"); assert.equal(g.stats.tumbles, 1); simulate(g, RIDE_FEEL.tumble + 0.1, () => ({})); assert.equal(g.p.tumble, 0); simulate(g, 1, () => ({ throttle: 1, steer: 1 })); assert.ok(Math.abs(g.p.speed) > 3, "and rides on"); }); test("the camera never ends up inside a building", () => { for (const seed of [1, 4]) { const g = play(seed); for (let i = 0; i < 150 * 60; i++) { step(g, autopilot(g)); const b = blockAt(g.cam.x, g.cam.z); assert.ok(!b || g.cam.y > b.top, `seed ${seed}, t=${g.time.toFixed(1)}: camera at ${g.cam.x.toFixed(1)}, ${g.cam.y.toFixed(1)}, ${g.cam.z.toFixed(1)}`); } } }); test("traffic stays on the streets", () => { const g = createGame(5); for (let t = 0; t < 600; t += 0.7) for (const p of posesAt(g.traffic, t)) assert.equal(blockAt(p.x, p.z), null, `${p.v.id} at t=${t.toFixed(1)}`); }); // --------------------------------------------------------------------------------------------- // Burritos, GPUs and the men with shopping carts. test("street things sit on the road, and the layout is the same every ride", () => { const a = play(1).items; const b = play(5).items; assert.deepEqual(a.gpus.map((g) => [g.x, g.z]), b.gpus.map((g) => [g.x, g.z])); assert.ok(a.gpus.length >= 60 && a.burritos.length >= 10 && a.walkers.length >= 30, `${a.gpus.length} GPUs, ${a.burritos.length} burritos, ${a.walkers.length} walkers`); for (const o of [...a.gpus, ...a.burritos]) assert.ok(rideable(o.x, o.z), `${o.x}, ${o.z}`); }); test("a burrito doubles her speed for a few seconds, then she eases back", () => { const g = play(); simulate(g, 4, () => ({ throttle: 1 })); const cruise = g.p.speed; const ahead = { x: g.p.x, z: g.p.z - 3 }; g.items.burritos = [{ x: ahead.x, z: ahead.z, y: g.p.y, id: 0 }]; simulate(g, 3, () => ({ throttle: 1 })); assert.equal(g.stats.burritos, 1); assert.ok(g.p.speed > cruise * 1.7, `${cruise.toFixed(1)} → ${g.p.speed.toFixed(1)} m/s`); simulate(g, 4, () => ({ throttle: 1 })); assert.ok(Math.abs(g.p.speed - cruise) < 1.5, `back to ${g.p.speed.toFixed(1)} m/s`); }); test("rolling over a GPU takes it, once", () => { const g = play(); const at = { x: g.p.x, z: g.p.z - 4, y: g.p.y, id: 9999 }; g.items.gpus.push(at); simulate(g, 2, () => ({ throttle: 1 })); assert.equal(g.stats.gpus, 1); assert.ok(g.items.taken.gpus.has(9999)); simulate(g, 0.5, () => ({ brake: 1 })); assert.equal(g.stats.gpus, 1); }); test("riding into a man's cart at speed throws her off", () => { const g = play(); simulate(g, 3, () => ({ throttle: 1 })); // put a man and his cart, standing still, right in her path const p = g.p; const w = { id: 999, x0: p.x, z0: p.z - 12, dx: 1, dz: 0, depth: 0, wait: 1e9, walk: 1, linger: 1, period: 2e9, phase: 0, sway: 1, look: 0 }; g.items.walkers = [...g.items.walkers, w]; const before = g.stats.tumbles; simulate(g, 1.5, () => ({ throttle: 1 })); assert.equal(g.stats.tumbles, before + 1); });