sim
This commit is contained in:
parent
746d218eda
commit
e590baf28d
44 changed files with 8308 additions and 0 deletions
293
sim/src/state.ts
Normal file
293
sim/src/state.ts
Normal file
|
|
@ -0,0 +1,293 @@
|
|||
/**
|
||||
* Small helpers for reading and changing a GameState. Nothing here knows about individual cards,
|
||||
* so both the engine and the card scripts can use it.
|
||||
*/
|
||||
import type { CardDef } from './cards.ts';
|
||||
import { GRID, LINES_AT, otherEnd, type Line, type LocId } from './map.ts';
|
||||
import { d6 } from './rng.ts';
|
||||
import {
|
||||
PLAYER_NAMES,
|
||||
type AvatarRef,
|
||||
type AvatarState,
|
||||
type CaptureStat,
|
||||
type Choice,
|
||||
type Clock,
|
||||
type Combat,
|
||||
type GameState,
|
||||
type Json,
|
||||
type Modifier,
|
||||
type PlayerId,
|
||||
type StatKey,
|
||||
type Task,
|
||||
} from './types.ts';
|
||||
|
||||
export const other = (p: PlayerId): PlayerId => (p === 0 ? 1 : 0);
|
||||
export const defOf = (s: GameState, id: number): CardDef => s.defs[s.cards[id].name];
|
||||
export const nameOf = (s: GameState, id: number): string => (id < 0 ? 'a hidden card' : s.cards[id].name);
|
||||
export const ownerOf = (s: GameState, id: number): PlayerId => s.cards[id].owner;
|
||||
export const controls = (s: GameState, p: PlayerId, loc: LocId): boolean => s.locations[loc].owner === p;
|
||||
export const isOpen = (s: GameState, line: Line): boolean => !s.firewalls[line.id];
|
||||
/** RAM to place, upgrade to, or clear a Firewall of `level`, before discounts; 0 for no Firewall. */
|
||||
export const firewallCost = (s: GameState, level: number): number => s.rules.firewallCost[level - 1] ?? 0;
|
||||
export const gridCount = (s: GameState, p: PlayerId): number => GRID.filter((loc) => controls(s, p, loc)).length;
|
||||
|
||||
/** Rolls a d6 (or takes the next rigged result). */
|
||||
export const rollDie = (s: GameState): number => (s.rigged?.length ? s.rigged.shift()! : d6(s.rng));
|
||||
|
||||
export function say(s: GameState, msg: string): void {
|
||||
s.log?.push(msg);
|
||||
}
|
||||
|
||||
export function stat(s: GameState, key: StatKey, p: PlayerId, n = 1): void {
|
||||
s.stats[key][p] += n;
|
||||
}
|
||||
|
||||
function use(s: GameState, name: string) {
|
||||
return (s.cardUse[name] ??= { played: [0, 0], fired: [0, 0] });
|
||||
}
|
||||
/** Counts a card being played, installed or activated. */
|
||||
export const played = (s: GameState, name: string, p: PlayerId): void => void use(s, name).played[p]++;
|
||||
/** Counts a card's effect happening. */
|
||||
export const fired = (s: GameState, name: string, p: PlayerId): void => void use(s, name).fired[p]++;
|
||||
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
// Time
|
||||
|
||||
export const endOfTurn = (s: GameState): Clock => 2 * s.turn + 1;
|
||||
export const startOf = (turn: number): Clock => 2 * turn;
|
||||
export const endOf = (turn: number): Clock => 2 * turn + 1;
|
||||
/** The turn number of `p`'s next turn (not the current one). */
|
||||
export const nextTurnOf = (s: GameState, p: PlayerId): number => (s.active === p ? s.turn + 2 : s.turn + 1);
|
||||
export const running = (s: GameState, until: Clock): boolean => until > s.clock;
|
||||
|
||||
/** Once-per-turn bookkeeping. Returns how many times `key` was used this turn. */
|
||||
export const usedThisTurn = (s: GameState, key: string): number => s.used[key] ?? 0;
|
||||
export function markUsed(s: GameState, key: string): void {
|
||||
s.used[key] = (s.used[key] ?? 0) + 1;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
// Avatars
|
||||
|
||||
export const refKey = (r: AvatarRef): string => `${r.player}:${r.slot}`;
|
||||
export const sameRef = (a: AvatarRef | null | undefined, b: AvatarRef | null | undefined): boolean =>
|
||||
!!a && !!b && a.player === b.player && a.slot === b.slot;
|
||||
export const avatar = (s: GameState, r: AvatarRef): AvatarState => s.players[r.player].avatars[r.slot - 1];
|
||||
export const isCopy = (r: AvatarRef): boolean => r.slot > 4;
|
||||
/** The printed Avatar a ref belongs to (a copy's original). */
|
||||
export const originalOf = (r: AvatarRef): AvatarRef => (r.slot > 4 ? { player: r.player, slot: r.slot - 4 } : r);
|
||||
export const copyOf = (r: AvatarRef): AvatarRef => ({ player: r.player, slot: originalOf(r).slot + 4 });
|
||||
export const inPlay = (s: GameState, r: AvatarRef): boolean => avatar(s, r).at !== null;
|
||||
export const avatarDef = (s: GameState, r: AvatarRef): CardDef => defOf(s, avatar(s, r).card);
|
||||
|
||||
export function avatarName(s: GameState, r: AvatarRef): string {
|
||||
const n = originalOf(r).slot;
|
||||
const card = avatar(s, r).card;
|
||||
const name = card < 0 ? '?' : nameOf(s, card);
|
||||
return isCopy(r) ? `the copy of Avatar ${n} (${name})` : `Avatar ${n} (${name})`;
|
||||
}
|
||||
|
||||
/** Every Avatar of `p` in play, originals first. */
|
||||
export function refsInPlay(s: GameState, p: PlayerId): AvatarRef[] {
|
||||
const out: AvatarRef[] = [];
|
||||
s.players[p].avatars.forEach((av, i) => {
|
||||
if (av.at !== null) out.push({ player: p, slot: i + 1 });
|
||||
});
|
||||
return out;
|
||||
}
|
||||
|
||||
/** Avatars standing on `loc`. Only the location's owner can have Avatars there. */
|
||||
export function avatarsAt(s: GameState, loc: LocId): AvatarRef[] {
|
||||
const owner = s.locations[loc].owner;
|
||||
return owner === null ? [] : refsInPlay(s, owner).filter((r) => avatar(s, r).at === loc);
|
||||
}
|
||||
export const defendersAt = avatarsAt;
|
||||
|
||||
/** Hardware that counts for an Avatar. A copy uses its original's Hardware. */
|
||||
export const hardwareOf = (s: GameState, r: AvatarRef): number[] => avatar(s, originalOf(r)).hardware;
|
||||
|
||||
/** The Avatar wearing a Hardware card, if it's installed. */
|
||||
export function wearerOf(s: GameState, hw: number): AvatarRef | null {
|
||||
const p = ownerOf(s, hw);
|
||||
const i = s.players[p].avatars.findIndex((a) => a.hardware.includes(hw));
|
||||
return i < 0 ? null : { player: p, slot: i + 1 };
|
||||
}
|
||||
|
||||
export const isRooted = (s: GameState, r: AvatarRef): boolean => running(s, avatar(s, r).rootedUntil);
|
||||
export const isDisabledAvatar = (s: GameState, r: AvatarRef): boolean => running(s, avatar(s, r).disabledUntil);
|
||||
/** Can act: advance, attack, move, use its Ability. */
|
||||
export const canAct = (s: GameState, r: AvatarRef): boolean =>
|
||||
inPlay(s, r) && !isRooted(s, r) && !isDisabledAvatar(s, r);
|
||||
|
||||
/** Temporary modifiers that currently apply to an Avatar. */
|
||||
export function activeMods(s: GameState, av: AvatarState): Modifier[] {
|
||||
return av.mods.filter((m) =>
|
||||
m.combat !== undefined ? s.combat?.id === m.combat : running(s, m.until ?? 0));
|
||||
}
|
||||
|
||||
export function addMod(
|
||||
s: GameState,
|
||||
r: AvatarRef,
|
||||
cores: number,
|
||||
source: string,
|
||||
by: PlayerId,
|
||||
scope: { combat: number } | { until: Clock },
|
||||
): void {
|
||||
avatar(s, r).mods.push({ seq: ++s.seq, cores, source, by, ...scope });
|
||||
}
|
||||
|
||||
export const infectionOf = (av: AvatarState): number => av.infection + av.permanentInfection;
|
||||
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
// Combat
|
||||
|
||||
export const inCombat = (s: GameState, r: AvatarRef): boolean =>
|
||||
!!s.combat && !s.combat.ended && (sameRef(s.combat.attacker, r) || s.combat.defenders.some((d) => sameRef(d, r)));
|
||||
|
||||
/** The other side of the current combat from `r`'s point of view. */
|
||||
export function opponentsInCombat(s: GameState, r: AvatarRef): AvatarRef[] {
|
||||
const c = s.combat;
|
||||
if (!c || c.ended) return [];
|
||||
if (sameRef(c.attacker, r)) return [...c.defenders];
|
||||
if (c.defenders.some((d) => sameRef(d, r))) return [c.attacker];
|
||||
return [];
|
||||
}
|
||||
|
||||
export const fightersOf = (c: Combat): AvatarRef[] => [c.attacker, ...c.defenders];
|
||||
|
||||
/**
|
||||
* Avatars "at" a location for card text: the ones standing there, plus an enemy attacking it in the
|
||||
* current combat.
|
||||
*/
|
||||
export function atServer(s: GameState, loc: LocId): AvatarRef[] {
|
||||
const out = avatarsAt(s, loc);
|
||||
const c = s.combat;
|
||||
if (c && !c.ended && c.loc === loc && !out.some((r) => sameRef(r, c.attacker))) out.push(c.attacker);
|
||||
return out;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
// Map
|
||||
|
||||
/** Locations joined to `loc` by a line with no Firewall. */
|
||||
export function neighbors(s: GameState, loc: LocId): LocId[] {
|
||||
return LINES_AT.get(loc)!.filter((l) => isOpen(s, l)).map((l) => otherEnd(l, loc));
|
||||
}
|
||||
export const adjacent = (s: GameState, a: LocId, b: LocId): boolean => neighbors(s, a).includes(b);
|
||||
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
// Cards moving between zones
|
||||
|
||||
/** Clears play-time status when a card leaves play. */
|
||||
export function resetCard(s: GameState, id: number): void {
|
||||
const c = s.cards[id];
|
||||
c.disabledUntil = 0;
|
||||
c.ignoredUntil = 0;
|
||||
delete c.wormTo;
|
||||
delete c.protects;
|
||||
delete c.copies;
|
||||
delete c.spent;
|
||||
delete c.pushedUntil;
|
||||
}
|
||||
|
||||
export function toDiscard(s: GameState, id: number): void {
|
||||
resetCard(s, id);
|
||||
s.players[ownerOf(s, id)].discard.push(id);
|
||||
}
|
||||
|
||||
export function toHand(s: GameState, id: number): void {
|
||||
resetCard(s, id);
|
||||
s.players[ownerOf(s, id)].hand.push(id);
|
||||
}
|
||||
|
||||
export function removeFrom(list: number[], id: number): boolean {
|
||||
const i = list.indexOf(id);
|
||||
if (i < 0) return false;
|
||||
list.splice(i, 1);
|
||||
return true;
|
||||
}
|
||||
|
||||
/** Takes an installed Program off its location. Returns where it was. */
|
||||
export function uninstallProgram(s: GameState, id: number): LocId | null {
|
||||
for (const [loc, L] of Object.entries(s.locations)) {
|
||||
if (removeFrom(L.programs, id)) return loc;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
export function uninstallHardware(s: GameState, id: number): AvatarRef | null {
|
||||
const r = wearerOf(s, id);
|
||||
if (r) removeFrom(avatar(s, r).hardware, id);
|
||||
return r;
|
||||
}
|
||||
|
||||
export function programLocation(s: GameState, id: number): LocId | null {
|
||||
for (const [loc, L] of Object.entries(s.locations)) if (L.programs.includes(id)) return loc;
|
||||
return null;
|
||||
}
|
||||
|
||||
export function gainRam(s: GameState, p: PlayerId, n: number, key: StatKey = 'ramFromEffects'): void {
|
||||
if (n <= 0) return;
|
||||
s.players[p].ram += n;
|
||||
stat(s, key, p, n);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
// Tasks and choices
|
||||
|
||||
/** Queues tasks to run next, in the order given. */
|
||||
export function pushTasks(s: GameState, ...tasks: Task[]): void {
|
||||
s.tasks.unshift(...tasks);
|
||||
}
|
||||
|
||||
/** Asks a player to choose. The game waits until they do. */
|
||||
export function ask(s: GameState, choice: Choice): void {
|
||||
if (s.choice) throw new Error(`Two choices at once: ${s.choice.kind} and ${choice.kind}`);
|
||||
if (choice.options.length === 0) throw new Error(`Choice ${choice.kind} has no options`);
|
||||
s.choice = choice;
|
||||
}
|
||||
|
||||
/** Runs a script function after the current task, like a choice would. */
|
||||
export const later = (name: string, fn: string, data: Json = null): Task => ({ t: 'call', name, fn, data });
|
||||
|
||||
export function startCombat(
|
||||
s: GameState,
|
||||
attacker: AvatarRef,
|
||||
origin: LocId,
|
||||
loc: LocId,
|
||||
kind: Combat['kind'],
|
||||
): Combat {
|
||||
const combat: Combat = {
|
||||
id: ++s.seq,
|
||||
loc,
|
||||
attacker,
|
||||
origin,
|
||||
defenders: defendersAt(s, loc),
|
||||
kind,
|
||||
dice: {},
|
||||
rolled: false,
|
||||
ended: false,
|
||||
suppressed: [],
|
||||
race: [null, null],
|
||||
ties: 0,
|
||||
};
|
||||
s.combat = combat;
|
||||
const window = (kind: 'preRoll' | 'postRoll'): Task => ({
|
||||
t: 'window', kind, priority: attacker.player, passes: 0, base: s.stack.length, resolving: false,
|
||||
});
|
||||
pushTasks(
|
||||
s,
|
||||
{ t: 'combat', stage: 'start' },
|
||||
window('preRoll'),
|
||||
{ t: 'combat', stage: 'roll' },
|
||||
window('postRoll'),
|
||||
{ t: 'combat', stage: 'result' },
|
||||
{ t: 'combat', stage: 'end' },
|
||||
);
|
||||
return combat;
|
||||
}
|
||||
|
||||
export const pushCapture = (s: GameState, ref: AvatarRef, loc: LocId, how: CaptureStat): void =>
|
||||
pushTasks(s, { t: 'capture', ref, loc, stage: 'hooks', how });
|
||||
|
||||
export const playerName = (p: PlayerId): string => PLAYER_NAMES[p];
|
||||
Loading…
Add table
Add a link
Reference in a new issue