SOURCE / PINNED RELEASE
Made of little things.
Gem Warden
- Release
- 7c457bfe5427…
- Author-recorded commit
- fb5453b5fc06…
- License
- LICENSE
- Author’s source reference
- nostr://npub1eg5pfnws6tg6rtp33tugpppqtv5u05crmf983qp438cdycwzngys0gxzcs/wss%3A%2F%2Fgit.napplet.soy%2F/n-3929629ef1d
Archive hash verified: 895b8dd430aad9fe…. The source-to-build association is the author’s claim; it has not been independently rebuilt.
// Headless balance check: a simple greedy bot plays every field.
import * as G from '../src/game.ts';
function coverage(g: G.Game, x: number, y: number, r: number): number {
let c = 0;
for (const k of g.pathTiles) {
const [px, py] = k.split(',').map(Number);
if (Math.hypot(px - x, py - y) <= r) c++;
}
return c;
}
function bestSpot(g: G.Game): [number, number] | null {
let best: [number, number] | null = null;
let bc = -1;
for (let x = 0; x < G.COLS; x++)
for (let y = 0; y < G.ROWS; y++) {
if (!G.canBuild(g, 'tower', x, y)) continue;
const c = coverage(g, x, y, 3) + (g.pathLength - 0) * 0;
if (c > bc) { bc = c; best = [x, y]; }
}
return best;
}
// A trap goes on the path tile next to the most tower coverage, away from other traps.
function bestTrapSpot(g: G.Game): [number, number] | null {
let best: [number, number] | null = null;
let bc = -1;
for (const k of g.pathTiles) {
const [x, y] = k.split(',').map(Number);
if (!G.canBuild(g, 'trap', x, y)) continue;
if (g.structures.some((s) => s.kind === 'trap' && Math.hypot(s.x - x, s.y - y) < 3)) continue;
const c = coverage(g, x, y, 3);
if (c > bc) { bc = c; best = [x, y]; }
}
return best;
}
/** traps: how many traps the bot builds alongside its towers, all with trapColor gems. */
export function play(fieldIndex: number, skill: number, traps = 0, trapColor: G.GemColor = 'green') {
const g = G.newGame(fieldIndex);
const colors = g.field.colors;
if (traps && !colors.includes(trapColor)) return null;
let t = 0;
let ci = 0;
const maxTowers = 4 + Math.round(skill * 4);
while (g.state === 'playing' && g.time < 3000) {
G.step(g, 0.05);
t += 0.05;
if (t < 0.5) continue;
t = 0;
const towers = g.structures.filter((s) => s.kind === 'tower');
const trapList = g.structures.filter((s) => s.kind === 'trap');
// fill empty structures
for (const s of [...towers, ...trapList]) {
if (s.gem) continue;
const slot = s.kind === 'tower' ? g.inventory.findIndex((x) => x) : -1;
if (slot >= 0) G.moveGem(g, { kind: 'inv', index: slot }, { kind: 'struct', id: s.id });
else {
const sl = G.createGem(g, s.kind === 'trap' ? trapColor : colors[ci++ % colors.length], 1);
if (sl >= 0) G.moveGem(g, { kind: 'inv', index: sl }, { kind: 'struct', id: s.id });
}
}
if (towers.length < maxTowers && g.mana > G.towerCost(g) + 80) {
const sp = bestSpot(g);
if (sp) G.build(g, 'tower', sp[0], sp[1]);
continue;
}
if (towers.length >= 3 && trapList.length < traps && g.mana > G.trapCost(g) + 80) {
const sp = bestTrapSpot(g);
if (sp) G.build(g, 'trap', sp[0], sp[1]);
continue;
}
if (skill > 0.5 && g.poolLevel < 1 + skill * 3 && g.mana > G.poolCost(g) + 50 && g.mana >= g.manaCap * 0.9) {
G.expandPool(g);
continue;
}
// upgrade weakest tower gem: create equal grade, combine in
const filled = [...towers, ...trapList].filter((s) => s.gem).sort((a, b) => a.gem!.mass - b.gem!.mass);
if (filled.length) {
const s = filled[0];
const gr = G.gemGrade(s.gem!);
const cost = G.gemCost(gr);
if (g.mana > cost + 40) {
const color = skill > 0.5 ? G.dominantColor(s.gem!) : colors[ci++ % colors.length];
const sl = G.createGem(g, color, gr);
if (sl >= 0) G.moveGem(g, { kind: 'inv', index: sl }, { kind: 'struct', id: s.id });
}
}
if (skill > 0.7 && g.monsters.length === 0 && g.nextWave < g.waves.length && g.nextWave > 0) G.callWaveEarly(g);
}
const grades = g.structures.map((s) => (s.gem ? (s.kind === 'trap' ? 't' : '') + G.gemGrade(s.gem) : 0));
return { field: fieldIndex, skill, traps, trapColor, state: g.state, wave: g.nextWave, of: g.waves.length, banished: g.banished, time: Math.round(g.time), grades: grades.join(','), mana: Math.round(g.mana), cap: g.manaCap };
}
// node tests/balance.ts [traps] [colour]: also build that many traps with that colour.
const traps = Number(process.argv[2] ?? 0);
const trapColor = (process.argv[3] ?? 'green') as G.GemColor;
let won = 0;
let ran = 0;
for (let f = 0; f < G.FIELDS.length; f++)
for (const skill of [0.2, 0.6, 1]) {
const r = play(f, skill, traps, trapColor);
if (!r) continue;
ran++;
if (r.state === 'won') won++;
console.log(JSON.stringify(r));
}
console.log(`won ${won} of ${ran}`);
