files-tcg/sim/test/health.test.ts
2026-10-01 17:20:33 -04:00

247 lines
11 KiB
TypeScript

/** The health rule (rules v0.9): the loser of the roll takes damage; destruction at 0 Health; recall. */
import assert from 'node:assert/strict';
import { test } from 'node:test';
import { healthLeft, healthOf } from '../src/query.ts';
import { withRule } from '../src/rules.ts';
import { FOREVER } from '../src/types.ts';
import { Table } from './table.ts';
/** Ends steps until player `p` is in their Deploy step. */
function turnOf(t: Table, p: 0 | 1): Table {
for (let i = 0; i < 12 && !(t.decider === p && t.s.step === 'deploy'); i++) t.end();
assert.equal(t.decider, p);
assert.equal(t.s.step, 'deploy');
return t;
}
/** South's Anomaly 1 (Broken Bits, 4 Cores, 9 Health) on B1 facing North's Anomaly 1 on C1. */
function standoff(o: { northMain?: string[]; southMain?: string[] } = {}): Table {
const t = new Table({ health: true, north: { main: o.northMain }, south: { main: o.southMain } });
t.own(0, 'B1');
t.own(1, 'C1');
t.place(0, 1, 'B1');
t.place(1, 1, 'C1');
return t;
}
test('health: printed Health, and the override rule', () => {
const t = standoff();
assert.equal(healthOf(t.s, { player: 0, slot: 1 }), 9, 'Broken Bits: 4 Cores, 9 Health');
assert.equal(healthOf(t.s, { player: 0, slot: 2 }), 10, 'Pixelated Picture: 3 Cores, 10 Health');
t.s.rules = withRule(t.s.rules, 'health.override=8');
assert.equal(healthOf(t.s, { player: 0, slot: 1 }), 8);
});
test('health: the loser takes the margin as damage, and nobody moves', () => {
const t = standoff();
t.to('attack').go({ type: 'attack', slot: 1, to: 'C1' });
t.rig(6, 6, 6, 6, 5, 5, 5, 4); // 24 to 19
t.end();
assert.equal(t.s.locations.C1.owner, 1, 'C1 holds');
assert.equal(t.anomaly(0, 1).at, 'B1', 'the attacker stays where it attacked from');
assert.equal(t.anomaly(1, 1).at, 'C1');
assert.equal(t.anomaly(1, 1).damage, 5);
assert.equal(healthLeft(t.s, { player: 1, slot: 1 }), 4);
assert.equal(t.s.stats.attacksWon[0], 1);
assert.equal(t.s.stats.damageDealt[0], 5);
assert.equal(t.s.stats.capturesInCombat[0], 0);
assert.equal(t.s.combat, null);
});
test('health: damage adds up; the Anomaly is destroyed at 0 Health, loses its Hardware, and the location falls', () => {
const t = standoff({ northMain: ['Additional Core'] });
const hw = t.hardware(1, 1, 'Additional Core');
t.anomaly(1, 1).damage = 6;
t.to('attack').go({ type: 'attack', slot: 1, to: 'C1' });
t.rig(6, 6, 6, 6, 3, 3, 3, 3, 3); // 24 to 15: 9 damage, 3 was enough
t.end();
assert.equal(t.anomaly(1, 1).at, null, 'destroyed: back in its slot');
assert.equal(t.anomaly(1, 1).damage, 0, 'a destroyed Anomaly comes back at full Health');
assert.deepEqual(t.anomaly(1, 1).hardware, []);
assert.ok(t.s.players[1].discard.includes(hw));
assert.equal(t.s.locations.C1.owner, 0);
assert.equal(t.anomaly(0, 1).at, 'C1');
assert.equal(t.s.stats.capturesInCombat[0], 1);
assert.equal(t.s.stats.anomaliesDestroyed[1], 1);
assert.equal(t.s.stats.damageDealt[0], 9, 'the whole margin counts as dealt');
});
test('health: a losing attacker takes damage and stays put, or is destroyed by a big enough loss', () => {
const t = standoff();
t.to('attack').go({ type: 'attack', slot: 1, to: 'C1' });
t.rig(3, 3, 3, 3, 4, 4, 4, 4); // 12 to 16
t.end();
assert.equal(t.anomaly(0, 1).at, 'B1');
assert.equal(t.anomaly(0, 1).damage, 4);
assert.equal(t.anomaly(1, 1).damage, 0, 'the winner takes no damage');
assert.equal(t.s.stats.damageDealt[1], 4);
const u = standoff();
u.anomaly(0, 1).damage = 5;
u.to('attack').go({ type: 'attack', slot: 1, to: 'C1' });
u.rig(1, 1, 1, 1, 2, 2, 2, 2); // 4 to 8: 4 damage, 4 left
u.end();
assert.equal(u.anomaly(0, 1).at, null, 'destroyed');
assert.equal(u.anomaly(0, 1).damage, 0);
assert.equal(u.s.locations.B1.owner, 0, 'losing an attack never loses the origin');
});
test('health: a tie is a stalemate, with no reroll and no damage', () => {
const t = standoff();
t.to('attack').go({ type: 'attack', slot: 1, to: 'C1' });
t.rig(3, 3, 3, 3, 4, 4, 2, 2);
t.end();
assert.equal(t.anomaly(0, 1).damage, 0);
assert.equal(t.anomaly(1, 1).damage, 0);
assert.equal(t.anomaly(0, 1).at, 'B1');
assert.equal(t.anomaly(1, 1).at, 'C1');
assert.deepEqual(t.s.rigged, [], 'no dice were rerolled');
assert.match(t.logText(), /stalemate/);
assert.equal(t.s.stats.combatsTied[0], 1);
assert.equal(t.s.stats.attacksWon[0], 0);
});
test('health: recall heals fully, keeps Hardware, counts as the placement, and leaves the location undefended', () => {
const t = standoff({ northMain: ['Additional Core'] });
const hw = t.hardware(1, 1, 'Additional Core');
t.anomaly(1, 1).damage = 7;
t.anomaly(1, 1).infection = 2;
turnOf(t, 1); // South's turn passes without acting
assert.ok(t.can({ type: 'recall', slot: 1 }));
assert.ok(!t.can({ type: 'recall', slot: 2 }), 'only Anomalies in play can be recalled');
t.go({ type: 'recall', slot: 1 });
const av = t.anomaly(1, 1);
assert.equal(av.at, null);
assert.equal(av.damage, 0);
assert.equal(av.infection, 0, 'leaving play removes counters');
assert.deepEqual(av.hardware, [hw], 'Hardware stays on it');
assert.ok(av.placed, 'the recall was its placement');
assert.ok(!t.can({ type: 'place', slot: 1, to: 'C1' }), "it can't come back this turn");
assert.equal(t.s.stats.recalls[1], 1);
assert.equal(t.s.locations.C1.owner, 1, 'C1 is still North\'s, just undefended');
// South can now walk in.
turnOf(t, 0);
assert.ok(t.can({ type: 'advance', slot: 1, to: 'C1' }));
});
test('health: a rooted Anomaly can\'t be recalled, and the rule can be switched off', () => {
const t = standoff();
t.anomaly(0, 1).rootedUntil = FOREVER;
assert.ok(!t.can({ type: 'recall', slot: 1 }), 'Packet Flood: no retreat');
t.anomaly(0, 1).rootedUntil = 0;
assert.ok(t.can({ type: 'recall', slot: 1 }));
t.s.rules = withRule(t.s.rules, 'health.recall=false');
assert.ok(!t.can({ type: 'recall', slot: 1 }));
});
test('health: Redundant Power Supply still saves the Hardware of a destroyed Anomaly', () => {
const t = standoff({ northMain: ['Redundant Power Supply', 'Additional Core'] });
const rps = t.hardware(1, 1, 'Redundant Power Supply');
const core = t.hardware(1, 1, 'Additional Core');
t.anomaly(1, 1).damage = 8;
t.to('attack').go({ type: 'attack', slot: 1, to: 'C1' });
t.rig(6, 6, 6, 6, 1, 1, 1, 1, 1); // 24 to 5
t.end();
assert.equal(t.anomaly(1, 1).at, null);
assert.deepEqual([...t.anomaly(1, 1).hardware].sort(), [rps, core].sort());
assert.equal(t.s.locations.C1.owner, 0);
});
test('health: with two defenders the damage destroys the weakest first, and the location falls only when both are gone', () => {
const t = new Table({ health: true, north: { servers: ['Cluster Server'] } });
t.own(0, 'B1');
t.own(1, 'C1', 'Cluster Server');
t.place(0, 1, 'B1');
t.place(1, 1, 'C1'); // Broken Bits, 4 Cores, 9 Health
t.place(1, 2, 'C1'); // Pixelated Picture, 3 Cores, 10 Health
t.anomaly(1, 2).damage = 7; // 3 left
t.to('attack').go({ type: 'attack', slot: 1, to: 'C1' });
t.rig(6, 6, 6, 6, 1, 1, 1, 1, 5, 5, 5); // 24 to 4 + 15 = 19: 5 damage
t.end();
assert.equal(t.anomaly(1, 2).at, null, 'the weak one is destroyed with 3 of the damage');
assert.equal(t.anomaly(1, 1).damage, 2, 'the other 2 land on the survivor');
assert.equal(t.s.locations.C1.owner, 1, 'C1 holds with a defender left');
assert.equal(t.anomaly(0, 1).at, 'B1');
// Enough damage for both takes the location.
const u = new Table({ health: true, north: { servers: ['Cluster Server'] } });
u.own(0, 'B1');
u.own(1, 'C1', 'Cluster Server');
u.place(0, 1, 'B1');
u.place(1, 1, 'C1');
u.place(1, 2, 'C1');
u.anomaly(1, 1).damage = 8;
u.anomaly(1, 2).damage = 8;
u.to('attack').go({ type: 'attack', slot: 1, to: 'C1' });
u.rig(6, 6, 6, 6, 1, 1, 1, 1, 1, 1, 1); // 24 to 7: 17 damage for 1 + 2 left
u.end();
assert.equal(u.anomaly(1, 1).at, null);
assert.equal(u.anomaly(1, 2).at, null);
assert.equal(u.s.locations.C1.owner, 0);
assert.equal(u.anomaly(0, 1).at, 'C1');
});
test('health: Resource Leech and Persistent Infection fire on a lost roll, not only a destruction', () => {
const t = standoff({ northMain: ['Resource Leech', 'Persistent Infection'] });
t.program(1, 'C1', 'Resource Leech');
t.program(1, 'C1', 'Persistent Infection');
const bandwidth = t.s.players[1].bandwidth;
t.to('attack').go({ type: 'attack', slot: 1, to: 'C1' });
t.rig(3, 3, 3, 3, 4, 4, 4, 4); // 12 to 16: the attacker takes 4 and lives
t.end();
assert.equal(t.anomaly(0, 1).at, 'B1');
assert.equal(t.anomaly(0, 1).damage, 4);
assert.equal(t.s.players[1].bandwidth, bandwidth + 3, 'Resource Leech pays 3');
assert.equal(t.anomaly(0, 1).permanentInfection, 1, 'a permanent Infection counter');
});
test('health: Overflow Error only disables, until the next command, while the attacker lives', () => {
const t = standoff({ northMain: ['Overflow Error'], southMain: ['Stack Overflow'] });
t.program(1, 'C1', 'Overflow Error');
const so = t.program(0, 'B1', 'Stack Overflow');
t.to('attack').go({ type: 'attack', slot: 1, to: 'C1' });
t.rig(3, 3, 3, 3, 4, 4, 4, 4);
t.end();
assert.equal(t.s.choice?.kind, 'Overflow Error:hit');
assert.ok(t.s.choice!.options.every((o) => o.label.startsWith('Disable')), 'no destroy option while the attacker lives');
t.choose('Disable Stack Overflow').settle();
assert.ok(t.s.cards[so].disabledUntil > t.s.clock && t.s.cards[so].disabledUntil < FOREVER, 'disabled until the next command');
assert.ok(t.s.locations.B1.programs.includes(so), 'still installed');
});
test('health: Overflow Error may destroy a Program when the attacker is destroyed', () => {
const t = standoff({ northMain: ['Overflow Error'], southMain: ['Stack Overflow'] });
t.program(1, 'C1', 'Overflow Error');
const so = t.program(0, 'B1', 'Stack Overflow');
t.to('attack').go({ type: 'attack', slot: 1, to: 'C1' });
t.rig(1, 1, 1, 1, 6, 6, 6, 6); // 4 to 24: 20 damage on 9 Health
t.end();
assert.equal(t.s.choice?.kind, 'Overflow Error:hit');
t.choose('Destroy Stack Overflow').settle();
assert.ok(t.s.players[0].discard.includes(so));
assert.equal(t.anomaly(0, 1).at, null);
});
test("health: Backup Machine's Restore Point heals 3 damage", () => {
const t = new Table({ health: true, south: { anomalies: ['Backup Machine', 'Broken Bits', 'Pixelated Picture', 'Corrupt Driver'] } });
t.own(0, 'B1');
t.place(0, 1, 'B1');
const bm = t.anomaly(0, 1).card;
assert.ok(!t.legal().some((a) => a.type === 'activate' && a.card === bm && a.args.heal), 'nothing to heal yet');
t.anomaly(0, 1).damage = 5;
t.bandwidth(0, 5).use(bm, { heal: true }).settle();
assert.equal(t.anomaly(0, 1).damage, 2);
assert.equal(t.s.players[0].bandwidth, 0);
assert.equal(healthLeft(t.s, { player: 0, slot: 1 }), 8);
});
test('health: RAM Expansion adds 2 Health, so a hit that would destroy the wearer leaves it standing', () => {
const t = standoff({ northMain: ['RAM Expansion'] });
t.hardware(1, 1, 'RAM Expansion');
assert.equal(healthOf(t.s, { player: 1, slot: 1 }), 11);
t.to('attack').go({ type: 'attack', slot: 1, to: 'C1' });
t.rig(6, 6, 6, 6, 4, 4, 4, 3); // 24 to 15: 9 damage against 11 Health
t.end();
assert.equal(t.anomaly(1, 1).at, 'C1', 'C1 holds');
assert.equal(healthLeft(t.s, { player: 1, slot: 1 }), 2);
});