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>
420 lines
14 KiB
TypeScript
420 lines
14 KiB
TypeScript
/**
|
|
* The parts of a turn that run on their own once started: resolving declarations, combat,
|
|
* defeat, capture and drawing. Card scripts call into these too (Trace Route starts a combat,
|
|
* Disconnect pulls an Avatar out of one).
|
|
*/
|
|
import { isStart, levelFor, type LocId } from './map.ts';
|
|
import { scriptOf } from './scripts.ts';
|
|
import { coresOf, emit, fighterOf, hardwareActive, sideCores } from './query.ts';
|
|
import {
|
|
adjacent,
|
|
avatar,
|
|
avatarName,
|
|
controls,
|
|
copyOf,
|
|
defendersAt,
|
|
fightersOf,
|
|
fired,
|
|
gainRam,
|
|
gridCount,
|
|
isCopy,
|
|
isDisabledAvatar,
|
|
isRooted,
|
|
nameOf,
|
|
neighbors,
|
|
played,
|
|
playerName,
|
|
pushCapture,
|
|
pushTasks,
|
|
refKey,
|
|
resetCard,
|
|
rollDie,
|
|
sameRef,
|
|
say,
|
|
startCombat,
|
|
stat,
|
|
toDiscard,
|
|
} from './state.ts';
|
|
import type { AvatarRef, AvatarState, CaptureStat, Combat, GameState, PlayerId, Task } from './types.ts';
|
|
|
|
// ---------------------------------------------------------------------------------------------
|
|
// Drawing
|
|
|
|
export function drawCards(s: GameState, p: PlayerId, n: number): void {
|
|
pushTasks(s, ...Array.from({ length: n }, (): Task => ({ t: 'draw', player: p })));
|
|
}
|
|
|
|
/** Draws one card without "when drawing" effects. Returns it, or null for an empty deck. */
|
|
export function drawNow(s: GameState, p: PlayerId): number | null {
|
|
const pl = s.players[p];
|
|
const card = pl.deck.shift();
|
|
if (card === undefined) return null;
|
|
pl.hand.push(card);
|
|
stat(s, 'cardsDrawn', p);
|
|
return card;
|
|
}
|
|
|
|
export function drawTask(s: GameState, p: PlayerId): void {
|
|
const card = drawNow(s, p);
|
|
if (card !== null) emit(s, 'onDraw', { player: p, card });
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------
|
|
// Avatars leaving and entering play
|
|
|
|
function clearStatus(av: AvatarState): void {
|
|
av.infection = 0;
|
|
av.bugs = 0;
|
|
av.mods = [];
|
|
av.disabledUntil = 0;
|
|
av.rootedUntil = 0;
|
|
}
|
|
|
|
/** A copy leaves play. It has no Hardware of its own. */
|
|
export function removeCopy(s: GameState, ref: AvatarRef): void {
|
|
const cp = avatar(s, ref);
|
|
if (cp.at === null) return;
|
|
say(s, `${avatarName(s, ref)} is removed.`);
|
|
clearStatus(cp);
|
|
cp.permanentInfection = 0;
|
|
cp.at = null;
|
|
cp.faceUp = false;
|
|
dropFromCombat(s, ref);
|
|
}
|
|
|
|
/** Recruit rule: an Avatar recruited over leaves for the discard pile with its Hardware, and its copy leaves play. */
|
|
export function retireAvatar(s: GameState, ref: AvatarRef): void {
|
|
const av = avatar(s, ref);
|
|
removeCopy(s, copyOf(ref));
|
|
if (av.hardware.length) stat(s, 'hardwareLost', ref.player, av.hardware.length);
|
|
for (const hw of av.hardware) toDiscard(s, hw);
|
|
say(s, `${playerName(ref.player)} retires ${avatarName(s, ref)}${av.at ? ` from ${av.at}` : ''}.`);
|
|
dropFromCombat(s, ref);
|
|
toDiscard(s, av.card);
|
|
stat(s, 'avatarsRetired', ref.player);
|
|
}
|
|
|
|
/**
|
|
* The loser of a combat goes back to its slot. Its Hardware is discarded unless `keepHardware`,
|
|
* and its counters and effects are removed (Persistent Infection's counters stay).
|
|
*/
|
|
export function knockOut(s: GameState, ref: AvatarRef, loc: LocId, role: 'attack' | 'defend', keepHardware: boolean): void {
|
|
const av = avatar(s, ref);
|
|
if (av.at === null) return;
|
|
if (isCopy(ref)) {
|
|
removeCopy(s, ref);
|
|
} else {
|
|
if (!keepHardware && av.hardware.length) {
|
|
say(s, `${playerName(ref.player)} loses ${av.hardware.map((h) => nameOf(s, h)).join(', ')}.`);
|
|
stat(s, 'hardwareLost', ref.player, av.hardware.length);
|
|
for (const hw of av.hardware) toDiscard(s, hw);
|
|
av.hardware = [];
|
|
}
|
|
clearStatus(av);
|
|
av.at = null;
|
|
say(s, `${avatarName(s, ref)} returns to its slot.`);
|
|
removeCopy(s, copyOf(ref));
|
|
}
|
|
emit(s, 'onDefeat', { ref, loc, role });
|
|
}
|
|
|
|
/** Places a copy of `original` on `to`. */
|
|
export function createCopy(s: GameState, original: AvatarRef, to: LocId): void {
|
|
const ref = copyOf(original);
|
|
const cp = avatar(s, ref);
|
|
Object.assign(cp, {
|
|
card: avatar(s, original).card,
|
|
faceUp: true,
|
|
at: to,
|
|
hardware: [],
|
|
placed: false,
|
|
advanced: false,
|
|
attacks: 0,
|
|
infection: 0,
|
|
bugs: 0,
|
|
permanentInfection: 0,
|
|
mods: [],
|
|
disabledUntil: 0,
|
|
rootedUntil: 0,
|
|
breachedTurn: 0,
|
|
createdTurn: s.turn,
|
|
} satisfies Partial<AvatarState>);
|
|
say(s, `${playerName(ref.player)} creates ${avatarName(s, ref)} on ${to}.`);
|
|
}
|
|
|
|
/** Moves an Avatar by a card effect. Callers check the move is legal. */
|
|
export function moveAvatar(s: GameState, ref: AvatarRef, to: LocId): void {
|
|
const av = avatar(s, ref);
|
|
say(s, `${avatarName(s, ref)} moves ${av.at} → ${to}.`);
|
|
av.at = to;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------
|
|
// Step 5: declarations
|
|
|
|
export function resolveDeclaration(s: GameState, index: number): void {
|
|
const d = s.declarations[index];
|
|
const p = d.ref.player;
|
|
const av = avatar(s, d.ref);
|
|
const who = avatarName(s, d.ref);
|
|
if (av.at === null) return;
|
|
if (isRooted(s, d.ref) || isDisabledAvatar(s, d.ref)) {
|
|
say(s, `${who} can't move, so its ${d.kind} on ${d.to} fails.`);
|
|
return;
|
|
}
|
|
if (controls(s, p, d.to)) return;
|
|
const defended = defendersAt(s, d.to).length > 0;
|
|
const how: CaptureStat = s.locations[d.to].owner === null ? 'capturesUnowned' : 'capturesUndefended';
|
|
if (d.kind === 'advance') {
|
|
if (!neighbors(s, d.to).some((n) => controls(s, p, n))) {
|
|
say(s, `${who} can no longer reach ${d.to}.`);
|
|
return;
|
|
}
|
|
if (defended) startCombat(s, d.ref, av.at, d.to, 'advance');
|
|
else pushCapture(s, d.ref, d.to, how);
|
|
return;
|
|
}
|
|
if ((!d.second && av.at !== d.from) || !controls(s, p, d.from) || !adjacent(s, d.from, d.to)) {
|
|
say(s, `${who}'s attack on ${d.to} fails: it's no longer in position.`);
|
|
return;
|
|
}
|
|
if (defended) startCombat(s, d.ref, d.from, d.to, 'attack');
|
|
else pushCapture(s, d.ref, d.to, how);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------
|
|
// Combat
|
|
|
|
const total = (dice: number[]) => dice.reduce((a, b) => a + b, 0);
|
|
const roll = (s: GameState, n: number) => Array.from({ length: n }, () => rollDie(s));
|
|
|
|
function sideTotals(c: Combat): { atk: number; def: number } {
|
|
return {
|
|
atk: total(c.dice[refKey(c.attacker)] ?? []),
|
|
def: c.defenders.reduce((t, d) => t + total(c.dice[refKey(d)] ?? []), 0),
|
|
};
|
|
}
|
|
|
|
function diceText(s: GameState, c: Combat): string {
|
|
const one = (r: AvatarRef) => `${nameOf(s, avatar(s, r).card)} [${(c.dice[refKey(r)] ?? []).join(',')}]`;
|
|
const t = sideTotals(c);
|
|
return `${one(c.attacker)}=${t.atk} vs ${c.defenders.map(one).join(' + ')}=${t.def}`;
|
|
}
|
|
|
|
/** Rolls every fighter's dice, rerolling all of them on a tie. */
|
|
function rollAll(s: GameState, c: Combat): void {
|
|
do {
|
|
for (const r of fightersOf(c)) c.dice[refKey(r)] = roll(s, coresOf(s, fighterOf(s, r)));
|
|
const t = sideTotals(c);
|
|
if (t.atk !== t.def) break;
|
|
c.ties++;
|
|
} while (true);
|
|
c.rolled = true;
|
|
}
|
|
|
|
/**
|
|
* Cores that change after the roll change the dice: each Core gained rolls one more die, and each
|
|
* Core lost removes that Avatar's highest die.
|
|
*/
|
|
export function syncDice(s: GameState, c: Combat): void {
|
|
if (!c.rolled) return;
|
|
for (const r of fightersOf(c)) {
|
|
const key = refKey(r);
|
|
const dice = c.dice[key] ?? [];
|
|
const want = coresOf(s, fighterOf(s, r));
|
|
while (dice.length < want) dice.push(rollDie(s));
|
|
if (dice.length > want) {
|
|
dice.sort((a, b) => a - b);
|
|
dice.length = want;
|
|
}
|
|
c.dice[key] = dice;
|
|
}
|
|
}
|
|
|
|
/** Current dice totals, after syncing to Core changes. */
|
|
export function combatTotals(s: GameState): { atk: number; def: number } {
|
|
const c = s.combat!;
|
|
syncDice(s, c);
|
|
return sideTotals(c);
|
|
}
|
|
|
|
/** Stops the current combat with no winner. */
|
|
export function endCombat(s: GameState, why: string): void {
|
|
const c = s.combat;
|
|
if (!c || c.ended) return;
|
|
c.ended = true;
|
|
stat(s, 'combatsEnded', c.attacker.player);
|
|
say(s, `The combat at ${c.loc} ends: ${why}.`);
|
|
}
|
|
|
|
/**
|
|
* Takes an Avatar out of the current combat. If the attacker leaves, the combat ends. If the last
|
|
* defender leaves, the attacker takes the location as undefended.
|
|
*/
|
|
export function dropFromCombat(s: GameState, ref: AvatarRef): void {
|
|
const c = s.combat;
|
|
if (!c || c.ended) return;
|
|
if (sameRef(c.attacker, ref)) {
|
|
endCombat(s, `${avatarName(s, ref)} left it`);
|
|
return;
|
|
}
|
|
const i = c.defenders.findIndex((d) => sameRef(d, ref));
|
|
if (i < 0) return;
|
|
c.defenders.splice(i, 1);
|
|
delete c.dice[refKey(ref)];
|
|
if (c.defenders.length === 0) {
|
|
endCombat(s, 'no defender is left');
|
|
if (avatar(s, c.attacker).at !== null) pushCapture(s, c.attacker, c.loc, 'capturesUndefended');
|
|
}
|
|
}
|
|
|
|
export function combatStage(s: GameState, stage: 'start' | 'roll' | 'result' | 'end' | 'cleanup'): void {
|
|
const c = s.combat;
|
|
if (!c) return;
|
|
if (stage === 'cleanup') {
|
|
for (const r of fightersOf(c)) {
|
|
const av = avatar(s, r);
|
|
av.mods = av.mods.filter((m) => m.combat !== c.id);
|
|
}
|
|
s.combat = null;
|
|
return;
|
|
}
|
|
if (stage === 'end') {
|
|
pushTasks(s, { t: 'combat', stage: 'cleanup' });
|
|
if (c.rolled || c.ended) emit(s, 'onCombatEnd', null);
|
|
return;
|
|
}
|
|
if (c.ended) return;
|
|
if (stage === 'start') {
|
|
stat(s, 'attacks', c.attacker.player);
|
|
const { atk, def } = sideCores(s);
|
|
const defs = c.defenders.map((d) => avatarName(s, d)).join(' and ');
|
|
say(s, `Combat at ${c.loc}: ${avatarName(s, c.attacker)} (${atk} Cores, from ${c.origin}) vs ${defs} (${def} Cores).`);
|
|
emit(s, 'onCombatStart', null);
|
|
return;
|
|
}
|
|
if (stage === 'roll') {
|
|
// Static Core effects count as firing once per combat.
|
|
const credited = new Set<string>();
|
|
for (const r of fightersOf(c)) {
|
|
coresOf(s, fighterOf(s, r), (name, p) => {
|
|
if (!credited.has(`${name}:${p}`)) {
|
|
credited.add(`${name}:${p}`);
|
|
fired(s, name, p);
|
|
}
|
|
});
|
|
}
|
|
rollAll(s, c);
|
|
say(s, `Roll: ${diceText(s, c)}${c.ties ? ` after ${c.ties} tie reroll(s)` : ''}.`);
|
|
return;
|
|
}
|
|
// result
|
|
const t = combatTotals(s);
|
|
if (t.atk === t.def) {
|
|
c.ties++;
|
|
rollAll(s, c);
|
|
say(s, `Tie, so both sides reroll: ${diceText(s, c)}.`);
|
|
pushTasks(
|
|
s,
|
|
{ t: 'window', kind: 'postRoll', priority: c.attacker.player, passes: 0, base: s.stack.length, resolving: false },
|
|
{ t: 'combat', stage: 'result' },
|
|
);
|
|
return;
|
|
}
|
|
const attackerWins = t.atk > t.def;
|
|
const winner = attackerWins ? c.attacker.player : s.locations[c.loc].owner!;
|
|
say(s, `${playerName(winner)} wins the combat, ${t.atk} to ${t.def}.`);
|
|
if (attackerWins) {
|
|
stat(s, 'attacksWon', c.attacker.player);
|
|
pushTasks(
|
|
s,
|
|
...c.defenders.map((ref): Task => ({ t: 'defeat', ref, loc: c.loc, role: 'defend' })),
|
|
{ t: 'capture', ref: c.attacker, loc: c.loc, stage: 'hooks', how: 'capturesInCombat' },
|
|
);
|
|
} else {
|
|
pushTasks(s, { t: 'defeat', ref: c.attacker, loc: c.loc, role: 'attack' });
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------
|
|
// Defeat
|
|
|
|
export function defeat(s: GameState, ref: AvatarRef, loc: LocId, role: 'attack' | 'defend'): void {
|
|
const av = avatar(s, ref);
|
|
if (av.at === null) return;
|
|
if (!isCopy(ref)) {
|
|
// Redundant Power Supply, then RAID Array, may save the Hardware.
|
|
const savers = av.hardware
|
|
.filter((hw) => hardwareActive(s, hw) && scriptOf(s.cards[hw].name)?.onWearerDefeated)
|
|
.sort((a, b) => (scriptOf(s.cards[a].name)!.savePriority ?? 0) - (scriptOf(s.cards[b].name)!.savePriority ?? 0));
|
|
for (const hw of savers) {
|
|
if (scriptOf(s.cards[hw].name)!.onWearerDefeated!(s, hw, { ref, loc, role })) return;
|
|
}
|
|
}
|
|
knockOut(s, ref, loc, role, false);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------
|
|
// Capture
|
|
|
|
export function captureHooks(s: GameState, ref: AvatarRef, loc: LocId, how: CaptureStat): void {
|
|
const p = ref.player;
|
|
if (avatar(s, ref).at === null || controls(s, p, loc) || isStart(loc)) return;
|
|
pushTasks(s, { t: 'capture', ref, loc, stage: 'main', how });
|
|
const prev = s.locations[loc].owner;
|
|
if (prev !== null && prev !== p) {
|
|
avatar(s, ref).breachedTurn = s.turn;
|
|
emit(s, 'onCaptured', { ref, loc });
|
|
emit(s, 'onBreach', { ref, loc });
|
|
}
|
|
}
|
|
|
|
export function captureMain(s: GameState, ref: AvatarRef, loc: LocId, how: CaptureStat): void {
|
|
const p = ref.player;
|
|
const me = s.players[p];
|
|
const L = s.locations[loc];
|
|
if (avatar(s, ref).at === null || controls(s, p, loc)) return;
|
|
const previous = L.owner;
|
|
if (L.server !== null) {
|
|
resetCard(s, L.server);
|
|
s.players[s.cards[L.server].owner].serverPile.push(L.server);
|
|
}
|
|
if (previous !== null && L.programs.length) stat(s, 'programsLost', previous, L.programs.length);
|
|
for (const prog of L.programs) toDiscard(s, prog);
|
|
L.programs = [];
|
|
L.owner = p;
|
|
L.server = me.serverPile.shift() ?? null;
|
|
avatar(s, ref).at = loc;
|
|
|
|
const level = levelFor(p, loc);
|
|
const turn1 = s.turn === 1 ? s.rules.firstTurn : null;
|
|
const bonus = L.server === null ? 0 : (scriptOf(s.cards[L.server].name)?.ramOnPlace ?? 0);
|
|
const ram = (turn1?.captureRam === false ? 0 : s.rules.levelRam[level]) + bonus;
|
|
gainRam(s, p, ram, 'ramFromCaptures');
|
|
stat(s, how, p);
|
|
const from = previous === null ? '' : ` from ${playerName(previous)}`;
|
|
say(s, `${playerName(p)} captures ${loc}${from} (Lv ${level}) with ${L.server === null ? 'no Server' : nameOf(s, L.server)}: +${ram} RAM (now ${me.ram}).`);
|
|
|
|
const unlocks = !s.rules.recruit.on && !s.rules.buyAvatars.on;
|
|
if (unlocks && level > me.unlockedLevel && turn1?.unlock !== false) {
|
|
me.unlockedLevel = level;
|
|
me.avatars.slice(0, 4).forEach((av, i) => {
|
|
if (i < level && !av.faceUp) {
|
|
av.faceUp = true;
|
|
av.unlockedTurn = s.turn;
|
|
say(s, `${playerName(p)} unlocks Avatar ${i + 1}: ${nameOf(s, av.card)}.`);
|
|
}
|
|
});
|
|
}
|
|
if (gridCount(s, p) >= s.rules.winLocations) {
|
|
s.over = true;
|
|
s.winner = p;
|
|
s.endReason = 'win';
|
|
say(s, `${playerName(p)} wins with ${gridCount(s, p)} locations.`);
|
|
return;
|
|
}
|
|
if (L.server !== null) {
|
|
played(s, nameOf(s, L.server), p);
|
|
emit(s, 'onPlaced', { card: L.server });
|
|
}
|
|
}
|