// The autopilot: it lights the ship in the most danger. The title screen and the checks use it, // so its scores are a rough measure of how hard a build of the game is. import { LAMP, dangerAhead } from "./game.js"; export function autopilot(game) { let target = null; let soonest = Infinity; for (const ship of game.ships) { if (ship.x < -10) continue; const danger = dangerAhead(game, ship, 220); if (!danger) continue; const seconds = danger.distance / ship.speed; if (seconds < soonest) { soonest = seconds; target = ship; } } // Nothing in danger: watch the newest ship, which has the longest way to go. if (!target) for (const ship of game.ships) if (ship.x > -10 && (!target || ship.x < target.x)) target = ship; if (!target) return { aim: -Math.PI / 2 }; // Lead the ship a little: aim where it will be in a third of a second. const x = target.x + Math.cos(target.heading) * target.speed * 0.33; const y = target.y + Math.sin(target.heading) * target.speed * 0.33; return { aim: Math.atan2(y - LAMP.y, x - LAMP.x) }; }