towerdef/test/balance.test.js
Claude b4ae6cb57d Difficulty picker (Easy/Normal/Hard) and a balance pass
Difficulty:
- TD.DIFFICULTIES (starting credits, lives, enemy hp/shield, kill reward), validated
  create-room / newgame option, state carries it; solo menu picker and co-op lobby
  picker (44px segmented control, reachable on every layout), HUD/room/end-screen
  labels, best-wave record per map AND difficulty (Normal keeps the legacy key),
  Play again keeps the running game's difficulty for host and guest

Balance (12 bot runs, deterministic, 48/48 games replay identically in QA):
- Normal ~71% bot wins at wave 30 (was 94%), Easy 100%, Hard 25% on open
- credits tamed: reward growth 3% -> 2% per wave, clear bonus 10+2n -> 10+n, hp scaling
  eased, Rootkit pays 60 (was 100) but is tougher, flyers slower; unspent credits at
  wave 30: 1309 -> 335
- IDS Sniper is now a slow heavy hitter (essential, no longer sufficient: IDS-only
  dies by wave 7); Mortar, Arc and Filter buffed
- Orbital Uplink price step per owned unit 30% (buy and upgrade)
- remaining imbalances documented in docs/BALANCE.md

Tests: difficulty (19) and balance (8) unit suites, 14-difficulty e2e, layout matrix
reachability of the picker. 156 unit tests, 17 e2e scenarios.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Tb8pW2KAN4oFkh1aK1BfD8
2026-10-10 23:41:21 +00:00

112 lines
6.6 KiB
JavaScript

