starfleet-ui
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#6 · main · 2 min 01 s
0Étoiles
0Forks
0Issues
| 1 | /** |
| 2 | * Route planning over the lane network, for previews and the trade helper. |
| 3 | * Times and fuel come from `rules.ts` (estimates; the server decides). |
| 4 | */ |
| 5 | import type { SystemDetail, Waypoint } from '../types'; |
| 6 | import { distance, fuelFor, jumpSeconds, tripSeconds } from './rules'; |
| 7 | |
| 8 | export interface GraphSystem { |
| 9 | symbol: string; |
| 10 | x: number; |
| 11 | y: number; |
| 12 | nebula: boolean; |
| 13 | gate: Waypoint | null; |
| 14 | lanes: { system: string; distance: number }[]; |
| 15 | } |
| 16 | |
| 17 | export interface GalaxyGraph { |
| 18 | systems: Map<string, GraphSystem>; |
| 19 | waypoints: Map<string, Waypoint>; |
| 20 | } |
| 21 | |
| 22 | export function buildGraph(details: readonly SystemDetail[]): GalaxyGraph { |
| 23 | const systems = new Map<string, GraphSystem>(); |
| 24 | const waypoints = new Map<string, Waypoint>(); |
| 25 | for (const d of details) { |
| 26 | const wps = d.waypoints.map((w) => w.data); |
| 27 | for (const w of wps) waypoints.set(w.symbol, w); |
| 28 | systems.set(d.symbol, { |
| 29 | symbol: d.symbol, |
| 30 | x: d.x, |
| 31 | y: d.y, |
| 32 | nebula: d.nebula, |
| 33 | gate: wps.find((w) => w.kind === 'jump_gate') ?? null, |
| 34 | lanes: d.lanes.map((l) => ({ system: l.system, distance: l.distance })) |
| 35 | }); |
| 36 | } |
| 37 | return { systems, waypoints }; |
| 38 | } |
| 39 | |
| 40 | export interface Leg { |
| 41 | kind: 'trip' | 'jump'; |
| 42 | from: string; |
| 43 | to: string; |
| 44 | distance: number; |
| 45 | seconds: number; |
| 46 | fuel: number; |
| 47 | } |
| 48 | |
| 49 | export interface TripPlan { |
| 50 | legs: Leg[]; |
| 51 | seconds: number; |
| 52 | fuel: number; |
| 53 | jumps: number; |
| 54 | /** Some leg flies inside a nebula: the real trip is slower than `seconds`. */ |
| 55 | nebula: boolean; |
| 56 | } |
| 57 | |
| 58 | function trip(graph: GalaxyGraph, from: Waypoint, to: Waypoint, speed: number): Leg { |
| 59 | const d = distance(from, to); |
| 60 | return { kind: 'trip', from: from.symbol, to: to.symbol, distance: d, seconds: tripSeconds(d, speed), fuel: fuelFor(d) }; |
| 61 | } |
| 62 | |
| 63 | /** Fastest chain of systems from `from` to `to` (Dijkstra on jump time), inclusive. */ |
| 64 | export function systemPath(graph: GalaxyGraph, from: string, to: string): string[] | null { |
| 65 | if (!graph.systems.has(from) || !graph.systems.has(to)) return null; |
| 66 | const dist = new Map<string, number>([[from, 0]]); |
| 67 | const prev = new Map<string, string>(); |
| 68 | const open = new Set([from]); |
| 69 | while (open.size) { |
| 70 | let cur = ''; |
| 71 | let best = Infinity; |
| 72 | for (const s of open) { |
| 73 | const d = dist.get(s)!; |
| 74 | if (d < best) [best, cur] = [d, s]; |
| 75 | } |
| 76 | open.delete(cur); |
| 77 | if (cur === to) break; |
| 78 | for (const lane of graph.systems.get(cur)?.lanes ?? []) { |
| 79 | if (!graph.systems.has(lane.system)) continue; |
| 80 | const nd = best + jumpSeconds(lane.distance); |
| 81 | if (nd < (dist.get(lane.system) ?? Infinity)) { |
| 82 | dist.set(lane.system, nd); |
| 83 | prev.set(lane.system, cur); |
| 84 | open.add(lane.system); |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | if (!dist.has(to)) return null; |
| 89 | const path = [to]; |
| 90 | while (path[0] !== from) path.unshift(prev.get(path[0])!); |
| 91 | return path; |
| 92 | } |
| 93 | |
| 94 | /** Plan a trip between two waypoints, crossing systems through their gates. */ |
| 95 | export function planTrip(graph: GalaxyGraph, fromWp: string, toWp: string, speed: number): TripPlan | null { |
| 96 | const from = graph.waypoints.get(fromWp); |
| 97 | const to = graph.waypoints.get(toWp); |
| 98 | if (!from || !to) return null; |
| 99 | |
| 100 | const legs: Leg[] = []; |
| 101 | if (from.system === to.system) { |
| 102 | if (from.symbol !== to.symbol) legs.push(trip(graph, from, to, speed)); |
| 103 | } else { |
| 104 | const path = systemPath(graph, from.system, to.system); |
| 105 | if (!path) return null; |
| 106 | const gates = path.map((s) => graph.systems.get(s)!.gate); |
| 107 | if (gates.some((g) => !g)) return null; |
| 108 | if (from.symbol !== gates[0]!.symbol) legs.push(trip(graph, from, gates[0]!, speed)); |
| 109 | for (let i = 1; i < path.length; i++) { |
| 110 | const lane = graph.systems.get(path[i - 1])!.lanes.find((l) => l.system === path[i])!; |
| 111 | legs.push({ |
| 112 | kind: 'jump', |
| 113 | from: gates[i - 1]!.symbol, |
| 114 | to: gates[i]!.symbol, |
| 115 | distance: lane.distance, |
| 116 | seconds: jumpSeconds(lane.distance), |
| 117 | fuel: fuelFor(lane.distance) |
| 118 | }); |
| 119 | } |
| 120 | const last = gates[gates.length - 1]!; |
| 121 | if (last.symbol !== to.symbol) legs.push(trip(graph, last, to, speed)); |
| 122 | } |
| 123 | |
| 124 | const touched = new Set([from.system, to.system, ...legs.map((l) => graph.waypoints.get(l.to)?.system ?? '')]); |
| 125 | return { |
| 126 | legs, |
| 127 | seconds: legs.reduce((a, l) => a + l.seconds, 0), |
| 128 | fuel: legs.reduce((a, l) => a + l.fuel, 0), |
| 129 | jumps: legs.filter((l) => l.kind === 'jump').length, |
| 130 | nebula: [...touched].some((s) => graph.systems.get(s)?.nebula) |
| 131 | }; |
| 132 | } |