import type { Session } from '../../../session/Session'; import { pool } from '../repo/q'; import { getPilot, exploredSet } from '../repo/players'; import { lanes, portsIn, getUniverse, regionOf } from '../repo/world'; import { planetsFor } from '../repo/planets'; import { defensesFor, MODE_NAMES } from '../repo/defenses'; import { shortestPath, distances } from '../engine/pathfind'; import { patternTag, COMMODITIES } from '../engine/commodities'; import { planetType } from '../data/planets'; import { ship } from '../data/ships'; import { autopilot } from './move'; import { header, padRight, padLeft, commas, esc, readNumber, confirm } from './ui'; export async function computer(s: Session, uid: number): Promise { for (;;) { await s.println(); await s.println(header('Nav Computer')); await s.println('|08 [|15P|08]|07lot course |08[|15R|08]|07eport: nearby ports |08[|15F|08]|07ind port type |08[|15A|08]|07ssets'); await s.println('|08 [|15T|08]|07racers |08[|15E|08]|07xplored stats |08[|15Q|08]|07uit computer'); const k = await s.promptChoice('|08 Computer|07: ', 'PRFATEQ\r'); await s.println(); if (k === 'Q' || k === '\r') return; if (k === 'P') await plot(s, uid); if (k === 'R') await portReport(s, uid); if (k === 'F') await findPort(s, uid); if (k === 'A') await assets(s, uid); if (k === 'T') await tracers(s, uid); if (k === 'E') await exploredStats(s, uid); } } async function plot(s: Session, uid: number): Promise { const p = (await getPilot(pool, uid))!; await s.print(`|03 Destination sector |08(1-${getUniverse().size})|03: `); const to = await readNumber(s, 1, getUniverse().size, null, 5); if (to === null) return; await s.print('|03 Sectors to avoid |08(space separated, Enter=none)|03: '); const avoid = new Set((await s.readLine({ maxLen: 40 })).split(/[\s,]+/).filter(Boolean).map(Number).filter((n) => n > 0)); const path = shortestPath(lanes(), p.sector, to, avoid); if (!path) return void (await s.println(' |12No route found.')); const known = await exploredSet(uid); await s.println(` |07Route: ${path.map((x) => (known.has(x) ? `|11${x}` : `|08(${x})`)).join(' |08> ')}`); const cost = (path.length - 1) * ship(p.ship_type).turnsPerWarp; await s.println(` |07${path.length - 1} jumps, |15${cost}|07 turns.`); if (path.length > 1 && (await confirm(s, ' Engage autopilot?', false))) await autopilot(s, uid, path.slice(1)); } /** Ports you've visited, nearest first. */ async function portReport(s: Session, uid: number): Promise { const p = (await getPilot(pool, uid))!; const known = await exploredSet(uid); const dist = distances(lanes(), p.sector); const ports = (await portsIn([...known])).sort((a, b) => (dist.get(a.sector) ?? 999) - (dist.get(b.sector) ?? 999)).slice(0, 16); if (!ports.length) return void (await s.println(' |07You have not visited any ports yet.')); await s.println(`|03 ${padRight('Sector', 8)}${padRight('Jumps', 6)}${padRight('Port', 22)}${padRight('Type', 6)}` + COMMODITIES.map((c) => padLeft(c.name, 11)).join('')); for (const pt of ports) { await s.println( ` |15${padRight(String(pt.sector), 8)}|07${padRight(String(dist.get(pt.sector) ?? '?'), 6)}|14${padRight(esc(pt.name).slice(0, 21), 22)}${padRight(patternTag(pt.pattern), 6)}` + pt.stock.map((x, i) => (pt.pattern[i] === 'B' ? '|12' : '|10') + padLeft(commas(x), 11)).join(''), ); } await s.println('|08 Red = units the port will buy, green = units for sale.'); } /** Nearest explored port that buys (or sells) a given commodity. */ async function findPort(s: Session, uid: number): Promise { const p = (await getPilot(pool, uid))!; await s.println(' |07Find a port that: |08[|151|08] buys Ore [|152|08] buys Biomass [|153|08] buys Circuitry'); await s.println(' |08[|154|08] sells Ore [|155|08] sells Biomass [|156|08] sells Circuitry'); const k = await s.promptChoice('|08 Choice|07: ', '123456\r'); await s.println(); if (k === '\r') return; const n = Number(k) - 1; const c = n % 3; const want = n < 3 ? 'B' : 'S'; const known = await exploredSet(uid); const dist = distances(lanes(), p.sector); const hits = (await portsIn([...known])).filter((pt) => pt.pattern[c] === want).sort((a, b) => (dist.get(a.sector) ?? 999) - (dist.get(b.sector) ?? 999)).slice(0, 8); if (!hits.length) return void (await s.println(' |07None among the sectors you have explored.')); for (const h of hits) await s.println(` |15${padRight(String(h.sector), 6)}|14${padRight(esc(h.name), 24)}|07${dist.get(h.sector) ?? '?'} jumps, ${commas(h.stock[c])} units`); } async function assets(s: Session, uid: number): Promise { const p = (await getPilot(pool, uid))!; const planets = await planetsFor(uid, p.syndicate_id); const defs = await defensesFor(uid, p.syndicate_id); await s.println('|15 Planets'); if (!planets.length) await s.println(' |08none'); for (const pl of planets) { await s.println( ` |10${padRight(esc(pl.name), 18)}|07sector |15${padRight(String(pl.sector), 6)}|08${padRight(planetType(pl.type).name, 9)}|07B${pl.bastion}` + ` |07col ${commas(pl.colonists)} ftr ${commas(pl.fighters)} ` + pl.stock.map((x) => commas(x)).join('/'), ); } await s.println('|15 Sector defenses'); if (!defs.length) await s.println(' |08none'); for (const d of defs) { await s.println(` |07sector |15${padRight(String(d.sector), 6)}|07${padRight(commas(d.fighters) + ' ftrs', 12)}|08${padRight(MODE_NAMES[d.mode], 10)}|07${d.mines} mines |08(${esc(d.owner_handle)})`); } } async function tracers(s: Session, uid: number): Promise { const { rows } = await pool.query<{ handle: string; sector: number; landed_planet: number | null; placed_at: Date }>( `SELECT u.handle, p.sector, p.landed_planet, t.placed_at FROM sl_tracers t JOIN sl_players p ON p.user_id = t.target_id JOIN users u ON u.id = t.target_id WHERE t.owner_id = $1 ORDER BY u.handle`, [uid], ); if (!rows.length) return void (await s.println(' |07No active tracers. Tag ships with T at the sector prompt.')); for (const r of rows) { await s.println(` |11${padRight(esc(r.handle), 22)}|07sector |15${padRight(String(r.sector), 6)}|08${r.landed_planet ? '(landed on a planet)' : regionOf(r.sector)}`); } } async function exploredStats(s: Session, uid: number): Promise { const known = await exploredSet(uid); const size = getUniverse().size; await s.println(` |07You have charted |15${commas(known.size)}|07 of |15${commas(size)}|07 sectors (${((known.size / size) * 100).toFixed(1)}%).`); }