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

@ -5,13 +5,14 @@
* ctrl.kind 'local' | 'remote'
* ctrl.board TD.Board (own for local, a mirror for remote: used for route previews)
* ctrl.view render state, refreshed by ctrl.update(nowMs):
* gold lives maxLives wave state auto nextWaveIn kills winWave rev queued speed paused
* gold lives maxLives wave state auto nextWaveIn kills winWave rev queued speed paused difficulty
* towers[{id,type,x,y,level,mode,spent,owner,tgt,kills}]
* monsters[{id,kind,x,y,hp,maxhp,slow,air}] bullets[{id,k,x,y}] cursors[{slot,x,y}]
* ctrl.send(cmd) place / sell / upgrade / target / wave / auto / continue. An upgrade carries the level the player saw (lv)
* and a wave carries the wave it expects to start (n), filled in here from the view: a second player who
* pressed the same button at the same moment is refused as 'stale' (see sim.js) instead of paying twice.
* ctrl.ctl(c, v) 'pause' | 'speed'
* ctrl.difficulty the game's difficulty id (TD.DIFFICULTIES); `newGame(map, difficulty)` keeps the current one when none is given
* ctrl.preview(x,y,routes) would a block there be legal, and what does it do to the route?
* events ctrl.on('events', list) | 'reject' | 'mark' | 'players' | 'status' | 'joined' | 'newgame' | 'fatal' | 'rtt'
*/
@ -55,18 +56,19 @@
super();
this.kind = 'local';
this.mapId = (opts && opts.map) || 'open';
this.difficulty = TD.difficultyId(opts && opts.difficulty);
this.speed = 1; this.paused = false;
this.players = [{ slot: 0, name: 'You', online: true, host: true }];
this.you = { slot: 0, host: true };
this._fresh();
}
_fresh() {
this.sim = new TD.Sim({ map: this.mapId });
this.sim = new TD.Sim({ map: this.mapId, difficulty: this.difficulty });
this.board = this.sim.board;
this.acc = 0; this.last = 0;
this.view = {
gold: 0, lives: 0, maxLives: 0, wave: 0, state: 'playing', auto: false, nextWaveIn: -1, kills: 0, winWave: 0, rev: 0, queued: 0,
speed: this.speed, paused: this.paused, time: 0,
speed: this.speed, paused: this.paused, time: 0, difficulty: this.difficulty,
towers: this.sim.towers, monsters: this.sim.monsters, bullets: this.sim.bullets, cursors: [],
};
this._sync();
@ -76,7 +78,7 @@
v.gold = s.gold; v.lives = s.lives; v.maxLives = s.maxLives; v.wave = s.wave; v.state = s.state;
v.auto = s.autoWaves; v.nextWaveIn = s.autoWaves ? s.nextWaveIn : -1; v.kills = s.kills;
v.winWave = isFinite(s.winWave) ? s.winWave : 0; v.rev = s.rev; v.queued = s.spawnQ.length;
v.speed = this.speed; v.paused = this.paused; v.time = s.time;
v.speed = this.speed; v.paused = this.paused; v.time = s.time; v.difficulty = s.difficulty;
v.towers = s.towers; v.monsters = s.monsters; v.bullets = s.bullets;
}
update(now) {
@ -102,7 +104,12 @@
this._sync();
}
preview(x, y, routes) { return this.sim.preview(x, y, routes); }
newGame(map) { if (map) this.mapId = map; this._fresh(); this.emit('newgame'); }
// `difficulty` absent: the same one again ("Play again"); an unknown one is normal (the sim's rule)
newGame(map, difficulty) {
if (map) this.mapId = map;
if (difficulty !== undefined && difficulty !== null) this.difficulty = TD.difficultyId(difficulty);
this._fresh(); this.emit('newgame');
}
sendCursor() {}
sendMark(x, y) { this.emit('mark', { slot: 0, x, y }); }
requestSync() {}
@ -118,6 +125,7 @@
this.pid = opts.pid; this.name = opts.name || 'Player';
this.room = opts.room || null; // null => create a room on first connect
this.mapId = opts.map || 'open';
this.difficulty = TD.difficultyId(opts.difficulty); // what a CREATING join asks for; afterwards whatever the server's state says
this.status = 'idle'; // idle | connecting | joined | reconnecting | closed
this.ws = null; this.players = []; this.you = null; this.board = null;
this.buf = []; this.delay = 120;
@ -132,7 +140,7 @@
this.speed = 1; this.paused = false;
this.view = {
gold: 0, lives: 0, maxLives: 0, wave: 0, state: 'playing', auto: false, nextWaveIn: -1, kills: 0, winWave: 0, rev: 0, queued: 0,
speed: 1, paused: false, time: 0, towers: [], monsters: [], bullets: [], cursors: [],
speed: 1, paused: false, time: 0, difficulty: this.difficulty, towers: [], monsters: [], bullets: [], cursors: [],
};
this._onVis = () => { if (!document.hidden) this._wake(); };
document.addEventListener('visibilitychange', this._onVis);
@ -154,7 +162,7 @@
this.ws = ws;
ws.onopen = () => {
const j = { t: 'join', pid: this.pid, name: this.name };
if (this.room) j.room = this.room; else j.map = this.mapId;
if (this.room) j.room = this.room; else { j.map = this.mapId; j.difficulty = this.difficulty; } // only a creating join carries them: a joiner takes the room's
this._send(j);
};
ws.onmessage = (e) => { this.lastMsg = performance.now(); this._onMsg(e.data); };
@ -260,7 +268,7 @@
this._die('restarted', this.everJoined ? 'The server restarted, your game was lost.' : 'That co-op game is gone: the server restarted or the room timed out.');
break;
}
const msgs = { no_room: 'That room does not exist (any more).', full: 'That room already has two players.', bad_request: 'The server did not understand the request.', too_many_rooms: 'The server is full right now. Try again in a minute.', bad_map: 'Unknown map.' };
const msgs = { no_room: 'That room does not exist (any more).', full: 'That room already has two players.', bad_request: 'The server did not understand the request.', too_many_rooms: 'The server is full right now. Try again in a minute.', bad_map: 'Unknown map.', bad_difficulty: 'Unknown difficulty.' };
this._die(m.code || 'error', msgs[m.code] || m.msg || 'Server error.');
break;
}
@ -282,6 +290,8 @@
this._setTowers(state.towers);
this.rev = this.recvRev = state.rev;
this.maxLives = state.maxLives; this.winWave = state.winWave;
this.difficulty = TD.isDifficulty(state.difficulty) ? state.difficulty : 'normal'; // (a server from before difficulties sends none)
this.view.difficulty = this.difficulty;
this.buf.length = 0; this._carryTw = null; this.monCache.clear(); this.bulCache.clear();
if (snap) this._push(snap, true);
}
@ -384,7 +394,7 @@
for (const m of this.view.monsters) if (!m.air) reserved.add(Math.floor(m.y) * this.board.cols + Math.floor(m.x));
return this.board.trial(x, y, reserved, null, routes);
}
newGame(map) { this._send({ t: 'newgame', map }); }
newGame(map, difficulty) { this._send({ t: 'newgame', map, difficulty }); } // (JSON drops an undefined difficulty: the server keeps the room's)
// Moves are throttled to one per 70 ms. A clear (x === null) or `force` always goes out, so the final position / the
// "cursor gone" message of a touch is never swallowed by the throttle (touch-9).
sendCursor(x, y, force) {