files-tcg/sim/src/types.ts
2026-09-28 01:56:25 -04:00

329 lines
10 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import type { CardDef } from './cards.ts';
import type { LocId } from './map.ts';
import type { Rng } from './rng.ts';
import type { RulesConfig } from './rules.ts';
/** 0 = South (starts on S1), 1 = North (starts on S2). */
export type PlayerId = 0 | 1;
export const PLAYER_NAMES = ['South', 'North'] as const;
/**
* Status timers count half-turns: the clock is 2T at the start of player-turn T and 2T + 1 at its
* end. A status with `until: n` is active while the clock is below n.
*/
export type Clock = number;
export const FOREVER: Clock = 1e9;
export interface CardInstance {
id: number;
name: string;
owner: PlayerId;
/** Hardware or Program: disabled until this clock (0 = not disabled). */
disabledUntil: Clock;
/** Program: text ignored until this clock (Execute: Override). */
ignoredUntil: Clock;
/** Turn it was last installed, for Execute: Rollback. */
installedTurn: number;
/** Worm Protocol: the adjacent location it spreads to. */
wormTo?: LocId;
/** Hardware Sandbox: the Program it protects. */
protects?: number;
/** Duplicate Server: the Server ability it copies. */
copies?: string;
/** One-time effects already used (Redundant Power Supply, Visual Artifact). */
spent?: boolean;
/** Hardware attached by Push Update: discarded at this clock. */
pushedUntil?: Clock;
}
/** Slots 1–4 are the printed Avatars; slot k + 4 is a copy of Avatar k. */
export interface AvatarRef {
player: PlayerId;
slot: number;
}
/** A temporary Core bonus or penalty on one Avatar. */
export interface Modifier {
/** Increases with every new modifier, so "most recent" is the highest. */
seq: number;
cores: number;
source: string;
/** Player whose card created it. */
by: PlayerId;
/** Ends with this combat, or at `until` on the clock. */
combat?: number;
until?: Clock;
}
export interface AvatarState {
/** Card id, or -1 in a player view where the Avatar is still face down. */
card: number;
/** Face up = unlocked. Copies are face up only while they exist. */
faceUp: boolean;
/** null while the Avatar is in its slot (or, for a copy, doesn't exist). */
at: LocId | null;
hardware: number[];
/** Placed (entered play or repositioned) this turn. */
placed: boolean;
advanced: boolean;
/** Attacks declared this turn (Infinite Loop allows 2). */
attacks: number;
/** Removed when the Avatar is defeated. */
infection: number;
/** Bug counters: like Infection, for Bug cards. Removed when the Avatar is defeated. */
bugs: number;
/** Persistent Infection counters survive defeat. */
permanentInfection: number;
mods: Modifier[];
/** Emergency Shutdown. */
disabledUntil: Clock;
/** Packet Flood: can't move or retreat. */
rootedUntil: Clock;
/** Last turn it captured an enemy location. */
breachedTurn: number;
/** For copies: the original's slot. */
copyOf: number | null;
}
export interface PlayerState {
ram: number;
hand: number[];
deck: number[];
discard: number[];
serverPile: number[];
/** Avatar N is avatars[N - 1]; avatars[N + 3] is its copy slot. */
avatars: AvatarState[];
/** Highest location level captured so far; unlocks Avatars up to this number. */
unlockedLevel: number;
}
export interface LocationState {
owner: PlayerId | null;
server: number | null;
programs: number[];
/** Execute: Root Access, by player: that player ignores protection here until this clock. */
rootAccess: [Clock, Clock];
}
/** Main turn steps. Draw (1), Resolve (5) and Pass (7) happen automatically. */
export type Step = 'deploy' | 'play' | 'attack' | 'resolve' | 'playAgain';
export interface Declaration {
kind: 'advance' | 'attack';
ref: AvatarRef;
to: LocId;
/** Where the attacker stood when it declared. */
from: LocId;
/** Infinite Loop: this Avatar's second attack this turn. */
second: boolean;
}
/** Parameters of an Execution or ability. Each card uses the fields it needs. */
export interface Args {
ref?: AvatarRef;
card?: number;
cards?: number[];
to?: LocId;
mode?: 'destroy' | 'disable';
/** Bit Flip: reroll dice showing this or less. */
k?: number;
}
export interface StackItem {
kind: 'execution' | 'ability';
player: PlayerId;
/** The Execution card, or the card whose ability this is. */
card: number;
name: string;
target: LocId | null;
args: Args;
/** The Server (one of the player's locations) performing an Execution, when it matters. */
from: LocId | null;
/** Dedicated GPU: +1 effectiveness. */
power: number;
}
/** A copied Program effect (Fork, Process Clone, Remote Access, Undefined Behavior). */
export interface VirtualProgram {
id: number;
name: string;
controller: PlayerId;
at: LocId;
until: Clock;
/** Counts as an installed Program (Process Clone, Remote Access). */
installed: boolean;
/** Card the effect was copied from. */
from: number;
}
export interface Combat {
id: number;
/** The defended location. */
loc: LocId;
attacker: AvatarRef;
/** Where the attacker stands, for its Server bonus. */
origin: LocId;
defenders: AvatarRef[];
kind: 'attack' | 'advance' | 'traceRoute';
/** Dice by Avatar, keyed `player:slot`. */
dice: Record<string, number[]>;
rolled: boolean;
/** Ended early (Emergency Shutdown, Disconnect). */
ended: boolean;
/** Hardware disabled for this combat only (Memory Parasite). */
suppressed: number[];
/** Race Condition picks, by player. */
race: [('attack' | 'defend') | null, ('attack' | 'defend') | null];
ties: number;
}
/** Data stored in the state (task and choice payloads). Keep it JSON-serializable. */
export type Json = unknown;
export interface ChoiceOption {
label: string;
value: Json;
/** RAM needed to pick this option. */
cost?: number;
}
/** A decision a card asks a player to make before the game can continue. */
export interface Choice {
player: PlayerId;
/** Stable id for bots, e.g. "RAID Array:keep". */
kind: string;
prompt: string;
options: ChoiceOption[];
/** Script function that receives the picked value. */
resume: { name: string; fn: string; data: Json };
}
export type WindowKind = 'response' | 'preRoll' | 'postRoll';
export type HookName =
| 'onTurnStart'
| 'onDraw'
| 'onAttackDeclared'
| 'onBreach'
| 'onCaptured'
| 'onPlaced'
| 'onInstall'
| 'onCombatStart'
| 'onDefeat'
| 'onCombatEnd';
/** Something that has an effect in play. */
export interface Source {
kind: 'server' | 'program' | 'hardware' | 'avatar';
/** The card providing it (the original card for copied effects). */
id: number;
/** Script that runs. Differs from the card for copies (Duplicate Server, Fork...). */
name: string;
controller: PlayerId;
/** Where the effect applies. */
at: LocId | null;
/** Hardware: its wearer. Avatar: itself. */
ref?: AvatarRef;
/** Virtual copy id, or -1 for a Worm Protocol spread. */
virtual?: number;
}
/** Engine work still to do. Tasks run front to back; a task may add tasks in front of the rest. */
export type Task =
| { t: 'window'; kind: WindowKind; priority: PlayerId; passes: number; base: number; resolving: boolean }
| { t: 'declaration'; index: number }
| { t: 'afterResolve' }
| { t: 'combat'; stage: 'start' | 'roll' | 'result' | 'end' | 'cleanup' }
| { t: 'defeat'; ref: AvatarRef; loc: LocId; role: 'attack' | 'defend' }
| { t: 'capture'; ref: AvatarRef; loc: LocId; stage: 'hooks' | 'main'; how: CaptureStat }
| { t: 'hook'; hook: HookName; src: Source; data: Json }
| { t: 'call'; name: string; fn: string; data: Json }
| { t: 'draw'; player: PlayerId }
| { t: 'endTurn' };
export type Action =
| { type: 'end' }
| { type: 'pass' }
| { type: 'choose'; index: number }
| { type: 'place'; slot: number; to: LocId }
| { type: 'advance'; slot: number; to: LocId }
| { type: 'attack'; slot: number; to: LocId }
| { type: 'clearFirewall'; line: string }
| { type: 'firewall'; line: string; level: number }
| { type: 'salvage'; card: number }
| { type: 'installHardware'; card: number; slot: number }
| { type: 'installProgram'; card: number; at: LocId }
| { type: 'play'; card: number; target: LocId | null; args: Args; from: LocId | null }
| { type: 'activate'; card: number; name: string; args: Args };
export const STAT_KEYS = [
'capturesUnowned',
'capturesUndefended',
'capturesInCombat',
'attacks',
'attacksWon',
'combatsEnded',
'firewallsPlaced',
'firewallsUpgraded',
'firewallsCleared',
'ramFromCaptures',
'ramFromSalvage',
'ramFromEffects',
'ramOnFirewalls',
'ramOnCards',
'ramOnAbilities',
'cardsSalvaged',
'cardsDrawn',
'hardwareInstalled',
'hardwareLost',
'programsInstalled',
'programsLost',
'executionsPlayed',
'abilitiesUsed',
] as const;
export type StatKey = (typeof STAT_KEYS)[number];
export type CaptureStat = 'capturesUnowned' | 'capturesUndefended' | 'capturesInCombat';
export type GameStats = Record<StatKey, [number, number]>;
/** Per card name, per player: times played or used, and times its effect fired. */
export interface CardUse {
played: [number, number];
fired: [number, number];
}
export interface GameState {
rules: RulesConfig;
rng: Rng;
defs: Record<string, CardDef>;
cards: CardInstance[];
players: [PlayerState, PlayerState];
locations: Record<LocId, LocationState>;
/** Firewall level by line id; a missing entry means no Firewall. */
firewalls: Record<string, number>;
firstPlayer: PlayerId;
active: PlayerId;
/** Player turns so far, counting the current one. */
turn: number;
clock: Clock;
step: Step;
declarations: Declaration[];
tasks: Task[];
stack: StackItem[];
choice: Choice | null;
combat: Combat | null;
virtuals: VirtualProgram[];
/** Once-per-turn uses this turn, by key. */
used: Record<string, number>;
/** Counter for ids and modifier order. */
seq: number;
over: boolean;
winner: PlayerId | null;
endReason: 'win' | 'turnLimit' | null;
stats: GameStats;
cardUse: Record<string, CardUse>;
/** Play-by-play text, when logging is on. */
log: string[] | null;
/** Dice results to use before the RNG. Tests set this to script a combat. */
rigged?: number[];
}