'use strict'; const test = require('node:test'); const assert = require('node:assert/strict'); const TD = require('../public/js/sim.js'); const { Sim, Board, MAPS, TOWERS, ENEMIES, STEP } = TD; function run(sim, seconds) { const n = Math.round(seconds / STEP); for (let i = 0; i < n && sim.state === 'playing'; i++) sim.tick(STEP); } // Tiny hand-made maps for edge cases. function mini(rows) { return { id: 'mini', name: 'mini', rows }; } test('all built-in maps parse and have a route from every spawn to the core', () => { for (const id of TD.MAP_ORDER) { const b = new Board(id); assert.ok(b.cols > 0 && b.rows > 0); for (const s of b.spawns) assert.ok(isFinite(b.field[b.idx(s.x, s.y)]), id + ' spawn unreachable'); assert.ok(b.routes.every((r) => r.length > 1)); assert.equal(MAPS[id].rows.length, b.rows); } }); test('placement validation: bounds, special cells, obstacles, occupancy, gold', () => { const s = new Sim({ map: 'open', gold: 100 }); const b = s.board; assert.equal(s.place('wall', -1, 0).reason, 'bounds'); assert.equal(s.place('wall', b.cols, 0).reason, 'bounds'); assert.equal(s.place('wall', 1.5, 2).reason, 'bounds'); assert.equal(s.place('wall', NaN, 2).reason, 'bounds'); assert.equal(s.place('wall', b.spawns[0].x, b.spawns[0].y).reason, 'spawn'); assert.equal(s.place('wall', b.core.x, b.core.y).reason, 'core'); assert.equal(s.place('wall', 4, 2).reason, 'obstacle'); assert.equal(s.place('nope', 5, 5).reason, 'bad_type'); assert.equal(s.place('__proto__', 5, 5).reason, 'bad_type'); assert.equal(s.place('constructor', 5, 5).reason, 'bad_type'); assert.equal(s.place({}, 5, 5).reason, 'bad_type'); assert.ok(s.place('wall', 5, 5).ok); assert.equal(s.place('wall', 5, 5).reason, 'occupied'); assert.equal(s.gold, 100 - TOWERS.wall.levels[0].cost); s.gold = TOWERS.wall.levels[0].cost - 1; assert.equal(s.place('wall', 6, 5).reason, 'gold'); assert.equal(s.place('ids', 6, 5).reason, 'gold'); }); test('a placement that would seal the core off is rejected (corner cannot be cut)', () => { // core in the corner of an open 5x5 room. Its only neighbours are (3,4), (4,3) and (3,3); the diagonal step // (3,3)->(4,4) needs both (3,4) and (4,3) free, so walls on those two already seal it. const s = new Sim({ map: mini(['S....', '.....', '.....', '.....', '....C']), gold: 1000 }); assert.ok(s.place('wall', 3, 4).ok); const r = s.place('wall', 4, 3); assert.equal(r.ok, false); assert.equal(r.reason, 'blocks'); assert.equal(s.towers.length, 1); }); test('diagonal corner cutting is impossible: a diagonal staircase of walls is a seal', () => { const s = new Sim({ map: mini(['S....', '.....', '.....', '.....', '....C']), gold: 1000 }); // staircase from (0,3) to (3,0) separates S (0,0) from C (4,4) once the last step is placed assert.ok(s.place('wall', 0, 3).ok); assert.ok(s.place('wall', 1, 2).ok); assert.ok(s.place('wall', 2, 1).ok); const r = s.place('wall', 3, 0); assert.equal(r.ok, false, 'last step would seal the spawn off'); assert.equal(r.reason, 'blocks'); }); test('sealing the spawn behind a diagonal staircase is refused at the final cell', () => { const s = new Sim({ map: mini(['S....', '.....', '.....', '.....', '....C']), gold: 1000 }); // S at (0,0). Seal with walls at (1,0),(0,1) and (1,1) is not needed: diagonal (0,0)->(1,1) needs both (1,0),(0,1) free. assert.ok(s.place('wall', 1, 0).ok); const r = s.place('wall', 0, 1); assert.equal(r.ok, false); assert.equal(r.reason, 'blocks'); }); test('route length grows when a wall forces a detour, and sells restore it', () => { const s = new Sim({ map: 'open', gold: 1000 }); const before = s.board.routeLen; // wall off a vertical line, leaving a gap at the bottom for (let y = 0; y < 14; y++) assert.ok(s.place('wall', 12, y).ok, 'wall ' + y); assert.ok(s.board.routeLen > before + 3, 'route should lengthen: ' + before + ' -> ' + s.board.routeLen); // closing the last gap must be refused (gap rows 14 and 15) assert.ok(s.place('wall', 12, 14).ok); assert.equal(s.place('wall', 12, 15).reason, 'blocks'); const before2 = s.board.routeLen; const wall0 = s.towerAt[7 * s.board.cols + 12]; // the wall straight in front of the spawn s.sell(wall0.id); assert.ok(s.board.routeLen < before2, 'selling a wall shortens the route'); for (const t of s.towers.slice()) s.sell(t.id); assert.equal(s.board.routeLen, before); assert.equal(s.gold, 1000, 'walls refund 100%'); }); test('preview reports the route delta and the new routes without mutating state', () => { const s = new Sim({ map: 'open', gold: 100 }); const solidBefore = Uint8Array.from(s.board.solid); const p = s.preview(12, 7, true); assert.ok(p.ok); assert.ok(p.after >= p.before); assert.ok(Array.isArray(p.routes) && p.routes.length === 1); assert.deepEqual(Array.from(s.board.solid), Array.from(solidBefore)); assert.equal(s.board.routeLen, p.before); }); test('monsters walk to the core and cost lives; flyers go straight', () => { const s = new Sim({ map: 'open', gold: 0, lives: 20 }); s.spawnQ.push({ t: 0, k: 'ping', wave: 1 }); s.spawnQ.push({ t: 0, k: 'ghost', wave: 1 }); s.wave = 1; run(s, 40); assert.equal(s.lives, 20 - ENEMIES.ping.leak - ENEMIES.ghost.leak, 'ping + ghost leaked'); assert.equal(s.monsters.length, 0); }); test('monster speed: a ping covers roughly route length / speed seconds', () => { const s = new Sim({ map: 'open', gold: 0 }); s.wave = 1; s.spawnQ.push({ t: 0, k: 'ping', wave: 1 }); const expected = s.board.routeLen / ENEMIES.ping.speed; let t = 0; while (s.lives === 20 && t < 120) { s.tick(STEP); t += STEP; } assert.ok(Math.abs(t - expected) < 1.0, `took ${t.toFixed(2)}s expected ~${expected.toFixed(2)}s`); }); test('cells monsters occupy cannot be built on, and they never get trapped', () => { const s = new Sim({ map: 'open', gold: 5000 }); s.wave = 1; for (let i = 0; i < 6; i++) s.spawnQ.push({ t: i * 0.6, k: 'ping', wave: 1 }); let rejectedMonster = 0; const rnd = (() => { let a = 12345; return () => (a = (a * 1664525 + 1013904223) >>> 0) / 4294967296; })(); for (let step = 0; step < 60 * 40 && s.state === 'playing'; step++) { s.tick(STEP); if (step % 5 === 0) { const x = Math.floor(rnd() * s.board.cols), y = Math.floor(rnd() * s.board.rows); const r = s.place('wall', x, y); if (r.reason === 'monster') rejectedMonster++; } // invariant: every living ground monster can always reach the core for (const m of s.monsters) { assert.ok(isFinite(s.board.field[m.cy * s.board.cols + m.cx]), 'monster stranded at ' + m.cx + ',' + m.cy); assert.equal(s.board.solid[m.cy * s.board.cols + m.cx], 0, 'monster inside a wall'); if (m.tx >= 0) assert.equal(s.board.solid[m.ty * s.board.cols + m.tx], 0, 'monster target is a wall'); } } assert.ok(s.towers.length > 20, 'random walls were placed'); assert.ok(rejectedMonster > 0, 'some placements were refused because a monster was on that cell'); }); test('hammering random walls never lets a spawn be sealed', () => { for (const id of TD.MAP_ORDER) { const s = new Sim({ map: id, gold: 1e6 }); let seed = 7; const rnd = () => (seed = (seed * 1103515245 + 12345) >>> 0) / 4294967296; for (let i = 0; i < 600; i++) { s.place('wall', Math.floor(rnd() * s.board.cols), Math.floor(rnd() * s.board.rows)); for (const sp of s.board.spawns) assert.ok(isFinite(s.board.field[sp.y * s.board.cols + sp.x]), id + ' sealed'); } assert.ok(s.board.routeLen > 0); } }); test('selling and upgrading', () => { const s = new Sim({ map: 'open', gold: 1000 }); const r = s.place('filter', 10, 3); assert.ok(r.ok); const t = s.towerById(r.id); assert.equal(t.level, 1); assert.ok(s.upgrade(t.id).ok); assert.ok(s.upgrade(t.id).ok); assert.equal(s.upgrade(t.id).reason, 'max'); assert.equal(t.level, 3); const lv = TOWERS.filter.levels; assert.equal(lv.length, 3); assert.equal(t.spent, lv[0].cost + lv[1].cost + lv[2].cost); const g = s.gold; const sold = s.sell(t.id); assert.equal(sold.refund, Math.floor(t.spent * TD.SELL_RATIO)); assert.equal(s.gold, g + sold.refund); assert.equal(s.towers.length, 0); assert.equal(s.sell(t.id).reason, 'no_tower'); assert.equal(s.board.solid[3 * s.board.cols + 10], 0); }); test('targeting modes pick the intended monster', () => { const s = new Sim({ map: 'open', gold: 1000 }); const r = s.place('ids', 12, 6); const t = s.towerById(r.id); const def = TOWERS.ids; const mk = (id, x, y, hp, rem) => ({ id, kind: 'ping', air: false, x, y, cx: 0, cy: 0, tx: -1, ty: -1, hp, maxhp: hp, armor: 0, speed: 1, slow: 0, rem, dead: false }); s.monsters = [mk(101, 10, 7, 10, 20), mk(102, 13, 7, 99, 5), mk(103, 12.6, 6.6, 50, 12)]; const range = def.levels[0].range; t.mode = 'first'; assert.equal(s.findTarget(t, range, def).id, 102); t.mode = 'last'; assert.equal(s.findTarget(t, range, def).id, 101); t.mode = 'strong'; assert.equal(s.findTarget(t, range, def).id, 102); t.mode = 'close'; assert.equal(s.findTarget(t, range, def).id, 103); assert.ok(s.setTargetMode(t.id, 'close').ok); assert.equal(s.setTargetMode(t.id, 'bogus').reason, 'bad_mode'); }); test('towers kill monsters and pay out; mortar cannot hit flyers', () => { const s = new Sim({ map: 'open', gold: 1000 }); const route = s.board.routes[0]; const mid = route[8]; // put a few filters beside the route for (const [dx, dy] of [[0, 1], [1, 1], [0, -1], [1, -1]]) s.place('filter', mid[0] + dx, mid[1] + dy); const g0 = s.gold; s.wave = 1; for (let i = 0; i < 5; i++) s.spawnQ.push({ t: i * 0.5, k: 'ping', wave: 1 }); run(s, 30); assert.ok(s.kills >= 4, 'kills=' + s.kills); assert.ok(s.gold > g0); const s2 = new Sim({ map: 'open', gold: 1000 }); const mid2 = s2.board.routes[0][8]; s2.place('mortar', mid2[0], mid2[1] + 1); s2.wave = 1; s2.spawnQ.push({ t: 0, k: 'ghost', wave: 1 }); run(s2, 20); assert.equal(s2.kills, 0); assert.equal(s2.lives, 20 - ENEMIES.ghost.leak); }); test('throttle slows monsters; rootkit is partly resistant', () => { const s = new Sim({ map: 'open', gold: 1000 }); s.place('throttle', 5, 6); const mk = (kind, x, y) => ({ id: s.nid++, kind, air: false, x, y, cx: 5, cy: 7, tx: -1, ty: -1, hp: 100, maxhp: 100, armor: 0, speed: 1, slow: 0, rem: 10, dead: false }); const a = mk('ping', 5.5, 7.5), b = mk('rootkit', 5.5, 7.5); s.monsters = [a, b]; s.updateTower(s.towers[0], STEP); const slow1 = TOWERS.throttle.levels[0].slow; assert.ok(Math.abs(a.slow - slow1) < 1e-9); assert.ok(Math.abs(b.slow - slow1 * (1 - ENEMIES.rootkit.slowRes)) < 1e-9); assert.ok(b.slow < a.slow); }); test('waves: queue, clear bonus, win and lose states, auto waves', () => { const s = new Sim({ map: 'open', gold: 0, winWave: 1 }); assert.ok(s.startWave().ok); assert.ok(s.spawnQ.length > 0); run(s, 60); assert.equal(s.state, 'won'); const clear = s.drainEvents().find((e) => e.e === 'clear'); assert.ok(clear && clear.bonus > 0 && clear.n === 1, 'a clear event with a positive bonus'); assert.equal(s.gold, clear.bonus, 'the wave clear bonus is the only income of an undefended wave'); assert.ok(s.continueEndless().ok); assert.equal(s.state, 'playing'); assert.ok(s.startWave().ok); const s2 = new Sim({ map: 'open', gold: 0, lives: 1 }); s2.startWave(); run(s2, 60); assert.equal(s2.state, 'lost'); assert.equal(s2.lives, 0); assert.equal(s2.place('wall', 5, 5).reason, 'over'); const s3 = new Sim({ map: 'open', gold: 0 }); s3.setAuto(true); run(s3, TD.WAVE_INTERVAL + 1); assert.equal(s3.wave, 1); s3.setAuto(false); }); test('wave definitions are sane for 60 waves', () => { let prev = 0; for (let n = 1; n <= 60; n++) { const g = TD.waveGroups(n); assert.ok(g.length > 0); for (const e of g) { assert.ok(ENEMIES[e.k]); assert.ok(e.c >= 1 && e.gap > 0); } assert.ok(TD.hpScale(n) >= prev); prev = TD.hpScale(n); } assert.ok(TD.waveGroups(10).some((e) => e.k === 'rootkit')); }); test('snapshot / fullState are JSON round-trippable and compact', () => { const s = new Sim({ map: 'twin', gold: 1000 }); s.place('filter', 5, 5, 1); s.startWave(); run(s, 5); const snap = JSON.parse(JSON.stringify(s.snapshot())); assert.equal(snap.s, 'playing'); assert.ok(Array.isArray(snap.mo) && snap.mo.length > 0); assert.equal(snap.mo[0].length, 7); const full = JSON.parse(JSON.stringify(s.fullState())); assert.equal(full.map, 'twin'); assert.deepEqual(full.towers[0].slice(0, 4).length, 4); assert.equal(full.towers[0][7], 1, 'owner slot is carried'); }); test('a client-side Board mirror predicts the server route exactly', () => { const s = new Sim({ map: 'hub', gold: 1000 }); const mirror = new Board('hub'); for (const [x, y] of [[5, 5], [6, 6], [7, 7], [8, 8], [9, 4]]) { const r = s.place('wall', x, y); assert.ok(r.ok); } for (const t of s.fullState().towers) mirror.setSolid(t[2], t[3], 1); mirror.recompute(); assert.equal(mirror.routeLen, s.board.routeLen); const pm = mirror.trial(10, 10, null, null, true); const ps = s.preview(10, 10, true); assert.equal(pm.after, ps.after); }); test('fuzz: garbage commands never throw', () => { const s = new Sim({ map: 'open', gold: 100 }); const junk = [null, undefined, 1, 'x', [], {}, { c: 'place' }, { c: 'place', type: 'wall' }, { c: 'sell', id: 'a' }, { c: 'upgrade', id: {} }, { c: 'target', id: 1, mode: 5 }, { c: '__proto__' }, { c: 'place', type: 'wall', x: Infinity, y: -0 }, { c: 'place', type: 'wall', x: '5', y: '5' }, { c: 'auto' }, { c: 'continue' }, { c: 'wave' }]; for (const j of junk) assert.doesNotThrow(() => s.cmd(j)); assert.equal(s.towers.length, 0); }); test('balance tables keep the shape the client and server rely on', () => { assert.deepEqual(TD.TOWER_ORDER, ['wall', 'filter', 'ids', 'mortar', 'throttle', 'arc', 'sandbox', 'balancer', 'uplink']); assert.deepEqual(TD.ENEMY_ORDER, ['ping', 'worm', 'trojan', 'spam', 'ghost', 'rootkit', 'daemon', 'proxy', 'botnet']); assert.equal(Object.keys(TOWERS).length, TD.TOWER_ORDER.length, 'every tower is in the palette order'); assert.equal(Object.keys(ENEMIES).length, TD.ENEMY_ORDER.length, 'every enemy is in the wire order'); assert.equal(TOWERS.wall.levels.length, 1); assert.ok(TOWERS.wall.refund, 'walls refund in full'); const ROLES = ['wall', 'attack', 'aura', 'control', 'support']; for (const k of TD.TOWER_ORDER) { const t = TOWERS[k]; assert.ok(t.name && t.color && t.key && t.desc, k + ' has display fields'); assert.ok(ROLES.includes(t.role), k + ' has a known role'); if (k === 'wall') continue; assert.equal(t.levels.length, k === 'uplink' ? 4 : 3, k + ' has the expected number of levels'); t.levels.forEach((lv, i) => { assert.ok(lv.cost > 0 && lv.range > 0, k + ' L' + (i + 1) + ' cost/range'); if (k === 'throttle') assert.ok(lv.slow > 0 && lv.slow <= 0.6, 'slow is capped at 0.6 by the sim'); else if (k === 'sandbox') assert.ok(lv.pulse > 0 && lv.stun > 0 && lv.stun < lv.pulse, 'sandbox pulses and stuns for less than the recharge'); else if (k === 'balancer') assert.ok(lv.boost > 0 && lv.boost <= 1, 'balancer boosts the fire rate'); else assert.ok(lv.dmg > 0 && lv.rate > 0, k + ' L' + (i + 1) + ' dmg/rate'); if (t.role === 'attack') assert.ok(lv.dmg > 0 && lv.rate > 0, k + ' attack towers have dmg and rate'); else assert.ok(!lv.dmg && !lv.rate, k + ' deals no damage of its own'); if (k === 'mortar') assert.ok(lv.splash > 0); if (k === 'arc') assert.ok(lv.chains >= 1 && lv.chainRange > 0 && lv.falloff > 0 && lv.falloff < 1); if (k === 'uplink') assert.ok(lv.width > 0 && lv.splash > 0 && lv.slowOnHit > 0 && lv.slowOnHit <= 0.6 && lv.slowFor > 0); if (i > 0) assert.ok(lv.range >= t.levels[i - 1].range, 'range never shrinks on upgrade'); }); } for (const k of TD.ENEMY_ORDER) { const e = ENEMIES[k]; assert.ok(e.name && e.color && e.hp > 0 && e.speed > 0 && e.reward > 0 && e.leak > 0 && e.armor >= 0 && e.r > 0, k); assert.equal(typeof e.air, 'boolean', k + ' says whether it flies'); assert.ok(Number.isInteger(e.buyCost) && e.buyCost > 0, k + ' has a versus buyCost'); } assert.ok(TD.START_GOLD >= TOWERS.filter.levels[0].cost * 2, 'the opening credits buy at least two turrets'); assert.ok(TD.SELL_RATIO > 0 && TD.SELL_RATIO < 1, 'selling a turret always loses credits'); // no way to turn credits into more credits by buying, upgrading and selling for (const k of TD.TOWER_ORDER) { if (k === 'wall') continue; const spent = TOWERS[k].levels.reduce((a, l) => a + l.cost, 0); assert.ok(Math.floor(spent * TD.SELL_RATIO) < spent); } // the opening waves are survivable with the opening credits and wave pressure never decreases for (let n = 2; n <= 60; n++) assert.ok(TD.hpScale(n) > TD.hpScale(n - 1)); for (let n = 2; n <= 60; n++) assert.ok(TD.rewardScale(n) >= TD.rewardScale(n - 1)); }); test('Orbital Uplink: expensive endgame beam that pierces, ignores armour, blasts, slows and hits flyers', () => { const s = new Sim({ map: 'open', gold: 100000 }); const r = s.place('uplink', 5, 7); assert.ok(r.ok); const t = s.towerById(r.id), st = TOWERS.uplink.levels[0]; const idsTotal = TOWERS.ids.levels.reduce((a, l) => a + l.cost, 0); const upTotal = TOWERS.uplink.levels.reduce((a, l) => a + l.cost, 0); assert.ok(TOWERS.uplink.levels[0].cost >= 5 * TOWERS.ids.levels[0].cost, 'buying it costs several times an IDS Sniper'); assert.ok(upTotal >= 8 * idsTotal, 'a fully upgraded Uplink costs many times a fully upgraded IDS'); assert.ok(st.range > TOWERS.ids.levels[2].range, 'it out-ranges even a level 3 IDS'); assert.ok(st.dmg > TOWERS.ids.levels[2].dmg, 'and hits harder than a level 3 IDS from the start'); const mk = (id, x, y, extra) => Object.assign({ id, kind: 'trojan', air: false, x, y, cx: 0, cy: 0, tx: -1, ty: -1, hp: 5000, maxhp: 5000, armor: 50, speed: 1, slow: 0, slowT: 0, slowAmt: 0, rem: 10, dead: false }, extra); // tower centre is (5.5, 7.5): three monsters on the horizontal line, one flyer on it, one far off the line, one beside the first target const a = mk(201, 8.5, 7.5), b = mk(202, 11.5, 7.5), c = mk(203, 14.5, 7.5), air = mk(204, 12.5, 7.5, { air: true, kind: 'ghost' }); const off = mk(205, 9.5, 12.5), near = mk(206, 8.5, 8.6); s.monsters = [a, b, c, air, off, near]; t.mode = 'close'; t.cd = 0; s.updateTower(t, TD.STEP); for (const o of [a, b, c, air]) assert.equal(o.hp, 5000 - st.dmg, 'on-line monster took full beam damage ignoring armour (armour 50)'); assert.equal(off.hp, 5000, 'a monster off the line is untouched'); assert.equal(near.hp, 5000 - st.dmg * 0.5, 'a monster beside the target takes half from the blast, also ignoring armour'); assert.ok(a.slowT > 0 && a.slowAmt > 0, 'hit monsters are slowed'); assert.equal(off.slowT, 0); // slow lingers after the shot and then wears off t.cd = 1e9; // stop it re-firing so we can watch the slow wear off s.tick(TD.STEP); assert.ok(a.slow > 0, 'slow is applied on the next tick'); for (let i = 0; i < 60 * 3; i++) s.tick(TD.STEP); assert.equal(a.slow, 0, 'slow wears off after a couple of seconds'); // upgrades to level 4 and refunds 70% of everything spent assert.ok(s.upgrade(t.id).ok); assert.ok(s.upgrade(t.id).ok); assert.ok(s.upgrade(t.id).ok); assert.equal(s.upgrade(t.id).reason, 'max'); assert.equal(t.level, 4); const g = s.gold; s.sell(t.id); assert.equal(s.gold, g + Math.floor(upTotal * TD.SELL_RATIO)); }); // ---------------------------------------------------------------------------------------------------- new content // A real monster (spawned through the sim so it has every field) moved to (x, y) and, by default, held still. function put(s, kind, x, y, wave) { s.spawnMonster({ k: kind, wave: wave || 1 }); const m = s.monsters[s.monsters.length - 1]; m.x = x; m.y = y; m.cx = Math.floor(x); m.cy = Math.floor(y); m.tx = -1; m.ty = -1; m.speed = 0; return m; } const tick = (s, seconds) => { const n = Math.round(seconds / STEP); for (let i = 0; i < n; i++) s.tick(STEP); }; test('hotkeys: one unique key 1-9 per tower, in palette order; enemy kinds are only ever appended', () => { const keys = TD.TOWER_ORDER.map((k) => TOWERS[k].key); assert.deepEqual(keys, ['1', '2', '3', '4', '5', '6', '7', '8', '9']); assert.equal(new Set(keys).size, keys.length); assert.equal(TOWERS.wall.key, '1'); assert.equal(TOWERS.sandbox.key, '7'); assert.equal(TOWERS.balancer.key, '8'); assert.equal(TOWERS.uplink.key, '9'); // the first six indexes are on the wire already (snapshot kindIndex) assert.deepEqual(TD.ENEMY_ORDER.slice(0, 6), ['ping', 'worm', 'trojan', 'spam', 'ghost', 'rootkit']); assert.deepEqual(TD.ENEMY_ORDER.slice(6), ['daemon', 'proxy', 'botnet']); }); test('buyCost: every enemy has one, about three times its kill reward', () => { for (const k of TD.ENEMY_ORDER) { const e = ENEMIES[k]; assert.ok(Number.isInteger(e.buyCost) && e.buyCost > 0, k); const ratio = e.buyCost / e.reward; assert.ok(ratio >= 2.5 && ratio <= 3.5, k + ' buyCost/reward = ' + ratio.toFixed(2)); } assert.ok(ENEMIES.daemon.air === false && ENEMIES.proxy.air === false && ENEMIES.botnet.air === true); }); test('every tower can be built, upgraded through all its levels, and sold (solid blocks like the others)', () => { const s = new Sim({ map: 'open', gold: 100000 }); let x = 3; for (const type of TD.TOWER_ORDER) { const def = TOWERS[type]; const r = s.place(type, x, 10); assert.ok(r.ok, type + ' can be placed'); assert.equal(s.board.solid[10 * s.board.cols + x], 1, type + ' is a solid block'); assert.equal(s.place(type, x, 10).reason, 'occupied'); const t = s.towerById(r.id); for (let lv = 1; lv < def.levels.length; lv++) assert.ok(s.upgrade(t.id).ok, type + ' upgrade to level ' + (lv + 1)); assert.equal(s.upgrade(t.id).reason, 'max', type); assert.equal(t.level, def.levels.length); assert.equal(t.spent, def.levels.reduce((a, l) => a + l.cost, 0)); assert.ok(s.setTargetMode(t.id, 'close').ok); const g = s.gold, sold = s.sell(t.id); assert.equal(sold.refund, def.refund ? t.spent : Math.floor(t.spent * TD.SELL_RATIO)); assert.equal(s.gold, g + sold.refund); assert.equal(s.board.solid[10 * s.board.cols + x], 0, type + ' sold: the cell is free again'); x++; } assert.equal(s.towers.length, 0); // a Sandbox or Load Balancer cannot seal the route either const tiny = new Sim({ map: mini(['S....', '.....', '.....', '.....', '....C']), gold: 1000 }); assert.ok(tiny.place('sandbox', 3, 4).ok); assert.equal(tiny.place('balancer', 4, 3).reason, 'blocks'); assert.equal(tiny.place('sandbox', 5, 5).reason, 'bounds'); }); test('Daemon heals every other enemy within 2.2 cells for 6% of max hp, never itself, never above max', () => { const s = new Sim({ map: 'open', gold: 0 }); const d = put(s, 'daemon', 10.5, 5.5); const near = put(s, 'trojan', 12.5, 5.5), far = put(s, 'trojan', 13.0, 5.5); // 2.0 and 2.5 cells away const air = put(s, 'botnet', 10.5, 7.5); // 2.0 cells away, a flyer const full = put(s, 'ping', 10.5, 4.0); const dead = put(s, 'trojan', 10.5, 6.0); for (const m of [near, far, d, air]) m.hp = m.maxhp / 2; air.maxhp = air.hp = 100; air.hp = 50; dead.hp = 1; dead.dead = true; const per = (m) => 0.06 * m.maxhp; const n0 = near.hp, f0 = far.hp, d0 = d.hp, a0 = air.hp; s.healPulse(); assert.ok(Math.abs(near.hp - (n0 + per(near))) < 1e-9, 'a trojan 2.0 cells away is healed by 6% of its max'); assert.equal(far.hp, f0, 'one 2.5 cells away is not'); assert.equal(d.hp, d0, 'a daemon never heals itself'); assert.ok(Math.abs(air.hp - (a0 + per(air))) < 1e-9, 'flyers are healed too'); assert.equal(full.hp, full.maxhp); assert.equal(dead.hp, 1, 'dead enemies are not healed'); near.hp = near.maxhp - 1; s.healPulse(); assert.equal(near.hp, near.maxhp, 'never above max'); // two daemons heal each other, a lone one does not const d2 = put(s, 'daemon', 10.5, 6.5); d.hp = d.maxhp / 2; d2.hp = d2.maxhp / 2; s.healPulse(); assert.ok(d.hp > d.maxhp / 2 && d2.hp > d2.maxhp / 2); assert.equal(d.hp, d.maxhp / 2 + per(d), 'the healing came from the other daemon only (6%, not 12%)'); }); test('several daemons never heal one enemy for more than 12% of its max hp per second', () => { const s = new Sim({ map: 'open', gold: 0 }); const t = put(s, 'trojan', 10.5, 5.5); for (let i = 0; i < 5; i++) put(s, 'daemon', 10.5 + 0.3 * i, 6.0); t.hp = t.maxhp * 0.1; s.healPulse(); assert.ok(Math.abs(t.hp - t.maxhp * 0.22) < 1e-9, 'five daemons heal 12%, not 30%: ' + (t.hp / t.maxhp)); // two daemons already give the full 12%; one gives 6% const s1 = new Sim({ map: 'open', gold: 0 }); const t1 = put(s1, 'trojan', 10.5, 5.5); put(s1, 'daemon', 11.0, 5.5); t1.hp = 0; s1.healPulse(); assert.ok(Math.abs(t1.hp - 0.06 * t1.maxhp) < 1e-9); }); test('daemons pulse once a second on one clock; heals scale with wave hp (max hp, not base hp)', () => { const s = new Sim({ map: 'open', gold: 0 }); const d = put(s, 'daemon', 10.5, 5.5, 10), t = put(s, 'trojan', 11.5, 5.5, 10); t.hp = 100; assert.equal(t.maxhp, Math.round(ENEMIES.trojan.hp * TD.hpScale(10))); tick(s, 0.9); assert.equal(t.hp, 100, 'no heal before the first second is up'); tick(s, 0.2); assert.ok(Math.abs(t.hp - (100 + 0.06 * t.maxhp)) < 1e-6, 'one pulse after a second'); tick(s, 1.0); assert.ok(Math.abs(t.hp - (100 + 2 * 0.06 * t.maxhp)) < 1e-6, 'one more pulse a second later'); const ev = s.drainEvents().filter((e) => e.e === 'heal'); assert.equal(ev.length, 2, 'each pulse tells the client where the daemon healed'); assert.ok(ev[0].r === ENEMIES.daemon.healRange && Number.isFinite(ev[0].x)); s.monsters.splice(s.monsters.indexOf(d), 1); const before = t.hp; tick(s, 2.5); assert.equal(t.hp, before, 'no daemon, no healing'); }); test('Proxy: the shield soaks damage first, armour applies once, overflow hurts hp, the snapshot carries hp + shield', () => { const s = new Sim({ map: 'open', gold: 0 }); const m = put(s, 'proxy', 10.5, 5.5); const E = ENEMIES.proxy; assert.equal(m.hp, E.hp); assert.equal(m.shield, E.shield); assert.equal(m.maxShield, E.shield); s.damage(m, 10); // armour 1: 9 get through to the shield assert.equal(m.shield, E.shield - 9); assert.equal(m.hp, E.hp, 'hp untouched while the shield holds'); const snap1 = s.snapshot().mo[0]; assert.equal(snap1[4], E.hp + E.shield - 9, 'hp field = hp + shield'); assert.equal(snap1[5], E.hp + E.shield, 'maxhp field = maxhp + maxShield'); assert.equal(snap1.length, 7); s.damage(m, 150); // 149 after armour: 111 left in the shield, 38 into hp (armour NOT applied a second time) assert.equal(m.shield, 0); assert.equal(m.hp, E.hp - (149 - (E.shield - 9))); assert.equal(s.snapshot().mo[0][4], m.hp); assert.equal(m.dead, false); s.damage(m, 1e6); assert.equal(m.dead, true); assert.equal(s.kills, 1); // the whole pool (shield + hp) must be dealt before it dies; armour-ignoring damage also hits the shield first const m2 = put(s, 'proxy', 10.5, 5.5); s.damage(m2, E.shield + E.hp - 1, null, true); assert.equal(m2.dead, false); s.damage(m2, 1, null, true); assert.equal(m2.dead, true); }); test('Proxy shield is scaled with the wave like hp, regrows 10%/s after 3 s without damage, and the timer restarts on every hit', () => { const s = new Sim({ map: 'open', gold: 0 }); const m = put(s, 'proxy', 10.5, 5.5, 12); const sh = Math.round(ENEMIES.proxy.shield * TD.hpScale(12)); assert.equal(m.maxShield, sh); assert.equal(m.maxhp, Math.round(ENEMIES.proxy.hp * TD.hpScale(12))); s.damage(m, 400, null, true); const left = sh - 400; assert.equal(m.shield, left); tick(s, 2.8); assert.equal(m.shield, left, 'nothing regrows during the 3 s delay'); tick(s, 1.2); // 4.0 s since the hit: one second of regrow assert.ok(Math.abs(m.shield - (left + 0.1 * sh)) < 0.5, 'regrew 10% of max in the first second after the delay: ' + (m.shield - left) + ' vs ' + 0.1 * sh); s.damage(m, 5, null, true); const after = m.shield; tick(s, 2.9); assert.equal(m.shield, after, 'a hit restarts the delay'); tick(s, 60); assert.equal(m.shield, sh, 'regrows to the maximum and not beyond'); assert.equal(m.hp, m.maxhp, 'the shield regrows, hp is not touched'); // a daemon never tops up a shield, only hp s.damage(m, sh + 40, null, true); const d = put(s, 'daemon', 11.0, 5.5, 12); s.healPulse(); assert.equal(m.shield, 0); assert.ok(Math.abs(m.hp - (m.maxhp - 40 + 0.06 * m.maxhp)) < 1e-6 || m.hp === m.maxhp); assert.ok(d.hp > 0); }); test('Botnet: a fast, tiny flyer that goes straight for the core and is hit by anti-air only', () => { const s = new Sim({ map: 'open', gold: 1000 }); const E = ENEMIES.botnet; assert.ok(E.air && E.hp <= 15 && E.speed > ENEMIES.ghost.speed && E.reward === 1); const m = put(s, 'botnet', 12.5, 7.5); s.place('mortar', 12, 6); tick(s, 1); assert.equal(m.dead, false, 'a mortar cannot hit flyers'); s.place('filter', 11, 6); tick(s, 1); assert.ok(m.dead || m.hp < m.maxhp, 'a filter can'); // and it flies straight at the core at its own speed const s2 = new Sim({ map: 'open', gold: 0 }); const f = put(s2, 'botnet', 0.5, 7.5); f.speed = E.speed; tick(s2, 2); assert.ok(Math.abs(f.x - (0.5 + 2 * E.speed * Math.cos(Math.atan2(8 - 7.5, 25.5 - 0.5)))) < 0.1, 'x=' + f.x); }); test('Sandbox stuns everything in range (ground and air), not what is outside, and stunned enemies do not move', () => { const s = new Sim({ map: 'open', gold: 1000 }); s.place('sandbox', 2, 6); // centre (2.5, 6.5), range 2.6 const L = TOWERS.sandbox.levels[0]; const ground = put(s, 'ping', 0.5, 7.5), air = put(s, 'ghost', 3.5, 7.5), out = put(s, 'ping', 8.5, 7.5); for (const m of [ground, air, out]) m.speed = ENEMIES[m.kind].speed; const gx = ground.x, ax = air.x, ox = out.x; tick(s, L.stun * 0.9); assert.ok(ground.stunT > 0 && air.stunT > 0, 'ground and air are stunned'); assert.equal(out.stunT, 0, 'out of range: untouched'); assert.equal(ground.x, gx, 'stunned ground unit stands still'); assert.equal(air.x, ax, 'stunned flyer stands still'); assert.ok(out.x > ox, 'the unit outside keeps moving'); assert.equal(s.snapshot().mo.find((e) => e[0] === ground.id)[6], 1, 'stunned units are flagged in the snapshot (the 0/1 slow flag)'); tick(s, L.stun * 0.3); assert.ok(ground.x > gx, 'moves again once the stun is over'); const ev = s.drainEvents().filter((e) => e.e === 'stun'); assert.equal(ev.length, 1); assert.equal(ev[0].n, 2); }); test('Sandbox: pulse cadence by level, only pulses when something is in range, stun does not stack, bosses get half', () => { const s = new Sim({ map: 'open', gold: 10000 }); const r = s.place('sandbox', 5, 5), t = s.towerById(r.id); tick(s, 12); assert.equal(s.drainEvents().filter((e) => e.e === 'stun').length, 0, 'nothing in range: no pulse and the charge is kept'); assert.equal(t.cd, 0); const boss = put(s, 'rootkit', 6.5, 6.5, 10), ping = put(s, 'ping', 5.5, 7.0); const L1 = TOWERS.sandbox.levels[0]; tick(s, 0.1); assert.ok(Math.abs(ping.stunT - (L1.stun - 0.1 + STEP)) < 0.05, 'a ping is stunned for the full time'); assert.ok(Math.abs(boss.stunT - (L1.stun * (1 - ENEMIES.rootkit.slowRes) - 0.1 + STEP)) < 0.05, 'a boss for half'); assert.ok(boss.stunT < ping.stunT); tick(s, 8.8); // 8.9 s: pulses at 0 and 5 assert.equal(s.drainEvents().filter((e) => e.e === 'stun').length, 2); // level 3 pulses faster and stuns longer; a second pulse never adds to a running stun, it only refreshes it s.upgrade(t.id); s.upgrade(t.id); const L3 = TOWERS.sandbox.levels[2]; assert.ok(L3.pulse < L1.pulse && L3.stun > L1.stun && L3.range > L1.range); s.drainEvents(); t.cd = 0; ping.stunT = 0.5; tick(s, 0.05); assert.ok(Math.abs(ping.stunT - (L3.stun - 0.05 + STEP)) < 0.05 && ping.stunT <= L3.stun, 'stun is set to the pulse stun, not added'); t.cd = 0; ping.stunT = L3.stun * 2; // an already longer stun stays s.tick(STEP); assert.ok(ping.stunT > L3.stun, 'a shorter stun never cuts a longer one'); s.drainEvents(); tick(s, 12.1); assert.equal(s.drainEvents().filter((e) => e.e === 'stun').length, 3, 'level 3 recharges every ' + L3.pulse + ' s'); }); test('Load Balancer: boosts attack towers in range, best boost only, not itself, not control/aura towers, ends when sold', () => { const s = new Sim({ map: 'open', gold: 100000 }); const f1 = s.towerById(s.place('filter', 10, 5).id); // 1 cell from the balancer at (11,5) const fFar = s.towerById(s.place('filter', 14, 5).id); // 3 cells: outside the level 1 and 2 range, exactly inside level 3 const sb = s.towerById(s.place('sandbox', 10, 6).id); const th = s.towerById(s.place('throttle', 9, 5).id); const wl = s.towerById(s.place('wall', 11, 6).id); const b1 = s.towerById(s.place('balancer', 11, 5).id); const B = TOWERS.balancer.levels; s.updateAuras(); assert.equal(f1.buff, B[0].boost); assert.equal(fFar.buff, 0, 'out of range'); for (const t of [sb, th, wl, b1]) assert.equal(t.buff, 0, t.type + ' is not an attack tower'); // upgrading makes it stronger; a second, weaker balancer never adds to it s.upgrade(b1.id); s.updateAuras(); assert.equal(f1.buff, B[1].boost); assert.equal(fFar.buff, 0); const b2 = s.towerById(s.place('balancer', 10, 4).id); s.updateAuras(); assert.equal(f1.buff, B[1].boost, 'the best boost applies, they do not stack (not 0.15 + 0.25)'); assert.equal(b1.buff, 0, 'a balancer does not boost another balancer'); assert.equal(b2.buff, 0); s.upgrade(b1.id); s.updateAuras(); assert.equal(f1.buff, B[2].boost); assert.equal(fFar.buff, B[2].boost, 'level 3 reaches 3 cells'); // selling the strong one drops back to the weak one, selling the last ends the boost s.sell(b1.id); s.updateAuras(); assert.equal(f1.buff, B[0].boost); assert.equal(fFar.buff, 0); s.sell(b2.id); s.tick(STEP); assert.equal(f1.buff, 0, 'selling the last balancer ends the boost'); }); test('Load Balancer: cooldowns really elapse faster, so a boosted tower fires more often', () => { const mk = (withBalancer) => { const s = new Sim({ map: 'open', gold: 10000 }); const t = s.towerById(s.place('ids', 10, 5).id); if (withBalancer) s.place('balancer', 11, 5); const target = put(s, 'trojan', 10.5, 7.5, 1); target.hp = target.maxhp = 1e12; target.armor = 0; return { s, t, target }; }; const a = mk(false), b = mk(true); a.t.cd = b.t.cd = 1; a.s.tick(0.1); b.s.tick(0.1); assert.ok(Math.abs(a.t.cd - 0.9) < 1e-9); assert.ok(Math.abs(b.t.cd - (1 - 0.1 * (1 + TOWERS.balancer.levels[0].boost))) < 1e-9, 'cooldown ran 15% faster'); const shots = (x) => { tick(x.s, 40); return x.s.drainEvents().filter((e) => e.e === 'shot' && e.k === 'ids').length; }; const na = shots(a), nb = shots(b); assert.ok(na >= 25, 'baseline fires ~28 shots in 40 s: ' + na); assert.ok(nb > na * 1.1 && nb < na * 1.2, 'boosted ' + nb + ' vs ' + na); }); test('waves 1-8 are exactly the original introduction', () => { const W = [ [{ k: 'ping', c: 8, gap: 0.7 }], [{ k: 'worm', c: 11, gap: 0.4 }], [{ k: 'ping', c: 9, gap: 0.6 }, { k: 'trojan', c: 1, gap: 1.4 }], [{ k: 'spam', c: 30, gap: 0.18 }], [{ k: 'ghost', c: 7, gap: 0.9 }, { k: 'ping', c: 8, gap: 0.5 }], [{ k: 'trojan', c: 4, gap: 1.2 }, { k: 'worm', c: 11, gap: 0.35 }], [{ k: 'ghost', c: 12, gap: 0.6 }, { k: 'spam', c: 24, gap: 0.2 }], [{ k: 'ping', c: 16, gap: 0.4 }, { k: 'trojan', c: 5, gap: 1 }, { k: 'ghost', c: 6, gap: 0.8 }], ]; for (let n = 1; n <= 8; n++) assert.deepEqual(TD.waveGroups(n), W[n - 1], 'wave ' + n); for (let n = 1; n <= 8; n++) assert.ok(!TD.waveGroups(n).some((g) => ['daemon', 'proxy', 'botnet'].includes(g.k))); }); test('new kinds are woven into the waves from 9 on: daemons with trojans, proxies from 12, botnets from 14', () => { const first = {}; for (let n = 1; n <= 60; n++) { const g = TD.waveGroups(n); for (const e of g) { assert.ok(ENEMIES[e.k], 'wave ' + n + ' kind ' + e.k); assert.ok(Number.isInteger(e.c) && e.c >= 1 && e.gap > 0, 'wave ' + n); if (first[e.k] === undefined) first[e.k] = n; } assert.ok(g.length <= 8, 'wave ' + n + ' has a sane number of groups'); const t = g.reduce((a, e) => a + e.c * e.gap + 1.5, 0); assert.ok(t < 120, 'wave ' + n + ' finishes spawning within two minutes (' + t.toFixed(0) + ' s)'); if (n >= 9 && g.some((e) => e.k === 'trojan')) { const tr = g.find((e) => e.k === 'trojan'), dm = g.find((e) => e.k === 'daemon'); assert.ok(dm && dm.c >= 1, 'wave ' + n + ' has a daemon escort'); assert.ok(g.indexOf(dm) > g.indexOf(tr), 'the escort spawns behind the trojans'); assert.ok(dm.c <= tr.c, 'the escort is not bigger than the trojan group'); } } assert.equal(first.daemon, 9); assert.equal(first.proxy, 12); assert.equal(first.botnet, 14); for (const k of TD.ENEMY_ORDER) assert.ok(first[k] !== undefined, k + ' appears somewhere in waves 1-60'); for (let n = 9; n <= 60; n++) { const kinds = TD.waveGroups(n).map((e) => e.k); if (n < 12) assert.ok(!kinds.includes('proxy'), 'no proxy before 12'); if (n < 14) assert.ok(!kinds.includes('botnet'), 'no botnet before 14'); } // the late waves combine them assert.ok(['daemon', 'proxy', 'botnet'].every((k) => TD.waveGroups(30).some((e) => e.k === k)), 'wave 30 has all three'); assert.ok(TD.waveGroups(20).some((e) => e.k === 'rootkit') && TD.waveGroups(20).some((e) => e.k === 'proxy')); // the loop keeps growing: the same slot gets more units later const cnt = (n, k) => TD.waveGroups(n).filter((e) => e.k === k).reduce((a, e) => a + e.c, 0); assert.ok(cnt(22, 'botnet') > cnt(14, 'botnet') && cnt(36, 'proxy') > cnt(12, 'proxy')); }); test('waveSummary lists one entry per kind with counts and flags flyers', () => { for (let n = 1; n <= 60; n++) { const sum = TD.waveSummary(n), groups = TD.waveGroups(n); assert.equal(new Set(sum.map((e) => e.k)).size, sum.length, 'one entry per kind in wave ' + n); for (const e of sum) { assert.equal(e.air, !!ENEMIES[e.k].air, 'wave ' + n + ' ' + e.k + ' air flag'); assert.equal(e.c, groups.filter((g) => g.k === e.k).reduce((a, g) => a + g.c, 0)); } assert.equal(sum.reduce((a, e) => a + e.c, 0), groups.reduce((a, g) => a + g.c, 0)); } assert.deepEqual(TD.waveSummary(5), [{ k: 'ghost', c: 7, air: true }, { k: 'ping', c: 8, air: false }]); assert.ok(TD.waveSummary(14).some((e) => e.k === 'botnet' && e.air === true)); assert.ok(TD.waveSummary(7).some((e) => e.k === 'ghost' && e.air)); }); test('the new waves run end to end and the snapshot (incl. daemon, proxy, botnet) survives JSON', () => { const s = new Sim({ map: 'twin', gold: 0, lives: 1000 }); s.wave = 14; // every new kind in the queue at once s.spawnQ.push({ t: 0, k: 'daemon', wave: 14 }, { t: 0, k: 'proxy', wave: 14 }, { t: 0, k: 'botnet', wave: 14 }, { t: 0, k: 'trojan', wave: 14 }); s.spawnQ.push(...TD.waveGroups(14).flatMap((g, gi) => Array.from({ length: g.c }, (_, i) => ({ t: gi * 5 + i * g.gap, k: g.k, wave: 14 })))); s.spawnQ.sort((a, b) => a.t - b.t); let sawNew = false, sawShield = false; for (let i = 0; i < 60 * 25 && s.state === 'playing'; i++) { s.tick(STEP); if (i % 30 === 0) { const snap = JSON.parse(JSON.stringify(s.snapshot())); assert.deepEqual(snap, JSON.parse(JSON.stringify(snap))); for (const e of snap.mo) { assert.equal(e.length, 7); assert.ok(Number.isInteger(e[1]) && e[1] >= 0 && e[1] < TD.ENEMY_ORDER.length, 'kindIndex indexes ENEMY_ORDER'); assert.ok(e[4] > 0 && e[4] <= e[5], 'hp (incl. shield) within maxhp (incl. maxShield)'); if (e[1] >= 6) sawNew = true; if (TD.ENEMY_ORDER[e[1]] === 'proxy') { sawShield = true; assert.ok(e[5] > ENEMIES.proxy.hp * TD.hpScale(14), 'proxy maxhp includes the shield'); } } } } assert.ok(sawNew && sawShield); assert.ok(s.leaks > 0, 'undefended waves leak'); assert.equal(JSON.stringify(s.fullState()).includes('null'), false); }); test('Orbital Uplink pricing: very expensive, and every one you own makes the next dearer', () => { // (25% per owned one, for buying and for upgrading) const base = TOWERS.uplink.levels[0].cost; assert.ok(base >= 8 * TD.START_GOLD, 'the first Uplink costs many times the opening credits'); assert.ok(base >= 20 * TOWERS.ids.levels[0].cost, 'and a lot more than an IDS Sniper'); // the shared helper: arrays of towers and plain counts agree; other towers have a flat price assert.equal(TD.priceFor('uplink', 0), base); assert.equal(TD.priceFor('uplink', [{ type: 'uplink' }, { type: 'filter' }, { type: 'uplink' }]), TD.priceFor('uplink', 2)); assert.equal(TD.priceFor('filter', 50), TOWERS.filter.levels[0].cost, 'a Packet Filter never gets dearer'); assert.equal(TD.priceFor('wall', [{ type: 'wall' }, { type: 'wall' }]), TOWERS.wall.levels[0].cost); for (let n = 1; n < 6; n++) assert.ok(TD.priceFor('uplink', n) >= 1.2 * TD.priceFor('uplink', n - 1), 'each Uplink is about 25% dearer than the one before'); const s = new Sim({ map: 'open', gold: 1e6 }); const ids = []; let prev = 0; for (let i = 0; i < 4; i++) { const want = TD.priceFor('uplink', s.towers); assert.ok(want > prev, 'price keeps rising'); prev = want; const g = s.gold; const r = s.place('uplink', 4 + 2 * i, 6); assert.ok(r.ok); assert.equal(g - s.gold, want, 'the sim charges exactly the price the UI shows'); assert.equal(s.towerById(r.id).spent, want, 'spent records what was actually paid'); ids.push(r.id); } // refunds follow what each one cost, and selling brings the price back down const before = TD.priceFor('uplink', s.towers); const g0 = s.gold, t0 = s.towerById(ids[0]); const refund = s.sell(ids[0]).refund; assert.equal(refund, Math.floor(t0.spent * TD.SELL_RATIO)); assert.equal(s.gold, g0 + refund); assert.ok(TD.priceFor('uplink', s.towers) < before, 'owning fewer makes the next one cheaper again'); // not enough credits for the dearer one: refused without charging const poor = new Sim({ map: 'open', gold: base }); assert.ok(poor.place('uplink', 5, 5).ok); poor.gold = TD.priceFor('uplink', poor.towers) - 10; const gp = poor.gold; assert.equal(poor.place('uplink', 8, 5).reason, 'gold'); assert.equal(poor.gold, gp); }); test('Orbital Uplink upgrades get dearer by the same factor as the tower they belong to', () => { const s = new Sim({ map: 'open', gold: 1e7 }); const a = s.towerById(s.place('uplink', 4, 6).id), b = s.towerById(s.place('uplink', 8, 6).id), c = s.towerById(s.place('uplink', 12, 6).id); assert.equal(a.pm, 1); assert.ok(Math.abs(b.pm - TOWERS.uplink.priceGrowth) < 1e-9 && Math.abs(c.pm - TOWERS.uplink.priceGrowth ** 2) < 1e-9, 'each remembers the multiplier it was bought at'); for (const t of [a, b, c]) { const m = t.pm; const g0 = s.gold, up1 = TD.upgradePrice('uplink', 1, m); assert.equal(s.upgradeCost(t), up1, 'the sim and the shared helper agree'); assert.ok(Math.abs(up1 / TOWERS.uplink.levels[1].cost - m) < 0.01 * m + 0.01, 'level 2 costs the base upgrade times the multiplier'); assert.ok(s.upgrade(t.id).ok); assert.equal(g0 - s.gold, up1); assert.equal(t.spent, TD.priceFor('uplink', t.pm === 1 ? 0 : Math.round(Math.log(t.pm) / Math.log(TOWERS.uplink.priceGrowth))) + up1); } assert.ok(TD.upgradePrice('uplink', 3, c.pm) > TD.upgradePrice('uplink', 3, a.pm) * 1.5, 'the third Uplink is much dearer to upgrade than the first'); // flat-price towers are untouched const f = s.towerById(s.place('filter', 6, 10).id); assert.equal(f.pm, 1); assert.equal(TD.upgradePrice('filter', 1, f.pm), TOWERS.filter.levels[1].cost); // a tower bought after another was sold gets the lower multiplier back s.sell(a.id); const d = s.towerById(s.place('uplink', 16, 6).id); assert.ok(d.pm < c.pm * 1.0001 && d.pm >= b.pm - 1e-9, 'owning fewer when you buy lowers the multiplier again'); // the multiplier travels in the tower list so a co-op client can show the same upgrade price const row = s.towerList().find((r) => r[0] === c.id); assert.equal(row.length, 9); assert.ok(Math.abs(row[8] - c.pm) < 0.002); assert.equal(s.towerList().find((r) => r[0] === f.id)[8], 1); });