// Things in the street: burritos that double her speed for a few seconds, GPUs to collect // before the labs can, and men pushing shopping carts who stumble off the curb into the road. // Pure and seeded like the rest of the rules: the layout is the same in every ride, and where // each man is at any moment depends only on the time. import { autopilotRoute } from "./bot.js"; import { LANES, RIDE, XS, ZS, groundAt, obstacleHit, surfaceAt } from "./city.js"; import { between, rng } from "./rng.js"; /** The street things' numbers. Critics: these are the feel. */ export const ITEM_FEEL = { reach: 1.2, // m, how close she has to pass to pick something up burrito: 4, // s of double speed burritoKick: 8, // m/s added the moment she takes a bite burritoBack: 25, // s until another burrito sits in the same spot walk: 0.85, // m/s, a man pushing a heavy cart cartAhead: 1.05, // m from the man to the middle of his cart }; // The streets she can ride, west to east and north to south. const xsIn = XS.filter((s) => s.c >= RIDE.x0 - 10 && s.c <= RIDE.x1); const zsIn = ZS.filter((s) => s.c >= RIDE.z0 - 20 && s.c <= RIDE.z1); /** Every stretch of road between two crossings: { axis, c, lanes, from, to, curbs }. A stretch * on a north–south street runs along z at x = c; one on an east–west street along x at z = c. */ function stretches() { const out = []; for (const s of xsIn) { const lanes = s.kind === "embarcadero" ? [LANES.bikeNorth, LANES.carNorth, LANES.carSouth, LANES.bikeSouth] : [s.c - 3.2, s.c + 3.2]; const curbs = s.kind === "embarcadero" ? [LANES.promenade[0], s.r0] : [s.r0, s.r1]; for (let j = 0; j + 1 < zsIn.length; j++) out.push({ axis: "z", street: s, c: s.c, lanes, curbs, from: zsIn[j].b1, to: zsIn[j + 1].b0 }); } for (const s of zsIn) { const lanes = [s.c - 3.2, s.c + 3.2]; const curbs = [s.r0, s.r1]; for (let i = 0; i + 1 < xsIn.length; i++) out.push({ axis: "x", street: s, c: s.c, lanes, curbs, from: xsIn[i].b1, to: xsIn[i + 1].b0 }); } return out.filter((r) => r.to - r.from > 20); } const at = (r, lane, u) => (r.axis === "z" ? { x: lane, z: r.from + (r.to - r.from) * u } : { x: r.from + (r.to - r.from) * u, z: lane }); /** A spot for something small: on the road, clear of posts, poles and planters. */ function clear(x, z) { return surfaceAt(x, z) === "road" && !obstacleHit(x, z, 0.9) && x <= RIDE.x1 && z >= RIDE.z0 && z <= RIDE.z1; } function layout() { const random = rng(20261201); const burritos = []; const gpus = []; const near = (list, x, z, d) => list.some((o) => Math.hypot(o.x - x, o.z - z) < d); const place = (list, x, z, gap) => { if (!clear(x, z) || near(burritos, x, z, gap) || near(gpus, x, z, gap)) return false; list.push({ x, z, y: groundAt(x, z), id: list.length }); return true; }; // Along the autopilot's loop, so every ride (and every review screenshot) meets some: a GPU // every 60 m, and a burrito at the start of each long straight. const route = autopilotRoute(); for (const g of route) { for (let s = 30; s < g.len - 20; s += 60) place(gpus, g.a[0] + g.dx * s, g.a[1] + g.dz * s, 8); if (g.len > 250) place(burritos, g.a[0] + g.dx * 45, g.a[1] + g.dz * 45, 8); } // And scattered through the rest of the neighborhood. for (const r of stretches()) { if (random() < 0.45) { const lane = r.lanes[Math.floor(random() * r.lanes.length)]; const { x, z } = at(r, lane, between(random, 0.25, 0.75)); place(gpus, x, z, 12); } if (random() < 0.14) { const lane = r.lanes[Math.floor(random() * r.lanes.length)]; const { x, z } = at(r, lane, between(random, 0.3, 0.7)); place(burritos, x, z, 12); } } return { burritos, gpus }; } /** The men with shopping carts. Each waits on the curb, then stumbles out into the road * pushing his cart, stands there a while, turns round and goes back. */ function walkers() { const random = rng(77031); const list = []; for (const r of stretches()) { if (random() > 0.32) continue; const side = random() < 0.5 ? 0 : 1; const curb = r.curbs[side]; const inward = Math.sign((r.curbs[0] + r.curbs[1]) / 2 - curb); // toward the middle of the road const u = between(random, 0.2, 0.8); const along = r.from + (r.to - r.from) * u; // the curb point, and how far into the road he gets const x0 = r.axis === "z" ? curb - inward * 1.2 : along; const z0 = r.axis === "z" ? along : curb - inward * 1.2; const dx = r.axis === "z" ? inward : 0; const dz = r.axis === "z" ? 0 : inward; if (surfaceAt(x0 + dx * 4, z0 + dz * 4) !== "road") continue; const depth = between(random, 4.5, r.street.kind === "embarcadero" ? 11 : 8.5); const walk = depth / ITEM_FEEL.walk; const wait = between(random, 5, 14); const linger = between(random, 2, 5); list.push({ id: list.length, x0, z0, dx, dz, depth, wait, walk, linger, period: wait + walk * 2 + linger + 1.2, phase: between(random, 0, 40), sway: between(random, 0.6, 1.1), look: random() }); } return list; } let LAYOUT = null; /** The street things for a new ride: the same layout every time, nothing taken yet. */ export function createItems() { LAYOUT ??= { ...layout(), walkers: walkers() }; return { burritos: [...LAYOUT.burritos], gpus: [...LAYOUT.gpus], walkers: [...LAYOUT.walkers], taken: { burritos: new Map(), gpus: new Set() } }; } const smooth = (t) => t * t * (3 - 2 * t); /** Where walker w is at time t: the man, his cart, which way he faces, and how he moves. */ export function walkerAt(w, t) { let c = (t + w.phase) % w.period; let out = 0; // 0 on the curb … 1 as far out as he goes let heading = 1; // +1 pushing out, −1 pushing back let moving = 0; if (c < w.wait) out = 0; else if ((c -= w.wait) < w.walk) { out = smooth(c / w.walk); moving = 1; } else if ((c -= w.walk) < w.linger) out = 1; else if ((c -= w.linger) < 0.6) { out = 1; heading = -1; // turning round } else if ((c -= 0.6) < w.walk) { out = 1 - smooth(c / w.walk); heading = -1; moving = 1; } else { out = 0; heading = -1; } // the stumble: a slow weave side to side while he moves const weave = moving * Math.sin((t + w.phase) * 1.7 * w.sway) * 0.35; const sx = -w.dz; const sz = w.dx; const x = w.x0 + w.dx * w.depth * out + sx * weave; const z = w.z0 + w.dz * w.depth * out + sz * weave; const fx = w.dx * heading; const fz = w.dz * heading; return { w, x, z, cx: x + fx * ITEM_FEEL.cartAhead, cz: z + fz * ITEM_FEEL.cartAhead, heading: Math.atan2(fx, -fz) + weave * 0.25, moving, stride: (t + w.phase) * 2.2, }; } /** The walkers within `range` of (x, z), posed at time t. */ export function walkersNear(items, x, z, range, t) { const out = []; for (const w of items.walkers) { if (Math.abs(w.x0 - x) > range + 12 || Math.abs(w.z0 - z) > range + 12) continue; out.push(walkerAt(w, t)); } return out; } /** Does a circle at (x, z) of radius r touch the man or his cart? Push-out normal if so. * `above`: how high her wheels are off the ground (a good hop clears the cart, not him). */ export function walkerHit(pose, x, z, r, above = 0) { for (const [cx, cz, cr, h] of [ [pose.cx, pose.cz, 0.55, 1.0], [pose.x, pose.z, 0.32, 1.8], ]) { if (above > h) continue; const dx = x - cx; const dz = z - cz; const d = Math.hypot(dx, dz); if (d < cr + r) return { nx: d > 1e-6 ? dx / d : 1, nz: d > 1e-6 ? dz / d : 0, depth: cr + r - d }; } return null; } /** Is the burrito at index k on the road at time t? They come back a while after a bite. */ export function burritoHere(items, k, t) { const took = items.taken.burritos.get(k); return took === undefined || t - took >= ITEM_FEEL.burritoBack; }