sim
This commit is contained in:
parent
746d218eda
commit
e590baf28d
44 changed files with 8308 additions and 0 deletions
63
sim/src/invariants.ts
Normal file
63
sim/src/invariants.ts
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
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) {
|
||||
own([av.card], `avatar${slot}`);
|
||||
own(av.hardware, `avatar${slot}.hardware`);
|
||||
const ported = av.hardware.filter((h) => !scriptOf(s.cards[h].name)?.noPort && s.cards[h].pushedUntil === undefined);
|
||||
if (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}`);
|
||||
});
|
||||
|
||||
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;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue