Files
wikideck/scripts/sim/toy-engine.ts

117 lines
5.1 KiB
TypeScript

// Toy engine: a tiny lane-less creature game used to self-test the simulation harness
// before shared/game exists. Not the WikiDeck design; do not tune it.
import { mulberry32 } from '../../shared/bots/index.ts';
import type { DeckSpec, EngineAdapter, MatchOutcome, Seat } from '../../shared/bots/types.ts';
interface Card { id: string; cost: number; atk: number; hp: number; guard?: boolean; haste?: boolean }
interface Unit { card: Card; hp: number; ready: boolean }
interface Side { hp: number; deck: Card[]; hand: Card[]; board: Unit[]; mana: number; maxMana: number }
export interface ToyState { ruleset: string; turn: number; active: Seat; sides: [Side, Side]; over: boolean; winner: Seat | null; seed: number }
export type ToyAction = { kind: 'end' } | { kind: 'play'; hand: number } | { kind: 'attack'; unit: number; target: number /* -1 = face */ };
const POOL: Card[] = [];
for (let cost = 1; cost <= 6; cost++) {
POOL.push({ id: `c${cost}-beater`, cost, atk: cost + 1, hp: cost });
POOL.push({ id: `c${cost}-wall`, cost, atk: Math.max(1, cost - 1), hp: cost + 2, guard: true });
POOL.push({ id: `c${cost}-rush`, cost, atk: cost, hp: Math.max(1, cost - 1), haste: true });
}
const ARCHETYPES: Record<string, (c: Card) => number> = {
aggro: c => (c.cost <= 3 ? 3 : 0) + (c.haste ? 1 : 0),
control: c => (c.guard ? 2 : 0) + (c.cost >= 4 ? 2 : 0),
midrange: c => (c.cost >= 2 && c.cost <= 5 ? 2 : 0),
};
function buildDeck(arch: string): Card[] {
const w = ARCHETYPES[arch];
return POOL.filter(c => w(c) > 0).flatMap(c => Array(w(c)).fill(c)).slice(0, 20);
}
const clone = (s: ToyState): ToyState => ({
...s, sides: s.sides.map(p => ({ ...p, deck: [...p.deck], hand: [...p.hand], board: p.board.map(u => ({ ...u })) })) as [Side, Side],
});
function startTurn(s: ToyState): void {
const p = s.sides[s.active];
p.maxMana = Math.min(s.ruleset === 'toy-fast' ? 10 : 8, p.maxMana + 1);
p.mana = p.maxMana;
for (const u of p.board) u.ready = true;
const c = p.deck.shift();
if (c) p.hand.push(c); else p.hp -= 2; // fatigue
check(s);
}
function check(s: ToyState): void {
const [a, b] = s.sides;
if (a.hp <= 0 || b.hp <= 0) { s.over = true; s.winner = a.hp <= 0 && b.hp <= 0 ? null : a.hp <= 0 ? 1 : 0; }
}
export const toyEngine: EngineAdapter<ToyState, ToyAction> = {
name: 'toy',
rulesets: () => ['toy', 'toy-fast'],
decks: () => Object.keys(ARCHETYPES).map(a => ({ id: a, tags: [`arch:${a}`], payload: a })),
createMatch(ruleset, decks, seed) {
const rng = mulberry32(seed);
const side = (d: DeckSpec): Side => {
const deck = buildDeck(d.payload as string);
for (let i = deck.length - 1; i > 0; i--) { const j = Math.floor(rng() * (i + 1)); [deck[i], deck[j]] = [deck[j], deck[i]]; }
return { hp: 20, deck, hand: deck.splice(0, 3), board: [], mana: 0, maxMana: 0 };
};
const s: ToyState = { ruleset, turn: 1, active: (seed & 1) as Seat, sides: [side(decks[0]), side(decks[1])], over: false, winner: null, seed };
s.sides[1 - s.active].hand.push(s.sides[1 - s.active].deck.shift()!); // second player gets an extra card
startTurn(s);
return s;
},
toAct: s => s.active,
legalActions(s, seat) {
if (s.over || seat !== s.active) return [];
const me = s.sides[seat], them = s.sides[1 - seat];
const out: ToyAction[] = [{ kind: 'end' }];
me.hand.forEach((c, i) => { if (c.cost <= me.mana && me.board.length < 6) out.push({ kind: 'play', hand: i }); });
const guards = them.board.map((u, i) => (u.card.guard ? i : -2)).filter(i => i >= 0);
me.board.forEach((u, i) => {
if (!u.ready) return;
const targets = guards.length ? guards : [-1, ...them.board.map((_, j) => j)];
for (const t of targets) out.push({ kind: 'attack', unit: i, target: t });
});
return out;
},
applyAction(s0, a) {
const s = clone(s0);
const me = s.sides[s.active], them = s.sides[1 - s.active];
if (a.kind === 'end') {
s.active = (1 - s.active) as Seat;
s.turn++;
startTurn(s);
if (s.turn > 60) { s.over = true; s.winner = null; }
} else if (a.kind === 'play') {
const [c] = me.hand.splice(a.hand, 1);
me.mana -= c.cost;
me.board.push({ card: c, hp: c.hp, ready: !!c.haste });
} else {
const u = me.board[a.unit];
u.ready = false;
if (a.target < 0) them.hp -= u.card.atk;
else {
const v = them.board[a.target];
v.hp -= u.card.atk; u.hp -= v.card.atk;
them.board = them.board.filter(x => x.hp > 0);
me.board = me.board.filter(x => x.hp > 0);
}
check(s);
}
return s;
},
isOver: s => s.over,
result: (s): MatchOutcome => ({ winner: s.winner, reason: s.over ? (s.winner === null ? 'draw' : 'hp') : undefined }),
turn: s => s.turn,
evaluate(s, seat) {
const me = s.sides[seat], them = s.sides[1 - seat];
const board = (p: Side) => p.board.reduce((t, u) => t + u.card.atk + u.hp, 0);
const v = (me.hp - them.hp) / 20 + (board(me) - board(them)) / 20 + (me.hand.length - them.hand.length) / 20;
return Math.max(-1, Math.min(1, v));
},
cardsOf: (s, a) => (a.kind === 'play' ? [s.sides[s.active].hand[a.hand].id] : []),
actionKind: a => a.kind,
};