Back to Gem Warden
SOURCE / PINNED RELEASE

Made of little things.

Gem Warden

Release
d7ee3a74bdb5…
Author-recorded commit
1bfb3fd0904c…
License
LICENSE
Author’s source reference
nostr://npub1eg5pfnws6tg6rtp33tugpppqtv5u05crmf983qp438cdycwzngys0gxzcs/wss%3A%2F%2Fgit.napplet.soy%2F/n-3929629ef1d

Archive hash verified: 73f8647f6ce3701b…. The source-to-build association is the author’s claim; it has not been independently rebuilt.

tests/rush.ts
// The pause-and-rush exploit: build a few traps while paused, call every wave
// at once and spend each early-call bonus on upgrades, then let the whole
// crowd walk in and keep upgrading as kill mana floods in.
// It must lose on every field. node tests/rush.ts
import * as G from '../src/game.ts';
import { towerSpot } from './strategies.ts';

export interface Rush {
  name: string;
  traps: G.GemColor[];
  towers: G.GemColor[];
}

export const RUSHES: Rush[] = [
  { name: 'tear/slow/splash traps', traps: ['purple', 'blue', 'red'], towers: [] },
  { name: '+1 ruby tower', traps: ['purple', 'blue', 'red'], towers: ['red'] },
  { name: '+2 ruby towers', traps: ['purple', 'blue', 'red'], towers: ['red', 'red'] },
  { name: '5 traps +2 towers', traps: ['purple', 'blue', 'red', 'blue', 'red'], towers: ['red', 'yellow'] },
];

/** Upgrade the weakest structure by one grade while mana allows; grow the pool before it caps. */
function spendAll(g: G.Game, color: Map<number, G.GemColor>): void {
  for (let guard = 0; guard < 200; guard++) {
    if (g.mana >= g.manaCap * 0.85 && g.mana >= G.poolCost(g)) {
      G.expandPool(g);
      continue;
    }
    const target = g.structures.filter((s) => s.gem).sort((a, b) => a.gem!.mass - b.gem!.mass)[0];
    if (!target) return;
    const gr = G.gemGrade(target.gem!);
    if (g.mana < G.gemCost(gr)) return;
    const sl = G.createGem(g, color.get(target.id)!, gr);
    if (sl < 0) return;
    G.moveGem(g, { kind: 'inv', index: sl }, { kind: 'struct', id: target.id });
  }
}

export function rush(f: number, r: Rush) {
  const g = G.newGame(f);
  const ok = (c: G.GemColor) => g.field.colors.includes(c);
  // Fields without a colour use ruby in its place.
  const pick = (c: G.GemColor): G.GemColor => (ok(c) ? c : 'red');
  const color = new Map<number, G.GemColor>();
  const place = (kind: G.StructKind, c: G.GemColor, spot: [number, number] | null) => {
    const s = spot && G.build(g, kind, spot[0], spot[1]);
    if (!s) return;
    color.set(s.id, pick(c));
    const sl = G.createGem(g, pick(c), 1);
    if (sl >= 0) G.moveGem(g, { kind: 'inv', index: sl }, { kind: 'struct', id: s.id });
  };
  for (const c of r.towers) place('tower', c, towerSpot(g));
  // Traps sit next to each other where the towers (if any) cover the path, else mid-path.
  const towers = g.structures.filter((s) => s.kind === 'tower');
  const tiles: [number, number][] = [];
  for (let d = 0; d <= g.pathLength; d++) {
    const [px, py] = G.pathPos(g, d);
    const t: [number, number] = [Math.floor(px), Math.floor(py)];
    if (!tiles.some(([x, y]) => x === t[0] && y === t[1])) tiles.push(t);
  }
  const covered = tiles.filter(([x, y]) => towers.some((s) => Math.hypot(s.x - x, s.y - y) <= 3.2));
  const start = towers.length && covered.length ? tiles.indexOf(covered[0]) : Math.floor(tiles.length * 0.4);
  let i = start;
  for (const c of r.traps) {
    while (i < tiles.length && !G.canBuild(g, 'trap', ...tiles[i])) i++;
    if (i < tiles.length) place('trap', c, tiles[i++]);
  }
  // Paused: call every wave and upgrade with each bonus.
  spendAll(g, color);
  while (g.nextWave < g.waves.length) {
    G.callWaveEarly(g);
    spendAll(g, color);
  }
  // Unpaused, but upgrading between frames, which pausing makes free.
  while (g.state === 'playing' && g.time < 3000) {
    G.step(g, 0.05);
    g.events.length = 0;
    spendAll(g, color);
  }
  const grades = g.structures.map((s) => (s.kind === 'trap' ? 't' : '') + (s.gem ? G.gemGrade(s.gem) : 0)).join(',');
  return { state: g.state, banished: g.banished, time: Math.round(g.time), grades, alive: g.monsters.length };
}

if (import.meta.url === `file://${process.argv[1]}`) {
  const verbose = process.argv.includes('-v');
  for (const r of RUSHES) {
    const rows: string[] = [];
    for (let f = 0; f < G.FIELDS.length; f++) {
      const res = rush(f, r);
      if (verbose) console.log(f, r.name, JSON.stringify(res));
      rows.push(res.state === 'won' ? `W${res.banished ? '/b' + res.banished : ''}` : `L@${res.time}s`);
    }
    console.log(r.name.padEnd(24), rows.map((x) => x.padEnd(9)).join(''));
  }
}