files-tcg/sim/test/avatars.test.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

167 lines
7.1 KiB
TypeScript

import assert from 'node:assert/strict';
import { test } from 'node:test';
import { avatarCores, openWindow } from '../src/engine.ts';
import { hasRoom } from '../src/query.ts';
import { withRule } from '../src/rules.ts';
import { card } from './card.ts';
import { Table, type Side } from './table.ts';
/** South's Avatar 1 (`south`) on B1 attacks North's Avatar 1 (`north`) on C1. */
function scene(south: string, north: string, o: { south?: Side; north?: Side } = {}) {
const others = ['Broken Bits', 'Pixelated Picture', 'Corrupt Driver', 'Code Zero', 'Beta Version 1.0', 'Data Leaker'];
const four = (first: string) => [first, ...others.filter((n) => n !== first)].slice(0, 4);
const t = new Table({ south: { ...o.south, avatars: four(south) }, north: { ...o.north, avatars: four(north) } });
t.own(0, 'B1');
t.own(1, 'C1');
t.place(0, 1, 'B1');
t.place(1, 1, 'C1');
return t;
}
const fight = (t: Table) => t.to('attack').go({ type: 'attack', slot: 1, to: 'C1' }).act({ type: 'end' });
const dice = (t: Table, side: 'atk' | 'def') => {
const m = t.logText().match(/Roll: \S+(?: \S+)* \[([\d,]*)\]=\d+ vs \S+(?: \S+)* \[([\d,]*)\]/)!;
return m[side === 'atk' ? 1 : 2].split(',').length;
};
const avatarCard = (t: Table, p: 0 | 1, slot: number) => t.avatar(p, slot).card;
card('Carbon NanoBots', () => {
const t = scene('Carbon NanoBots', 'Broken Bits', { south: { main: ['Additional Core'] } });
t.own(0, 'A2');
t.hardware(0, 1, 'Additional Core');
t.ram(0, 5).use(avatarCard(t, 0, 1), { to: 'A2' }).settle();
assert.equal(t.avatar(0, 5).at, 'A2', 'the copy is in play');
assert.equal(avatarCores(t.s, 0, 5, 'defend'), 3, "2 Cores + the original's Additional Core");
// The copy attacks and loses: the original keeps its Hardware.
t.place(0, 1, 'S1');
t.s.players[0].avatars[4].at = 'B1';
t.to('attack').go({ type: 'attack', slot: 5, to: 'C1' });
t.rig(1, 1, 1, 6, 6, 6, 6).end();
assert.equal(t.avatar(0, 5).at, null);
assert.equal(t.avatar(0, 1).hardware.length, 1);
});
test('copy rule variants: a new copy defends but only advances or attacks from its next turn', () => {
const t = scene('Carbon NanoBots', 'Broken Bits');
t.s.rules = withRule(t.s.rules, 'copies.waitATurn=true');
t.own(0, 'A2');
t.ram(0, 5).use(avatarCard(t, 0, 1), { to: 'A2' }).settle();
assert.equal(t.avatar(0, 5).at, 'A2', 'the copy is in play, defending A2');
const acts = () => t.legal().filter((a) => (a.type === 'advance' || a.type === 'attack') && a.slot === 5);
assert.deepEqual(acts(), []);
assert.ok(t.legal().some((a) => a.type === 'advance' && a.slot === 1), 'the original still can');
t.to('attack');
assert.deepEqual(acts(), []);
t.to('playAgain').end().to('playAgain').end();
assert.ok(acts().length > 0, 'next turn it can advance');
});
test('copy rule variants: copies can need Avatar 2 unlocked, or be off', () => {
const t = scene('Carbon NanoBots', 'Broken Bits');
t.own(0, 'A2');
t.ram(0, 5);
const replicate = () => t.legal().some((a) => a.type === 'activate' && a.name === 'Carbon NanoBots');
assert.ok(replicate());
t.s.rules = withRule(t.s.rules, 'copies.needAvatar2=true');
assert.ok(!replicate(), 'Avatar 2 is still face down');
t.avatar(0, 2).faceUp = true;
assert.ok(replicate());
t.s.rules = withRule(t.s.rules, 'copies.allowed=false');
assert.ok(!replicate());
});
test('no copy can enter a location while a combat is going on there', () => {
const t = scene('Broken Bits', 'Carbon NanoBots', { north: { servers: ['Cluster Server'] } });
t.own(1, 'C1', 'Cluster Server');
assert.ok(hasRoom(t.s, 1, 'C1'), 'the Cluster Server has room for a second defender');
t.ram(1, 5);
fight(t);
assert.ok(t.s.combat, 'North can Replicate, so the combat stops for a response');
assert.ok(!hasRoom(t.s, 1, 'C1'));
});
card('Corrupt Driver', () => {
const t = scene('Corrupt Driver', 'Broken Bits', { north: { main: ['Additional Core'] } });
const hw = t.hardware(1, 1, 'Additional Core');
t.ram(0, 4);
fight(t);
t.rig(1, 1, 1, 6, 6, 6, 6);
t.use(avatarCard(t, 0, 1), { card: hw }).settle();
assert.equal(dice(t, 'def'), 4, 'the Additional Core is off');
assert.ok(t.s.cards[hw].disabledUntil > t.s.clock);
});
card('Pixelated Picture', () => {
const t = scene('Broken Bits', 'Pixelated Picture');
t.ram(1, 3);
fight(t);
assert.equal(t.decider, 1);
t.rig(6, 6, 6, 1, 1, 1);
t.use(avatarCard(t, 1, 1)).settle();
assert.equal(dice(t, 'atk'), 3, 'the attacker has -1 Core');
});
card('Beta Version 1.0', () => {
const t = scene('Beta Version 1.0', 'Broken Bits', { south: { main: ['Additional Core', 'RAM Expansion', 'Packet Accelerator'] } });
t.hardware(0, 1, 'RAM Expansion');
t.hardware(0, 1, 'Packet Accelerator');
const [ac] = t.hand(0, 'Additional Core');
t.ram(0, 4 + 5);
fight(t);
t.rig(6, 6, 6, 6, 6, 6, 6, 6, 1, 1, 1, 1);
t.use(avatarCard(t, 0, 1), { card: ac }).settle();
assert.equal(dice(t, 'atk'), 4 + 2 + 1 + 1, 'a third Hardware on a 2-port Avatar');
t.to('playAgain').end();
assert.ok(t.s.players[0].discard.includes(ac), 'gone at the end of the turn');
});
card('Code Zero', () => {
const t = scene('Broken Bits', 'Code Zero');
t.ram(1, 4);
fight(t);
assert.equal(t.decider, 1);
t.rig(6, 6, 6, 1, 1, 1);
t.use(avatarCard(t, 1, 1)).settle();
assert.equal(t.avatar(0, 1).infection >= 1 || t.avatar(0, 1).at === null, true);
assert.equal(dice(t, 'atk'), 3, '1 Infection counter: -1 Core while Code Zero is in play');
assert.equal(dice(t, 'def'), 3, 'Code Zero has 3 Cores');
});
card('Data Leaker', () => {
const t = scene('Data Leaker', 'Broken Bits', { south: { main: ['Additional Core'] }, north: { main: ['Cooling System', 'Hotfix'] } });
const [cool] = t.hand(1, 'Cooling System', 'Hotfix');
t.top(0, 'Additional Core');
t.ram(0, 3);
// Breach first: take an undefended enemy location this turn.
t.own(1, 'C2');
t.go({ type: 'advance', slot: 1, to: 'C2' }).to('attack').end();
assert.equal(t.s.locations.C2.owner, 0);
t.use(avatarCard(t, 0, 1));
assert.equal(t.s.choice?.kind, 'Data Leaker:discard');
t.choose('Cooling System').settle();
assert.ok(t.s.players[1].discard.includes(cool));
assert.equal(t.s.players[1].ram, 0, 'no RAM for the discard');
assert.equal(t.s.players[0].hand.length, 1, 'it breached this turn, so it draws');
});
card('Backup Machine', () => {
const t = scene('Backup Machine', 'Broken Bits', { south: { main: ['Additional Core'] } });
const ac = t.take(0, 'Additional Core');
t.s.players[0].discard.push(ac);
t.ram(0, 5 + 3).use(avatarCard(t, 0, 1), { card: ac });
assert.equal(t.s.choice?.kind, 'Backup Machine:install');
t.choose('Install').settle();
assert.deepEqual(t.avatar(0, 1).hardware, [ac]);
assert.equal(t.s.players[0].ram, 0, '5 for the Ability, 5 - 2 for the Hardware');
});
card('Broken Bits', () => {
const t = scene('Broken Bits', 'Broken Bits');
t.ram(0, 4);
t.rig(1, 1, 2, 2, 3, 3, 3, 3);
fight(t);
assert.equal(openWindow(t.s)?.kind, 'postRoll', 'nothing to do before the roll');
t.rig(6, 6, 6, 6);
t.use(avatarCard(t, 0, 1), { k: 2 }).settle();
assert.match(t.logText(), /Bit Flip rerolls 4 dice/);
assert.equal(t.s.locations.C1.owner, 0);
});