'use strict'; /* * Versus mode (attack vs defend): economy, spawn / stage / launch, tech, abilities, defender tools, role checks, round end, private state, * the shared snapshot block, plus a regression check that solo / co-op are untouched. Every expected number is derived from TD.VS (the one * table the balance pass edits), never typed in. */ const test = require('node:test'); const assert = require('node:assert/strict'); const TD = require('../public/js/sim.js'); const { Sim, VS, ENEMIES, TOWERS, STEP } = TD; const A = (c) => Object.assign({ role: 'attacker' }, c); // an attacker command const D = (c) => Object.assign({ role: 'defender' }, c); // a defender command const RICH = 1e6; function vsim(o) { return new Sim(Object.assign({ map: 'open', mode: 'versus' }, o)); } function run(s, secs) { const n = Math.round(secs / STEP); for (let i = 0; i < n; i++) s.tick(STEP); } // what the base income alone pays between two instants of the round const baseIncome = (t0, t1) => VS.ATT_INCOME * (t1 - t0) + VS.ATT_INCOME_GROWTH * (t1 * t1 - t0 * t0) / 2; const near = (a, b, eps, msg) => assert.ok(Math.abs(a - b) <= eps, (msg || 'value') + ': ' + a + ' vs ' + b); // put a unit of `kind` on the map right now, standing still (stunned for ever): the tests that measure one rule need it not to wander off function parked(s, kind, x, y) { const m = s.vsUnit(kind, 0); s.monsters.push(m); if (x !== undefined) { m.x = x + 0.5; m.y = y + 0.5; m.cx = x; m.cy = y; m.rem = s.board.field[y * s.board.cols + x]; } m.stunT = 1e9; return m; } // a Packet Filter next to the open map's gateway (gate at 0,7): it covers (0..4, 5..8) function filterAt(s, x = 2, y = 5, type = 'filter') { const r = s.cmd(D({ c: 'place', type, x, y })); assert.ok(r.ok, 'place ' + type + ': ' + JSON.stringify(r)); return s.towerById(r.id); } const shots = (s) => s.drainEvents().filter((e) => e.e === 'shot').length; // ---------------------------------------------------------------- the mode itself test('a versus sim: defender credits and integrity from the table, no difficulty, no waves, no clear bonus, no win', () => { const s = vsim({ difficulty: 'hard' }); assert.equal(s.gold, VS.DEF_GOLD); assert.equal(s.lives, TD.START_LIVES); assert.equal(s.maxLives, TD.START_LIVES); assert.equal(s.difficulty, 'normal', 'versus ignores the difficulty option'); assert.equal(s.state, 'playing'); assert.equal(s.vs.limit, VS.DEFAULT_LIMIT); assert.equal(s.winWave, Infinity); assert.equal(s.privateState('attacker').credits, VS.ATT_CREDITS); const o = vsim({ gold: 77, lives: 9, credits: 33, limit: 480 }); assert.deepEqual([o.gold, o.lives, o.maxLives, o.vs.limit, o.privateState('attacker').credits], [77, 9, 9, 480, 33], 'explicit options win, like gold / lives in co-op'); assert.equal(vsim({ limit: -5 }).vs.limit, VS.DEFAULT_LIMIT, 'a nonsense limit falls back'); assert.ok(VS.LIMITS.includes(VS.DEFAULT_LIMIT)); // no automatic waves, startWave and its siblings refused, no clear bonus, never 'won' for (const c of [{ c: 'wave' }, { c: 'auto', v: true }, { c: 'continue' }]) { for (const role of ['attacker', 'defender', undefined]) assert.deepEqual(s.cmd(Object.assign({ role }, c)), { ok: false, reason: 'versus' }, JSON.stringify(c)); } assert.deepEqual(s.startWave(), { ok: false, reason: 'versus' }, 'and the method itself refuses too'); assert.deepEqual([s.setAuto(true), s.continueEndless()], [{ ok: false, reason: 'versus' }, { ok: false, reason: 'versus' }]); assert.equal(s.autoWaves, false); assert.equal(s.wave, 0); }); test('no waves come by themselves and nothing pays a clear bonus: only the trickle moves the defender credits', () => { const s = vsim(); const g0 = s.gold; run(s, 100); assert.equal(s.wave, 0); assert.equal(s.monsters.length, 0); assert.equal(s.state, 'playing'); near(s.gold - g0, VS.DEF_TRICKLE * 100, 1, 'the trickle only (whole credits, rounding down)'); assert.equal(Number.isInteger(s.gold), true); assert.equal(s.snapshot().w, 0); }); test('unknown commands are bad_cmd; waves are refused as versus; the same command names do nothing special in co-op', () => { const s = vsim(); assert.equal(s.cmd(A({ c: 'nope' })).reason, 'bad_cmd'); assert.equal(s.cmd(null).reason, 'bad_cmd'); const coop = new Sim({ map: 'open' }); assert.equal(coop.vs, null); for (const c of [{ c: 'spawn', kind: 'ping' }, { c: 'stage', kind: 'ping' }, { c: 'launch' }, { c: 'tech', id: 'armor' }, { c: 'ability', id: 'surge' }, { c: 'tool', id: 'repair' }]) { for (const role of ['attacker', 'defender', undefined]) assert.equal(coop.cmd(Object.assign({ role }, c)).reason, 'bad_cmd', 'co-op does not know ' + c.c); } assert.equal(coop.cmd({ c: 'place', type: 'filter', x: 3, y: 3, role: 'attacker' }).ok, true, 'a role on a co-op command is ignored'); assert.equal(coop.cmd({ c: 'wave', role: 'defender' }).ok, true); }); test('the VS table is exported and complete; every enemy kind has a price (the shop) and the decoy is not one of them', () => { assert.equal(typeof VS, 'object'); for (const k of ['LIMITS', 'DEFAULT_LIMIT', 'ROUNDS', 'ATT_CREDITS', 'ATT_INCOME', 'ATT_INCOME_GROWTH', 'LIFE_RATE', 'LIFE_CAP', 'MAX_ALIVE', 'MAX_STAGED', 'LAUNCH_SPAN', 'DEF_GOLD', 'DEF_TRICKLE', 'KILL_REWARD', 'TECH_COST', 'TECH_ORDER', 'TECH', 'ABILITY_ORDER', 'ABILITIES', 'TOOL_ORDER', 'TOOLS', 'PRESETS']) assert.ok(k in VS, 'VS.' + k); assert.deepEqual(VS.TECH_ORDER.slice().sort(), Object.keys(VS.TECH).sort()); assert.deepEqual(VS.ABILITY_ORDER.slice().sort(), Object.keys(VS.ABILITIES).sort()); assert.deepEqual(VS.TOOL_ORDER.slice().sort(), Object.keys(VS.TOOLS).sort()); for (const id of VS.ABILITY_ORDER) assert.ok(VS.ABILITIES[id].cost > 0 && VS.ABILITIES[id].cd > 0 && VS.ABILITIES[id].dur > 0, id); for (const id of VS.TOOL_ORDER) assert.ok(VS.TOOLS[id].cost > 0 && VS.TOOLS[id].cd > 0, id); for (const id of VS.TECH_ORDER) { const T = VS.TECH[id]; assert.ok(T.max >= 1, id); if (T.secs) assert.equal(T.secs.length, T.max); if (T.res) assert.equal(T.res.length, T.max); } for (const k of TD.ENEMY_ORDER) assert.ok(Number.isInteger(ENEMIES[k].buyCost) && ENEMIES[k].buyCost > 0, k + ' has a price'); for (const [name, list] of Object.entries(VS.PRESETS)) for (const [kind, n] of list) assert.ok(ENEMIES[kind] && n >= 1, 'preset ' + name); assert.equal(TD.ENEMY_ORDER.length, Object.keys(ENEMIES).length, 'the decoy is not an enemy kind: the table the shop and the wire index is exactly what it was'); assert.ok(VS.LIMITS.every((n) => n > 0)); }); // ---------------------------------------------------------------- roles test('role refusal both ways: a defender cannot attack, an attacker cannot build or use tools, no role is refused, and nothing is charged', () => { const s = vsim({ credits: RICH }); const t = filterAt(s); const gold = s.gold, credits = s.privateState('attacker').credits, rev = s.rev; const attackerCmds = [{ c: 'spawn', kind: 'ping' }, { c: 'stage', kind: 'ping', n: 2 }, { c: 'launch' }, { c: 'tech', id: 'armor' }, { c: 'ability', id: 'surge' }, { c: 'ability', id: 'emp', tid: t.id }]; const defenderCmds = [{ c: 'place', type: 'filter', x: 6, y: 6 }, { c: 'sell', id: t.id }, { c: 'upgrade', id: t.id }, { c: 'target', id: t.id, mode: 'last' }, { c: 'tool', id: 'repair' }, { c: 'tool', id: 'quarantine' }]; for (const c of attackerCmds) { assert.deepEqual(s.cmd(D(c)), { ok: false, reason: 'role' }, 'defender sending ' + c.c); assert.deepEqual(s.cmd(c), { ok: false, reason: 'role' }, 'nobody sending ' + c.c); assert.deepEqual(s.cmd({ ...c, role: 'spectator' }), { ok: false, reason: 'role' }); } for (const c of defenderCmds) { assert.deepEqual(s.cmd(A(c)), { ok: false, reason: 'role' }, 'attacker sending ' + c.c); assert.deepEqual(s.cmd(c), { ok: false, reason: 'role' }, 'nobody sending ' + c.c); } assert.equal(s.gold, gold); assert.equal(s.privateState('attacker').credits, credits); assert.equal(s.rev, rev); assert.equal(s.monsters.length, 0); assert.equal(s.towers.length, 1); // the right role works assert.equal(s.cmd(A({ c: 'spawn', kind: 'ping' })).ok, true); assert.equal(s.cmd(D({ c: 'target', id: t.id, mode: 'last' })).ok, true); assert.equal(t.mode, 'last'); }); test('the defender builds as in co-op (walls never seal the route, sell, upgrade) with credits from `gold`', () => { const s = vsim(); const g0 = s.gold; const r = s.cmd(D({ c: 'place', type: 'filter', x: 4, y: 4, by: 1 })); assert.ok(r.ok); assert.equal(s.gold, g0 - TD.priceFor('filter', 0)); assert.equal(s.towerById(r.id).owner, 1); assert.equal(s.cmd(D({ c: 'upgrade', id: r.id, lv: 1 })).level, 2); assert.equal(s.cmd(D({ c: 'upgrade', id: r.id, lv: 1 })).reason, 'stale'); assert.equal(s.cmd(D({ c: 'place', type: 'filter', x: 4, y: 4 })).reason, 'occupied'); const poor = vsim({ gold: 1 }); assert.equal(poor.cmd(D({ c: 'place', type: 'filter', x: 4, y: 4 })).reason, 'gold'); assert.equal(s.cmd(D({ c: 'sell', id: r.id })).ok, true); // a seal is refused: a full column of walls across the open field const w = vsim({ gold: 1000 }); let blocked = false; for (let y = 0; y < w.board.rows && !blocked; y++) { const q = w.cmd(D({ c: 'place', type: 'wall', x: 12, y })); if (!q.ok && q.reason === 'blocks') blocked = true; } assert.ok(blocked, 'the last wall of a sealing line is refused'); }); // ---------------------------------------------------------------- attacker economy test('attacker income: start credits, ATT_INCOME per second and ATT_INCOME_GROWTH more every second that has passed', () => { const s = vsim(); assert.equal(s.privateState('attacker').credits, VS.ATT_CREDITS); run(s, 10); near(s.privateState('attacker').credits, VS.ATT_CREDITS + baseIncome(0, 10), 1e-6, 'credits after 10 s'); run(s, 50); near(s.privateState('attacker').credits, VS.ATT_CREDITS + baseIncome(0, 60), 1e-6, 'credits after 60 s'); near(s.privateState('attacker').income, VS.ATT_INCOME + VS.ATT_INCOME_GROWTH * 60, 0.01, 'the reported income rate rises'); }); test('spawn: pays buyCost x n now, the unit is on the map at once with BASE stats (no wave scaling, no difficulty), a kill pays round(buyCost x KILL_REWARD)', () => { const s = vsim({ credits: RICH, difficulty: 'hard' }); for (const kind of TD.ENEMY_ORDER) { const c0 = s.privateState('attacker').credits, n0 = s.monsters.length; const r = s.cmd(A({ c: 'spawn', kind })); assert.deepEqual([r.ok, r.n], [true, 1], kind); near(c0 - s.privateState('attacker').credits, ENEMIES[kind].buyCost, 1e-9, kind + ' price'); assert.equal(s.monsters.length, n0 + 1, kind + ' is on the map'); const m = s.monsters[s.monsters.length - 1], d = ENEMIES[kind]; assert.equal(m.kind, kind); assert.equal(m.hp, d.hp, kind + ' hp: base, not scaled'); assert.equal(m.maxhp, d.hp); assert.equal(m.armor, d.armor); assert.equal(m.speed, d.speed); assert.equal(m.maxShield, d.shield || 0); assert.equal(m.reward, Math.max(1, Math.round(d.buyCost * VS.KILL_REWARD)), kind + ' reward'); assert.equal(m.leak, d.leak); assert.ok(m.buyCost > 0); } // a kill pays the defender const k = vsim({ credits: RICH }); k.cmd(A({ c: 'spawn', kind: 'trojan' })); const m = k.monsters[0], g0 = k.gold; k.damage(m, 1e9, null); assert.equal(k.gold - g0, Math.round(ENEMIES.trojan.buyCost * VS.KILL_REWARD)); assert.equal(k.vs.stats.kills, 1); }); test('spawn refusals change nothing: not enough credits, unknown kind, bad count, bad gate', () => { const s = vsim({ credits: ENEMIES.trojan.buyCost - 1 }); const before = JSON.stringify([s.privateState('attacker'), s.monsters.length]); assert.equal(s.cmd(A({ c: 'spawn', kind: 'trojan' })).reason, 'credits'); assert.equal(s.cmd(A({ c: 'spawn', kind: 'ping', n: 100 })).reason, 'credits'); assert.equal(s.cmd(A({ c: 'spawn', kind: 'nope' })).reason, 'bad_kind'); assert.equal(s.cmd(A({ c: 'spawn', kind: '__proto__' })).reason, 'bad_kind'); assert.equal(s.cmd(A({ c: 'spawn' })).reason, 'bad_kind'); for (const n of [0, -1, 1.5, '3', NaN, VS.MAX_ALIVE + 1]) assert.equal(s.cmd(A({ c: 'spawn', kind: 'spam', n })).reason, 'bad_cmd', 'n=' + n); for (const gate of [-1, 1, 7, 'a', 0.5, NaN]) assert.equal(s.cmd(A({ c: 'spawn', kind: 'spam', gate })).reason, 'bad_target', 'gate=' + gate); assert.equal(JSON.stringify([s.privateState('attacker'), s.monsters.length]), before, 'no charge, no unit'); }); test('spawn n: the first unit is out at once, the rest stream out within LAUNCH_SPAN seconds, all paid for up front', () => { const s = vsim({ credits: RICH }); const n = 20, c0 = s.privateState('attacker').credits; assert.equal(s.cmd(A({ c: 'spawn', kind: 'ping', n })).n, n); near(c0 - s.privateState('attacker').credits, n * ENEMIES.ping.buyCost, 1e-9); assert.equal(s.monsters.length, 1, 'one unit at once'); run(s, VS.RELEASE_GAP * 1.5); assert.ok(s.monsters.length > 1 && s.monsters.length < n, 'a stream, not a lump: ' + s.monsters.length); run(s, VS.LAUNCH_SPAN + 0.2); assert.equal(s.monsters.length, n, 'everything is out within the span'); assert.equal(s.vs.rel.length, 0); }); test('gates: round robin by default, a chosen gate when given', () => { const s = vsim({ map: 'twin', credits: RICH }); assert.equal(s.board.spawns.length, 2); s.cmd(A({ c: 'spawn', kind: 'spam', n: 4 })); run(s, VS.LAUNCH_SPAN + 0.5); const first = s.monsters.slice().sort((a, b) => a.id - b.id).map((m) => (m.y < 8 ? 0 : 1)); assert.deepEqual(first, [0, 1, 0, 1], 'alternating gateways'); const t = vsim({ map: 'twin', credits: RICH }); t.cmd(A({ c: 'spawn', kind: 'ping', n: 3, gate: 1 })); run(t, VS.LAUNCH_SPAN + 0.5); assert.ok(t.monsters.every((m) => m.y > 8), 'all three came in at gate 1: ' + t.monsters.map((m) => m.y.toFixed(1))); }); // ---------------------------------------------------------------- stage and launch test('stage pays now and holds the units in the convoy; launch lets them out as a stream; the snapshot shows only the SIZE', () => { const s = vsim({ credits: RICH }); const c0 = s.privateState('attacker').credits; assert.deepEqual(s.cmd(A({ c: 'stage', kind: 'trojan', n: 4 })), { ok: true, n: 4, staged: 4 }); assert.deepEqual(s.cmd(A({ c: 'stage', kind: 'daemon' })), { ok: true, n: 1, staged: 5 }); assert.equal(s.cmd(A({ c: 'stage', kind: 'trojan' })).staged, 6, 'the same kind joins its entry'); near(c0 - s.privateState('attacker').credits, 5 * ENEMIES.trojan.buyCost + ENEMIES.daemon.buyCost, 1e-9); assert.equal(s.monsters.length, 0, 'staged units are not on the map'); assert.deepEqual(s.privateState('attacker').staged, [['trojan', 5], ['daemon', 1]]); assert.equal(s.snapshot().vs.st, 6); assert.equal(JSON.stringify(s.snapshot().vs).includes('trojan'), false, 'the defender never sees what is in the convoy'); run(s, 20); assert.equal(s.monsters.length, 0, 'it waits for a launch, however long'); const r = s.cmd(A({ c: 'launch' })); assert.deepEqual([r.ok, r.n], [true, 6]); assert.equal(s.monsters.length, 1, 'a stream: one now'); assert.equal(s.snapshot().vs.st, 0, 'the convoy is empty again'); assert.deepEqual(s.privateState('attacker').staged, []); run(s, VS.LAUNCH_SPAN + 0.1); assert.equal(s.monsters.length, 6, 'all out within the span'); assert.deepEqual(s.monsters.slice().sort((a, b) => a.id - b.id).map((m) => m.kind), ['trojan', 'trojan', 'trojan', 'trojan', 'trojan', 'daemon'], 'in the order it was staged'); assert.equal(s.cmd(A({ c: 'launch' })).reason, 'empty'); }); test('launch can name a gate; a convoy splits over the gates round robin otherwise; an empty convoy is refused', () => { const s = vsim({ map: 'twin', credits: RICH }); assert.equal(s.cmd(A({ c: 'launch' })).reason, 'empty'); s.cmd(A({ c: 'stage', kind: 'worm', n: 6 })); assert.equal(s.cmd(A({ c: 'launch', gate: 5 })).reason, 'bad_target'); assert.equal(s.vsStaged(), 6, 'a refused launch keeps the convoy'); s.cmd(A({ c: 'launch', gate: 0 })); run(s, VS.LAUNCH_SPAN + 0.5); assert.ok(s.monsters.every((m) => m.y < 8), 'all at the top gate'); s.cmd(A({ c: 'stage', kind: 'worm', n: 6 })); s.cmd(A({ c: 'launch' })); run(s, VS.LAUNCH_SPAN + 0.5); assert.ok(s.monsters.some((m) => m.y > 8), 'the second convoy used the other gate too'); }); test('caps: MAX_ALIVE units on the map (queued releases count, decoys do not), MAX_STAGED in the convoy', () => { const s = vsim({ credits: RICH }); assert.equal(s.cmd(A({ c: 'spawn', kind: 'spam', n: VS.MAX_ALIVE })).ok, true); assert.equal(s.cmd(A({ c: 'spawn', kind: 'spam' })).reason, 'cap', 'the units still queued count'); run(s, VS.LAUNCH_SPAN + 0.5); const alive = () => s.monsters.filter((m) => !m.dead && !m.decoy).length; assert.equal(alive(), VS.MAX_ALIVE); const c0 = s.privateState('attacker').credits; assert.equal(s.cmd(A({ c: 'spawn', kind: 'spam' })).reason, 'cap'); assert.equal(s.cmd(A({ c: 'ability', id: 'decoy' })).ok, true, 'a decoy is not a unit: it ignores the cap'); assert.equal(s.privateState('attacker').credits, c0 - VS.ABILITIES.decoy.cost + 0, 'only the decoy was paid'); assert.equal(alive(), VS.MAX_ALIVE); // staged const t = vsim({ credits: RICH }); assert.equal(t.cmd(A({ c: 'stage', kind: 'spam', n: VS.MAX_STAGED })).ok, true); assert.equal(t.cmd(A({ c: 'stage', kind: 'ping' })).reason, 'cap'); assert.equal(t.privateState('attacker').credits, RICH - VS.MAX_STAGED * ENEMIES.spam.buyCost, 'the refused stage cost nothing'); assert.equal(t.cmd(A({ c: 'spawn', kind: 'spam', n: VS.MAX_ALIVE - 10 })).ok, true); const r = t.cmd(A({ c: 'launch' })); assert.deepEqual([r.ok, r.n, r.staged], [true, 10, VS.MAX_STAGED - 10], 'a launch lets out as many as fit and keeps the rest staged'); assert.equal(t.cmd(A({ c: 'launch' })).reason, 'cap'); }); // ---------------------------------------------------------------- lifetime bonus and leaks test('lifetime bonus: a living unit pays buyCost x LIFE_RATE per second, in total at most LIFE_CAP of its price', () => { const s = vsim(); s.privateState('attacker'); s.vs.credits = 1000; const m = parked(s, 'trojan'); const price = m.buyCost, rate = price * VS.LIFE_RATE, cap = price * VS.LIFE_CAP; const c0 = s.privateState('attacker').credits; run(s, 10); near(s.privateState('attacker').credits - c0 - baseIncome(0, 10), 10 * rate, 1e-6, 'ten seconds of bonus'); near(m.earned, 10 * rate, 1e-6); near(s.privateState('attacker').income - (VS.ATT_INCOME + VS.ATT_INCOME_GROWTH * s.time), rate, 1e-6, 'the income readout includes it'); const capSecs = VS.LIFE_CAP / VS.LIFE_RATE; run(s, capSecs + 20 - 10); near(m.earned, cap, 1e-6, 'earned stops at the cap'); const c1 = s.privateState('attacker').credits, t1 = s.time; run(s, 10); near(s.privateState('attacker').credits - c1, baseIncome(t1, s.time), 1e-6, 'after the cap only the base income is left'); near(s.privateState('attacker').income, VS.ATT_INCOME + VS.ATT_INCOME_GROWTH * s.time, 1e-6); }); test('units in the convoy earn no lifetime bonus, and decoys never do', () => { const s = vsim({ credits: 1000 }); s.cmd(A({ c: 'stage', kind: 'rootkit', n: 1 })); s.cmd(A({ c: 'ability', id: 'decoy' })); const c0 = s.privateState('attacker').credits; run(s, 4); near(s.privateState('attacker').credits - c0, baseIncome(0, 4), 1e-6); }); test('a leak pays the unit\'s price back, costs integrity and is counted in the integrity taken off', () => { const s = vsim({ credits: 1000 }); s.cmd(A({ c: 'spawn', kind: 'trojan' })); const afterBuy = s.privateState('attacker').credits; let guard = 0; while (s.lives === s.maxLives && guard++ < 60 * 60) s.tick(STEP); assert.equal(s.lives, s.maxLives - ENEMIES.trojan.leak); assert.equal(s.vs.lost, ENEMIES.trojan.leak); assert.equal(s.monsters.length, 0); const price = ENEMIES.trojan.buyCost, bonus = price * VS.LIFE_RATE * s.time; // (it lived `time` seconds, far from the cap) assert.ok(bonus < price * VS.LIFE_CAP); near(s.privateState('attacker').credits - afterBuy, baseIncome(0, s.time) + bonus + price, 0.1, 'income + the lifetime bonus + the price back'); assert.equal(s.vs.stats.leaked, 1); assert.equal(s.state, 'playing'); }); // ---------------------------------------------------------------- tech test('tech: level n costs TECH_COST x 2^(n-1), stops at the maximum, refuses what you cannot pay without charging', () => { for (const id of VS.TECH_ORDER) { const s = vsim({ credits: RICH }); for (let lv = 1; lv <= VS.TECH[id].max; lv++) { const c0 = s.privateState('attacker').credits; assert.equal(s.techCost(id), VS.TECH_COST * Math.pow(2, lv - 1)); assert.deepEqual(s.cmd(A({ c: 'tech', id })), { ok: true, level: lv }, id + ' level ' + lv); assert.equal(c0 - s.privateState('attacker').credits, VS.TECH_COST * Math.pow(2, lv - 1)); assert.equal(s.privateState('attacker').tech[id], lv); } const c1 = s.privateState('attacker').credits; assert.equal(s.cmd(A({ c: 'tech', id })).reason, 'max'); assert.equal(s.techCost(id), null); assert.equal(s.privateState('attacker').credits, c1); } const poor = vsim({ credits: VS.TECH_COST - 1 }); assert.equal(poor.cmd(A({ c: 'tech', id: 'armor' })).reason, 'credits'); assert.equal(poor.cmd(A({ c: 'tech', id: 'luck' })).reason, 'bad_target'); assert.equal(poor.cmd(A({ c: 'tech' })).reason, 'bad_target'); assert.equal(poor.cmd(A({ c: 'tech', id: 'toString' })).reason, 'bad_target'); assert.equal(poor.privateState('attacker').credits, VS.TECH_COST - 1); }); function techUnit(id, lv, kind = 'trojan', extra) { const s = vsim(Object.assign({ credits: RICH }, extra)); for (let i = 0; i < lv; i++) assert.ok(s.cmd(A({ c: 'tech', id })).ok); s.cmd(A({ c: 'spawn', kind })); return { s, m: s.monsters[s.monsters.length - 1] }; } test('tech armor: +per armour per level, and it really cuts the damage taken', () => { for (let lv = 0; lv <= VS.TECH.armor.max; lv++) { const { s, m } = techUnit('armor', lv, 'ping'); assert.equal(m.armor, ENEMIES.ping.armor + VS.TECH.armor.per * lv); const hp = m.hp; s.damage(m, 10, null); near(hp - m.hp, Math.max(10 - m.armor, 10 * 0.25), 1e-9, 'damage at armour level ' + lv); } }); test('tech speed: +per x base speed per level', () => { for (let lv = 0; lv <= VS.TECH.speed.max; lv++) near(techUnit('speed', lv).m.speed, ENEMIES.trojan.speed * (1 + VS.TECH.speed.per * lv), 1e-9, 'speed level ' + lv); }); test('tech health: +per x base hp per level', () => { for (let lv = 0; lv <= VS.TECH.health.max; lv++) { const m = techUnit('health', lv).m; assert.equal(m.hp, Math.round(ENEMIES.trojan.hp * (1 + VS.TECH.health.per * lv))); assert.equal(m.maxhp, m.hp); } }); test('tech regen: a hurt unit heals per x max hp per second per level, never above max', () => { assert.equal(techUnit('regen', 0).m.regen, 0); for (let lv = 1; lv <= VS.TECH.regen.max; lv++) { const { s, m } = techUnit('regen', lv); m.stunT = 1e9; m.hp = m.maxhp / 2; run(s, 4); near(m.hp, m.maxhp / 2 + 4 * VS.TECH.regen.per * lv * m.maxhp, 0.2, 'hp after 4 s at regen level ' + lv); m.hp = m.maxhp - 0.01; run(s, 1); assert.ok(m.hp <= m.maxhp + 1e-9); } const none = techUnit('regen', 0); none.m.stunT = 1e9; none.m.hp = 10; run(none.s, 3); assert.equal(none.m.hp, 10, 'no regen without the tech'); }); test('tech shield: +per hit points that soak damage first, on any kind, and a tech shield does not regrow (a Proxy\'s own does)', () => { for (let lv = 0; lv <= VS.TECH.shield.max; lv++) { const { s, m } = techUnit('shield', lv, 'ping'); assert.equal(m.shield, VS.TECH.shield.per * lv); assert.equal(m.maxShield, VS.TECH.shield.per * lv); if (!lv) continue; s.damage(m, VS.TECH.shield.per * lv - 1, null); assert.equal(m.hp, ENEMIES.ping.hp, 'the shield took it all'); s.damage(m, 5, null); near(m.hp, ENEMIES.ping.hp - 4, 1e-9, 'hp only after the shield'); m.stunT = 1e9; run(s, 12); assert.ok(m.shield < 1e-9, 'no regrow for a kind without a shield of its own, and no NaN: ' + m.shield); assert.ok(Number.isFinite(m.hp)); } const p = techUnit('shield', 2, 'proxy').m; assert.equal(p.maxShield, ENEMIES.proxy.shield + 2 * VS.TECH.shield.per); assert.equal(JSON.stringify(techUnit('shield', 3, 'ping').s.snapshot()).includes('null'), false); }); test('tech stealth: towers cannot acquire the unit for its first secs[level] seconds on the map, then they can', () => { for (let lv = 1; lv <= VS.TECH.stealth.max; lv++) { const s = vsim({ credits: RICH }); for (let i = 0; i < lv; i++) s.cmd(A({ c: 'tech', id: 'stealth' })); const tw = filterAt(s); s.cmd(A({ c: 'spawn', kind: 'rootkit' })); const m = s.monsters[0]; m.x = 2.5; m.y = 5.5; m.cx = 2; m.cy = 5; m.stunT = 1e9; // inside the filter's range, parked s.drainEvents(); const secs = VS.TECH.stealth.secs[lv - 1]; near(m.stealthT, secs, 1e-9); run(s, secs - 0.2); assert.equal(tw.tgt, 0, 'level ' + lv + ': hidden, no target'); assert.equal(shots(s), 0, 'and no shot'); run(s, 0.5); assert.equal(tw.tgt, m.id, 'level ' + lv + ': visible after ' + secs + ' s'); assert.ok(shots(s) > 0); } const s = vsim({ credits: RICH }); s.cmd(A({ c: 'spawn', kind: 'ping' })); assert.equal(s.monsters[0].stealthT, 0, 'no tech, no stealth'); }); test('stealth hides a unit from target acquisition only: a shot that is already flying still splashes it', () => { const s = vsim({ credits: RICH }); s.cmd(A({ c: 'tech', id: 'stealth' })); const mortar = filterAt(s, 2, 5, 'mortar'); s.cmd(A({ c: 'spawn', kind: 'rootkit' })); const hidden = s.monsters[0]; hidden.x = 2.5; hidden.y = 6.5; hidden.cx = 2; hidden.cy = 6; hidden.stunT = 1e9; const seen = parked(s, 'rootkit', 3, 6); seen.stealthT = 0; run(s, 0.3); assert.equal(mortar.tgt, seen.id, 'the visible one is the target'); s.damage(hidden, 1, null); assert.ok(hidden.hp < hidden.maxhp, 'a hidden unit can still be hurt'); }); test('tech crypt: slows work res[level] weaker (Rate Limiter and Orbital Uplink), stacked with a boss\'s own resistance; a stun is not a slow', () => { for (let lv = 0; lv <= VS.TECH.crypt.max; lv++) { const res = lv ? VS.TECH.crypt.res[lv - 1] : 0; const s = vsim({ credits: RICH, gold: RICH }); for (let i = 0; i < lv; i++) s.cmd(A({ c: 'tech', id: 'crypt' })); filterAt(s, 2, 5, 'throttle'); const m = parked(s, 'ping', 2, 6), r = parked(s, 'rootkit', 3, 6); m.stunT = 0; r.stunT = 0; m.speed = r.speed = 0; // stand still on their own s.tick(STEP); near(m.slow, TOWERS.throttle.levels[0].slow * (1 - res), 1e-9, 'Rate Limiter on a ping, crypt ' + lv); near(r.slow, TOWERS.throttle.levels[0].slow * (1 - ENEMIES.rootkit.slowRes) * (1 - res), 1e-9, 'and on a rootkit'); // the Uplink's lingering slow const u = vsim({ credits: RICH, gold: RICH }); for (let i = 0; i < lv; i++) u.cmd(A({ c: 'tech', id: 'crypt' })); const up = u.cmd(D({ c: 'place', type: 'uplink', x: 5, y: 3 })); assert.ok(up.ok); const t = parked(u, 'trojan', 6, 5); t.stunT = 0; t.speed = 0; u.tick(STEP); near(t.slowAmt, Math.min(0.6, TOWERS.uplink.levels[0].slowOnHit * (1 - res)), 1e-9, 'Uplink slow, crypt ' + lv); } }); test('tech applies to units that enter the map afterwards, not to the ones already out', () => { const s = vsim({ credits: RICH }); s.cmd(A({ c: 'spawn', kind: 'ping' })); s.cmd(A({ c: 'tech', id: 'health' })); s.cmd(A({ c: 'tech', id: 'armor' })); s.cmd(A({ c: 'spawn', kind: 'ping' })); const [old, young] = s.monsters; assert.equal(old.hp, ENEMIES.ping.hp); assert.equal(old.armor, 0); assert.equal(young.armor, VS.TECH.armor.per); assert.ok(young.hp > old.hp); // a convoy gets the tech of the moment it is let out s.cmd(A({ c: 'stage', kind: 'ping' })); s.cmd(A({ c: 'tech', id: 'armor' })); s.cmd(A({ c: 'launch' })); assert.equal(s.monsters[2].armor, 2 * VS.TECH.armor.per); }); // ---------------------------------------------------------------- abilities test('abilities: cost credits, start their cooldown only on success, refuse a second use until it has run down', () => { for (const id of VS.ABILITY_ORDER) { const A_ = VS.ABILITIES[id]; const s = vsim({ credits: RICH, gold: RICH }); const tw = filterAt(s); const arg = id === 'emp' ? { tid: tw.id } : {}; const c0 = s.privateState('attacker').credits; assert.equal(s.privateState('attacker').cd[id], 0); assert.equal(s.cmd(A({ c: 'ability', id, ...arg })).ok, true, id); assert.equal(c0 - s.privateState('attacker').credits, A_.cost, id + ' cost'); assert.equal(s.privateState('attacker').cd[id], A_.cd, id + ' cooldown'); const c1 = s.privateState('attacker').credits; assert.equal(s.cmd(A({ c: 'ability', id, ...arg })).reason, 'cooldown'); assert.equal(s.privateState('attacker').credits, c1, 'a refused use costs nothing'); run(s, A_.cd / 2); near(s.privateState('attacker').cd[id], A_.cd / 2, 0.05, id + ' cooldown runs down'); assert.equal(s.cmd(A({ c: 'ability', id, ...arg })).reason, 'cooldown'); run(s, A_.cd / 2 + 0.1); assert.equal(s.privateState('attacker').cd[id], 0); assert.equal(s.cmd(A({ c: 'ability', id, ...arg })).ok, true, id + ' again'); // too poor: refused, no cooldown started const p = vsim({ credits: A_.cost - 1 }); const t2 = filterAt(p); assert.equal(p.cmd(A({ c: 'ability', id, ...(id === 'emp' ? { tid: t2.id } : {}) })).reason, 'credits'); assert.equal(p.privateState('attacker').cd[id], 0, 'no cooldown after a refusal'); assert.equal(p.privateState('attacker').credits, A_.cost - 1); } const s = vsim({ credits: RICH }); assert.equal(s.cmd(A({ c: 'ability', id: 'nuke' })).reason, 'bad_target'); assert.equal(s.cmd(A({ c: 'ability' })).reason, 'bad_target'); assert.equal(s.cmd(A({ c: 'ability', id: 'constructor' })).reason, 'bad_target'); }); test('emp: needs a tower that exists and does something; the tower is dead for dur seconds (attack, slow, stun, boost), then works again', () => { const s = vsim({ credits: RICH, gold: RICH }); const wall = s.towerById(s.cmd(D({ c: 'place', type: 'wall', x: 8, y: 2 })).id); const tw = filterAt(s); for (const bad of [{}, { tid: 999 }, { tid: wall.id }, { tid: 'x' }, { tid: null }]) { assert.equal(s.cmd(A({ c: 'ability', id: 'emp', ...bad })).reason, 'bad_target', JSON.stringify(bad)); } assert.equal(s.privateState('attacker').credits, RICH, 'refusals cost nothing'); assert.equal(s.privateState('attacker').cd.emp, 0); const m = parked(s, 'rootkit', 2, 6); run(s, 0.5); assert.ok(shots(s) > 0, 'the filter fires before the EMP'); assert.ok(s.cmd(A({ c: 'ability', id: 'emp', tid: tw.id })).ok); assert.equal(tw.tgt, 0); const ev = s.drainEvents().find((e) => e.e === 'emp'); assert.deepEqual([ev.id, ev.d], [tw.id, VS.ABILITIES.emp.dur]); const tt = s.snapshot().tt.find((r) => r[0] === tw.id); assert.equal(tt.length, 4); near(tt[3], VS.ABILITIES.emp.dur, 0.01, 'the snapshot tells how long it is out'); run(s, VS.ABILITIES.emp.dur - 0.3); assert.equal(shots(s), 0, 'no shot while EMPed'); assert.equal(tw.tgt, 0); const hp = m.hp; run(s, 0.6); assert.ok(shots(s) > 0, 'works again after dur'); assert.equal(s.snapshot().tt.find((r) => r[0] === tw.id)[3], 0); assert.ok(m.hp < hp); // slow, stun and boost go with it const u = vsim({ credits: RICH, gold: RICH }); const th = filterAt(u, 2, 5, 'throttle'), sb = filterAt(u, 6, 5, 'sandbox'), lb = filterAt(u, 10, 5, 'balancer'), f2 = filterAt(u, 11, 6, 'filter'); const a = parked(u, 'ping', 2, 6); a.stunT = 0; a.speed = 0; u.tick(STEP); assert.ok(a.slow > 0, 'the Rate Limiter slows'); assert.ok(f2.buff > 0, 'the Load Balancer boosts'); u.cmd(A({ c: 'ability', id: 'emp', tid: th.id })); a.slow = 0; u.tick(STEP); assert.equal(a.slow, 0, 'an EMPed Rate Limiter slows nothing'); u.cmd(D({ c: 'tool', id: 'patch', tid: th.id })); run(u, VS.ABILITIES.emp.cd + 0.1); u.cmd(A({ c: 'ability', id: 'emp', tid: lb.id })); u.tick(STEP); assert.equal(f2.buff, 0, 'an EMPed Load Balancer boosts nothing'); assert.ok(sb && lb); }); test('decoy: a harmless unit every tower prefers for dur seconds, even over a unit closer to the server; unkillable, free of caps and credits', () => { const s = vsim({ credits: RICH, gold: RICH }); const tw = filterAt(s); const credits0 = s.privateState('attacker').credits; assert.equal(s.cmd(A({ c: 'ability', id: 'decoy', tid: 999 })).reason, 'bad_target'); assert.equal(s.cmd(A({ c: 'ability', id: 'decoy', gate: 3 })).reason, 'bad_target'); assert.equal(s.privateState('attacker').credits, credits0, 'refusals cost nothing'); assert.ok(s.cmd(A({ c: 'ability', id: 'decoy', tid: tw.id })).ok); const d = s.monsters[0]; assert.ok(d.decoy); assert.equal(d.leak, 0); assert.equal(d.reward, 0); assert.equal(TD.ENEMY_ORDER.includes('decoy'), false, 'no new enemy kind on the wire'); assert.ok(Math.hypot(d.x - 2.5, d.y - 5.5) <= TOWERS.filter.levels[0].range, 'it appeared next to the tower it was aimed at'); const ev = s.drainEvents().find((e) => e.e === 'decoy'); assert.deepEqual([ev.id, ev.d], [d.id, VS.ABILITIES.decoy.dur]); const real = parked(s, 'ping', 2, 7); // a real unit, closer to the server than the decoy real.rem = d.rem - 3; d.stunT = 1e9; tw.mode = 'first'; run(s, 0.5); assert.equal(tw.tgt, d.id, 'the decoy wins over "first"'); tw.mode = 'strong'; run(s, 0.1); assert.equal(tw.tgt, d.id, 'and over "strong"'); tw.mode = 'close'; run(s, 0.1); assert.equal(tw.tgt, d.id, 'and over "close"'); assert.equal(d.hp, d.maxhp, 'it cannot be hurt'); s.damage(d, 1e9, null); assert.equal(d.dead, false); assert.equal(s.kills, 0); assert.equal(real.hp, real.maxhp, 'while the decoy soaks every shot the real unit is left alone'); run(s, VS.ABILITIES.decoy.dur); assert.equal(d.dead, true, 'it is gone after dur'); s.tick(STEP); assert.equal(s.monsters.includes(d), false); assert.equal(tw.tgt, real.id, 'and the tower goes back to the real unit'); const credits1 = s.privateState('attacker').credits; run(s, 1); near(s.privateState('attacker').credits - credits1, baseIncome(s.time - 1, s.time) + real.buyCost * VS.LIFE_RATE, 0.02, 'a decoy never earned a lifetime bonus (only the real unit did)'); }); test('decoy at a gate that reaches the server is silent: no integrity lost, no credits paid', () => { const s = vsim({ credits: 500 }); s.cmd(A({ c: 'ability', id: 'decoy' })); const d = s.monsters[0]; d.ttl = 1e9; d.x = 24.5; d.y = 8.5; d.cx = 24; d.cy = 8; d.tx = -1; const c0 = s.privateState('attacker').credits, t0 = s.time; run(s, 1); assert.equal(d.dead, true); assert.equal(s.lives, s.maxLives); assert.equal(s.vs.lost, 0); assert.equal(s.leaks, 0); assert.equal(s.vs.stats.leaked, 0); near(s.privateState('attacker').credits - c0, baseIncome(t0, s.time), 1e-6); }); test('surge: every unit is VS surge speed faster for dur seconds, units that enter meanwhile too, then normal again', () => { const dist = (surge) => { const s = vsim({ credits: RICH }); s.cmd(A({ c: 'spawn', kind: 'ping' })); const m = s.monsters[0], r0 = m.rem; if (surge) assert.ok(s.cmd(A({ c: 'ability', id: 'surge' })).ok); run(s, 2); return r0 - m.rem; }; near(dist(true) / dist(false), 1 + VS.ABILITIES.surge.speed, 0.03, 'distance ratio during a surge'); const s = vsim({ credits: RICH }); s.cmd(A({ c: 'spawn', kind: 'ping' })); s.cmd(A({ c: 'ability', id: 'surge' })); assert.equal(s.drainEvents().some((e) => e.e === 'surge' && e.d === VS.ABILITIES.surge.dur), true); near(s.privateState('attacker').active.surge, VS.ABILITIES.surge.dur, 0.01); run(s, VS.ABILITIES.surge.dur + 0.5); assert.equal(s.privateState('attacker').active.surge, 0); const m = s.monsters[0], r0 = m.rem; run(s, 2); near((r0 - m.rem) / 2, ENEMIES.ping.speed, 0.02, 'normal speed again'); }); test('ddos: every tower\'s cooldown runs (1 - slow) as fast for dur seconds', () => { const count = (ddos) => { const s = vsim({ credits: RICH, gold: RICH }); filterAt(s); parked(s, 'rootkit', 2, 6); s.drainEvents(); if (ddos) assert.ok(s.cmd(A({ c: 'ability', id: 'ddos' })).ok); s.drainEvents(); run(s, 3.5); return shots(s); }; const base = count(false), slowed = count(true); assert.ok(base >= 9, 'a Packet Filter fires about rate x time: ' + base); near(slowed / base, 1 - VS.ABILITIES.ddos.slow, 0.1, 'shots with / without DDoS'); const s = vsim({ credits: RICH }); s.cmd(A({ c: 'ability', id: 'ddos' })); assert.ok(s.drainEvents().some((e) => e.e === 'ddos' && e.d === VS.ABILITIES.ddos.dur)); near(s.privateState('attacker').active.ddos, VS.ABILITIES.ddos.dur, 0.01); run(s, VS.ABILITIES.ddos.dur + 0.1); assert.equal(s.privateState('attacker').active.ddos, 0); }); // ---------------------------------------------------------------- defender tools test('tools: cost credits (gold), start their cooldown only on success, refuse a second use until it ran down', () => { for (const id of VS.TOOL_ORDER) { const T = VS.TOOLS[id]; const s = vsim({ gold: RICH, credits: RICH, lives: 5 }); const tw = filterAt(s); s.cmd(A({ c: 'ability', id: 'emp', tid: tw.id })); s.lives = s.maxLives - 4; const arg = id === 'scan' ? { x: 2.5, y: 5.5 } : id === 'patch' ? { tid: tw.id } : {}; const g0 = s.gold; assert.equal(s.privateState('defender').cd[id], 0); assert.equal(s.cmd(D({ c: 'tool', id, ...arg })).ok, true, id); assert.equal(g0 - s.gold, T.cost, id + ' cost'); assert.equal(s.privateState('defender').cd[id], T.cd); // (re-arm the preconditions of patch / repair, which refuse when there is nothing to do) if (id === 'patch') { s.vs.acd.emp = 0; s.cmd(A({ c: 'ability', id: 'emp', tid: tw.id })); } if (id === 'repair') s.lives = s.maxLives - 1; const g1 = s.gold; assert.equal(s.cmd(D({ c: 'tool', id, ...arg })).reason, 'cooldown'); assert.equal(s.gold, g1); run(s, T.cd + 0.1); assert.equal(s.privateState('defender').cd[id], 0); if (id === 'patch') { s.vs.acd.emp = 0; s.cmd(A({ c: 'ability', id: 'emp', tid: tw.id })); } if (id === 'repair') s.lives = s.maxLives - 1; assert.equal(s.cmd(D({ c: 'tool', id, ...arg })).ok, true, id + ' again'); // too poor: everything else is in order, only the credits are one short const p = vsim({ gold: RICH, credits: RICH }); const pt = filterAt(p); p.cmd(A({ c: 'ability', id: 'emp', tid: pt.id })); p.lives = p.maxLives - 4; p.gold = T.cost - 1; assert.equal(p.cmd(D({ c: 'tool', id, ...(id === 'scan' ? { x: 2.5, y: 5.5 } : id === 'patch' ? { tid: pt.id } : {}) })).reason, 'credits', id + ' too poor'); assert.equal(p.gold, T.cost - 1); assert.equal(p.privateState('defender').cd[id], 0, 'no cooldown after a refusal'); } const s = vsim({ gold: RICH }); assert.equal(s.cmd(D({ c: 'tool', id: 'nuke' })).reason, 'bad_target'); assert.equal(s.cmd(D({ c: 'tool' })).reason, 'bad_target'); assert.equal(s.cmd(D({ c: 'tool', id: 'constructor' })).reason, 'bad_target'); }); test('scan: clears EMP on towers within the radius only, and lets the towers target stealthed units there for the reveal time', () => { const T = VS.TOOLS.scan; const s = vsim({ gold: RICH, credits: RICH }); const a = filterAt(s, 2, 5), b = filterAt(s, 2 + Math.ceil(T.radius) + 3, 5); for (const t of [a, b]) { s.vs.acd.emp = 0; assert.ok(s.cmd(A({ c: 'ability', id: 'emp', tid: t.id })).ok); } assert.ok(a.empT > 0 && b.empT > 0); for (const bad of [{}, { x: -1, y: 3 }, { x: 3, y: 99 }, { x: NaN, y: 3 }, { x: '3', y: 3 }, { x: Infinity, y: 3 }]) { assert.equal(s.cmd(D({ c: 'tool', id: 'scan', ...bad })).reason, 'bad_target', JSON.stringify(bad)); } assert.equal(s.vs.dcd.scan, 0, 'a refused scan starts no cooldown'); assert.ok(s.cmd(D({ c: 'tool', id: 'scan', x: 2.5, y: 5.5 })).ok); assert.equal(a.empT, 0, 'the tower inside the radius is back'); assert.ok(b.empT > 0, 'the one outside is not'); assert.ok(s.drainEvents().some((e) => e.e === 'scan' && e.x === 2.5 && e.y === 5.5)); // reveal: stealth would hide the unit for its whole 2 s; a scan on it makes it a target at once const r = vsim({ gold: RICH, credits: RICH }); for (let i = 0; i < VS.TECH.stealth.max; i++) r.cmd(A({ c: 'tech', id: 'stealth' })); const tw = filterAt(r); const m = parked(r, 'rootkit', 2, 6); m.stealthT = VS.TECH.stealth.secs[VS.TECH.stealth.max - 1]; run(r, 0.3); assert.equal(tw.tgt, 0, 'hidden'); assert.ok(r.cmd(D({ c: 'tool', id: 'scan', x: 2.5, y: 6.5 })).ok); run(r, 0.1); assert.equal(tw.tgt, m.id, 'revealed by the scan'); run(r, T.reveal + 0.2); assert.equal(r.vs.scans.length, 0, 'the zone expires'); // a unit outside the scanned circle is not revealed const o = vsim({ gold: RICH, credits: RICH }); const tw2 = filterAt(o); const hidden = parked(o, 'rootkit', 2, 6); hidden.stealthT = 100; o.cmd(D({ c: 'tool', id: 'scan', x: 2.5 + T.radius + 1, y: 6.5 })); run(o, 0.3); assert.equal(tw2.tgt, 0, 'outside the radius stays hidden'); assert.ok(Math.abs(T.radius) > 0); }); test('patch: re-enables an EMPed tower at once; a tower that is fine (or missing) is refused for free', () => { const s = vsim({ gold: RICH, credits: RICH }); const tw = filterAt(s); const g0 = s.gold; assert.equal(s.cmd(D({ c: 'tool', id: 'patch', tid: tw.id })).reason, 'bad_target', 'nothing to patch'); assert.equal(s.cmd(D({ c: 'tool', id: 'patch', tid: 12345 })).reason, 'bad_target'); assert.equal(s.cmd(D({ c: 'tool', id: 'patch' })).reason, 'bad_target'); assert.equal(s.gold, g0); assert.equal(s.vs.dcd.patch, 0); s.cmd(A({ c: 'ability', id: 'emp', tid: tw.id })); assert.ok(tw.empT > 0); assert.ok(s.cmd(D({ c: 'tool', id: 'patch', tid: tw.id })).ok); assert.equal(tw.empT, 0); const m = parked(s, 'rootkit', 2, 6); run(s, 0.3); assert.equal(tw.tgt, m.id, 'it works again right away'); }); test('repair: + amount integrity, never above the maximum; at the maximum it is refused (nothing charged, no cooldown)', () => { const T = VS.TOOLS.repair; const s = vsim({ gold: RICH }); const g0 = s.gold; assert.equal(s.cmd(D({ c: 'tool', id: 'repair' })).reason, 'max'); assert.equal(s.gold, g0); assert.equal(s.vs.dcd.repair, 0); s.lives = s.maxLives - 10; assert.ok(s.cmd(D({ c: 'tool', id: 'repair' })).ok); assert.equal(s.lives, s.maxLives - 10 + T.amount); assert.ok(s.drainEvents().some((e) => e.e === 'repair')); run(s, T.cd + 0.1); s.lives = s.maxLives - 1; assert.ok(s.cmd(D({ c: 'tool', id: 'repair' })).ok); assert.equal(s.lives, s.maxLives, 'it stops at the maximum'); assert.ok(s.lives <= s.maxLives); }); test('quarantine: every unit is frozen for dur seconds (towers keep firing), then moves on', () => { const T = VS.TOOLS.quarantine; const s = vsim({ gold: RICH, credits: RICH }); const tw = filterAt(s); s.cmd(A({ c: 'spawn', kind: 'rootkit' })); const m = s.monsters[0]; run(s, 0.5); const x0 = m.x, y0 = m.y, hp0 = m.hp; assert.ok(s.cmd(D({ c: 'tool', id: 'quarantine' })).ok); assert.ok(s.drainEvents().some((e) => e.e === 'quarantine' && e.d === T.dur)); s.cmd(A({ c: 'spawn', kind: 'ping' })); const late = s.monsters[1], lx = late.x; run(s, T.dur - 0.2); assert.equal(m.x, x0); assert.equal(m.y, y0); assert.equal(late.x, lx, 'a unit that enters during the freeze is frozen too'); assert.ok(m.hp < hp0, 'the towers still shoot a frozen unit'); assert.ok(tw.tgt === m.id || tw.tgt === late.id); run(s, 0.6); assert.ok(m.x > x0 || m.y !== y0, 'moving again after dur'); }); // ---------------------------------------------------------------- the round test('the round ends when integrity reaches 0: the attacker wins, the result carries the time and the integrity taken off', () => { const s = vsim({ credits: RICH, lives: 3 }); s.cmd(A({ c: 'spawn', kind: 'ping', n: 6 })); let guard = 0; while (s.state === 'playing' && guard++ < 60 * 120) s.tick(STEP); assert.equal(s.state, 'over'); assert.deepEqual(Object.keys(s.vs.result).sort(), ['integrityLost', 't', 'winner']); assert.equal(s.vs.result.winner, 'attacker'); assert.equal(s.vs.result.integrityLost, 3, 'clamped to the integrity there was'); assert.equal(s.vs.result.t, TD.round2(s.time)); assert.ok(s.vs.result.t > 5 && s.vs.result.t < 60, 'about the time a ping takes to cross: ' + s.vs.result.t); assert.equal(s.lives, 0); assert.ok(s.drainEvents().some((e) => e.e === 'round' && e.winner === 'attacker' && e.t === s.vs.result.t)); assert.equal(s.snapshot().s, 'over'); // nothing moves any more, and every command answers 'over' const snap = JSON.stringify(s.snapshot()); run(s, 5); assert.equal(JSON.stringify(s.snapshot()), snap); for (const c of [A({ c: 'spawn', kind: 'ping' }), A({ c: 'stage', kind: 'ping' }), A({ c: 'launch' }), A({ c: 'tech', id: 'armor' }), A({ c: 'ability', id: 'surge' }), D({ c: 'place', type: 'filter', x: 4, y: 4 }), D({ c: 'sell', id: 1 }), D({ c: 'upgrade', id: 1 }), D({ c: 'target', id: 1, mode: 'last' }), D({ c: 'tool', id: 'repair' })]) { assert.equal(s.cmd(c).reason, 'over', c.c); } assert.equal(s.cmd({ c: 'spawn', kind: 'ping' }).reason, 'role', 'the role is checked before the state'); }); test('the round ends when the timer runs out: the defender wins at `limit` with the integrity that was taken off', () => { const s = vsim({ limit: 30, credits: RICH }); s.cmd(A({ c: 'spawn', kind: 'ping' })); s.cmd(A({ c: 'spawn', kind: 'worm' })); let guard = 0; while (s.state === 'playing' && guard++ < 60 * 100) s.tick(STEP); assert.equal(s.state, 'over'); assert.equal(s.vs.result.winner, 'defender'); near(s.vs.result.t, 30, STEP * 2); assert.equal(s.vs.result.integrityLost, s.vs.lost); assert.equal(s.vs.result.integrityLost, ENEMIES.ping.leak + ENEMIES.worm.leak, 'both crossed the empty field during those 30 s'); const quiet = vsim({ limit: 10 }); run(quiet, 11); assert.deepEqual(quiet.vs.result, { winner: 'defender', t: quiet.vs.result.t, integrityLost: 0 }); }); test('integrity 0 beats the clock when both happen on the same tick; repairs do not give integrity taken off back', () => { const s = vsim({ limit: 10 }); s.time = 10 - STEP / 2; s.lives = 0; s.tick(STEP); assert.equal(s.vs.result.winner, 'attacker'); const r = vsim({ credits: RICH, gold: RICH, lives: 10 }); r.cmd(A({ c: 'spawn', kind: 'trojan' })); let guard = 0; while (r.lives === 10 && guard++ < 60 * 60) r.tick(STEP); assert.equal(r.vs.lost, ENEMIES.trojan.leak); r.cmd(D({ c: 'tool', id: 'repair' })); assert.equal(r.lives, 10, 'repaired'); assert.equal(r.vs.lost, ENEMIES.trojan.leak, 'but what was taken off stays counted'); }); // ---------------------------------------------------------------- what each side may know test('privateState: exactly the contract keys, copies, null for anyone else and for co-op', () => { const s = vsim({ credits: RICH }); s.cmd(A({ c: 'stage', kind: 'ping', n: 2 })); const a = s.privateState('attacker'), before = JSON.stringify(a); assert.deepEqual(Object.keys(a), ['credits', 'income', 'tech', 'staged', 'cd', 'active']); assert.deepEqual(Object.keys(a.tech), VS.TECH_ORDER); assert.deepEqual(Object.keys(a.cd), VS.ABILITY_ORDER); assert.deepEqual(Object.keys(a.active), ['surge', 'ddos']); assert.deepEqual(a.staged, [['ping', 2]]); assert.equal(typeof a.credits, 'number'); assert.equal(a.income, VS.ATT_INCOME); const d = s.privateState('defender'); assert.deepEqual(Object.keys(d), ['cd']); assert.deepEqual(Object.keys(d.cd), VS.TOOL_ORDER); assert.equal(s.privateState('spectator'), null); assert.equal(s.privateState(), null); assert.equal(new Sim({ map: 'open' }).privateState('attacker'), null); // copies: scribbling on one changes nothing in the sim a.tech.armor = 5; a.staged.push(['rootkit', 9]); a.cd.emp = 99; a.credits = 0; d.cd.scan = 99; assert.equal(s.privateState('attacker').tech.armor, 0); assert.deepEqual(s.privateState('attacker').staged, [['ping', 2]]); assert.equal(s.privateState('attacker').cd.emp, 0); assert.equal(s.privateState('defender').cd.scan, 0); assert.equal(JSON.stringify(s.privateState('attacker')), before, 'nothing about the sim changed'); }); test('the shared snapshot: a vs block of exactly { t, lim, st }, JSON round trip, and nothing the other side may not know', () => { const s = vsim({ credits: RICH, limit: 480 }); s.cmd(A({ c: 'stage', kind: 'rootkit', n: 2 })); s.cmd(A({ c: 'tech', id: 'stealth' })); s.cmd(A({ c: 'ability', id: 'surge' })); run(s, 3); const snap = s.snapshot(); assert.deepEqual(Object.keys(snap.vs), ['t', 'lim', 'st']); assert.deepEqual(snap.vs, { t: TD.round2(s.time), lim: 480, st: 2 }); assert.deepEqual(JSON.parse(JSON.stringify(snap)), snap); const wire = JSON.stringify(snap); assert.equal(/credits|rootkit|tech|stealth|surge|staged|income/.test(wire.replace(/"mo":\[[^\]]*\]/g, '')), false, 'no attacker secrets in the shared snapshot'); const full = s.fullState(); assert.equal(full.mode, 'versus'); assert.equal(full.limit, 480); assert.equal(full.map, 'open'); assert.equal(full.winWave, 0); assert.equal(full.maxLives, s.maxLives); assert.deepEqual(JSON.parse(JSON.stringify(full)), full); assert.equal(JSON.stringify(full).includes('null'), false); // the monster rows are the co-op format, unchanged s.cmd(A({ c: 'spawn', kind: 'ghost' })); const row = s.snapshot().mo[0]; assert.equal(row.length, 7); assert.equal(row[1], TD.ENEMY_ORDER.indexOf('ghost')); }); test('co-op and solo are untouched: no vs block, no mode in the state, 3-field tower rows, no role needed', () => { const s = new Sim({ map: 'open' }); assert.equal('vs' in s.snapshot(), false); assert.deepEqual(Object.keys(s.fullState()), ['map', 'rev', 'towers', 'winWave', 'maxLives', 'difficulty']); s.cmd({ c: 'place', type: 'filter', x: 4, y: 4 }); assert.equal(s.snapshot().tt[0].length, 3); assert.deepEqual(Object.keys(s.snapshot()), ['time', 'g', 'l', 'w', 's', 'au', 'nw', 'k', 'rev', 'mo', 'bu', 'tt', 'q']); assert.equal(s.cmd({ c: 'wave' }).wave, 1); for (let i = 0; i < 60 * 30; i++) s.tick(STEP); assert.ok(s.monsters.length > 0 || s.kills > 0); assert.equal(s.hpScaleUsed, undefined); const mo = s.monsters[0]; if (mo) assert.ok(!('buyCost' in mo) && !('stealthT' in mo) && !('decoy' in mo), 'co-op monsters carry no versus fields'); assert.equal(new Sim({ map: 'open', mode: 'coop' }).vs, null); }); test('events are small objects and can be switched off like in co-op', () => { const s = vsim({ credits: RICH, events: false }); s.cmd(A({ c: 'ability', id: 'surge' })); assert.deepEqual(s.drainEvents(), []); const t = vsim({ credits: RICH }); t.cmd(A({ c: 'ability', id: 'surge' })); assert.ok(JSON.stringify(t.drainEvents()).length < 100); }); test('maps: a versus round can be set up on every map and its units reach the server; flyers fly straight', () => { for (const id of TD.MAP_ORDER) { const s = vsim({ map: id, credits: RICH, limit: 400 }); s.cmd(A({ c: 'spawn', kind: 'worm', n: s.board.spawns.length * 2 })); s.cmd(A({ c: 'spawn', kind: 'ghost' })); let guard = 0; while (s.vs.stats.leaked < s.board.spawns.length * 2 + 1 && guard++ < 60 * 200) s.tick(STEP); assert.ok(s.vs.stats.leaked >= s.board.spawns.length * 2 + 1, id + ': everything reached the server (' + s.vs.stats.leaked + ')'); } }); // ================================================================================================================ the bots const Bot = require('../public/js/bot.js'); const fs = require('node:fs'); const path = require('node:path'); // A bot plays like a human: through sim.cmd. These are the refusals a human meets too (a packet stands in the way of a wall or a turret, // or a cell was taken a moment ago); anything else means the bot sent a command its own checks should have caught. const HUMAN_REFUSALS = new Set(['monster', 'blocks', 'occupied']); // One bot-vs-bot round; every command both bots send is logged with its answer. function botRound(o) { const sim = vsim({ map: o.map || 'open', limit: o.limit || 100, events: false, ...(o.sim || {}) }); const log = []; const send = sim.cmd.bind(sim); sim.cmd = (c) => { const r = send(c); log.push({ role: c.role, by: c.by, c: c.c, id: c.id || c.type || c.kind, ok: r.ok, reason: r.reason, t: sim.time }); return r; }; const att = Bot.create(sim, 'attacker', { seed: o.seed || 1, skill: o.attackerSkill, by: 1 }); const def = Bot.create(sim, 'defender', { seed: o.seed || 1, skill: o.defenderSkill, by: 0 }); for (let i = 0; sim.state === 'playing' && i < 60 * ((o.limit || 100) + 5); i++) { def.think(STEP); att.think(STEP); sim.tick(STEP); } return { sim, log, att, def }; } const refusals = (log) => log.filter((e) => !e.ok && !HUMAN_REFUSALS.has(e.reason)); test('bots: a bot-vs-bot round runs to the end on every map, each bot sends only its own side\'s commands, and nothing a human UI would have refused', () => { for (const map of TD.MAP_ORDER) { const { sim, log } = botRound({ map, limit: 100, seed: 2 }); assert.equal(sim.state, 'over', map + ' ended'); assert.ok(sim.vs.result && (sim.vs.result.winner === 'attacker' || sim.vs.result.winner === 'defender')); assert.deepEqual(refusals(log), [], map + ': illegal commands'); for (const e of log) { assert.equal(e.role === 'attacker', e.by === 1, 'the attacker bot (seat 1) sends attacker commands only: ' + JSON.stringify(e)); assert.ok(['attacker', 'defender'].includes(e.role)); } assert.ok(log.some((e) => e.role === 'defender' && e.c === 'place' && e.ok), map + ': the defender built'); assert.ok(log.some((e) => e.role === 'attacker' && (e.c === 'spawn' || e.c === 'stage') && e.ok), map + ': the attacker bought units'); } }); test('bots: every skill level plays legally, and a weaker bot is slower to decide', () => { for (const skill of [0, 0.25, 0.6, 1]) { for (const [a, d] of [[skill, 1], [1, skill]]) { const { sim, log } = botRound({ map: 'twin', limit: 80, seed: 5, attackerSkill: a, defenderSkill: d }); assert.equal(sim.state, 'over'); assert.deepEqual(refusals(log), [], 'attacker skill ' + a + ', defender skill ' + d); } } const sim = vsim(); assert.ok(Bot.create(sim, 'defender', { skill: 0 }).interval > Bot.create(sim, 'defender', { skill: 1 }).interval); assert.ok(Bot.create(sim, 'attacker', { skill: 0 }).interval > Bot.create(sim, 'attacker', { skill: 1 }).interval); assert.throws(() => Bot.create(sim, 'spectator'), /role/); assert.throws(() => Bot.create(new Sim({ map: 'open' }), 'defender'), /versus/); }); test('bots: the same seed plays the same round, other seeds play another one (a seeded PRNG, never Math.random)', () => { const a = botRound({ map: 'open', limit: 150, seed: 7 }), b = botRound({ map: 'open', limit: 150, seed: 7 }), c = botRound({ map: 'open', limit: 150, seed: 8 }); assert.equal(JSON.stringify(a.sim.snapshot()), JSON.stringify(b.sim.snapshot())); assert.equal(JSON.stringify(a.log), JSON.stringify(b.log)); assert.notEqual(JSON.stringify(a.log), JSON.stringify(c.log)); const src = fs.readFileSync(path.join(__dirname, '..', 'public', 'js', 'bot.js'), 'utf8'); assert.equal(/Math\.random|Date\.now/.test(src.replace(/\/\*[^]*?\*\//g, '').replace(/\/\/.*$/gm, '')), false, 'no ambient randomness or clock'); assert.equal(/require\(['"](?!\.\/sim\.js)/.test(src), false, 'the only thing it loads is sim.js'); }); test('bots: each side looks only at what its own seat may see', () => { const src = fs.readFileSync(path.join(__dirname, '..', 'public', 'js', 'bot.js'), 'utf8'); const code = src.replace(/\/\*[^]*?\*\//g, '').replace(/\/\/.*$/gm, ''); const def = code.slice(code.indexOf('class Defender'), code.indexOf('class Attacker')), att = code.slice(code.indexOf('class Attacker'), code.indexOf('function create')); assert.equal(/\.vs\b/.test(def), false, 'the defender never touches the versus state (convoy, credits, tech)'); assert.equal(/privateState\(\s*'attacker'/.test(def), false); assert.equal(/vsStaged|\.rel\b/.test(def), false); assert.equal(/stealthT|\.decoy|\.earned|\.buyCost|\.crypt|\.regen|\.stunT/.test(def), false, 'nor anything the wire does not carry (stealth, decoy flag, tech)'); assert.equal(/\bm\.(armor|leak|shield|maxShield|speed)\b/.test(def), false, 'a unit\'s armour, shield, speed and leak value come from the kind table, not from the unit (tech is secret)'); assert.equal(/privateState\(\s*'defender'/.test(att), false, 'the attacker never reads the defender\'s cooldowns'); assert.deepEqual([...att.matchAll(/\.vs\.(\w+)/g)].map((m) => m[1]), ['limit'], 'the attacker reads the round limit (public) and nothing else of the versus state'); // and at run time: only ever its own role's private state const sim = vsim({ credits: 500 }); const asked = []; const ps = sim.privateState.bind(sim); sim.privateState = (r) => { asked.push(r); return ps(r); }; const a = Bot.create(sim, 'attacker', { seed: 1 }), d = Bot.create(sim, 'defender', { seed: 1 }); for (let i = 0; i < 60 * 30; i++) { asked.length = 0; d.think(STEP); const nd = asked.length; a.think(STEP); sim.tick(STEP); assert.ok(asked.slice(0, nd).every((r) => r === 'defender'), 'defender asked for ' + asked.slice(0, nd)); assert.ok(asked.slice(nd).every((r) => r === 'attacker')); } }); test('defender bot: builds at once with its start credits (cheap rapid fire first), then keeps spending as it is paid', () => { const sim = vsim({ gold: VS.DEF_GOLD }); const def = Bot.create(sim, 'defender', { seed: 3 }); def.think(STEP); const turrets = () => sim.towers.filter((t) => t.type !== 'wall'); assert.ok(turrets().length >= Math.floor(0.4 * VS.DEF_GOLD / TD.priceFor('filter', 0)), 'a first line at once: ' + turrets().length); assert.ok(sim.gold < VS.DEF_GOLD * 0.35, 'and most of the purse spent: ' + sim.gold); assert.ok(turrets().filter((t) => t.type === 'filter').length >= turrets().length / 2, 'mostly Packet Filters to open with'); assert.ok(turrets().every((t) => !sim.board.routes.some((r) => r.some((c) => c[0] === t.x && c[1] === t.y))), 'off the route'); const rich = vsim({ gold: 800 }); // (the economy is the balance pass's to change: give it plenty) const d2 = Bot.create(rich, 'defender', { seed: 3 }); for (let i = 0; i < 60 * 90; i++) { d2.think(STEP); rich.tick(STEP); } assert.ok(rich.towers.filter((t) => t.type !== 'wall').length >= 8, 'a line of turrets'); assert.ok(rich.towers.some((t) => t.level > 1) || rich.towers.filter((t) => t.type !== 'wall').length >= 12, 'and upgrades or more of them as it is paid'); assert.ok(rich.towers.filter((t) => t.type === 'wall').length > 0, 'and, once it had a line of turrets, a maze'); assert.ok(rich.board.routeLen > new TD.Board('open').routeLen, 'the maze made the way to the server longer'); assert.deepEqual(def.refused.filter((e) => !HUMAN_REFUSALS.has(e.reason)), []); }); test('defender bot: patches a lone EMPed tower, scans when several in one place are out, repairs, and quarantines a rush at the server', () => { const setup = () => { const sim = vsim({ gold: 2000, credits: 2000 }); const def = Bot.create(sim, 'defender', { seed: 1 }); def.clock = 0; return { sim, def, tick: (n) => { for (let i = 0; i < n; i++) { def.think(STEP); sim.tick(STEP); } } }; }; { // one tower out: a Patch const { sim, def, tick } = setup(); const t = filterAt(sim, 6, 4); sim.cmd(A({ c: 'ability', id: 'emp', tid: t.id })); const g = sim.gold; def.minTurretsDone = true; tick(61); assert.equal(t.empT, 0, 'patched'); assert.ok(sim.vs.dcd.patch > 0 || sim.vs.dcd.scan > 0); assert.ok(sim.gold <= g, 'it paid'); } { // two next to each other: one Scan clears both const { sim, def, tick } = setup(); const a = filterAt(sim, 6, 4), b = filterAt(sim, 7, 4); sim.cmd(A({ c: 'ability', id: 'emp', tid: a.id })); sim.vs.acd.emp = 0; sim.cmd(A({ c: 'ability', id: 'emp', tid: b.id })); tick(61); assert.ok(a.empT === 0 && b.empT === 0, 'both clear'); assert.ok(sim.vs.dcd.scan > 0 && sim.vs.dcd.patch === 0, 'with one Scan, not two Patches'); } { // integrity is down const { sim, tick } = setup(); sim.lives = sim.maxLives - VS.TOOLS.repair.amount - 1; tick(61); assert.ok(sim.lives > sim.maxLives - VS.TOOLS.repair.amount - 1 && sim.vs.dcd.repair > 0, 'repaired'); } { // a crowd about to reach the server const { sim, tick } = setup(); sim.vs.credits = 1e6; for (let i = 0; i < 10; i++) { const m = sim.vsUnit('spam', 0); sim.monsters.push(m); m.x = 22.5; m.y = 8.5; m.cx = 22; m.cy = 8; m.stunT = 1e9; } tick(61); assert.ok(sim.vs.freezeT > 0 || sim.vs.dcd.quarantine > 0, 'quarantined'); } { // stealthed units walking through its guns: a Scan const { sim, tick } = setup(); filterAt(sim, 2, 5); for (const y of [6, 7]) { const m = parked(sim, 'rootkit', 2, y); m.stealthT = 50; } tick(61); assert.ok(sim.vs.dcd.scan > 0, 'scanned'); } }); test('attacker bot: against no defence it does not wait (units that leak pay for themselves) and fells the server', () => { const sim = vsim({ limit: 200 }); const att = Bot.create(sim, 'attacker', { seed: 1 }); let first = -1; for (let i = 0; sim.state === 'playing' && i < 60 * 205; i++) { att.think(STEP); sim.tick(STEP); if (first < 0 && sim.monsters.length) first = sim.time; } assert.ok(first >= 0 && first < 3, 'units in the first seconds: ' + first); assert.equal(sim.vs.result.winner, 'attacker', 'an undefended server falls within the round'); assert.ok(sim.vs.result.t < sim.vs.limit); assert.deepEqual(att.refused, []); }); // what the attacker bot buys against a given defence, over the first two minutes (the defence is placed for free; the integrity is kept up) function boughtAgainst(types, seed) { const sim = vsim({ gold: 1e6, limit: 900, events: false }); const spots = [[8, 5], [10, 5], [12, 5], [8, 10], [10, 10], [12, 10]]; spots.forEach((p, i) => assert.ok(sim.cmd(D({ c: 'place', type: types[i % types.length], x: p[0], y: p[1] })).ok)); const att = Bot.create(sim, 'attacker', { seed }); const kinds = {}; const send = sim.cmd.bind(sim); sim.cmd = (c) => { const r = send(c); if (r.ok && (c.c === 'stage' || c.c === 'spawn')) kinds[c.kind] = (kinds[c.kind] || 0) + (c.n || 1); return r; }; for (let i = 0; i < 60 * 120; i++) { att.think(STEP); sim.tick(STEP); sim.lives = sim.maxLives; } return kinds; } const share = (kinds, pick) => { let a = 0, t = 0; for (const k of Object.keys(kinds)) { t += kinds[k]; if (pick(k)) a += kinds[k]; } return t ? a / t : 0; }; test('attacker bot: reads the tower mix - flyers when nothing can hit them, armoured units and a healer against rapid fire', () => { const armoured = (k) => ENEMIES[k].armor >= 4 || !!ENEMIES[k].shield; for (const seed of [1, 2, 3]) { const noAA = boughtAgainst(['mortar'], seed); // a Flood Mortar cannot hit flyers const rapid = boughtAgainst(['filter'], seed); // 6 damage a shot: armour eats it assert.ok(share(noAA, (k) => ENEMIES[k].air) >= 0.5, 'against mortars only it goes for flyers: ' + JSON.stringify(noAA)); assert.ok(share(noAA, (k) => ENEMIES[k].air) > share(rapid, (k) => ENEMIES[k].air), 'more flyers where nothing can hit them'); assert.ok(share(rapid, armoured) > share(noAA, armoured), 'more armour against rapid fire than against mortars: ' + JSON.stringify(rapid)); assert.ok(share(rapid, armoured) > 0, 'against filters: armoured units ' + JSON.stringify(rapid)); assert.ok((rapid.trojan || 0) === 0 || (rapid.daemon || 0) > 0, 'a healer rides with its trojans: ' + JSON.stringify(rapid)); } }); test('attacker bot: saves a convoy against a real defence, launches it, buys tech and plays abilities', () => { let tech = 0, abilities = 0, launches = 0, staged = 0, tools = 0; for (const seed of [1, 2, 3]) { const { sim, log } = botRound({ map: 'open', limit: 400, seed }); tech += sim.vs.stats.techSpent; abilities += sim.vs.stats.abilitySpent; tools += sim.vs.stats.toolSpent; launches += log.filter((e) => e.c === 'launch' && e.ok).length; staged += log.filter((e) => e.c === 'stage' && e.ok).length; assert.deepEqual(refusals(log), []); } assert.ok(staged > 0 && launches > 0, 'stages and launches: ' + staged + ' / ' + launches); assert.ok(tech > 0, 'buys tech'); assert.ok(abilities > 0, 'uses abilities'); assert.ok(tools >= 0); }); // ================================================================================================================ the runner test('tools/versus.js: a round row says who won, how fast, and what each side spent; the CLI prints the table', () => { const { playRound } = require('../tools/versus.js'); const r = playRound({ map: 'open', limit: 60, seed: 1 }); assert.ok(['attacker', 'defender'].includes(r.winner)); for (const k of ['map', 'seed', 't', 'integrityLost', 'unitsSpawned', 'creditsSpent', 'creditsEarned', 'kills', 'towers', 'refusals', 'defenderInvested']) assert.ok(k in r, k); assert.ok(r.unitsSpawned > 0 && r.creditsSpent > 0); assert.equal(r.integrityLost <= r.maxLives + 100, true); const out = require('node:child_process').execFileSync(process.execPath, [path.join(__dirname, '..', 'tools', 'versus.js'), '--maps', 'open,twin', '--limit', '40', '--seeds', '2', '--jobs', '2'], { encoding: 'utf8' }); assert.match(out, /att win/); assert.match(out, /open\s+2/); assert.match(out, /twin\s+2/); assert.match(out, /ALL MAPS\s+4/); assert.match(out, /refused commands/); });