files-tcg/sim/src/cards/hardware.ts
2026-10-01 17:20:33 -04:00

373 lines
15 KiB
TypeScript

/**
* Hardware. It works while its wearer is in play. "The occupied Server" is the location the
* wearer stands on.
*/
import { describeArgs } from '../describe.ts';
import { knockOut } from '../flow.ts';
import { desynced, hardwareActive, slotsFit, takesSlot } from '../query.ts';
import { scriptOf, type Script } from '../scripts.ts';
import {
ask,
anomaly,
anomalyName,
defOf,
fired,
isCopy,
markUsed,
nameOf,
nextTurnOf,
ownerOf,
played,
programLocation,
refsInPlay,
removeFrom,
say,
startOf,
endOf,
stat,
toDiscard,
toHand,
uninstallHardware,
usedThisTurn,
wearerOf,
} from '../state.ts';
import type { AnomalyRef, GameState, LocationState, PlayerId, StackItem } from '../types.ts';
import { cardOptions, programsOn } from './util.ts';
/**
* Uses a Hardware card has left this turn. Its "once per turn" or "first ... each turn" effect gets
* one use, or two when Cooling System on the same Anomaly picks it (Cooling helps one Hardware a turn).
*/
export function hardwareUsesLeft(s: GameState, hw: number): number {
const used = usedThisTurn(s, `hw:${hw}`);
const cooler = coolerFor(s, hw);
return (cooler === null ? 1 : 2) - used;
}
function coolerFor(s: GameState, hw: number): number | null {
const ref = wearerOf(s, hw);
if (!ref) return null;
for (const id of anomaly(s, ref).hardware) {
if (id === hw || s.cards[id].name !== 'Cooling System' || !hardwareActive(s, id)) continue;
const pickedFor = s.used[`cool:${id}`];
if (pickedFor === undefined || pickedFor === hw) return id;
}
return null;
}
/**
* Reading: an Anomaly that loses an Expansion Slot (Network Interface Upgrade removed) discards Hardware until it
* fits, the most recently installed first.
*/
export function shedHardware(s: GameState, r: AnomalyRef): void {
while (!slotsFit(s, r)) {
const hw = anomaly(s, r).hardware.findLast((id) => takesSlot(s, id))!;
uninstallHardware(s, hw);
toDiscard(s, hw);
stat(s, 'hardwareLost', r.player);
say(s, `${anomalyName(s, r)} is out of Expansion Slots and discards ${nameOf(s, hw)}.`);
}
}
export function useHardware(s: GameState, hw: number): void {
const cooler = coolerFor(s, hw);
markUsed(s, `hw:${hw}`);
if (usedThisTurn(s, `hw:${hw}`) >= 2 && cooler !== null) {
s.used[`cool:${cooler}`] = hw;
fired(s, 'Cooling System', ownerOf(s, hw));
}
}
const wearing = (ref: AnomalyRef | undefined, f: { ref: AnomalyRef }) =>
!!ref && ref.player === f.ref.player && (ref.slot === f.ref.slot || ref.slot + 4 === f.ref.slot);
/** Installs a Hardware card from its owner's hand onto an Anomaly, without paying. */
export function attachHardware(s: GameState, card: number, ref: AnomalyRef): void {
removeFrom(s.players[ref.player].hand, card);
anomaly(s, ref).hardware.push(card);
s.cards[card].installedTurn = s.turn;
stat(s, 'hardwareInstalled', ref.player);
played(s, nameOf(s, card), ref.player);
}
export const HARDWARE: Record<string, Script> = {
// Health rule: the wearer's Health goes up by 2 (query.ts healthOf). Without the rule it does nothing.
'RAM Expansion': {
text: 'Anomaly gains +2 Health.',
health: 2,
},
'Additional Core': {
text: 'Anomaly gains +1 Core.',
cores: () => 1,
},
// Reading: the Program is one on the location the wearer fought from, the owner picks it, and it
// stays disabled through the end of the next turn.
'Overclocked CPU': {
text: 'Anomaly gains +2 Cores during combat, but one installed Program at the current server becomes disabled afterward for 2 commands.',
cores: (_s, _src, f) => (f.role !== null ? 2 : 0),
onCombatEnd(s, src) {
const c = s.combat!;
const role = wearing(src.ref, { ref: c.attacker }) ? 'attack' : c.defenders.some((d) => wearing(src.ref, { ref: d })) ? 'defend' : null;
if (!role) return;
const loc = role === 'attack' ? c.origin : c.loc;
if (s.locations[loc].owner !== src.controller) return;
const progs = programsOn(s, loc);
if (progs.length === 0) return;
fired(s, 'Overclocked CPU', src.controller);
if (progs.length === 1) {
disableForTwoTurns(s, progs[0]);
return;
}
ask(s, {
player: src.controller,
kind: 'Overclocked CPU:disable',
prompt: `Overclocked CPU: which Program on ${loc} is disabled for 2 turns?`,
options: progs.map((id) => ({ label: nameOf(s, id), value: id })),
resume: { name: 'Overclocked CPU', fn: 'disable', data: null },
});
},
resume: {
disable: (s, _data, id: number) => disableForTwoTurns(s, id),
},
},
'Solid-State Cache': {
text: 'The first Program installed on the occupied Server each round costs 3 less Gb.',
costMod: (s, src, pay) =>
pay.kind === 'program' && pay.player === src.controller && pay.at === src.at && hardwareUsesLeft(s, src.id) > 0 ? -3 : 0,
onPaid(s, src) {
useHardware(s, src.id);
fired(s, 'Solid-State Cache', src.controller);
},
},
// Reading: the first time the wearer is defeated with this installed, all its Hardware stays on
// it (in its slot) instead of being discarded.
'Redundant Power Supply': {
text: 'The first time this Anomaly would be destroyed, do not destroy the Hardware.',
savePriority: 0,
onWearerDefeated(s, hw, ev) {
if (s.cards[hw].spent) return false;
s.cards[hw].spent = true;
fired(s, 'Redundant Power Supply', ev.ref.player);
say(s, `Redundant Power Supply: ${anomalyName(s, ev.ref)} keeps its Hardware.`);
knockOut(s, ev.ref, ev.loc, ev.role, true);
return true;
},
},
// Ruling: it takes no Expansion Slot and adds one, so the wearer nets +1 Expansion Slot.
'Network Interface Upgrade': {
text: 'This Hardware can be played without taking an Expansion Slot on the Anomaly.',
noSlot: true,
extraSlots: 1,
},
// Ruling: Servers perform Executions, so this works for Executions performed by the Server the
// wearer stands on. +1 to the number the Execution uses (cards drawn, Cores, Bandwidth recovered).
'Dedicated GPU': {
text: 'Executions performed by this Anomaly receive +1 effectiveness or combat strength.',
executionPower: 1,
},
// Blocks opponents' Bug counters (see addBugs in util.ts; no card places them yet) and their
// Bug Program effects on the wearer (Bloatware, Stock Rice).
'ECC Memory': {
text: 'Opponents cannot place Bug counters or Bug status effects on this Anomaly.',
bugImmunity: true,
},
// Works through hardwareUsesLeft() above.
'Cooling System': {
text: 'Allow one Hardware to activate twice per round.',
},
// Reading: half the combined printed Bandwidth of its Hardware, rounded up.
'RAID Array': {
text: 'When this Anomaly is destroyed, all hardware can be kept by paying half the Bandwidth cost.',
savePriority: 1,
onWearerDefeated(s, hw, ev) {
const av = anomaly(s, ev.ref);
const cost = Math.ceil(av.hardware.reduce((t, h) => t + defOf(s, h).bandwidth, 0) / 2);
ask(s, {
player: ev.ref.player,
kind: 'RAID Array:keep',
prompt: `RAID Array: pay ${cost} Gb to keep ${av.hardware.map((h) => nameOf(s, h)).join(', ')}?`,
options: [
{ label: `Keep all Hardware for ${cost} Gb`, value: true, cost },
{ label: 'Lose the Hardware', value: false },
],
resume: { name: 'RAID Array', fn: 'keep', data: { ref: ev.ref as never, loc: ev.loc, role: ev.role, cost } },
});
return true;
},
resume: {
keep(s, data: { ref: AnomalyRef; loc: string; role: 'attack' | 'defend'; cost: number }, keep: boolean) {
if (keep) {
s.players[data.ref.player].bandwidth -= data.cost;
stat(s, 'bandwidthOnCards', data.ref.player, data.cost);
fired(s, 'RAID Array', data.ref.player);
}
knockOut(s, data.ref, data.loc, data.role, keep);
},
},
},
'Packet Accelerator': {
text: 'This Anomaly gains +1 Core hen attacking.',
cores: (_s, _src, f) => (f.role === 'attack' ? 1 : 0),
},
// Programs never target Anomalies, so this taxes Executions (and Hardware they hit).
'Firewall Appliance': {
text: 'The first hostile Program or Execution targeting this Anomaly each round costs the opponent +3 Gb.',
costMod: (s, src, pay) =>
pay.kind === 'execution' && pay.player !== src.controller && hardwareUsesLeft(s, src.id) > 0 &&
(pay.hostile ?? []).some((r) => wearing(src.ref, { ref: r })) ? 3 : 0,
onPaid(s, src) {
useHardware(s, src.id);
fired(s, 'Firewall Appliance', src.controller);
},
},
// Reading: pick the Program on install, and again at the start of your turn if the wearer has
// moved. Protection only holds while the wearer stands on that Program's location.
'Hardware Sandbox': {
text: 'Choose one Program installed on the occupied Server. It cannot be targeted by opponents.',
onInstall(s, src, ev: { card: number }) {
if (ev.card === src.id) askSandbox(s, src.id);
},
onTurnStart(s, src) {
if (s.active !== src.controller) return;
const p = s.cards[src.id].protects;
if (p === undefined || programLocation(s, p) !== src.at) askSandbox(s, src.id);
},
resume: {
protect(s, data: { card: number }, program: number) {
s.cards[data.card].protects = program;
fired(s, 'Hardware Sandbox', ownerOf(s, data.card));
},
},
},
// Reading: at the start of your turn, look at the top card and you may put it on the bottom.
'Diagnostic Module': {
text: 'Look at one face down card in your draw pile. You may place it at the bottom and draw another to start your first command each round.',
onTurnStart(s, src) {
if (s.active !== src.controller || hardwareUsesLeft(s, src.id) <= 0) return;
const deck = s.players[src.controller].deck;
if (deck.length < 2) return;
useHardware(s, src.id);
fired(s, 'Diagnostic Module', src.controller);
ask(s, {
player: src.controller,
kind: 'Diagnostic Module:bottom',
secret: true,
prompt: `Diagnostic Module: the top card of your deck is ${nameOf(s, deck[0])}. Put it on the bottom?`,
options: [
{ label: `Keep ${nameOf(s, deck[0])} on top`, value: false },
{ label: `Put ${nameOf(s, deck[0])} on the bottom`, value: true },
],
resume: { name: 'Diagnostic Module', fn: 'bottom', data: { player: src.controller } },
});
},
resume: {
bottom(s, data: { player: PlayerId }, bottom: boolean) {
const deck = s.players[data.player].deck;
if (bottom && deck.length) deck.push(deck.shift()!);
},
},
},
// Reading: swap Hardware on this Anomaly, paying the difference in printed Bandwidth (no refund).
'Hot-Swap Bay': {
text: 'Once per round, return one Hardware to your hand and install another Hardware from your hand, paying the difference in cost.',
ability: {
hardwareOncePerTurn: true,
uses(s, src) {
const av = anomaly(s, src.ref!);
const hand = s.players[src.controller].hand.filter((id) => defOf(s, id).kind === 'hardware');
return av.hardware.flatMap((out) => cardOptions(s, hand)
.filter((o) => slotsFit(s, src.ref!, out, o.value as number))
.map((o) => ({ card: out, cards: [o.value as number] })));
},
extraCost: (s, _src, args) => Math.max(0, defOf(s, args.cards![0]).bandwidth - defOf(s, args.card!).bandwidth),
resolve(s, item, src) {
const out = item.args.card!;
const into = item.args.cards![0];
const ref = src.ref!;
if (!anomaly(s, ref).hardware.includes(out) || !s.players[src.controller].hand.includes(into)) return;
if (!slotsFit(s, ref, out, into)) return;
uninstallHardware(s, out);
toHand(s, out);
attachHardware(s, into, ref);
fired(s, 'Hot-Swap Bay', src.controller);
say(s, `Hot-Swap Bay swaps ${nameOf(s, out)} for ${nameOf(s, into)} on ${anomalyName(s, ref)}.`);
},
},
},
// Ruling: Servers perform Executions, so "this Anomaly uses an Execution" means one performed by
// the Server the wearer stands on. "Disable this Hardware" lasts until your next turn.
'Quantum Coprocessor': {
text: "Once per round when this Anomaly uses an Execution, you may copy that Execution's effect with a different legal target, then disable this Hardware.",
copiesExecutions: true,
resume: {
offer(s, data: { item: StackItem }) {
const item = data.item;
const wearers = refsInPlay(s, item.player).filter((r) => !isCopy(r) && anomaly(s, r).at === item.from && !desynced(s, r));
const qc = wearers.flatMap((r) => anomaly(s, r).hardware)
.find((hw) => s.cards[hw].name === 'Quantum Coprocessor' && hardwareActive(s, hw) && hardwareUsesLeft(s, hw) > 0);
if (qc === undefined) return;
const ex = scriptOf(item.name)?.execution;
if (!ex) return;
const same = JSON.stringify({ t: item.target, a: item.args });
const plays = ex.plays(s, item.player).filter((p) => JSON.stringify({ t: p.target, a: p.args }) !== same);
if (plays.length === 0) return;
ask(s, {
player: item.player,
kind: 'Quantum Coprocessor:copy',
prompt: `Quantum Coprocessor: copy ${item.name} with a different target?`,
options: [
{ label: "Don't copy", value: null },
...plays.map((p) => ({ label: `${p.target ?? 'no target'} ${describeArgs(s, p.args, item.player)}`, value: { target: p.target, args: p.args as never } })),
],
resume: { name: 'Quantum Coprocessor', fn: 'copy', data: { item: item as never, qc } },
});
},
copy(s, data: { item: StackItem; qc: number }, v: { target: string | null; args: never } | null) {
if (!v) return;
useHardware(s, data.qc);
s.cards[data.qc].disabledUntil = startOf(nextTurnOf(s, data.item.player));
fired(s, 'Quantum Coprocessor', data.item.player);
say(s, `Quantum Coprocessor copies ${data.item.name} onto ${v.target ?? 'a new target'}.`);
scriptOf(data.item.name)!.execution!.resolve(s, { ...data.item, target: v.target, args: v.args, from: null });
},
},
},
};
function disableForTwoTurns(s: GameState, id: number): void {
s.cards[id].disabledUntil = Math.max(s.cards[id].disabledUntil, endOf(s.turn + 1));
say(s, `Overclocked CPU: ${nameOf(s, id)} is disabled for 2 turns.`);
}
function askSandbox(s: GameState, card: number): void {
const ref = wearerOf(s, card);
const at = ref ? anomaly(s, ref).at : null;
if (!ref || !at) return;
const progs = (s.locations[at] as LocationState).programs.filter((id) => ownerOf(s, id) === ref.player);
if (progs.length === 0) return;
if (progs.length === 1) {
s.cards[card].protects = progs[0];
return;
}
ask(s, {
player: ref.player,
kind: 'Hardware Sandbox:protect',
prompt: `Hardware Sandbox: protect which Program on ${at}?`,
options: progs.map((id) => ({ label: nameOf(s, id), value: id })),
resume: { name: 'Hardware Sandbox', fn: 'protect', data: { card } },
});
}