// Agent Arena Game Studio headless sim. Loads the SAME core code that runs in the browser (the text between /*CORE*/ and /*ENDCORE*/ // in games/studio/.html), so house-bot results match the playable game exactly. Deterministic: no Math.random anywhere. import fs from "fs"; import vm from "vm"; import path from "path"; import { fileURLToPath } from "url"; const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../.."); const OVERRIDE = {}; // slug -> candidate game file text (used by the weekly loop to test a change before it is written) export function setSource(slug, text) { if (text == null) delete OVERRIDE[slug]; else OVERRIDE[slug] = text; } export function gameSource(slug) { return OVERRIDE[slug] ?? fs.readFileSync(path.join(ROOT, "games/studio", slug + ".html"), "utf8"); } export function loadCore(slug, globalName) { const src = gameSource(slug); const a = src.indexOf("/*CORE*/"), b = src.indexOf("/*ENDCORE*/"); if (a < 0 || b < 0) throw new Error(slug + ": no /*CORE*/ block"); const ctx = {}; vm.createContext(ctx); vm.runInContext(src.slice(a + 8, b) + `;this.__G=${globalName};`, ctx); return ctx.__G; } // tiny seeded generator for bot "hands" (separate from the game's own generator) export function prng(seedStr) { let h = 2166136261; for (const ch of String(seedStr)) h = Math.imul(h ^ ch.charCodeAt(0), 16777619); let s = h | 0; return () => { s = (s + 0x6D2B79F5) | 0; let t = s; t = Math.imul(t ^ (t >>> 15), t | 1); t ^= t + Math.imul(t ^ (t >>> 7), t | 61); return ((t ^ (t >>> 14)) >>> 0) / 4294967296; }; } // ---------- Comet Shield players ---------- function cometPolicy(style, look) { return (G, g, rnd, mem) => { const PY = G.PY; let tx = g.x; let tgt = null; for (const e of g.en) if (e.y > 0 && (!tgt || (style === "boss" && e.k === "boss") || (tgt.k !== "boss" || style !== "boss") && e.y > tgt.y)) tgt = e; if (style === "hunter" || style === "boss") tx = tgt ? tgt.x : G.W / 2; if (style === "collector") { let it = null; for (const s of g.it) if (s.y < PY && (!it || s.y > it.y)) it = s; tx = it ? it.x : tgt ? tgt.x : G.W / 2; } if (style === "drifter") { if (!mem.t || --mem.t <= 0) { mem.tx = 30 + rnd() * (G.W - 60); mem.t = 40 + Math.floor(rnd() * 60); } tx = mem.tx; } if (style === "keeper") { tx = G.W / 2; if (tgt && Math.abs(tgt.x - g.x) < 90) tx = tgt.x; } for (const s of g.es) if (s.y > PY - look && s.y < PY + 5 && Math.abs(s.x + s.vx * 8 - g.x) < 24) tx = g.x + (s.x > g.x ? -70 : 70); for (const e of g.en) if (e.k !== "boss" && e.y > PY - look * 0.8 && e.y < PY + 10 && Math.abs(e.x - g.x) < e.r + 18) tx = g.x + (e.x > g.x ? -80 : 80); const nova = g.nova >= 100 && (style === "boss" ? g.en.some(e => e.k === "boss") : g.es.length + g.en.length > (style === "drifter" ? 3 : 6)); return { tx: Math.max(16, Math.min(G.W - 16, tx)), nova }; }; } // ---------- Sprout Squad players ---------- function sproutPolicy(o) { return (G, g, rnd, mem) => { const inp = {}; if (g.t % o.collectEvery === 0 && g.drops.length) inp.collectAll = true; const C = G.CARDS, can = i => g.energy >= C[i].cost && g.cd[i] <= 0, free = (r, c) => !g.grid[r][c]; const rowsWithBots = [0, 1, 2, 3, 4].filter(r => g.bots.some(b => b.row === r && b.x < G.W + 10)); const count = (k, r) => g.grid[r].filter(p => p && p.k === k).length, blooms = g.grid.flat().filter(p => p && p.k === "glowbloom").length; // 1. emergency Pop Pod if (o.pods && can(4)) for (const r of rowsWithBots) { const near = g.bots.filter(b => Math.abs(b.row - r) <= 1 && b.x < G.X0 + o.podLine * G.CS); if (near.length >= o.podMin) { const c = Math.max(0, Math.min(G.COLS - 1, Math.floor((Math.min(...near.map(b => b.x)) - G.X0) / G.CS))); if (free(r, c)) { inp.plant = { card: 4, row: r, col: c }; return inp; } } } // 2. economy if (blooms < o.blooms && can(0)) for (let c = 0; c < 2; c++) for (const r of [2, 1, 3, 0, 4]) if (free(r, c)) { inp.plant = { card: 0, row: r, col: c }; return inp; } // 3. shooters where Zombots are, fewest first const order = (rowsWithBots.length ? rowsWithBots : [2, 1, 3, 0, 4]).slice().sort((a, b) => (count("pip", a) + count("bubble", a)) - (count("pip", b) + count("bubble", b))); for (const r of order) { const have = count("pip", r) + count("bubble", r); if (have >= o.perRow) continue; const card = o.bubbleEvery && have % o.bubbleEvery === o.bubbleEvery - 1 && can(3) ? 3 : 1; if (!can(card)) break; for (let c = o.firstCol; c < 6; c++) if (free(r, c)) { inp.plant = { card, row: r, col: c }; return inp; } } // 4. walls if (o.wallCol >= 0 && can(2)) for (const r of rowsWithBots) if (free(r, o.wallCol) && count("wallvine", r) === 0) { inp.plant = { card: 2, row: r, col: o.wallCol }; return inp; } // 5. random noise player if (o.random && rnd() < 0.02) { const card = Math.floor(rnd() * 4), r = Math.floor(rnd() * 5), c = Math.floor(rnd() * 7); if (can(card) && free(r, c)) inp.plant = { card, row: r, col: c }; } return inp; }; } // Player roster per game (labelled house bots: the five studio designers play every game in character). export const GAMES = { "comet-shield": { global: "CometShield", create: (G, seed) => G.create(seed), maxSteps: 60 * 600, players: { "nova-nell": cometPolicy("boss", 150), "volley-val": cometPolicy("hunter", 110), "hurdle-hank": cometPolicy("collector", 100), "throttle-theo": cometPolicy("drifter", 60), "greenhouse-gwen": cometPolicy("keeper", 160) }, style: { "nova-nell": "Goes straight for the Mothercomet and saves Nova for it", "volley-val": "Lines up under the lowest enemy and dodges late", "hurdle-hank": "Chases shards and power-ups first", "throttle-theo": "Drifts to seeded random spots, dodges only close shots", "greenhouse-gwen": "Guards the middle of the sky, looks far ahead, dodges early" } }, "sprout-squad": { global: "SproutSquad", create: (G, seed) => G.create(seed, "normal"), maxSteps: 60 * 900, players: { "greenhouse-gwen": sproutPolicy({ collectEvery: 30, blooms: 8, perRow: 3, firstCol: 2, wallCol: 6, pods: true, podLine: 3, podMin: 2, bubbleEvery: 3 }), "nova-nell": sproutPolicy({ collectEvery: 20, blooms: 3, perRow: 4, firstCol: 1, wallCol: -1, pods: true, podLine: 2, podMin: 1, bubbleEvery: 0 }), "volley-val": sproutPolicy({ collectEvery: 60, blooms: 6, perRow: 2, firstCol: 2, wallCol: 5, pods: false, bubbleEvery: 2 }), "hurdle-hank": sproutPolicy({ collectEvery: 45, blooms: 5, perRow: 2, firstCol: 1, wallCol: 4, pods: true, podLine: 2, podMin: 3, bubbleEvery: 0 }), "throttle-theo": sproutPolicy({ collectEvery: 90, blooms: 4, perRow: 1, firstCol: 3, wallCol: -1, pods: false, random: true, bubbleEvery: 0 }) }, style: { "greenhouse-gwen": "Eight Glowblooms, three shooters a row, walls and Pop Pods", "nova-nell": "Few Glowblooms, stacks shooters fast", "volley-val": "Bubble-heavy, collects slowly", "hurdle-hank": "Wall Vines up front, Pop Pods for crowds", "throttle-theo": "One shooter per row plus seeded random planting" } }, }; // playState: one house run to the end; returns the game core, the final game state and the number of steps (used by the daily goal tests) export function playState(slug, player, seed, diff) { const def = GAMES[slug], G = loadCore(slug, def.global), g = diff ? G.create(seed, diff) : def.create(G, seed), pol = def.players[player], rnd = prng(`${slug}|${player}|${seed}`), mem = {}; let n = 0; while (!g.over && n < def.maxSteps) { G.step(g, pol(G, g, rnd, mem)); g.events.length = 0; n++; } return { G, g, n }; } export function playOne(slug, player, seed, diff) { const { g, n } = playState(slug, player, seed, diff); return { score: g.score, steps: n, over: g.over, won: !!g.won, wave: g.wave }; }