'use strict';
/*
* Design invariants of the tuned numbers (docs/BALANCE.md). These do NOT pin tuned values: every expectation is derived from the
* TOWERS / ENEMIES / DIFFICULTIES tables and the wave functions, and states a relationship the balance pass relies on. When a future
* pass retunes a number and one of these fails, either the pass broke a design rule (fix the number) or the rule moved (change it here
* and in docs/BALANCE.md together).
*/
const test = require('node:test');
const assert = require('node:assert/strict');
const TD = require('../public/js/sim.js');
const { TOWERS, ENEMIES, Sim, STEP } = TD;
// Everything a defence is paid for clearing waves 1..n on a difficulty (the opening credits + every kill + every clear bonus).
function income(n, difficulty) {
const dm = TD.DIFFICULTIES[difficulty || 'normal'];
let c = Math.round(TD.START_GOLD * dm.gold);
for (let w = 1; w <= n; w++) {
for (const g of TD.waveGroups(w)) c += g.c * Math.max(1, Math.round(ENEMIES[g.k].reward * TD.rewardScale(w) * dm.reward));
c += TD.clearBonus(w);
}
return c;
}
const dps = (lv) => lv.dmg * lv.rate;
test('the clear bonus is what the sim pays, and the same on every difficulty', () => {
for (const id of TD.DIFFICULTY_ORDER) {
const s = new Sim({ map: 'open', gold: 0, difficulty: id, winWave: 99 });
for (let w = 1; w <= 3; w++) {
s.startWave(); s.spawnQ.length = 0; s.monsters.length = 0;
const g = s.gold; s.tick(STEP);
assert.equal(s.gold - g, TD.clearBonus(w), id + ' wave ' + w + ' pays clearBonus(' + w + ')');
}
}
for (let w = 2; w <= 60; w++) assert.ok(TD.clearBonus(w) >= TD.clearBonus(w - 1), 'the bonus never shrinks');
});
test('economy: the credits a whole run pays are bounded, so they stay a constraint, and not starved either', () => {
const total = income(TD.WIN_WAVE) / TD.START_GOLD;
assert.ok(total <= 46, 'clearing 30 waves pays at most about 46 opening purses (the untuned game paid 55): ' + total.toFixed(1));
assert.ok(total >= 25, 'and at least 25, or credits starve');
// the snowball: late waves may pay more per wave, but the last ten must not pay more than the first twenty put together
const last10 = income(TD.WIN_WAVE) - income(TD.WIN_WAVE - 10), first20 = income(20) - income(0);
assert.ok(last10 <= first20 * 1.5, 'waves 21-30 pay ' + last10 + ' against ' + first20 + ' for waves 1-20');
// a harder game pays less, an easier one more, wave for wave
for (let n = 5; n <= 30; n += 5) assert.ok(income(n, 'easy') > income(n) && income(n) > income(n, 'hard'), 'income order at wave ' + n);
});
test('bosses are dangerous but are not a payday', () => {
const boss = ENEMIES.rootkit;
for (const k of TD.ENEMY_ORDER) {
if (k === 'rootkit') continue;
const e = ENEMIES[k];
assert.ok(boss.reward / boss.hp <= e.reward / (e.hp + (e.shield || 0)) + 1e-9, 'a rootkit pays no better per hit point than a ' + k);
}
for (let w = 10; w <= 60; w += 10) {
let bosses = 0;
for (const g of TD.waveGroups(w)) if (g.k === 'rootkit') bosses += g.c * Math.max(1, Math.round(ENEMIES.rootkit.reward * TD.rewardScale(w)));
assert.ok(bosses > 0, 'wave ' + w + ' has bosses');
assert.ok(bosses <= 0.09 * income(w), 'the bosses of wave ' + w + ' pay ' + bosses + ' credits, ' + (100 * bosses / income(w)).toFixed(1) + '% of everything earned so far (the untuned game: 12%)');
}
});
test('upgrades are a fair deal next to buying another tower, and a real sink', () => {
for (const k of ['filter', 'ids', 'mortar', 'arc']) {
const L = TOWERS[k].levels, base = dps(L[0]) / L[0].cost;
for (let i = 1; i < L.length; i++) {
const per = (dps(L[i]) - dps(L[i - 1])) / L[i].cost;
assert.ok(per >= 0.5 * base, k + ' L' + (i + 1) + ': an upgrade gives at least half the dps per credit of a new L1 (' + (per / base).toFixed(2) + 'x)');
assert.ok(per <= 1.3 * base, k + ' L' + (i + 1) + ': and is not so cheap that nobody buys a second tower (' + (per / base).toFixed(2) + 'x)');
}
}
// a fully upgraded tower costs clearly more than its first level: upgrading is where late credits go
for (const k of TD.TOWER_ORDER) {
if (k === 'wall') continue;
const L = TOWERS[k].levels;
assert.ok(L.reduce((a, l) => a + l.cost, 0) >= 2 * L[0].cost, k + ' upgrades add at least as much as the tower itself');
}
});
test('roles: the IDS Sniper is the heavy slow shot, so swarms are for the other towers', () => {
const ids = TOWERS.ids.levels;
for (const k of ['filter', 'mortar', 'arc']) {
TOWERS[k].levels.forEach((lv, i) => {
assert.ok(ids[i].dmg > lv.dmg, 'IDS L' + (i + 1) + ' hits harder than ' + k + ' L' + (i + 1));
assert.ok(ids[i].rate < lv.rate, 'and fires slower, so a swarm of 1-hit packets is not its job (' + k + ')');
});
}
const reach = Math.max(...['filter', 'mortar', 'arc'].map((k) => Math.max(...TOWERS[k].levels.map((l) => l.range))));
assert.ok(ids[0].range > reach * 0.9, 'it out-ranges the other attack towers (a level 1 IDS against their best)');
});
test('flyers: the botnet drone is still the fast swarm and the Spoof the slower, tougher flyer', () => {
assert.ok(ENEMIES.botnet.speed > ENEMIES.ghost.speed, 'a botnet drone is faster than a Spoof');
assert.ok(ENEMIES.botnet.hp < ENEMIES.ghost.hp, 'and has far fewer hit points');
for (const k of TD.ENEMY_ORDER) if (ENEMIES[k].air) assert.ok(ENEMIES[k].speed >= ENEMIES.rootkit.speed, k + ' flies at least as fast as the slowest boss walks');
});
test('the Orbital Uplink stays a luxury: about a quarter of what a run pays, never a carpet of them', () => {
const first = TD.priceFor('uplink', 0), total = income(TD.WIN_WAVE);
assert.ok(first >= 0.2 * total && first <= 0.4 * total, 'the first Uplink costs ' + (100 * first / total).toFixed(0) + '% of the 30-wave economy');
let three = 0; for (let n = 0; n < 4; n++) three += TD.priceFor('uplink', n);
assert.ok(three > total, 'four Uplinks cost more than a whole 30-wave run pays');
});
test('difficulty rows are a ladder of the whole game, not only of the first minute', () => {
const E = TD.DIFFICULTIES.easy, H = TD.DIFFICULTIES.hard;
const power = (d) => d.hp > 0 ? d.reward / d.hp : 0; // credits earned per hit point of enemy, the long-run strength of a defence
assert.ok(power(E) / power(TD.DIFFICULTIES.normal) >= 1.2, 'easy is clearly easier than normal');
assert.ok(power(TD.DIFFICULTIES.normal) / power(H) >= 1.05, 'hard is clearly harder than normal');
assert.ok(power(E) / power(H) <= 2.0, 'but the two ends are not a different game');
assert.ok(Math.round(TD.START_LIVES * H.lives) >= 10, 'hard still leaves room for a couple of leaks');
});