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
This commit is contained in:
Claude 2026-10-10 23:41:21 +00:00
parent 28502984ec
commit b4ae6cb57d
19 changed files with 1348 additions and 156 deletions

View file

@ -21,6 +21,29 @@
const START_LIVES = 20;
const SELL_RATIO = 0.7;
const WIN_WAVE = 30;
// Clearing wave n pays CLEAR_BONUS + CLEAR_BONUS_PER_WAVE * n credits on top of the kills (the same on every difficulty).
const CLEAR_BONUS = 10;
const CLEAR_BONUS_PER_WAVE = 1;
function clearBonus(n) { return CLEAR_BONUS + CLEAR_BONUS_PER_WAVE * n; }
// ---------------------------------------------------------------- difficulty
// One row per difficulty; every number is a multiplier on the base tables below (normal is all 1, so it plays exactly the tuned base game):
// gold the starting credits (START_GOLD) lives the starting integrity (START_LIVES), at least 1
// hp every enemy's hit points AND its shield reward what a kill pays (a kill always pays at least 1)
// The wave clear bonus and the tower prices do not change. Nothing outside this table may hard-code a number from it: tests, the
// UI and the balance bot read the table. The ids travel on the wire (create-room option, newgame, fullState), so only ever APPEND.
// (Not to be confused with the `difficulty` 1-3 of a MAP below: that is the layout's own rating, drawn as dots on the map cards.)
const DIFFICULTY_ORDER = ['easy', 'normal', 'hard'];
const DEFAULT_DIFFICULTY = 'normal';
const DIFFICULTIES = {
easy: { id: 'easy', name: 'Easy', gold: 1.5, lives: 1.5, hp: 0.8, reward: 1.1 },
normal: { id: 'normal', name: 'Normal', gold: 1, lives: 1, hp: 1, reward: 1 },
hard: { id: 'hard', name: 'Hard', gold: 0.9, lives: 0.85, hp: 1.08, reward: 0.97 },
};
// Is `v` the id of a difficulty? (own keys only: 'toString' and '__proto__' are not difficulties)
function isDifficulty(v) { return typeof v === 'string' && Object.prototype.hasOwnProperty.call(DIFFICULTIES, v); }
// `v` if it is a difficulty, otherwise normal (what the Sim does with an unknown value; the server rejects those instead).
function difficultyId(v) { return isDifficulty(v) ? v : DEFAULT_DIFFICULTY; }
// ---------------------------------------------------------------- maps
// '.' open, '#' dead node (permanent obstacle), 'S' spawn (internet gateway), 'C' core (the server)
@ -208,6 +231,7 @@
// Hotkeys: one unique key 1-9 per tower, in palette order.
const TOWER_ORDER = ['wall', 'filter', 'ids', 'mortar', 'throttle', 'arc', 'sandbox', 'balancer', 'uplink'];
const TARGET_MODES = ['first', 'last', 'strong', 'close'];
const UPLINK_GROWTH = 1.3; // each Orbital Uplink you own makes the next one (and its upgrades) this much dearer; was 1.25
const TOWERS = {
wall: {
name: 'Firewall', key: '1', color: '#5aa9ff', refund: 1, role: 'wall',
@ -218,28 +242,28 @@
name: 'Packet Filter', key: '2', color: '#35e0a1', role: 'attack', air: true, ground: true,
desc: 'Fast, cheap rapid fire. Weak against armour.',
levels: [
{ cost: 25, dmg: 6, range: 3.2, rate: 3.0 },
{ cost: 30, dmg: 9, range: 3.5, rate: 3.4 },
{ cost: 55, dmg: 13, range: 3.9, rate: 4.0 },
{ cost: 25, dmg: 6, range: 3.5, rate: 3.0 },
{ cost: 25, dmg: 9, range: 3.8, rate: 3.4 },
{ cost: 45, dmg: 13, range: 4.2, rate: 4.0 },
],
bulletSpeed: 14,
},
ids: {
name: 'IDS Sniper', key: '3', color: '#b78cff', role: 'attack', air: true, ground: true,
desc: 'Long range, hard hitting, slow. Great against armour and bosses.',
desc: 'Long range, hard hitting, slow. Great against armour and bosses, poor against swarms.',
levels: [
{ cost: 60, dmg: 50, range: 6.5, rate: 0.7 },
{ cost: 70, dmg: 85, range: 7.2, rate: 0.75 },
{ cost: 110, dmg: 150, range: 8.0, rate: 0.8 },
{ cost: 70, dmg: 83, range: 6.2, rate: 0.42 },
{ cost: 80, dmg: 143, range: 6.8, rate: 0.445 },
{ cost: 130, dmg: 250, range: 7.5, rate: 0.48 },
],
},
mortar: {
name: 'Flood Mortar', key: '4', color: '#d4f04a', role: 'attack', air: false, ground: true,
desc: 'Splash damage against ground swarms. Cannot hit flyers.',
levels: [
{ cost: 55, dmg: 28, range: 4.8, rate: 0.8, splash: 1.3 },
{ cost: 60, dmg: 45, range: 5.2, rate: 0.85, splash: 1.5 },
{ cost: 100, dmg: 75, range: 5.6, rate: 0.9, splash: 1.7 },
{ cost: 55, dmg: 31, range: 4.9, rate: 0.8, splash: 1.3 },
{ cost: 60, dmg: 50, range: 5.3, rate: 0.85, splash: 1.5 },
{ cost: 95, dmg: 82, range: 5.7, rate: 0.9, splash: 1.7 },
],
shellSpeed: 5.5,
},
@ -256,9 +280,9 @@
name: 'Router Arc', key: '6', color: '#ff7ad9', role: 'attack', air: true, ground: true,
desc: 'Chain lightning that jumps between nearby packets.',
levels: [
{ cost: 75, dmg: 16, range: 3.6, rate: 1.2, chains: 2, chainRange: 1.9, falloff: 0.7 },
{ cost: 70, dmg: 24, range: 3.8, rate: 1.25, chains: 3, chainRange: 2.0, falloff: 0.72 },
{ cost: 120, dmg: 36, range: 4.2, rate: 1.4, chains: 4, chainRange: 2.2, falloff: 0.75 },
{ cost: 70, dmg: 17, range: 3.8, rate: 1.2, chains: 2, chainRange: 1.9, falloff: 0.7 },
{ cost: 60, dmg: 26, range: 4.0, rate: 1.25, chains: 3, chainRange: 2.0, falloff: 0.72 },
{ cost: 105, dmg: 38, range: 4.4, rate: 1.4, chains: 4, chainRange: 2.2, falloff: 0.75 },
],
},
sandbox: {
@ -281,8 +305,8 @@
},
uplink: {
name: 'Orbital Uplink', key: '9', color: '#ffe27a', role: 'attack', air: true, ground: true,
desc: 'Endgame weapon. A piercing orbital beam that ignores armour, scorches everything in its line, blasts and slows the rest around the target, and reaches across most of the map. Extremely expensive to build and to upgrade, and every Uplink you already own makes the next one 25% dearer, and its upgrades too.',
priceGrowth: 1.25, // the Nth Uplink costs cost * 1.25^(N-1) to buy AND to upgrade (see priceMult), so kill rewards cannot buy a carpet of them
desc: 'Endgame weapon. A piercing orbital beam that ignores armour, scorches everything in its line, blasts and slows the rest around the target, and reaches across most of the map. Extremely expensive to build and to upgrade, and every Uplink you already own makes the next one ' + Math.round((UPLINK_GROWTH - 1) * 100) + '% dearer, and its upgrades too.',
priceGrowth: UPLINK_GROWTH, // the Nth Uplink costs cost * UPLINK_GROWTH^(N-1) to buy AND to upgrade (see priceMult), so kill rewards cannot buy a carpet of them
levels: [
{ cost: 1500, dmg: 260, range: 11, rate: 0.45, width: 0.5, splash: 1.3, slowOnHit: 0.4, slowFor: 1.5 },
{ cost: 1200, dmg: 400, range: 12, rate: 0.5, width: 0.55, splash: 1.5, slowOnHit: 0.45, slowFor: 1.75 },
@ -324,16 +348,16 @@
const ENEMIES = {
ping: { name: 'Ping', hp: 30, speed: 1.5, armor: 0, reward: 3, leak: 1, air: false, r: 0.26, color: '#ff5c7a', buyCost: 10 },
worm: { name: 'Worm', hp: 18, speed: 2.8, armor: 0, reward: 2, leak: 1, air: false, r: 0.22, color: '#ff9f43', buyCost: 6 },
trojan: { name: 'Trojan', hp: 140, speed: 0.85, armor: 3, reward: 9, leak: 2, air: false, r: 0.38, color: '#c8506b', buyCost: 30 },
trojan: { name: 'Trojan', hp: 140, speed: 0.85, armor: 4, reward: 9, leak: 2, air: false, r: 0.38, color: '#c8506b', buyCost: 30 },
spam: { name: 'Spam', hp: 7, speed: 2.0, armor: 0, reward: 1, leak: 1, air: false, r: 0.16, color: '#ffd166', buyCost: 3 },
ghost: { name: 'Spoof', hp: 40, speed: 1.6, armor: 0, reward: 5, leak: 1, air: true, r: 0.28, color: '#e29bff', buyCost: 15 },
rootkit: { name: 'Rootkit', hp: 1400, speed: 0.65, armor: 5, reward: 100, leak: 5, air: false, r: 0.55, color: '#ff3b3b', buyCost: 300, slowRes: 0.5 },
ghost: { name: 'Spoof', hp: 40, speed: 1.3, armor: 0, reward: 5, leak: 1, air: true, r: 0.28, color: '#e29bff', buyCost: 15 },
rootkit: { name: 'Rootkit', hp: 1400, speed: 0.65, armor: 7, reward: 60, leak: 5, air: false, r: 0.55, color: '#ff3b3b', buyCost: 180, slowRes: 0.5 },
// Healer: every HEAL_EVERY s it restores 'heal' of the max hp of every other enemy within healRange (several daemons stack up to HEAL_CAP).
daemon: { name: 'Daemon', hp: 70, speed: 1.1, armor: 0, reward: 8, leak: 1, air: false, r: 0.3, color: '#7ee08a', buyCost: 25, heal: 0.06, healRange: 2.2 },
// Shielded: 'shield' extra hit points (scaled like hp) that soak damage first and regrow shieldRegen/s of their max after shieldDelay s unhurt.
proxy: { name: 'Proxy', hp: 90, speed: 1.0, armor: 1, reward: 10, leak: 1, air: false, r: 0.34, color: '#7aa2ff', buyCost: 30, shield: 120, shieldRegen: 0.1, shieldDelay: 3 },
// Swarm flyer.
botnet: { name: 'Botnet', hp: 12, speed: 2.6, armor: 0, reward: 1, leak: 1, air: true, r: 0.14, color: '#4dd0c8', buyCost: 3 },
botnet: { name: 'Botnet', hp: 12, speed: 2.2, armor: 0, reward: 1, leak: 1, air: true, r: 0.14, color: '#4dd0c8', buyCost: 3 },
};
const HEAL_EVERY = 1; // seconds between daemon pulses (one global clock, so the 12%/s cap is exact)
const HEAL_CAP = 0.12; // most max-hp fraction one enemy can be healed per pulse, however many daemons are near
@ -391,8 +415,10 @@
}
return out;
}
function hpScale(n) { return 1 + 0.16 * (n - 1) + 0.022 * (n - 1) * (n - 1); }
function rewardScale(n) { return 1 + 0.03 * (n - 1); }
// Tuned numbers (docs/BALANCE.md): enemy hit points per wave and what a kill pays. Kill credits grow slower than the packets' hit points,
// so credits stay a constraint all game; bosses pay little per hit point (a rootkit is a danger, not a payday).
function hpScale(n) { return 1 + 0.145 * (n - 1) + 0.0165 * (n - 1) * (n - 1); }
function rewardScale(n) { return 1 + 0.02 * (n - 1); }
// ---------------------------------------------------------------- path finding
class MinHeap {
@ -561,8 +587,11 @@
this.board = new Board(opts.map || 'open');
this.mapId = this.board.mapId;
this.winWave = opts.winWave === undefined ? WIN_WAVE : opts.winWave;
this.gold = opts.gold === undefined ? START_GOLD : opts.gold;
this.lives = opts.lives === undefined ? START_LIVES : opts.lives;
// An explicit `gold` / `lives` (tests, the bot) is used exactly as given; the difficulty scales only the defaults.
this.difficulty = difficultyId(opts.difficulty);
this.diff = DIFFICULTIES[this.difficulty];
this.gold = opts.gold === undefined ? Math.round(START_GOLD * this.diff.gold) : opts.gold;
this.lives = opts.lives === undefined ? Math.max(1, Math.round(START_LIVES * this.diff.lives)) : opts.lives;
this.maxLives = this.lives;
this.collectEvents = opts.events !== false;
@ -740,13 +769,14 @@
const def = ENEMIES[q.k];
const b = this.board;
const s = b.spawns[this.spawnRR++ % b.spawns.length];
const hp = Math.round(def.hp * hpScale(q.wave));
const shield = def.shield ? Math.round(def.shield * hpScale(q.wave)) : 0;
const dm = this.diff; // the difficulty scales hit points and shield alike (the wire derives the shield bar from their ratio)
const hp = Math.max(1, Math.round(def.hp * hpScale(q.wave) * dm.hp));
const shield = def.shield ? Math.max(1, Math.round(def.shield * hpScale(q.wave) * dm.hp)) : 0;
const m = {
id: this.nid++, kind: q.k, air: !!def.air,
x: s.x + 0.5, y: s.y + 0.5, cx: s.x, cy: s.y, tx: -1, ty: -1,
hp, maxhp: hp, armor: def.armor, speed: def.speed,
reward: Math.max(1, Math.round(def.reward * rewardScale(q.wave))),
reward: Math.max(1, Math.round(def.reward * rewardScale(q.wave) * dm.reward)),
leak: def.leak, slow: 0, slowT: 0, slowAmt: 0, stunT: 0,
shield, maxShield: shield, shieldT: 0,
rem: 0, dead: false, wave: q.wave,
@ -793,7 +823,7 @@
return;
}
if (this.wave > this.bonusWave && !this.monsters.length && !this.spawnQ.length) {
const bonus = 10 + 2 * this.wave;
const bonus = clearBonus(this.wave);
this.gold += bonus;
this.bonusWave = this.wave;
this.emit({ e: 'clear', n: this.wave, bonus });
@ -1094,6 +1124,7 @@
map: this.mapId, rev: this.rev, towers: this.towerList(),
winWave: this.winWave === Infinity ? 0 : this.winWave,
maxLives: this.maxLives,
difficulty: this.difficulty,
};
}
@ -1117,8 +1148,9 @@
return {
STEP, WAVE_INTERVAL, START_GOLD, START_LIVES, SELL_RATIO, WIN_WAVE, HEAL_EVERY, HEAL_CAP,
DIFFICULTIES, DIFFICULTY_ORDER, DEFAULT_DIFFICULTY, isDifficulty, difficultyId,
MAPS, MAP_ORDER, TOWERS, TOWER_ORDER, TARGET_MODES, ENEMIES, ENEMY_ORDER,
priceFor, priceMult, upgradePrice, waveGroups, waveSummary, hpScale, rewardScale,
priceFor, priceMult, upgradePrice, waveGroups, waveSummary, hpScale, rewardScale, clearBonus,
buildField, nextCell, traceRoute, parseMap, Board, Sim, round2,
};
});