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>
This commit is contained in:
Forrest 2026-09-28 14:07:45 -04:00
parent 6382de19c9
commit a3eb6850ee
21 changed files with 1089 additions and 96 deletions

View file

@ -6,6 +6,7 @@
*/
import type { CardPool } from './cards.ts';
import { validateDeck, type DeckList } from './deck.ts';
import type { CardDef } from './cards.ts';
import {
captureHooks,
captureMain,
@ -14,9 +15,11 @@ import {
drawNow,
drawTask,
resolveDeclaration,
retireAvatar,
} from './flow.ts';
import { GRID, LINES, LINES_AT, LINE_BY_ID, LOCATIONS, START_LOC, isStart, otherEnd, type LocId } from './map.ts';
import {
avatarRoom,
costOf,
coresOf,
desynced,
@ -44,10 +47,14 @@ import {
endOf,
firewallCost,
gridCount,
commanderOf,
firstCommand,
freshCopy,
isCopy,
isOpen,
markUsed,
nameOf,
originalOf,
other,
fired,
played,
@ -55,6 +62,7 @@ import {
pushTasks,
refsInPlay,
removeFrom,
roundOf,
running,
say,
startOf,
@ -114,6 +122,32 @@ function newStats(): GameStats {
return Object.fromEntries(STAT_KEYS.map((k) => [k, [0, 0]])) as GameStats;
}
/** Recruit rule: an Avatar's printed RAM is its price, which is also what it salvages for. */
function withPrice(def: CardDef, rules: RulesConfig): CardDef {
if (!rules.recruit.on || def.kind !== 'avatar') return def;
return { ...def, ram: rules.recruit.price + rules.recruit.pricePerCore * def.cores };
}
/** Recruit rule: slots `p` can recruit into. The first empty one while there's room, or any Avatar to retire. */
export function recruitSlots(s: GameState, p: PlayerId): number[] {
const avs = s.players[p].avatars.slice(0, 4);
const out: number[] = [];
const empty = avs.findIndex((a) => a.card < 0);
if (empty >= 0 && avatarRoom(s, p)) out.push(empty + 1);
avs.forEach((a, i) => {
const declared = s.declarations.some((d) => d.ref.player === p && originalOf(d.ref).slot === i + 1);
if (a.card >= 0 && !declared) out.push(i + 1);
});
return out;
}
/** Buy-Avatars rule: the next Avatar `p` can buy (its slot) and its price, or null. */
export function avatarForSale(s: GameState, p: PlayerId): { slot: number; price: number } | null {
if (!s.rules.buyAvatars.on) return null;
const i = s.players[p].avatars.slice(0, 4).findIndex((a) => !a.faceUp);
return i < 1 ? null : { slot: i + 1, price: s.rules.buyAvatars.prices[i - 1] };
}
function newAvatar(card: number, faceUp: boolean, copyOf: number | null): AvatarState {
return {
card,
@ -131,6 +165,8 @@ function newAvatar(card: number, faceUp: boolean, copyOf: number | null): Avatar
rootedUntil: 0,
breachedTurn: 0,
copyOf,
createdTurn: 0,
unlockedTurn: 0,
};
}
@ -171,13 +207,15 @@ export function createGame({ pool, decks, seed, rules = DEFAULT_RULES, log = fal
log: log ? [] : null,
};
const add = (name: string, owner: PlayerId): number => {
s.defs[name] = pool.byName.get(name)!;
s.defs[name] = withPrice(pool.byName.get(name)!, rules);
s.cards.push({ id: s.cards.length, name, owner, disabledUntil: 0, ignoredUntil: 0, installedTurn: 0 });
return s.cards.length - 1;
};
for (const p of [0, 1] as const) {
const deck = decks[p];
// Recruit rule: slots after the starting Avatar begin empty (-1).
const avatars = shuffle(rng, deck.avatars.map((n) => add(n, p)));
const slots = [0, 1, 2, 3].map((i) => avatars[i] ?? -1);
s.players.push({
ram: 0,
hand: [],
@ -185,8 +223,8 @@ export function createGame({ pool, decks, seed, rules = DEFAULT_RULES, log = fal
discard: [],
serverPile: shuffle(rng, deck.servers.map((n) => add(n, p))),
avatars: [
...avatars.map((card, i) => newAvatar(card, i < rules.startingAvatars, null)),
...avatars.map((card, i) => newAvatar(card, false, i + 1)),
...slots.map((card, i) => newAvatar(card, card >= 0 && i < rules.startingAvatars, null)),
...slots.map((card, i) => newAvatar(card, false, i + 1)),
],
unlockedLevel: 1,
});
@ -207,7 +245,7 @@ export function createGame({ pool, decks, seed, rules = DEFAULT_RULES, log = fal
const p2 = other(s.firstPlayer);
const second = s.players[p2];
second.ram += rules.secondPlayer.ram;
second.avatars.slice(0, rules.startingAvatars + rules.secondPlayer.avatars).forEach((av) => (av.faceUp = true));
second.avatars.slice(0, rules.startingAvatars + rules.secondPlayer.avatars).forEach((av) => (av.faceUp = av.card >= 0));
for (let i = 0; i < rules.secondPlayer.cards; i++) drawNow(s, p2);
if (rules.secondPlayer.locations > 0) {
// The location next to the starting Server, with a Server from the pile: no RAM, no unlock.
@ -219,13 +257,14 @@ export function createGame({ pool, decks, seed, rules = DEFAULT_RULES, log = fal
}
s.stats.cardsDrawn = [0, 0];
say(s, `${playerName(s.firstPlayer)} goes first.`);
beginTurn(s, true);
beginTurn(s, !rules.firstTurn.draw);
run(s);
return s;
}
function beginTurn(s: GameState, skipDraw: boolean): void {
if (s.turn >= s.rules.maxTurns) {
const cmd = s.rules.commands.on;
if (s.turn >= s.rules.maxTurns * (cmd ? s.rules.commands.perPlayer : 1)) {
s.over = true;
s.endReason = 'turnLimit';
say(s, `Turn limit reached: draw (South ${gridCount(s, 0)}, North ${gridCount(s, 1)} locations).`);
@ -233,21 +272,34 @@ function beginTurn(s: GameState, skipDraw: boolean): void {
}
s.turn++;
s.clock = startOf(s.turn);
s.used = {};
// Command rule: a player's turn is their commands in a round, so its refreshes come at the first.
const fresh = !cmd || firstCommand(s, s.turn);
if (!cmd || roundOf(s, s.turn) !== roundOf(s, s.turn - 1)) s.used = {};
tidy(s);
const me = s.players[s.active];
for (const av of me.avatars) {
av.placed = false;
av.advanced = false;
av.attacks = 0;
if (fresh) {
for (const av of me.avatars) {
av.placed = false;
av.advanced = false;
av.attacks = 0;
}
}
s.declarations = [];
s.step = 'deploy';
say(s, `\n— Turn ${s.turn}: ${playerName(s.active)} (${me.ram} RAM, ${me.hand.length} cards, ${gridCount(s, s.active)} locations) —`);
if (!skipDraw) pushTasks(s, { t: 'draw', player: s.active });
emit(s, 'onTurnStart', null);
const title = cmd ? `Round ${roundOf(s, s.turn)}, command ${s.turn}` : `Turn ${s.turn}`;
say(s, `\n— ${title}: ${playerName(s.active)} (${me.ram} RAM, ${me.hand.length} cards, ${gridCount(s, s.active)} locations) —`);
const draws = s.rules.drawPerTurn + (s.turn === 2 ? s.rules.secondPlayer.firstDraw : 0);
if (fresh && !skipDraw) pushTasks(s, ...Array.from({ length: draws }, (): Task => ({ t: 'draw', player: s.active })));
if (fresh) emit(s, 'onTurnStart', null);
}
/** Command rule: this game turn's command is spent (an advance or attack is declared). */
const commandSpent = (s: GameState): boolean => s.rules.commands.on && s.declarations.length > 0;
/** Command rule: an Avatar unlocked this round waits in its slot until the next round. */
const waitsForRound = (s: GameState, av: AvatarState): boolean =>
s.rules.commands.on && av.at === null && av.unlockedTurn > 0 && roundOf(s, av.unlockedTurn) === roundOf(s, s.turn);
function endTurn(s: GameState): void {
s.clock = endOf(s.turn);
// Push Update: the extra Hardware leaves at the end of its controller's turn.
@ -264,7 +316,7 @@ function endTurn(s: GameState): void {
}
}
tidy(s);
s.active = other(s.active);
s.active = s.rules.commands.on ? commanderOf(s, s.turn + 1) : other(s.active);
beginTurn(s, false);
}
@ -321,7 +373,7 @@ export function maxAttacks(s: GameState, ref: AvatarRef): number {
export function attackTargets(s: GameState, ref: AvatarRef): LocId[] {
const av = avatar(s, ref);
if (!av.faceUp || av.at === null || av.advanced || av.attacks >= maxAttacks(s, ref) || !canAct(s, ref)) return [];
if (!av.faceUp || av.at === null || av.advanced || av.attacks >= maxAttacks(s, ref) || !canAct(s, ref) || freshCopy(s, ref)) return [];
const opp = other(ref.player);
return LINES_AT.get(av.at)!
.filter((line) => isOpen(s, line))
@ -507,14 +559,15 @@ export function legalActions(s: GameState): Action[] {
const ref = { player: p, slot: i + 1 };
if (!av.faceUp || av.advanced || av.attacks > 0) return;
if (av.at !== null && !canAct(s, ref)) return;
if (placementOpen && !av.placed) {
if (placementOpen && !av.placed && !waitsForRound(s, av)) {
for (const to of LOCATIONS) if (to !== av.at && hasRoom(s, p, to)) out.push({ type: 'place', slot: i + 1, to });
}
if (av.at !== null) for (const to of targets) out.push({ type: 'advance', slot: i + 1, to });
if (av.at !== null && !freshCopy(s, ref) && !commandSpent(s)) for (const to of targets) out.push({ type: 'advance', slot: i + 1, to });
});
pushClears(s, p, out);
} else if (s.step === 'attack') {
me.avatars.forEach((_, i) => {
if (commandSpent(s)) return;
for (const to of attackTargets(s, { player: p, slot: i + 1 })) out.push({ type: 'attack', slot: i + 1, to });
});
pushClears(s, p, out);
@ -531,6 +584,8 @@ export function legalActions(s: GameState): Action[] {
out.push({ type: 'installHardware', card, slot: ref.slot });
}
}
} else if (def.kind === 'avatar' && s.rules.recruit.on) {
if (def.ram <= me.ram) for (const slot of recruitSlots(s, p)) out.push({ type: 'recruit', card, slot });
} else if (def.kind === 'program') {
for (const at of LOCATIONS) {
if (controls(s, p, at) && s.locations[at].programs.length < programSlots(s, at) && programCost(s, p, card, at, all) <= me.ram) {
@ -539,6 +594,8 @@ export function legalActions(s: GameState): Action[] {
}
}
}
const sale = avatarForSale(s, p);
if (sale && sale.price <= me.ram) out.push({ type: 'buyAvatar', slot: sale.slot });
for (const line of LINES) {
if (!line.gateway || !(controls(s, p, line.a) || controls(s, p, line.b))) continue;
for (let level = (s.firewalls[line.id] ?? 0) + 1; level <= s.rules.firewallCost.length; level++) {
@ -560,6 +617,10 @@ export function actionCost(s: GameState, a: Action): number {
return firewallCost(s, s.firewalls[a.line] ?? 0);
case 'installHardware':
return hardwareCost(s, a.card, { player: p, slot: a.slot });
case 'recruit':
return defOf(s, a.card).ram;
case 'buyAvatar':
return avatarForSale(s, p)?.price ?? 0;
case 'installProgram':
return programCost(s, p, a.card, a.at);
case 'play':
@ -598,6 +659,10 @@ export function actionKey(a: Action): string {
return `salvage:${a.card}`;
case 'installHardware':
return `hw:${a.card}:${a.slot}`;
case 'recruit':
return `recruit:${a.card}:${a.slot}`;
case 'buyAvatar':
return `buy:${a.slot}`;
case 'installProgram':
return `prog:${a.card}:${a.at}`;
case 'play':
@ -653,6 +718,30 @@ export function applyAction(s: GameState, a: Action, legal: Action[] = legalActi
fn(s, ch.resume.data, option.value);
break;
}
case 'buyAvatar': {
const { price } = avatarForSale(s, p)!;
const av = me.avatars[a.slot - 1];
me.ram -= price;
stat(s, 'ramOnAvatars', p, price);
stat(s, 'avatarsBought', p);
av.faceUp = true;
say(s, `${who} buys Avatar ${a.slot}: ${nameOf(s, av.card)} (-${price} RAM, now ${me.ram}).`);
break;
}
case 'recruit': {
const cost = defOf(s, a.card).ram;
const av = me.avatars[a.slot - 1];
if (av.card >= 0) retireAvatar(s, ref(a.slot));
removeFrom(me.hand, a.card);
me.ram -= cost;
stat(s, 'ramOnCards', p, cost);
stat(s, 'avatarsRecruited', p);
played(s, nameOf(s, a.card), p);
Object.assign(av, newAvatar(a.card, true, null));
me.avatars[a.slot + 3].card = a.card;
say(s, `${who} recruits ${nameOf(s, a.card)} as Avatar ${a.slot} (-${cost} RAM, now ${me.ram}).`);
break;
}
case 'salvage': {
const gain = defOf(s, a.card).ram;
removeFrom(me.hand, a.card);