v0.27.0 (#44): LLM-placed named map point markers
CI / build + unit + e2e (pull_request) Successful in 2m36s
CI / build + unit + e2e (pull_request) Successful in 2m36s
- shared: 8 MarkerTypes + MARKER_TYPE_INFO (label/color/glyph); MapMarker;
GameState.markers (required, [] at init); TurnResult.markers {add,remove};
MarkerSpec/MarkerInfo; FNV-1a 4-char markerId; MAX_MARKERS=400.
- data: new public/region-centroids.json (1,800 anchors = largest-part centroid
+ spread ~0.1x min-bbox) built by new idempotent scripts/build-region-centroids.mjs.
- server: regions.ts centroid loader (public/ then dist/); engine applyTurnResult
marker ops (remove -> owner-recompute -> add, jitter, cap, never throws);
prompt documents the contract; buildUserPrompt carries markers; stub marker-free.
- client: WorldMap symbol layer rendered as canvas per-type icons (glyph CDN dead);
hover tooltip shows name; click focuses owning nation.
- tests: 168 -> 189 (centroids script, regions centroids, db legacy, engine markers).
This commit is contained in:
@@ -6,10 +6,12 @@ import type {
|
||||
EventOutcome,
|
||||
EventType,
|
||||
GameState,
|
||||
MarkerType,
|
||||
Nation,
|
||||
RelationStatus,
|
||||
StatKey,
|
||||
} from './types';
|
||||
import { MARKER_TYPES } from './types';
|
||||
|
||||
/** A numeric change to one of a nation's 0–100 stats. */
|
||||
export type StateChange =
|
||||
@@ -114,6 +116,100 @@ export interface TurnEventSpec {
|
||||
outcome: EventOutcome;
|
||||
}
|
||||
|
||||
// --- Map markers (v0.27, #44) -------------------------------------------------
|
||||
//
|
||||
// The LLM places named point markers (cities, factories, armies, ports,
|
||||
// capitals, fleets, rocket launch sites, gatherings) anchored to a region
|
||||
// key. It never sees or sets coordinates: the engine derives the position
|
||||
// from the region's precomputed centroid (public/region-centroids.json) plus a
|
||||
// small deterministic offset, and it recomputes each marker's owner from the
|
||||
// region's current owner on every turn (a released region → null).
|
||||
//
|
||||
// The marker id is a STABLE, deterministic 4-letter code (FNV-1a 32-bit →
|
||||
// base 26, the same scheme as the region keys' 4-char suffix) derived from
|
||||
// (type, name, regionKey) — so re-adding an identical marker is idempotent
|
||||
// (same id, skipped) and removing by id is unambiguous.
|
||||
|
||||
/** The spec the LLM emits to place a marker: type, name and the region it sits in. */
|
||||
export interface MarkerSpec {
|
||||
type: MarkerType;
|
||||
name: string;
|
||||
regionKey: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* One existing marker as the user prompt presents it to the model (v0.27,
|
||||
* #44): its stable `id` (the model may echo it in `TurnResult.markers.remove`),
|
||||
* type, name, region key, and the id of the nation currently owning its
|
||||
* region (`null` when the region is unowned / no man's land).
|
||||
*/
|
||||
export interface MarkerInfo {
|
||||
id: string;
|
||||
type: MarkerType;
|
||||
name: string;
|
||||
regionKey: string;
|
||||
owner: string | null;
|
||||
}
|
||||
|
||||
/**
|
||||
* The per-type presentation of a marker: its display label (tooltip /
|
||||
* dossier), its color, and its glyph character (a symbol-layer text label —
|
||||
* a small, font-safe character in front of the name). Pure + shared so the
|
||||
* client's marker layer and any server-side consumers agree on the look.
|
||||
*/
|
||||
export const MARKER_TYPE_INFO: Record<MarkerType, { label: string; color: string; glyph: string }> = {
|
||||
city: { label: 'City', color: '#cbd5e1', glyph: '●' },
|
||||
factory: { label: 'Factory', color: '#fb923c', glyph: '■' },
|
||||
army: { label: 'Army', color: '#ef4444', glyph: '▲' },
|
||||
port: { label: 'Port', color: '#38bdf8', glyph: '◆' },
|
||||
capital: { label: 'Capital', color: '#fbbf24', glyph: '★' },
|
||||
fleet: { label: 'Fleet', color: '#22d3ee', glyph: '◗' },
|
||||
rocket: { label: 'Rocket site', color: '#a78bfa', glyph: '↥' },
|
||||
gathering: { label: 'Gathering', color: '#4ade80', glyph: '◉' },
|
||||
};
|
||||
|
||||
/** Hard cap on a game's total markers (new adds beyond the cap are dropped, v0.27 #44). */
|
||||
export const MAX_MARKERS = 400;
|
||||
|
||||
/**
|
||||
* FNV-1a 32-bit — the same tiny, stable, pure string hash the region keys use
|
||||
* (see scripts/build-region-keys.mjs). Exported so the engine can derive
|
||||
* deterministic per-axis jitter fractions from it (marker placement, v0.27 #44).
|
||||
*/
|
||||
export function fnv1a32(str: string): number {
|
||||
let h = 0x811c9dc5;
|
||||
for (let i = 0; i < str.length; i++) {
|
||||
h ^= str.charCodeAt(i);
|
||||
h = Math.imul(h, 0x01000193) >>> 0;
|
||||
}
|
||||
return h >>> 0;
|
||||
}
|
||||
|
||||
/** Spread a 32-bit hash across four lowercase letters (base 26 per byte). */
|
||||
function code4(h: number): string {
|
||||
let out = '';
|
||||
for (let i = 0; i < 4; i++) {
|
||||
out += String.fromCharCode(97 + (((h >>> (i * 8)) & 0xff) % 26));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* The stable id for a marker: 4 base-26 lowercase chars from the FNV-1a hash
|
||||
* of `(type, name, regionKey)` (NUL-separated so no field boundary is
|
||||
* ambiguous). Deterministic across runs and platforms (pure string math), so
|
||||
* the same (type, name, regionKey) always maps to the same id — re-adding is
|
||||
* idempotent, removing by id is unambiguous.
|
||||
*/
|
||||
export function markerId(type: MarkerType, name: string, regionKey: string): string {
|
||||
return code4(fnv1a32(`${type}\u0000${name}\u0000${regionKey}`));
|
||||
}
|
||||
|
||||
/** True when `t` is one of the eight known marker types (v0.27, #44). */
|
||||
export function isMarkerType(t: unknown): t is MarkerType {
|
||||
return typeof t === 'string' && (MARKER_TYPES as string[]).includes(t);
|
||||
}
|
||||
|
||||
/** The exact JSON object the world-simulator LLM must return for a turn. */
|
||||
export interface TurnResult {
|
||||
date: string;
|
||||
@@ -141,6 +237,16 @@ export interface TurnResult {
|
||||
* survivor. Empty on turns where the model proposes none.
|
||||
*/
|
||||
nationOps: NationOp[];
|
||||
/**
|
||||
* (v0.27, #44) Map-marker ops this turn. `add` places new named markers
|
||||
* (the engine positions each at its region's centroid + a small
|
||||
* deterministic offset, sets the owner to the region's post-turn owner,
|
||||
* derives the stable id, and drops unknown types/region keys, duplicates,
|
||||
* and adds beyond MAX_MARKERS); `remove` drops markers by their stable id
|
||||
* (a no-op for an id that is not present). Both required, `[]` on turns
|
||||
* with no marker activity.
|
||||
*/
|
||||
markers: { add: MarkerSpec[]; remove: string[] };
|
||||
}
|
||||
|
||||
/** Per-region map payload: region key -> owning nation + its stable color. */
|
||||
|
||||
@@ -68,6 +68,60 @@ export const EVENT_TYPES: EventType[] = [
|
||||
|
||||
export type EventOutcome = 'success' | 'failure' | 'partial' | 'neutral';
|
||||
|
||||
// --- Map markers (v0.27, #44) -------------------------------------------------
|
||||
//
|
||||
// Named point markers the LLM can place on the map (cities, factories, armies,
|
||||
// ports, capitals, fleets, rocket launch sites, political/social gatherings).
|
||||
// A marker is anchored to a **region key** (see `public/region-keys.json`): the
|
||||
// engine positions it at that region's precomputed centroid (with a small
|
||||
// deterministic offset) and its `ownerNationId` follows the region's current
|
||||
// owner (recomputed on every turn; `null` when the region is unowned / no
|
||||
// man's land). See protocol.ts for the LLM-facing `TurnResult.markers`
|
||||
// contract and the `markerId` derivation.
|
||||
|
||||
/** The eight marker categories the model may place (v0.27, #44). */
|
||||
export type MarkerType =
|
||||
| 'city'
|
||||
| 'factory'
|
||||
| 'army'
|
||||
| 'port'
|
||||
| 'capital'
|
||||
| 'fleet'
|
||||
| 'rocket'
|
||||
| 'gathering';
|
||||
|
||||
/** All marker types, in canonical order (used by the LLM contract + map layer). */
|
||||
export const MARKER_TYPES: MarkerType[] = [
|
||||
'city',
|
||||
'factory',
|
||||
'army',
|
||||
'port',
|
||||
'capital',
|
||||
'fleet',
|
||||
'rocket',
|
||||
'gathering',
|
||||
];
|
||||
|
||||
/**
|
||||
* A named point marker on the map (v0.27, #44). `id` is a stable,
|
||||
* deterministic 4-character code derived from `type + name + regionKey`
|
||||
* (see protocol.markerId) — re-adding the same marker is idempotent and
|
||||
* removing by id is unambiguous. `lat`/`lon` are filled in by the engine from
|
||||
* the region's centroid manifest (never by the model); `ownerNationId` is the
|
||||
* marker's region's current owner (`null` when unowned) and is recomputed by
|
||||
* the engine on every committed turn, so a marker always follows its region.
|
||||
*/
|
||||
export interface MapMarker {
|
||||
id: string;
|
||||
type: MarkerType;
|
||||
name: string;
|
||||
regionKey: string;
|
||||
lat: number;
|
||||
lon: number;
|
||||
ownerNationId: string | null;
|
||||
placedTurn: number;
|
||||
}
|
||||
|
||||
/** A narrative event the simulation emitted on some turn. */
|
||||
export interface EventEntry {
|
||||
id: string;
|
||||
@@ -153,6 +207,16 @@ export interface GameState {
|
||||
* runtime dependencies.)
|
||||
*/
|
||||
pendingNationOps: NationOp[];
|
||||
/**
|
||||
* (v0.27, #44) Named point markers on the map: the LLM places them (via a
|
||||
* turn's `markers.add`) and removes them (`markers.remove`, by id). The
|
||||
* engine positions each at its region's centroid (deterministic small
|
||||
* offset) and recomputes `ownerNationId` from the region's current owner on
|
||||
* EVERY committed turn (a released region → `null`). Always `[]` on a fresh
|
||||
* game; rows saved before v0.27 load with this defaulted to `[]` (see
|
||||
* server/db.ts) — additive, no data wipe. Capped at protocol.MAX_MARKERS.
|
||||
*/
|
||||
markers: MapMarker[];
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user