feat(#54): non-military takeovers — automatic regime rename (TurnResult.takeovers)

This commit is contained in:
2026-10-15 10:58:23 +00:00
parent 8eea17148b
commit 3c187c6901
4 changed files with 398 additions and 20 deletions
+58
View File
@@ -33,6 +33,7 @@ import {
type MarkerSpec,
type NationOp,
type RegionStatChange,
type Takeover,
type TurnEventSpec,
type TurnResult,
} from '../shared/protocol';
@@ -199,6 +200,7 @@ export function buildSystemPrompt(preset: Preset, withNeighboursTool = false): s
' {"kind":"split","nationRef":"<nation id or name>","parts":[{"name":"<new name>","territory":["<region key>"]}]}',
' {"kind":"merge","nationRefs":["<id>","<id>"],"survivingRef":"<id of the surviving nation>","into":"<new name>"}',
' ],',
' "takeovers": [{"nationRef":"<nation id or name>","newRegimeName":"<name of the new regime>"}],',
' "markers": {"add":[{"type":"<city|factory|army|port|capital|fleet|rocket|gathering>","name":"<short name>","regionKey":"<region key>"}],"remove":["<marker id>"]}',
'}',
'',
@@ -216,6 +218,7 @@ export function buildSystemPrompt(preset: Preset, withNeighboursTool = false): s
'- Rename: add {"kind":"rename","nationRef":"<nation id or name>","to":"<new name>"} for a revolution, regime change or reunification that changes a nation\'s name.',
'- Split: add {"kind":"split","nationRef":"<nation id or name>","parts":[{"name":"<new name>","territory":["<region key>"]}, ...]} when a state breaks up (dissolution, secession). Use at least 2 parts and assign each of the nation\'s region keys to at most one part — any key you leave unassigned goes to the first part.',
'- Merge: add {"kind":"merge","nationRefs":["<id or name>","<id or name>"],"survivingRef":"<id or name of the surviving nation>","into":"<new name>"} when states reunify or one is absorbed. Use at least 2 input nations and "survivingRef" must name one of them — the survivor keeps its id and its stats.',
'- Takeover (automatic, NOT player-confirmed): add {"takeovers":[{"nationRef":"<nation id or name>","newRegimeName":"<name of the new regime>"}]} when a low-stability nation (e.g. stability < 30) hosting a "gathering" marker is overthrown in a coup or revolution. This renames the nation to the new regime (e.g. "France" → "Vichy Regime"). Also emit a narrative "event" (political or social, outcome "success") and a stability/economy shock via "stateChanges". A takeover is an automatic in-world regime change — use "nationOps" rename only for a diplomatic/political name change that the player must confirm.',
'- Prefer 1-4 events per turn; keep each text 1-3 sentences.',
'- Keep your internal reasoning brief (a few sentences) and go straight to the JSON result; do not over-deliberate or re-derive facts already present in the state.',
'- Keep every stat value between 0 and 100.',
@@ -509,6 +512,7 @@ export function validateTurnResult(
territoryChanges: [],
unowned: [],
nationOps: [],
takeovers: [],
markers: { add: [], remove: [] },
};
@@ -599,6 +603,10 @@ export function validateTurnResult(
// list for the player to confirm/reject.
result.nationOps = validateNationOps(o.nationOps);
// (v0.32.0, #54) Non-military regime changes (takeovers) — applied directly
// by applyTurnResult (automatic, not player-confirmed).
result.takeovers = validateTakeovers(o.takeovers);
// (v0.27, #44) Map-marker ops: new placements (validated here) and
// removals by stable id. Applied by applyTurnResult after the territory
// moves (a new marker's owner is the region's post-turn owner).
@@ -705,6 +713,29 @@ export function validateRegionChanges(
return out;
}
/**
* (v0.32.0, #54) Validate the raw `takeovers` array of a turn into a safe
* {@link Takeover} list — the non-military regime-change lever (a coup or
* revolution that renames a nation to its new regime name). Defensive like
* the rest of the parser — never throws. Each entry needs a non-empty
* `nationRef` (the target nation's id or name — capped at 200 chars) and a
* non-empty `newRegimeName` (capped at 200 chars). Resolution of `nationRef`
* to a concrete nation id happens at apply time (`resolveNationRef`), not
* here. Entries with either field missing or whitespace-only are dropped.
*/
export function validateTakeovers(raw: unknown): Takeover[] {
if (!Array.isArray(raw)) return [];
const out: Takeover[] = [];
for (const e of raw) {
if (!e || typeof e !== 'object') continue;
const o = e as Record<string, unknown>;
const nationRef = str(o.nationRef, 200);
const newRegimeName = str(o.newRegimeName, 200);
if (nationRef && newRegimeName) out.push({ nationRef, newRegimeName });
}
return out;
}
/**
* A turn is "meaningful" if it produced at least an event, a stat change, a
* no-man's-land release (v0.22 #16 P3: a release is a real world change even
@@ -721,6 +752,8 @@ export function isMeaningful(result: TurnResult): boolean {
result.regionChanges.length > 0 ||
result.unowned.length > 0 ||
result.nationOps.length > 0 ||
// (v0.32.0, #54) a takeover is a real world change (regime rename).
result.takeovers.length > 0 ||
// (v0.27, #44) a marker placed or removed this turn is a real world change.
result.markers.add.length > 0 ||
result.markers.remove.length > 0
@@ -973,6 +1006,30 @@ export function applyTurnResult(
n.stats = deriveNationStats(n.territory, next.regionStats, geoOf);
}
// --- Takeovers (v0.32.0, #54): non-military regime changes (coups /
// revolutions) that rename a nation to its new regime. Applied AFTER the
// per-region recompute (the rename doesn't depend on territory or stats,
// so it is order-independent with respect to those steps) and BEFORE the
// markers block (a marker's owner is a nation id, not a name, so the
// rename does not affect the owner recompute).
//
// For each takeover:
// 1. Resolve the target nation via `resolveNationRef` (id first, then
// case-insensitive unique name). Skip (no-op) if `null` — an
// unknown or ambiguous ref is silently dropped, never thrown.
// 2. Rename the resolved nation: set `n.name = newRegimeName`. Only
// the display name changes — the stable id, territory, relations
// (keyed by id), stats, and `playerNationId` (which references the
// id, not the name) are all preserved. The player keeps leading an
// overthrown nation under its new regime.
for (const t of result.takeovers) {
const id = resolveNationRef(next, t.nationRef);
if (id === null) continue; // unknown / ambiguous ref: no-op
const n = next.nations[id];
if (!n) continue; // defensive: id resolved but nation missing (corrupt state)
n.name = t.newRegimeName;
}
// --- Map markers (v0.27, #44) ---
//
// Markers are anchored to a region key and are processed against the FINAL
@@ -1500,6 +1557,7 @@ export async function runTurn(opts: {
territoryChanges: [],
unowned: [],
nationOps: [],
takeovers: [],
markers: { add: [], remove: [] },
};
}