gitflarePublicPartly agent-ready40
orbit-duel
Real-time two-player orbital combat game on Cloudflare Workers and Durable Objects.
- LicenseApache-2.0
- LanguageJavaScript
- Last landed
0413e56Oct 23, 2026
Add the Apache-2.0 licence
0413e56import { createRng } from './rng.js'; export const TICK_HZ = 30;export const DT = 1 / 30;export const WORLD = 1600;export const CX = 800;export const CY = 800;export const PLANET_R = 90;export const SHIP_R = 14;export const BULLET_R = 3;export const MAX_PLAYERS = 8;export const THRUST = 260;export const MAX_SPEED = 420;export const ROT = 4.5;export const DRAG = 0.15;export const GM = 4000000;export const BULLET_SPEED = 520;export const BULLET_LIFE = 1.6;export const COOLDOWN = 0.25;export const RAPID_COOLDOWN = 0.1;export const MAX_HP = 3;export const RESPAWN = 3;export const INVULN = 2;export const KILL_LIMIT = 10;export const MATCH_SECONDS = 240;export const RESULTS_SECONDS = 10;export const POWER_INTERVAL = 15;export const POWER_DURATION = 10;export const COUNTDOWN_SECONDS = 3;export const FLAG_DEAD = 1;export const FLAG_INVULN = 2;export const FLAG_SHIELD = 4;export const FLAG_TRIPLE = 8;export const FLAG_RAPID = 16; const _g = [0, 0];/** @param {number} x @param {number} y */export function gravity(x, y) { const dx = CX - x, dy = CY - y; const r = Math.sqrt(dx * dx + dy * dy) || 1; const a = GM / (Math.max(r, 120) ** 2); _g[0] = (dx / r) * a; _g[1] = (dy / r) * a; return _g;} /** Single movement path for ships (server step + client prediction). */export function stepShip(ship, input, dt) { if (input) { ship.angle += (input.rotate | 0) * ROT * dt; if (input.thrust) { ship.vx += Math.cos(ship.angle) * THRUST * dt; ship.vy += Math.sin(ship.angle) * THRUST * dt; } } const g = gravity(ship.x, ship.y); ship.vx += g[0] * dt; ship.vy += g[1] * dt; const k = 1 - DRAG * dt; ship.vx *= k; ship.vy *= k; const sp = Math.hypot(ship.vx, ship.vy); if (sp > MAX_SPEED) { ship.vx *= MAX_SPEED / sp; ship.vy *= MAX_SPEED / sp; } ship.x = wrap(ship.x + ship.vx * dt); ship.y = wrap(ship.y + ship.vy * dt); const TAU = Math.PI * 2; ship.angle = ((ship.angle % TAU) + TAU) % TAU;} /** Advances a bullet; returns false when expired or inside the planet. */export function stepBullet(b, dt) { b.life -= dt; const g = gravity(b.x, b.y); b.vx += g[0] * dt; b.vy += g[1] * dt; b.x = wrap(b.x + b.vx * dt); b.y = wrap(b.y + b.vy * dt); if (b.life <= 0) return false; return Math.hypot(b.x - CX, b.y - CY) >= PLANET_R;} function wrap(v) { return ((v % WORLD) + WORLD) % WORLD; }const r1 = (v) => Math.round(v * 10) / 10; export function sanitizeInput(raw) { if (!Array.isArray(raw) || raw[0] !== 'i') return null; const [, seq, rotate, thrust, fire, tick] = raw; if (!Number.isInteger(seq) || seq < 0 || seq > 2 ** 31) return null; if (rotate !== -1 && rotate !== 0 && rotate !== 1) return null; if (thrust !== 0 && thrust !== 1) return null; if (fire !== 0 && fire !== 1) return null; const t = Number.isInteger(tick) && tick >= 0 && tick < 2 ** 31 ? tick : null; return { seq, rotate, thrust, fire, tick: t };} export function createState(seed, config = {}) { return { seed, rng: createRng(seed), tick: 0, phase: 'waiting', phaseTicks: 0, matchTicks: 0, countdownTicks: 0, nextId: 1, ships: new Map(), bullets: [], powerups: [], powerTimer: POWER_INTERVAL, winner: null, config, results: null, };} function spawnPoint(state) { let best = null, bestD = -1; for (let i = 0; i < 10; i++) { const a = state.rng.range(0, Math.PI * 2), d = state.rng.range(250, 700); const x = CX + Math.cos(a) * d, y = CY + Math.sin(a) * d; let m = 1e9; for (const s of state.ships.values()) { if (s.dead) continue; m = Math.min(m, Math.hypot(s.x - x, s.y - y)); } if (m > bestD) { bestD = m; best = [x, y]; } } return best;} function respawn(state, s) { const [x, y] = spawnPoint(state); s.x = x; s.y = y; s.vx = 0; s.vy = 0; s.angle = state.rng.range(0, Math.PI * 2); s.hp = MAX_HP; s.dead = false; s.respawnT = 0; s.invuln = INVULN; s.shield = 0; s.triple = 0; s.rapid = 0; s.cool = 0;} export function addPlayer(state, id, name) { const s = { id, name, x: 0, y: 0, vx: 0, vy: 0, angle: 0, hp: MAX_HP, dead: false, respawnT: 0, invuln: 0, shield: 0, triple: 0, rapid: 0, cool: 0, kills: 0, deaths: 0, shots: 0, hits: 0 }; respawn(state, s); state.ships.set(id, s); return s;} export function removePlayer(state, id) { state.ships.delete(id); } function flags(s) { return (s.dead ? FLAG_DEAD : 0) | (s.invuln > 0 ? FLAG_INVULN : 0) | (s.shield > 0 ? FLAG_SHIELD : 0) | (s.triple > 0 ? FLAG_TRIPLE : 0) | (s.rapid > 0 ? FLAG_RAPID : 0);} function resetMatch(state) { state.matchTicks = 0; state.winner = null; state.results = null; state.bullets.length = 0; state.powerups.length = 0; state.powerTimer = POWER_INTERVAL; for (const s of state.ships.values()) { s.kills = 0; s.deaths = 0; s.shots = 0; s.hits = 0; respawn(state, s); }} function fireBullets(state, s, events) { const offs = s.triple > 0 ? [-0.2, 0, 0.2] : [0]; for (const off of offs) { const a = s.angle + off; const b = { id: state.nextId++, owner: s.id, spawn: state.tick, x: r1(s.x + Math.cos(a) * (SHIP_R + 4)), y: r1(s.y + Math.sin(a) * (SHIP_R + 4)), vx: r1(Math.cos(a) * BULLET_SPEED + s.vx), vy: r1(Math.sin(a) * BULLET_SPEED + s.vy), life: BULLET_LIFE }; state.bullets.push(b); events.push(['fire', b.id, b.owner, b.spawn, b.x, b.y, b.vx, b.vy]); } s.shots++; s.cool = s.rapid > 0 ? RAPID_COOLDOWN : COOLDOWN;} function killShip(state, s, killerId, events) { s.shield = 0; s.triple = 0; s.rapid = 0; s.dead = true; s.hp = 0; s.deaths++; s.respawnT = RESPAWN; const k = state.ships.get(killerId); if (k && k !== s) k.kills++; if (killerId) events.push(['kill', killerId, s.id]); else { events.push(['crash', s.id]); events.push(['kill', 0, s.id]); }} function endMatch(state, events) { let win = null; for (const s of state.ships.values()) if (!win || s.kills > win.kills) win = s; state.winner = win ? win.id : null; state.phase = 'results'; state.phaseTicks = 0; state.results = [...state.ships.values()].map((s) => ({ id: s.id, name: s.name, kills: s.kills, deaths: s.deaths, shots: s.shots, hits: s.hits, accuracy: s.shots ? Math.round((s.hits / s.shots) * 100) / 100 : 0 })); events.push(['phase', 'results']); events.push(['results', state.results.map((r) => [r.id, r.name, r.kills, r.deaths, r.shots, r.hits])]);} /** @returns {any[][]} events */export function step(state, inputs, dt = DT) { const events = []; state.tick++; const n = state.ships.size; if (state.phase === 'waiting') { if (n >= 2) { resetMatch(state); state.phase = 'countdown'; state.countdownTicks = 0; events.push(['phase', 'countdown']); } } else if (state.phase === 'countdown') { if (n < 2) { state.phase = 'waiting'; events.push(['phase', 'waiting']); } else if (++state.countdownTicks >= COUNTDOWN_SECONDS * TICK_HZ) { state.phase = 'playing'; events.push(['phase', 'playing']); } } else if (state.phase === 'playing') { state.matchTicks++; if (n < 2) { state.phase = 'waiting'; events.push(['phase', 'waiting']); } } else if (++state.phaseTicks >= RESULTS_SECONDS * TICK_HZ) { resetMatch(state); state.phase = n >= 2 ? 'countdown' : 'waiting'; state.countdownTicks = 0; events.push(['phase', state.phase]); } const ships = [...state.ships.values()].sort((a, b) => a.id - b.id); for (const s of ships) { s.invuln = Math.max(0, s.invuln - dt); s.shield = Math.max(0, s.shield - dt); s.triple = Math.max(0, s.triple - dt); s.rapid = Math.max(0, s.rapid - dt); s.cool = Math.max(0, s.cool - dt); if (s.dead) { s.respawnT -= dt; if (s.respawnT <= 0 && state.phase !== 'results') { respawn(state, s); events.push(['respawn', s.id]); } continue; } const inp = inputs && inputs[s.id]; const live = state.phase !== 'results' && state.phase !== 'countdown'; const use = inp && live ? inp : null; stepShip(s, use, dt); if (use && use.fire && s.cool <= 0) fireBullets(state, s, events); if (Math.hypot(s.x - CX, s.y - CY) < PLANET_R + SHIP_R) killShip(state, s, 0, events); } for (let i = state.bullets.length - 1; i >= 0; i--) { const b = state.bullets[i]; let gone = !stepBullet(b, dt); if (!gone && state.phase !== 'results') { for (const s of ships) { if (s.dead || s.id === b.owner || s.invuln > 0 || !state.ships.has(s.id)) continue; if (Math.hypot(s.x - b.x, s.y - b.y) < SHIP_R + BULLET_R) { gone = true; b.hit = true; const o = state.ships.get(b.owner); if (o) o.hits++; if (s.shield > 0) s.shield = 0; else s.hp--; events.push(['hit', b.id, s.id, Math.max(0, s.hp)]); if (s.hp <= 0) killShip(state, s, b.owner, events); break; } } } if (gone) { state.bullets.splice(i, 1); if (!b.hit) events.push(['expire', b.id]); } } if (state.phase === 'playing') { state.powerTimer -= dt; if (state.powerTimer <= 0) { state.powerTimer = POWER_INTERVAL; const kind = 1 + state.rng.int(3); const a = state.rng.range(0, Math.PI * 2), d = state.rng.range(200, 650); const p = { id: state.nextId++, kind, x: r1(CX + Math.cos(a) * d), y: r1(CY + Math.sin(a) * d) }; state.powerups.push(p); events.push(['power', p.id, kind, p.x, p.y]); } for (let i = state.powerups.length - 1; i >= 0; i--) { const p = state.powerups[i]; for (const s of ships) { if (s.dead || Math.hypot(s.x - p.x, s.y - p.y) > SHIP_R + 12) continue; if (p.kind === 1) s.shield = POWER_DURATION; else if (p.kind === 2) s.triple = POWER_DURATION; else s.rapid = POWER_DURATION; state.powerups.splice(i, 1); events.push(['pickup', s.id, p.kind]); break; } } let top = 0; for (const s of ships) top = Math.max(top, s.kills); if (top >= KILL_LIMIT || state.matchTicks >= MATCH_SECONDS * TICK_HZ) endMatch(state, events); } return events;} export function snapshotOf(state) { const ships = {}; for (const s of state.ships.values()) { ships[s.id] = [s.id, r1(s.x), r1(s.y), r1(s.vx), r1(s.vy), Math.round(s.angle * 1000) / 1000, s.hp, flags(s), s.kills]; } const ph = state.phase; const secs = ph === 'results' ? Math.ceil(Math.max(0, RESULTS_SECONDS - state.phaseTicks / TICK_HZ)) : ph === 'countdown' ? Math.ceil(Math.max(0, COUNTDOWN_SECONDS - state.countdownTicks / TICK_HZ)) : Math.ceil(Math.max(0, MATCH_SECONDS - state.matchTicks / TICK_HZ)); return { ships, meta: [ph === 'playing' ? 1 : ph === 'results' ? 2 : ph === 'countdown' ? 3 : 0, secs, KILL_LIMIT] };} export function extrasOf(state) { const ups = {}, names = {}; for (const p of state.powerups) ups[p.id] = [p.id, p.x, p.y, p.kind]; for (const s of state.ships.values()) names[s.id] = s.name; return { ups, names };} export function hashState(state) { let h = 2166136261; const mix = (v) => { const s = String(v); for (let i = 0; i < s.length; i++) h = Math.imul(h ^ s.charCodeAt(i), 16777619) >>> 0; }; mix(state.tick); mix(state.phase); mix(state.rng.state); for (const s of [...state.ships.values()].sort((a, b) => a.id - b.id)) { mix(s.id); mix(s.x); mix(s.y); mix(s.vx); mix(s.vy); mix(s.angle); mix(s.hp); mix(s.kills); } for (const b of state.bullets) { mix(b.id); mix(b.x); mix(b.y); } return h;}