ui/phase-16: cargo routes inspector + map pick foundation

Add per-planet cargo routes (COL/CAP/MAT/EMP) to the inspector with
a renderer-driven destination picker (faded out-of-reach planets,
cursor-line anchor, hover-highlight) and per-route arrows on the
map. The pick-mode primitives are exposed via `MapPickService` so
ship-group dispatch in Phase 19/20 can reuse the same surface.

Pass A — generic map foundation:
- hit-test now sizes the click zone to `pointRadiusPx + slopPx` so
  the visible disc is always part of the target.
- `RendererHandle` gains `onPointerMove`, `onHoverChange`,
  `setPickMode`, `getPickState`, `getPrimitiveAlpha`,
  `setExtraPrimitives`, `getPrimitives`. The click dispatcher is
  centralised: pick-mode swallows clicks atomically so the standard
  selection consumers do not race against teardown.
- `MapPickService` (`lib/map-pick.svelte.ts`) wraps the renderer
  contract in a promise-shaped `pick(...)`. The in-game shell
  layout owns the service so sidebar and bottom-sheet inspectors
  see the same instance.
- Debug-surface registry exposes `getMapPrimitives`,
  `getMapPickState`, `getMapCamera` to e2e specs without spawning a
  separate debug page after navigation.

Pass B — cargo-route feature:
- `CargoLoadType`, `setCargoRoute`, `removeCargoRoute` typed
  variants with `(source, loadType)` collapse rule on the order
  draft; round-trip through the FBS encoder/decoder.
- `GameReport` decodes `routes` and the local player's drive tech
  for the inline reach formula (40 × drive). `applyOrderOverlay`
  upserts/drops route entries for valid/submitting/applied
  commands.
- `lib/inspectors/planet/cargo-routes.svelte` renders the
  four-slot section. `Add` / `Edit` call `MapPickService.pick`,
  `Remove` emits `removeCargoRoute`.
- `map/cargo-routes.ts` builds shaft + arrowhead primitives per
  cargo type; the map view pushes them through
  `setExtraPrimitives` so the renderer never re-inits Pixi on
  route mutations (Pixi 8 doesn't support that on a reused
  canvas).

Docs:
- `docs/cargo-routes-ux.md` covers engine semantics + UI map.
- `docs/renderer.md` documents pick mode and the debug surface.
- `docs/calc-bridge.md` records the Phase 16 reach waiver.
- `PLAN.md` rewrites Phase 16 to reflect the foundation + feature
  split and the decisions baked in (map-driven picker, inline
  reach, optimistic overlay via `setExtraPrimitives`).

Tests:
- `tests/map-pick-mode.test.ts` — pure overlay-spec helper.
- `tests/map-cargo-routes.test.ts` — `buildCargoRouteLines`.
- `tests/inspector-planet-cargo-routes.test.ts` — slot rendering,
  picker invocation, collapse, cancel, remove.
- Extensions to `order-draft`, `submit`, `order-load`,
  `order-overlay`, `state-binding`, `inspector-planet`,
  `inspector-overlay`, `game-shell-sidebar`, `game-shell-header`.
- `tests/e2e/cargo-routes.spec.ts` — Playwright happy path: add
  COL, add CAP, remove COL, asserting both the inspector and the
  arrow count via `__galaxyDebug.getMapPrimitives()`.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
