ui/phase-19: ship-group decoder + map binding + selection store

Wires Phase 19's data and rendering layers without yet adding the
inspector UI:

  - game-state.ts grows ReportLocalShipGroup / ReportOtherShipGroup
    / ReportIncomingShipGroup / ReportUnidentifiedShipGroup /
    ReportLocalFleet types and walks the matching FlatBuffers
    vectors (LocalGroup, OtherGroup, IncomingGroup,
    UnidentifiedGroup, LocalFleet) inside decodeReport. The Tech
    map is folded into the fixed-shape ShipGroupTech struct;
    cargo strings normalise to the closed CargoLoadType | "NONE"
    union; UUIDs come back as canonical 36-char strings.
  - synthetic-report.ts mirrors the new fields so the DEV-only
    lobby loader can feed JSON produced by legacy-report-to-json
    straight into the live UI surface.
  - selection.svelte.ts widens its discriminated union with a
    `kind: "shipGroup"` branch carrying a ShipGroupRef
    (local UUID / other / incoming / unidentified by index).
  - world.ts adds Style.strokeDashPx and render.ts.drawLine
    honours it via manual segmentation (PixiJS v8 has no native
    dash API). Ignored on points and circles.
  - state-binding.ts now returns { world, hitLookup }: the
    hit-lookup map keys every primitive id back to a concrete
    HitTarget so the click handler can dispatch to selectPlanet
    or selectShipGroup. Ship-group primitives live in a separate
    ship-groups.ts that emits one point per local / other /
    unidentified group, plus a dashed origin→destination line +
    clickable point per incoming group. Position is interpolated
    along the trajectory for in-hyperspace groups.
  - map.svelte threads the hitLookup into handleMapClick.

Vitest:
  - tests/helpers/empty-ship-groups.ts exposes EMPTY_SHIP_GROUPS
    so existing fixtures can spread the new five empty arrays
    without enumerating every field.
  - state-binding-groups.test.ts covers each group variant's
    primitive geometry and lookup correctness.
  - All previously-existing fixture builders pick up the spread
    so GameReport stays a complete object.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
Ilia Denisov
2026-05-10 13:23:56 +02:00
parent 8839f46c25
commit 676556db4e
18 changed files with 1085 additions and 44 deletions
+50 -15
View File
@@ -1,9 +1,11 @@
// State binding between the typed game report and the renderer's
// World. Phase 11 only emits primitives for planets; later phases
// extend the binding with ship-class reach circles (Phase 17 / 18),
// hyperspace and incoming groups (Phase 11+ via separate primitives),
// cargo routes (Phase 16), reach / visibility zones (Phase 17), and
// battle / bombing markers (Phase 27).
// World. Phase 11 emitted primitives only for planets; Phase 19
// extends the binding with ship-group primitives (own / foreign / in-
// hyperspace / incoming / unidentified) plus a `hitLookup` map so the
// click handler can dispatch a renderer-side hit back to the right
// selection variant. Later phases extend with ship-class reach
// circles (Phase 17 / 18 in `ui/core/calc/`), reach / visibility
// zones, and battle / bombing markers (Phase 27).
//
// The four planet kinds in the report each map to a distinct style so
// the user can tell own / other-race / uninhabited / unidentified
@@ -12,7 +14,9 @@
// colours and adds theme switching.
import type { GameReport, ReportPlanet } from "../api/game-state";
import { World, type Primitive, type Style } from "./world";
import type { ShipGroupRef } from "../lib/selection.svelte";
import { shipGroupsToPrimitives } from "./ship-groups";
import { World, type Primitive, type PrimitiveID, type Style } from "./world";
const STYLE_LOCAL: Style = {
fillColor: 0x6dd2ff,
@@ -39,11 +43,11 @@ const STYLE_UNIDENTIFIED: Style = {
};
// PlanetIDs occupy the [0, 4_000_000_000) range — well below
// JavaScript's `Number.MAX_SAFE_INTEGER` — so the engine `number` (uint64)
// fits in a primitive id (number) without truncation. The binding
// uses the engine number directly as the primitive id so later phases
// can resolve a planet by its hit-test result without an extra
// lookup table.
// JavaScript's `Number.MAX_SAFE_INTEGER` — so the engine `number`
// (uint64) fits in a primitive id (number) without truncation. The
// binding uses the engine number directly as the primitive id so the
// click handler can recover a planet by hit-test result without an
// extra lookup.
function styleFor(kind: ReportPlanet["kind"]): Style {
switch (kind) {
case "local":
@@ -70,17 +74,38 @@ function priorityFor(kind: ReportPlanet["kind"]): number {
}
}
/**
* HitTarget describes which game entity a renderer-side hit-test
* resolves to. The click handler in `lib/active-view/map.svelte`
* looks the hit primitive's id up in the binding's hitLookup map
* and dispatches `selection.selectPlanet` or
* `selection.selectShipGroup` accordingly.
*/
export type HitTarget =
| { kind: "planet"; number: number }
| { kind: "shipGroup"; ref: ShipGroupRef };
export interface ReportToWorldResult {
world: World;
hitLookup: Map<PrimitiveID, HitTarget>;
}
/**
* reportToWorld translates a GameReport into a renderer-ready World
* containing one Point primitive per planet (all four planet kinds).
* The world rectangle matches `report.mapWidth` × `report.mapHeight`.
* containing one Point primitive per planet (all four planet kinds)
* plus the Phase 19 ship-group surface — own / foreign groups
* (on-planet or in-hyperspace), incoming groups (dashed trajectory
* line + clickable point), and unidentified-group blips. The world
* rectangle matches `report.mapWidth` × `report.mapHeight`.
*
* If the report carries zero planets (turn-zero edge cases or seeded
* tests), the World is still well-formed: the renderer mounts on an
* empty primitive list without errors.
*/
export function reportToWorld(report: GameReport): World {
export function reportToWorld(report: GameReport): ReportToWorldResult {
const primitives: Primitive[] = [];
const hitLookup = new Map<PrimitiveID, HitTarget>();
for (const planet of report.planets) {
primitives.push({
kind: "point",
@@ -91,8 +116,18 @@ export function reportToWorld(report: GameReport): World {
x: planet.x,
y: planet.y,
});
hitLookup.set(planet.number, { kind: "planet", number: planet.number });
}
const groups = shipGroupsToPrimitives(report);
for (const prim of groups.primitives) {
primitives.push(prim);
}
for (const [primId, ref] of groups.lookup) {
hitLookup.set(primId, { kind: "shipGroup", ref });
}
const width = report.mapWidth > 0 ? report.mapWidth : 1;
const height = report.mapHeight > 0 ? report.mapHeight : 1;
return new World(width, height, primitives);
return { world: new World(width, height, primitives), hitLookup };
}