// Beacon's rules: pure and deterministic. The same seed and the same inputs give the same game, // which is what makes the screenshot routes and the autopilot's scores comparable between builds. export const W = 960; export const H = 540; export const LAMP = { x: W / 2, y: H - 22 }; export const BEAM = { halfAngle: 0.23, range: 560, turnRate: 1.9 }; export const MIN_ANGLE = -Math.PI + 0.12; export const MAX_ANGLE = -0.12; export const DT = 1 / 60; const SHIP_RADIUS = 7; const LOOKAHEAD = 130; const STEER_RATE = 1.2; const STARTING_LIVES = 3; /** A small seeded random number generator (mulberry32). */ export function rng(seed) { let a = seed >>> 0; return () => { a = (a + 0x6d2b79f5) >>> 0; let t = a; t = Math.imul(t ^ (t >>> 15), t | 1); t ^= t + Math.imul(t ^ (t >>> 7), t | 61); return ((t ^ (t >>> 14)) >>> 0) / 4294967296; }; } function placeRocks(random) { const rocks = []; for (let tries = 0; rocks.length < 7 && tries < 500; tries++) { const r = 16 + random() * 14; const x = 180 + random() * 620; const y = 80 + random() * 330; const clear = Math.hypot(x - LAMP.x, y - LAMP.y) > 150 && rocks.every((o) => Math.hypot(o.x - x, o.y - y) > o.r + r + 46); if (clear) rocks.push({ x, y, r }); } return rocks; } export function createGame(seed = 1, opts = {}) { const random = rng(seed); return { seed, random, time: 0, state: "title", beam: -Math.PI / 2, rocks: placeRocks(random), ships: [], effects: [], nextShip: 2.5, spawned: 0, saved: 0, lives: opts.lives ?? STARTING_LIVES, nextId: 1, }; } export function start(game) { game.state = "play"; game.ships = []; game.effects = []; game.nextShip = 1.5; game.spawned = 0; game.saved = 0; return game; } function wrap(a) { while (a > Math.PI) a -= 2 * Math.PI; while (a < -Math.PI) a += 2 * Math.PI; return a; } /** Whether a point is inside the beam. */ export function inBeam(game, x, y) { const d = Math.hypot(x - LAMP.x, y - LAMP.y); if (d > BEAM.range) return false; return Math.abs(wrap(Math.atan2(y - LAMP.y, x - LAMP.x) - game.beam)) <= BEAM.halfAngle; } /** The nearest rock this ship would hit sailing straight on, as the distance to it, or null. */ export function dangerAhead(game, ship, lookahead = LOOKAHEAD) { const dx = Math.cos(ship.heading); const dy = Math.sin(ship.heading); let best = null; for (const rock of game.rocks) { const rx = rock.x - ship.x; const ry = rock.y - ship.y; const along = rx * dx + ry * dy; if (along < 0 || along > lookahead) continue; const across = rx * dy - ry * dx; const clearance = rock.r + SHIP_RADIUS + 10; if (Math.abs(across) < clearance && (!best || along < best.distance)) best = { rock, distance: along, side: Math.sign(across) || 1 }; } return best; } function spawnShip(game) { const random = game.random; const speed = 34 + Math.min(45, game.spawned * 0.7); game.ships.push({ id: game.nextId++, x: -20, y: 70 + random() * 330, heading: (random() - 0.5) * 0.3, speed, lit: false, }); game.spawned++; const interval = Math.max(1.0, 3.8 - 0.05 * game.spawned); game.nextShip = interval * (0.8 + random() * 0.4); } /** * Advances the game by dt seconds. `input.aim` turns the beam toward an angle; `input.turn` * (-1, 0 or 1) turns it by hand. Either way it turns at most BEAM.turnRate radians a second. */ export function step(game, input = {}, dt = DT) { game.time += dt; const maxTurn = BEAM.turnRate * dt; if (typeof input.aim === "number") { const target = Math.min(MAX_ANGLE, Math.max(MIN_ANGLE, input.aim)); const diff = wrap(target - game.beam); game.beam += Math.max(-maxTurn, Math.min(maxTurn, diff)); } else if (input.turn) { game.beam += Math.sign(input.turn) * maxTurn; } game.beam = Math.min(MAX_ANGLE, Math.max(MIN_ANGLE, game.beam)); for (const e of game.effects) e.age += dt; game.effects = game.effects.filter((e) => e.age < e.life); // On the title screen the sea runs on its own, and nothing counts. const counting = game.state === "play"; if (!counting && game.state !== "title") return game; game.nextShip -= dt; if (game.nextShip <= 0) spawnShip(game); for (const ship of game.ships) { ship.lit = inBeam(game, ship.x, ship.y); if (ship.lit) { // A captain who can see steers away from the rock ahead, and back on course once clear. const danger = dangerAhead(game, ship); if (danger) ship.heading += (danger.side > 0 ? 1 : -1) * STEER_RATE * dt; else ship.heading -= Math.sign(ship.heading) * Math.min(Math.abs(ship.heading), 0.5 * dt); } if (ship.y < 30) ship.heading = Math.max(ship.heading, 0.2); if (ship.y > H - 70) ship.heading = Math.min(ship.heading, -0.2); ship.heading = Math.max(-1.2, Math.min(1.2, ship.heading)); ship.x += Math.cos(ship.heading) * ship.speed * dt; ship.y += Math.sin(ship.heading) * ship.speed * dt; } const kept = []; for (const ship of game.ships) { const hit = game.rocks.find((r) => Math.hypot(r.x - ship.x, r.y - ship.y) < r.r + SHIP_RADIUS - 2); if (hit) { if (counting) game.lives--; game.effects.push({ kind: "wreck", x: ship.x, y: ship.y, age: 0, life: 1.4, heading: ship.heading }); } else if (ship.x > W + 20) { if (counting) game.saved++; game.effects.push({ kind: "home", x: W - 8, y: ship.y, age: 0, life: 0.9 }); } else kept.push(ship); } game.ships = kept; if (counting && game.lives <= 0) { game.lives = 0; game.state = "over"; } return game; } /** Runs the game for `seconds`, asking `control(game)` for input every step. */ export function simulate(game, seconds, control = () => ({})) { const steps = Math.round(seconds / DT); for (let i = 0; i < steps; i++) step(game, control(game)); return game; }