Ilia Denisov
2026-05-09 20:01:34 +02:00
parent 5fd67ed958
commit 7c8b5aeb23
43 changed files with 4559 additions and 98 deletions
+188 -3
View File
@@ -30,10 +30,12 @@ import {
Report,
} from "../proto/galaxy/fbs/report";
import type {
CargoLoadType,
CommandStatus,
OrderCommand,
ProductionType,
} from "../sync/order-types";
import { CARGO_LOAD_TYPE_VALUES, isCargoLoadType } from "../sync/order-types";
const MESSAGE_TYPE = "user.games.report";
@@ -82,6 +84,30 @@ export interface ShipClassSummary {
name: string;
}
/**
* ReportRouteEntry is one slot of a planet's cargo-route table —
* a (loadType, destinationPlanetNumber) pair. The engine stores
* the entries as `map[RouteType]uint` per planet
* (`game/internal/model/game/planet.go`); this type flattens that
* map into an array so iteration order is stable for tests and
* the map-arrow renderer.
*/
export interface ReportRouteEntry {
loadType: CargoLoadType;
destinationPlanetNumber: number;
}
/**
* ReportRoute groups every cargo-route slot configured on a
* single source planet. `entries` is sorted by
* `CARGO_LOAD_TYPE_VALUES` priority (COL → CAP → MAT → EMP) so
* the inspector and the map renderer see deterministic order.
*/
export interface ReportRoute {
sourcePlanetNumber: number;
entries: ReportRouteEntry[];
}
export interface GameReport {
turn: number;
mapWidth: number;
@@ -102,6 +128,24 @@ export interface GameReport {
* empty.
*/
localShipClass: ShipClassSummary[];
/**
* routes lists every cargo route the player has configured.
* Each entry is keyed by source planet; the per-planet
* `entries` array is sorted in turn-cutoff load order
* (`CARGO_LOAD_TYPE_VALUES`). Empty when no routes are set or
* when the report does not carry the route field.
*/
routes: ReportRoute[];
/**
* localPlayerDrive is the local player's drive tech level. The
* engine's reach formula is `40 * driveTech`
* (`game/internal/model/game/race.go.FlightDistance`); the
* cargo-route picker filters destinations through it, so the
* value is propagated all the way through `applyOrderOverlay`
* to the inspector subsection. Zero on boot or when the
* report's player block is missing the local entry.
*/
localPlayerDrive: number;
}
export async function fetchGameReport(
@@ -225,17 +269,94 @@ function decodeReport(report: Report): GameReport {
localShipClass.push({ name: sc.name() ?? "" });
}
const raceName = report.race() ?? "";
const routes = decodeReportRoutes(report);
const localPlayerDrive = findLocalPlayerDrive(report, raceName);
return {
turn: Number(report.turn()),
mapWidth: report.width(),
mapHeight: report.height(),
planetCount: report.planetCount(),
planets,
race: report.race() ?? "",
race: raceName,
localShipClass,
routes,
localPlayerDrive,
};
}
/**
* decodeReportRoutes flattens `report.route()[]` into the typed
* `ReportRoute[]`. Each `Route` carries `planet` (source) and an
* array of `RouteEntry` rows where `key` is the destination
* planet number and `value` is the load-type string. Entries
* with unknown load-types are dropped with a `console.warn` so a
* future schema bump never silently corrupts the inspector.
*/
function decodeReportRoutes(report: Report): ReportRoute[] {
const out: ReportRoute[] = [];
for (let i = 0; i < report.routeLength(); i++) {
const route = report.route(i);
if (route === null) continue;
const sourcePlanetNumber = Number(route.planet());
const entries: ReportRouteEntry[] = [];
for (let j = 0; j < route.routeLength(); j++) {
const entry = route.route(j);
if (entry === null) continue;
const value = entry.value() ?? "";
if (!isCargoLoadType(value)) {
console.warn(
`decodeReport: skipping RouteEntry with unknown load-type "${value}"`,
);
continue;
}
entries.push({
loadType: value,
destinationPlanetNumber: Number(entry.key()),
});
}
entries.sort(compareRouteEntriesByLoadType);
out.push({ sourcePlanetNumber, entries });
}
return out;
}
const LOAD_TYPE_ORDER: Record<CargoLoadType, number> = (() => {
const map = {} as Record<CargoLoadType, number>;
CARGO_LOAD_TYPE_VALUES.forEach((value, index) => {
map[value] = index;
});
return map;
})();
function compareRouteEntriesByLoadType(
a: ReportRouteEntry,
b: ReportRouteEntry,
): number {
return LOAD_TYPE_ORDER[a.loadType] - LOAD_TYPE_ORDER[b.loadType];
}
/**
* findLocalPlayerDrive locates the local player's drive tech
* level by matching `Player.name` against the report's `race`
* field (the engine uses race name as the runtime player
* identifier). Returns 0 when the lookup fails — boot state, an
* incomplete report, or a future schema bump that switches to
* UUIDs. Wrapping the lookup in one helper keeps the migration
* cost contained.
*/
function findLocalPlayerDrive(report: Report, raceName: string): number {
if (raceName === "") return 0;
for (let i = 0; i < report.playerLength(); i++) {
const player = report.player(i);
if (player === null) continue;
if ((player.name() ?? "") !== raceName) continue;
return player.drive();
}
return 0;
}
/**
* uuidToHiLo splits the canonical 36-character UUID string
* (`xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx`) into the two big-endian
@@ -280,6 +401,7 @@ export function applyOrderOverlay(
): GameReport {
if (commands.length === 0) return report;
let mutatedPlanets: ReportPlanet[] | null = null;
let mutatedRoutes: ReportRoute[] | null = null;
for (const cmd of commands) {
const status = statuses[cmd.id];
if (
@@ -317,9 +439,72 @@ export function applyOrderOverlay(
};
continue;
}
if (cmd.kind === "setCargoRoute") {
if (mutatedRoutes === null) {
mutatedRoutes = cloneRoutes(report.routes);
}
upsertRouteEntry(mutatedRoutes, cmd.sourcePlanetNumber, {
loadType: cmd.loadType,
destinationPlanetNumber: cmd.destinationPlanetNumber,
});
continue;
}
if (cmd.kind === "removeCargoRoute") {
if (mutatedRoutes === null) {
mutatedRoutes = cloneRoutes(report.routes);
}
deleteRouteEntry(mutatedRoutes, cmd.sourcePlanetNumber, cmd.loadType);
continue;
}
}
if (mutatedPlanets === null && mutatedRoutes === null) return report;
return {
...report,
planets: mutatedPlanets ?? report.planets,
routes: mutatedRoutes ?? report.routes,
};
}
function cloneRoutes(routes: ReportRoute[]): ReportRoute[] {
return routes.map((r) => ({
sourcePlanetNumber: r.sourcePlanetNumber,
entries: r.entries.map((e) => ({ ...e })),
}));
}
function upsertRouteEntry(
routes: ReportRoute[],
sourcePlanetNumber: number,
entry: ReportRouteEntry,
): void {
let route = routes.find((r) => r.sourcePlanetNumber === sourcePlanetNumber);
if (route === undefined) {
route = { sourcePlanetNumber, entries: [] };
routes.push(route);
}
const idx = route.entries.findIndex((e) => e.loadType === entry.loadType);
if (idx >= 0) {
route.entries[idx] = entry;
} else {
route.entries.push(entry);
}
route.entries.sort(compareRouteEntriesByLoadType);
}
function deleteRouteEntry(
routes: ReportRoute[],
sourcePlanetNumber: number,
loadType: CargoLoadType,
): void {
const routeIndex = routes.findIndex(
(r) => r.sourcePlanetNumber === sourcePlanetNumber,
);
if (routeIndex < 0) return;
const route = routes[routeIndex]!;
route.entries = route.entries.filter((e) => e.loadType !== loadType);
if (route.entries.length === 0) {
routes.splice(routeIndex, 1);
}
if (mutatedPlanets === null) return report;
return { ...report, planets: mutatedPlanets };
}
/**