// The autopilot: rides a loop through the neighborhood the way a fearless local would. Up // Telegraph Hill, down Montgomery over the crests, up California beside the cable car, down // Pine and back along the Embarcadero. The checks use it to measure the ride, and the // screenshot routes use it to put Claudia somewhere worth seeing. import { CALIFORNIA, LANES, XS, ZS } from "./city.js"; const street = (list, name) => list.find((s) => s.name === name).c; const OFF = 3.2; /** The loop, as lane centers at each corner, riding on the right. */ export const ROUTE = [ [LANES.bikeNorth, street(ZS, "Greenwich St") - OFF], [street(XS, "Montgomery St") - OFF, street(ZS, "Greenwich St") - OFF], [street(XS, "Montgomery St") - OFF, CALIFORNIA.c - 4.6], [street(XS, "Stockton St") - OFF, CALIFORNIA.c - 4.6], [street(XS, "Stockton St") - OFF, street(ZS, "Pine St") + OFF], [street(XS, "Battery St") - OFF, street(ZS, "Pine St") + OFF], [street(XS, "Battery St") - OFF, LANES.marketBikeEast], [LANES.bikeNorth, LANES.marketBikeEast], ]; const SEGS = ROUTE.map((a, k) => { const b = ROUTE[(k + 1) % ROUTE.length]; const len = Math.hypot(b[0] - a[0], b[1] - a[1]); return { a, b, len, dx: (b[0] - a[0]) / len, dz: (b[1] - a[1]) / len }; }); const LOOP = SEGS.reduce((s, g) => s + g.len, 0); /** Distance along the loop of the closest point to (x, z). */ function project(x, z) { let best = Infinity; let at = 0; let acc = 0; for (const g of SEGS) { const t = Math.max(0, Math.min(g.len, (x - g.a[0]) * g.dx + (z - g.a[1]) * g.dz)); const px = g.a[0] + g.dx * t; const pz = g.a[1] + g.dz * t; const d = Math.hypot(x - px, z - pz); if (d < best) { best = d; at = acc + t; } acc += g.len; } return { s: at, off: best }; } function pointAt(s) { let u = ((s % LOOP) + LOOP) % LOOP; for (const g of SEGS) { if (u <= g.len) return { x: g.a[0] + g.dx * u, z: g.a[1] + g.dz * u }; u -= g.len; } return { x: ROUTE[0][0], z: ROUTE[0][1] }; } /** Meters from s to the next corner of the loop. */ function toCorner(s) { let u = ((s % LOOP) + LOOP) % LOOP; for (const g of SEGS) { if (u < g.len) return g.len - u; u -= g.len; } return 0; } const wrap = (a) => Math.atan2(Math.sin(a), Math.cos(a)); /** The nearest man or cart in her path: how far ahead, and which way to swerve (+1 right). */ function ahead(game, speed) { const p = game.p; const fx = Math.sin(p.heading); const fz = -Math.cos(p.heading); const look = speed * 1.4 + 10; let best = null; for (const w of game.walkers ?? []) { for (const [x, z] of [[w.x, w.z], [w.cx, w.cz]]) { const dx = x - p.x; const dz = z - p.z; const along = dx * fx + dz * fz; const lat = dx * fz * -1 + dz * fx; // + to her right if (along < -0.5 || along > look || Math.abs(lat) > 2.2) continue; if (best && along >= best.dist) continue; // pass on the far side from the curb he came off: that way he isn't walking back into her const outX = w.w.dx * fz * -1 + w.w.dz * fx; best = { dist: along, side: Math.sign(outX) || (lat > 0 ? -1 : 1) }; } } return best; } export function autopilot(game) { const p = game.p; const bot = (game.bot ??= { stuck: 0, backing: 0 }); if (p.tumble > 0) return {}; const speed = p.speed; if (bot.backing > 0) { bot.backing -= 1 / 60; return { brake: 1, steer: 0.6 }; } if (Math.abs(speed) < 0.6) bot.stuck += 1 / 60; else bot.stuck = 0; if (bot.stuck > 1.5) { bot.stuck = 0; bot.backing = 0.8; } const here = project(p.x, p.z); const look = 6 + Math.max(0, speed) * 0.55; const target = pointAt(here.s + look); const want = Math.atan2(target.x - p.x, -(target.z - p.z)); const diff = wrap(want - p.heading); const steer = Math.max(-1, Math.min(1, diff * 2.4)); const corner = toCorner(here.s); const slowFor = corner < speed * 1.3 + 8 ? 8.5 : Math.abs(diff) > 0.5 ? 6 : 99; // A man with a cart ahead: swerve to the road side of him, and brake if he's close. const threat = ahead(game, speed); if (threat) { const dodge = Math.max(-1, Math.min(1, steer + threat.side * 0.9)); if (threat.dist < speed * 0.9 + 5 && speed > 6) return { brake: 1, steer: dodge }; return { throttle: 0.6, steer: dodge }; } if (speed > slowFor) return { brake: 0.8, steer }; // Boost down the long straights. const boost = corner > 95 && Math.abs(diff) < 0.12 && speed > 10 && !p.air; return { throttle: Math.abs(diff) > 1.2 ? 0.4 : 1, steer, boost }; } export const LOOP_LENGTH = LOOP; /** The loop's straight legs: { a, b, len, dx, dz }. */ export const autopilotRoute = () => SEGS;