// The rules: Claudia's scooter on the city's streets. Pure and deterministic: the same seed // and the same inputs give the same ride. No DOM, no three.js. // // Heading is measured from north, clockwise: 0 faces north (-z), π/2 faces east (+x). import { stepCam, createCam } from "./camera.js"; import { LANES, groundAt, obstacleHit, placeAt, rideable } from "./city.js"; import { pick, rng } from "./rng.js"; import { ITEM_FEEL, burritoHere, createItems, walkerHit, walkersNear } from "./items.js"; import { createTraffic, posesAt, vehicleHit } from "./traffic.js"; export const DT = 1 / 60; export const G = 9.8; export const START = { x: LANES.bikeNorth, z: 40, heading: 0 }; /** How the scooter rides. Critics: these numbers are the feel. */ export const RIDE_FEEL = { radius: 0.42, // m, for bumping into things motor: 38, // m/s², the shove from a standstill motorTop: 21, // m/s where the motor stops pushing; she cruises at about 43 mph boost: 50, // m/s², the shove while boosting (Shift) boostTop: 30, // m/s where the boost stops pushing; she tops out near 57 mph brake: 28, // m/s² reverse: 3, // m/s, backing up top: 31, // m/s, nothing goes faster, even downhill roll: 0.5, // m/s², rolling resistance drag: 0.008, // per meter, air drag hills: 2.6, // how much more than gravity a hill slows or speeds you hop: 4.6, // m/s, the hop tumbleAt: 7, // m/s, hitting something faster than this throws her off tumble: 1.1, // s, on the ground before she's back up yawSlow: 2.8, // rad/s, turning at low speed yawFast: 1.35, // rad/s, turning at top speed }; const clamp = (v, lo, hi) => Math.max(lo, Math.min(hi, v)); const lerp = (a, b, t) => a + (b - a) * t; const TUMBLES = ["Oof.", "Walk it off.", "Scooter 1, Claudia 0.", "Nobody saw that.", "Still faster than the 38."]; // Said as the GPU count climbs: every card she takes is one the labs don't get. const GPU_LINES = { 1: "One GPU the labs won't get.", 5: "Five GPUs. ChadAI's next run just slipped a week.", 10: "Ten GPUs. ClamCode is refreshing the supplier portal.", 20: "Twenty GPUs. That was somebody's whole cluster.", 40: "Forty GPUs. The labs are hiring a GPU detective.", }; export function createGame(seed = 1, opts = {}) { const x = opts.x ?? START.x; const z = opts.z ?? START.z; const game = { seed, random: rng(seed), time: 0, state: "title", started: 0, p: { x, z, y: groundAt(x, z), heading: opts.heading ?? START.heading, speed: 0, vy: 0, air: false, airTime: 0, steer: 0, yawRate: 0, lean: 0, pitch: 0, crouch: 0, tumble: 0, spin: 0, lastHop: -9, trail: [], trailClock: 0, grade: 0, accel: 0, lastSpeed: 0, burrito: 0 }, stats: { meters: 0, top: 0, airSeconds: 0, longestAir: 0, jumps: 0, tumbles: 0, gpus: 0, burritos: 0 }, msg: null, banner: null, place: placeAt(x, z), traffic: createTraffic(seed), poses: [], items: createItems(), walkers: [], view: { aspect: opts.aspect ?? 1.6 }, cam: createCam(), }; game.poses = posesAt(game.traffic, 0); stepCam(game.cam, game, DT); return game; } export function start(game) { game.state = "play"; game.started = game.time; game.banner = { text: game.place.district, sub: game.place.street, at: game.time }; return game; } /** Room for a circle of the scooter's size: every edge of it on a street, sidewalk or pier. */ function roomAt(x, z, r) { return rideable(x, z) && rideable(x + r, z) && rideable(x - r, z) && rideable(x, z + r) && rideable(x, z - r); } function tumble(game, hitSpeed, normal) { const p = game.p; p.tumble = RIDE_FEEL.tumble; p.spin = 0; p.speed = clamp(-p.speed * 0.15, -2, 2); // She gets up facing away from whatever she hit. p.recover = normal ? Math.atan2(normal[0], -normal[1]) : p.heading; game.stats.tumbles++; game.msg = { text: pick(game.random, TUMBLES), at: game.time, kind: "tumble", hit: hitSpeed }; } /** Moves Claudia along her heading in steps short enough that she can't pass through a post * or a man with a cart, even on a burrito. */ function move(game, dt, canTumble) { const n = Math.max(1, Math.ceil((Math.abs(game.p.speed) * dt) / 0.3)); for (let i = 0; i < n; i++) moveOnce(game, dt / n, canTumble && game.p.tumble === 0); } /** One step along her heading, sliding off walls, bouncing off things, dodging nothing. */ function moveOnce(game, dt, canTumble) { const p = game.p; const r = RIDE_FEEL.radius; const fx = Math.sin(p.heading); const fz = -Math.cos(p.heading); const vx = fx * p.speed; const vz = fz * p.speed; let nx = p.x + vx * dt; let nz = p.z + vz * dt; let hit = 0; let normal = null; let blocked = false; const harder = (speed, n) => { if (speed > hit) { hit = speed; normal = n; } }; if (!roomAt(nx, p.z, r)) { harder(Math.abs(vx), [-Math.sign(vx), 0]); nx = p.x; blocked = true; } if (!roomAt(nx, nz, r)) { harder(Math.abs(vz), [0, -Math.sign(vz)]); nz = p.z; blocked = true; } const above = p.y - groundAt(nx, nz); const o = obstacleHit(nx, nz, r, above); if (o) { const vn = vx * o.nx + vz * o.nz; if (vn < 0) harder(-vn, [o.nx, o.nz]); const px = nx + o.nx * o.depth; const pz = nz + o.nz * o.depth; if (roomAt(px, pz, r)) { nx = px; nz = pz; } else { nx = p.x; nz = p.z; } blocked = true; } for (const pose of game.poses) { if (Math.abs(pose.x - nx) > 10 || Math.abs(pose.z - nz) > 10) continue; if (above > pose.v.hgt) continue; const h = vehicleHit(pose, nx, nz, r); if (!h) continue; const wx = Math.sin(pose.heading) * pose.speed; const wz = -Math.cos(pose.heading) * pose.speed; const vn = (vx - wx) * h.nx + (vz - wz) * h.nz; if (vn < 0) harder(-vn, [h.nx, h.nz]); const px = nx + h.nx * (h.depth + 0.05); const pz = nz + h.nz * (h.depth + 0.05); if (roomAt(px, pz, r)) { nx = px; nz = pz; } blocked = true; } for (const w of game.walkers) { const h = walkerHit(w, nx, nz, r, above); if (!h) continue; const vn = vx * h.nx + vz * h.nz; if (vn < 0) harder(-vn, [h.nx, h.nz]); const px = nx + h.nx * (h.depth + 0.02); const pz = nz + h.nz * (h.depth + 0.02); if (roomAt(px, pz, r)) { nx = px; nz = pz; } else { nx = p.x; nz = p.z; } blocked = true; } const moved = Math.hypot(nx - p.x, nz - p.z); if (blocked) p.speed = Math.sign(p.speed) * Math.min(Math.abs(p.speed), moved / dt); p.x = nx; p.z = nz; game.stats.meters += moved; if (canTumble && hit > RIDE_FEEL.tumbleAt) tumble(game, hit, normal); } /** Stays on the ground unless the ground falls away faster than gravity: then she flies. */ function vertical(game, dt) { const p = game.p; const fx = Math.sin(p.heading); const fz = -Math.cos(p.heading); const ahead = groundAt(p.x + fx * 0.8, p.z + fz * 0.8); const behind = groundAt(p.x - fx * 0.8, p.z - fz * 0.8); p.grade = clamp((ahead - behind) / 1.6, -0.6, 0.6); const gh = groundAt(p.x, p.z); if (p.air) { p.vy -= G * dt; p.y += p.vy * dt; p.airTime += dt; game.stats.airSeconds += dt; if (p.y <= gh) { p.y = gh; p.air = false; if (p.airTime > 0.3) { game.stats.jumps++; if (p.airTime > game.stats.longestAir) game.stats.longestAir = p.airTime; if (p.airTime > 0.45) game.msg = { text: `Air ${p.airTime.toFixed(1)} s`, at: game.time, kind: "air" }; } p.vy = p.speed * p.grade * 0.7; p.landed = game.time; } return; } const ballistic = p.y + p.vy * dt - 0.5 * G * dt * dt; if (gh < ballistic - 0.02) { p.air = true; p.airTime = 0; p.y = ballistic; p.vy -= G * dt; } else { p.y = gh; p.vy = p.speed * p.grade * 0.7; } } /** The new speed, held under `cap`: past it (when a burrito wears off) she slows at 30 m/s² * instead of stopping dead at the limit. */ function capped(v, next, cap, dt) { if (next < -RIDE_FEEL.reverse) return -RIDE_FEEL.reverse; if (next <= cap) return next; return Math.max(cap, Math.min(next, v - 30 * dt)); } /** Rolling over a GPU takes it; rolling over a burrito doubles her speed for a few seconds. */ function pickups(game) { const p = game.p; if (p.tumble > 0) return; const items = game.items; const reach = ITEM_FEEL.reach; const close = (o) => Math.abs(o.x - p.x) < reach && Math.abs(o.z - p.z) < reach && Math.hypot(o.x - p.x, o.z - p.z) < reach && p.y - o.y < 1.6; for (const g of items.gpus) { if (items.taken.gpus.has(g.id) || !close(g)) continue; items.taken.gpus.add(g.id); const n = ++game.stats.gpus; game.msg = { text: GPU_LINES[n] ?? `${n} GPUs`, at: game.time, kind: "gpu" }; } for (const b of items.burritos) { if (!burritoHere(items, b.id, game.time) || !close(b)) continue; items.taken.burritos.set(b.id, game.time); game.stats.burritos++; p.burrito = ITEM_FEEL.burrito; if (p.speed > -0.5) p.speed = Math.min(RIDE_FEEL.top * 2, Math.max(p.speed, 0) + ITEM_FEEL.burritoKick); game.msg = { text: "Burrito! Double speed", at: game.time, kind: "burrito" }; } } /** Advances the game by dt seconds. * Input: { throttle, brake, steer (-1 left … 1 right), boost, hop }. */ export function step(game, input = {}, dt = DT) { game.time += dt; if (game.state === "over") return game; game.poses = posesAt(game.traffic, game.time); const p = game.p; game.walkers = walkersNear(game.items, p.x, p.z, 40, game.time); if (game.state === "play") { const throttle = clamp(input.throttle ?? 0, 0, 1); const brake = clamp(input.brake ?? 0, 0, 1); const steer = clamp(input.steer ?? 0, -1, 1); // Boost works while she's pushing on: hold Shift with the throttle. p.boost = !!input.boost && throttle > 0 && brake === 0 && p.tumble === 0; // A burrito doubles how fast she goes, easing back over its last half second. p.burrito = Math.max(0, p.burrito - dt); const fed = 1 + clamp(p.burrito / 0.5, 0, 1); if (p.boost) game.stats.boostSeconds = (game.stats.boostSeconds ?? 0) + dt; if (p.tumble > 0) { p.tumble = Math.max(0, p.tumble - dt); p.spin += dt; p.speed *= Math.exp(-5 * dt); p.steer = 0; p.yawRate = 0; move(game, dt, false); if (p.tumble === 0) { p.speed = 0; p.spin = 0; p.heading = p.recover ?? p.heading; } } else { p.steer += (steer - p.steer) * Math.min(1, dt * 12); const v = p.speed; const grip = p.air ? 0.2 : 1; const yawMax = lerp(RIDE_FEEL.yawSlow, RIDE_FEEL.yawFast, clamp(Math.abs(v) / 20, 0, 1)); // At a standstill she can still turn the scooter by hand while pushing off. const turning = clamp(Math.max(Math.abs(v), throttle * 1.6) / 2.5, 0, 1); p.yawRate = p.steer * yawMax * turning * (v < -0.2 ? -1 : 1) * grip; p.heading += p.yawRate * dt; let a = 0; if (!p.air) { const idle = throttle === 0 && brake === 0 && Math.abs(v) < 0.8; if (idle) p.speed = 0; else { const [push, pushTop] = p.boost ? [RIDE_FEEL.boost, RIDE_FEEL.boostTop] : [RIDE_FEEL.motor, RIDE_FEEL.motorTop]; if (throttle > 0) a += v >= -0.3 ? push * (1 + (fed - 1) * 0.5) * Math.max(0, 1 - v / (pushTop * fed)) * throttle : RIDE_FEEL.brake * throttle; if (brake > 0) a -= v > 0.3 ? RIDE_FEEL.brake * brake : v > -RIDE_FEEL.reverse ? 5 * brake : 0; a -= Math.sign(v) * (RIDE_FEEL.roll + RIDE_FEEL.drag * v * v); a -= G * p.grade * RIDE_FEEL.hills; p.speed = capped(v, v + a * dt, RIDE_FEEL.top * fed, dt); } if (input.hop && game.time - p.lastHop > 0.4) { p.vy = RIDE_FEEL.hop + Math.max(0, p.vy); p.air = true; p.airTime = 0; p.lastHop = game.time; } } else { p.speed = capped(v, v - Math.sign(v) * RIDE_FEEL.drag * v * v * dt, RIDE_FEEL.top * fed, dt); } move(game, dt, true); } vertical(game, dt); pickups(game); if (Math.abs(p.speed) > game.stats.top) game.stats.top = Math.abs(p.speed); // how hard she is speeding up, smoothed (her hair streams back with it) const gain = (Math.abs(p.speed) - p.lastSpeed) / dt; p.accel += (gain - p.accel) * Math.min(1, dt * 4); p.lastSpeed = Math.abs(p.speed); // How she looks: lean into turns, nose with the hill, crouch at speed. const leanTo = p.air || p.tumble > 0 ? p.lean * 0.9 : -p.steer * clamp(Math.abs(p.speed) / 12, 0, 1) * 0.5; p.lean += (leanTo - p.lean) * Math.min(1, dt * 9); const pitchTo = p.air ? clamp(Math.atan2(p.vy, Math.max(4, Math.abs(p.speed))) * 0.6, -0.5, 0.5) : Math.atan(p.grade); p.pitch += (pitchTo - p.pitch) * Math.min(1, dt * 10); p.crouch += (clamp(Math.abs(p.speed) / 16, 0, 1) + (p.air ? 0.4 : 0) - p.crouch) * Math.min(1, dt * 6); // Her trail: where the back wheel has been. p.trailClock += dt; if (p.trailClock >= 1 / 30) { p.trailClock = 0; p.trail.push([p.x - Math.sin(p.heading) * 0.45, p.y + 0.06, p.z + Math.cos(p.heading) * 0.45, Math.abs(p.speed), p.boost || p.burrito > 0 ? 1 : 0]); if (p.trail.length > 20) p.trail.shift(); } const place = placeAt(p.x, p.z); if (place.district !== game.place.district) game.banner = { text: place.district, sub: place.street, at: game.time }; game.place = place; } stepCam(game.cam, game, dt); return game; } /** Runs the game for `seconds`, asking `control(game)` for input every step. */ export function simulate(game, seconds, control = () => ({})) { for (let i = 0; i < Math.round(seconds / DT); i++) step(game, control(game)); return game; } /** A short summary of the ride so far, for the pause card. */ export function summary(game) { const s = game.stats; return { miles: s.meters / 1609.34, topMph: s.top * 2.23694, longestAir: s.longestAir, jumps: s.jumps, tumbles: s.tumbles, gpus: s.gpus, burritos: s.burritos, minutes: (game.time - game.started) / 60, }; }