files-tcg/sim/src/invariants.ts
Forrest a3eb6850ee Rules v0.8: rounds of alternating commands; sim updates
Rules (rules.md, new file):
- Rounds of 2 commands per player in snake order (A B B A, then B A A B).
  A command is one advance or one attack; each Avatar acts once per round.
- Flat 4 RAM captures, first row Lv 1, A2-C2 and C2-E2 lines cut,
  45-card decks, Code Zero at 3 Cores with Patient Zero at 1+ counters.

Simulator:
- Command rule (default on): commands.perPlayer, commands.leads,
  commands.snake; commands.on=false restores whole turns.
- Greedy bot values Avatar unlocks (unlockPoints) and picks one action
  per command.
- Prototype switches, off by default: Avatars in the deck (recruit.*),
  buying Avatars (buyAvatars.*), copy limits (copies.*), turn-1 draw and
  second-player draw options.
- cards.csv card-text revisions, with scripts and tests to match.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-28 14:07:45 -04:00

66 lines
3.5 KiB
TypeScript

import { LOCATIONS } from './map.ts';
import { capacity, portsTotal } from './query.ts';
import { scriptOf } from './scripts.ts';
import { controls } from './state.ts';
import type { GameState } from './types.ts';
/** Rule violations in a state; empty when it's consistent. Used by tests and `--check`. */
export function checkInvariants(s: GameState): string[] {
const errors: string[] = [];
const zone = new Map<number, string>();
const put = (id: number, where: string) => {
const prev = zone.get(id);
if (prev) errors.push(`card ${id} (${s.cards[id]?.name}) is in both ${prev} and ${where}`);
zone.set(id, where);
};
s.players.forEach((pl, p) => {
const own = (ids: number[], where: string) => {
for (const id of ids) {
put(id, `p${p}.${where}`);
if (s.cards[id].owner !== p) errors.push(`p${p}.${where} holds the opponent's card ${id}`);
}
};
own(pl.hand, 'hand');
own(pl.deck, 'deck');
own(pl.discard, 'discard');
own(pl.serverPile, 'serverPile');
if (pl.ram < 0) errors.push(`p${p} has ${pl.ram} RAM`);
const count = new Map<string, number>();
pl.avatars.forEach((av, i) => {
const slot = i + 1;
const copy = slot > 4;
if (!copy) {
if (av.card >= 0 || !s.rules.recruit.on) own([av.card], `avatar${slot}`);
else if (av.at !== null || av.hardware.length) errors.push(`p${p} empty slot ${slot} is in use`);
own(av.hardware, `avatar${slot}.hardware`);
const ported = av.hardware.filter((h) => !scriptOf(s.cards[h].name)?.noPort && s.cards[h].pushedUntil === undefined);
if (av.card >= 0 && ported.length > portsTotal(s, { player: p as 0 | 1, slot })) errors.push(`p${p} Avatar ${slot} is over its slots`);
} else {
if (av.hardware.length) errors.push(`p${p} copy ${slot - 4} has Hardware of its own`);
if (av.at !== null && pl.avatars[i - 4].at === null) errors.push(`p${p} copy ${slot - 4} is in play without its original`);
if (av.card !== pl.avatars[i - 4].card) errors.push(`p${p} copy ${slot - 4} doesn't match its original`);
}
if (av.at === null) return;
if (!av.faceUp) errors.push(`p${p} Avatar ${slot} is in play face down`);
if (!controls(s, p as 0 | 1, av.at)) errors.push(`p${p} Avatar ${slot} is on ${av.at}, which it doesn't control`);
count.set(av.at, (count.get(av.at) ?? 0) + 1);
});
for (const [loc, n] of count) if (n > capacity(s, loc)) errors.push(`p${p} has ${n} Avatars on ${loc}`);
const total = pl.avatars.filter((av, i) => (i < 4 ? av.card >= 0 : av.at !== null)).length;
if (s.rules.recruit.on && total > 4) errors.push(`p${p} has ${total} Avatars, counting copies`);
});
for (const loc of LOCATIONS) {
const L = s.locations[loc];
if (L.server !== null) put(L.server, `${loc}.server`);
for (const id of L.programs) put(id, `${loc}.programs`);
if (L.owner === null && (L.server !== null || L.programs.length > 0)) errors.push(`${loc} is unowned but has cards`);
const layers = L.programs.filter((id) => s.cards[id].name === 'Compatibility Layer').length;
if (L.programs.length > s.rules.maxProgramsPerLocation + layers) errors.push(`${loc} has too many Programs`);
}
for (const item of s.stack) if (item.kind === 'execution') put(item.card, 'stack');
if (zone.size !== s.cards.length) errors.push(`${s.cards.length - zone.size} cards are missing from every zone`);
if (s.choice && s.choice.options.length === 0) errors.push(`choice ${s.choice.kind} has no options`);
return errors;
}