diff --git a/docs/superpowers/contracts/reverse-survivors-test-split-2026-08-16.json b/docs/superpowers/contracts/reverse-survivors-test-split-2026-08-16.json new file mode 100644 index 00000000..0978da69 --- /dev/null +++ b/docs/superpowers/contracts/reverse-survivors-test-split-2026-08-16.json @@ -0,0 +1,17 @@ +{ + "title": "Split the reverse_survivors test files under the 250-line cap", + "objective": "Bring sim.test.ts (698) and director.test.ts (315) under the cap by partitioning at describe boundaries, and establish that its two core source files cannot be split without breaking a runtime dependency.", + "acceptance_criteria": [ + "No reverse_survivors test file is over 250 lines.", + "The suite still reports exactly 160 passing tests.", + "npx tsc --noEmit and npx eslint . --max-warnings 0 both pass, with no new suppression.", + "No test is deleted, skipped or weakened; whole describe blocks move verbatim.", + "sim.ts and types.ts are fully restored, leaving no partial split and no orphan module." + ], + "out_of_scope": [ + "sim.ts, whose exported step() calls survivorStep and the enemy helpers that any seam would move.", + "types.ts, whose difficulty tables are referenced by the type declarations above them.", + "Changing what any test asserts." + ], + "verifier": "npm test -- --run (expects exactly 160 passed), npx tsc --noEmit, npx eslint . --max-warnings 0, and check_file_length.sh over reverse_survivors." +} diff --git a/docs/superpowers/evidence/reverse-survivors-test-split-2026-08-16.json b/docs/superpowers/evidence/reverse-survivors-test-split-2026-08-16.json new file mode 100644 index 00000000..7d518197 --- /dev/null +++ b/docs/superpowers/evidence/reverse-survivors-test-split-2026-08-16.json @@ -0,0 +1,39 @@ +{ + "intent": "Clear the reverse_survivors test files under the cap, and defer its two core source files after both seams failed.", + "scope": [ + "src/core/sim.test.ts (698 -> 175) + simEnemies, simUnits, simFrenzy test modules", + "src/core/director.test.ts (315 -> 178) + directorPowers.test.ts", + "Reverted: src/core/sim.ts (361), src/core/types.ts (295)", + "Non-goal: no assertion changed, no test deleted" + ], + "changes": [ + "Four new test modules, each carrying the original preamble; the unused imports and orphaned local helpers were then pruned iteratively from tsc's own TS6133/TS6192/TS6196 output rather than guessed.", + "sim.ts was reverted: its exported step() calls survivorStep, which any seam moves out, so the suite failed with 'ReferenceError: survivorStep is not defined' in 63 tests. tsc reported only unused-import warnings on that same code.", + "types.ts was reverted: the difficulty tables sit below type declarations that reference them, so moving them left StatusKind, EnemyKind and BossKind unresolvable in the new module." + ], + "verification": [ + { + "command": "npm test -- --run", + "result": "fail twice, then pass", + "evidence": "160 passed in 13 files, matching the baseline of 160. The two failures were the source splits, not the test splits: 63 and then 64 tests failed with a ReferenceError, which is again a runtime-only symptom that tsc did not catch." + }, + { + "command": "npx tsc --noEmit && npx eslint . --max-warnings 0", + "result": "pass", + "evidence": "Both clean after pruning roughly a dozen imports and two orphaned test helpers." + }, + { + "command": "bash ~/utils/scripts/check_file_length.sh --all | grep reverse", + "result": "pass", + "evidence": "Only sim.ts and types.ts remain, both source files, both deliberately deferred." + } + ], + "risks": [ + "This is the third project where a TypeScript seam compiled clean and failed at runtime. The pattern is consistent enough to state plainly: in TS, tsc is not a check that a split worked - only the test run is.", + "sim.ts and types.ts stay over the cap. Splitting sim.ts means passing the step functions in or hoisting them into a third module; types.ts needs its type declarations separated from the tables that use them." + ], + "rollback": [ + "git revert the commit; the two originals return at 698 and 315 lines and the four new modules disappear.", + "After rollback, re-run npm test and expect 160 passed." + ] +} diff --git a/reverse_survivors/src/core/director.test.ts b/reverse_survivors/src/core/director.test.ts index 0f3c6708..3860aaf7 100644 --- a/reverse_survivors/src/core/director.test.ts +++ b/reverse_survivors/src/core/director.test.ts @@ -4,9 +4,7 @@ import { createInitialState } from './sim' import { snapshotOf } from './snapshot' import type { GameState } from './types' import { - AMBUSH_RADIUS, ARENA, BOSS_COOLDOWN, ENEMY_SPECS, ENERGY_CAP, FRENZY_DURATION, - POWER_COOLDOWN, POWER_COST, RIFT_DURATION, -} from './types' + ARENA, BOSS_COOLDOWN, ENERGY_CAP, } from './types' const fresh = (seed = 1): GameState => createInitialState(seed) @@ -172,144 +170,3 @@ describe('snapshot', () => { expect(snapshotOf(s).survivorHp).toBe(0) }) }) - -describe('director powers', () => { - it('power cooldowns drain and floor at zero', () => { - const s = fresh() - s.director.powerCooldowns.ambush = 0.3 - s.director.powerCooldowns.frenzy = 2 - tickDirector(s, 0.5) - expect(s.director.powerCooldowns.ambush).toBe(0) - expect(s.director.powerCooldowns.frenzy).toBeCloseTo(1.5) - }) - - it('frenzy and rift timers drain and floor at zero', () => { - const s = fresh() - s.director.frenzyTimer = 0.2 - s.director.riftTimer = 5 - tickDirector(s, 0.5) - expect(s.director.frenzyTimer).toBe(0) - expect(s.director.riftTimer).toBeCloseTo(4.5) - }) - - it('ambush lands a unit near the survivor and debits both costs', () => { - const s = fresh() - s.director.energy = 400 - expect(applyAction(s, { type: 'ambush', kind: 'rusher' })).toBe(true) - expect(s.enemies).toHaveLength(1) - const e = s.enemies[0] - const d = Math.hypot( - (e?.pos.x ?? 0) - s.survivor.pos.x, - (e?.pos.y ?? 0) - s.survivor.pos.y, - ) - expect(d).toBeCloseTo(AMBUSH_RADIUS, 5) - expect(s.director.energy).toBe(400 - POWER_COST.ambush - ENEMY_SPECS.rusher.cost) - expect(s.director.powerCooldowns.ambush).toBe(POWER_COOLDOWN.ambush) - }) - - it('ambush is rejected when it cannot be afforded', () => { - const s = fresh() - s.director.energy = 5 - expect(applyAction(s, { type: 'ambush', kind: 'rusher' })).toBe(false) - expect(s.enemies).toHaveLength(0) - expect(s.director.energy).toBe(5) - }) - - it('ambush is rejected while cooling', () => { - const s = fresh() - s.director.energy = 400 - expect(applyAction(s, { type: 'ambush', kind: 'rusher' })).toBe(true) - expect(applyAction(s, { type: 'ambush', kind: 'rusher' })).toBe(false) - expect(s.enemies).toHaveLength(1) - }) - - it('ambush lanes never repeat, even past a full turn', () => { - // Regression: a 2*PI/7 step cycled silently, so index 7 reused index 0. - const s = fresh() - const seen: string[] = [] - for (let i = 0; i < 24; i += 1) { - s.director.powerCooldowns.ambush = 0 - s.director.energy = 400 - s.enemies = [] - applyAction(s, { type: 'ambush', kind: 'rusher' }) - const e = s.enemies[0] - seen.push(`${(e?.pos.x ?? 0).toFixed(6)},${(e?.pos.y ?? 0).toFixed(6)}`) - } - expect(new Set(seen).size).toBe(24) - }) - - it('successive ambushes rotate to a different lane, deterministically', () => { - const angles = (): number[] => { - const s = fresh() - s.director.energy = 400 - const out: number[] = [] - for (let i = 0; i < 3; i += 1) { - s.director.powerCooldowns.ambush = 0 - s.director.energy = 400 - applyAction(s, { type: 'ambush', kind: 'rusher' }) - } - for (const e of s.enemies) { - out.push(Math.atan2(e.pos.y - s.survivor.pos.y, e.pos.x - s.survivor.pos.x)) - } - return out - } - const first = angles() - expect(new Set(first).size).toBe(3) - expect(angles()).toEqual(first) - }) - - it('frenzy sets its timer and debits the cost', () => { - const s = fresh() - s.director.energy = 400 - expect(applyAction(s, { type: 'frenzy' })).toBe(true) - expect(s.director.frenzyTimer).toBe(FRENZY_DURATION) - expect(s.director.energy).toBe(400 - POWER_COST.frenzy) - }) - - it('frenzy is rejected when unaffordable', () => { - const s = fresh() - s.director.energy = 10 - expect(applyAction(s, { type: 'frenzy' })).toBe(false) - expect(s.director.frenzyTimer).toBe(0) - }) - - it('a rift pins every spawn to the chosen edge', () => { - const s = fresh() - s.director.energy = 400 - expect(applyAction(s, { type: 'rift', edge: 2 })).toBe(true) - expect(s.director.riftTimer).toBe(RIFT_DURATION) - for (let i = 0; i < 20; i += 1) { - expect(edgeSpawnPos(s).x).toBe(0) - } - }) - - it('once the rift closes, all four edges return', () => { - const s = fresh(7) - s.director.riftTimer = 0 - const seen = new Set() - for (let i = 0; i < 40; i += 1) { - const p = edgeSpawnPos(s) - seen.add(`${String(p.x === 0 || p.x === ARENA.w)}-${String(p.y === 0 || p.y === ARENA.h)}`) - } - expect(seen.size).toBeGreaterThan(1) - }) - - it('a rift is rejected when unaffordable', () => { - const s = fresh() - s.director.energy = 1 - expect(applyAction(s, { type: 'rift', edge: 1 })).toBe(false) - expect(s.director.riftTimer).toBe(0) - }) -}) - -describe('difficulty economy', () => { - it('regen defaults to a rate of 1', () => { - expect(regenRate(0)).toBe(6) - expect(regenRate(30)).toBe(8) - }) - - it('regen scales with the tier rate', () => { - expect(regenRate(0, 1.25)).toBeCloseTo(7.5) - expect(regenRate(0, 0.85)).toBeCloseTo(5.1) - }) -}) diff --git a/reverse_survivors/src/core/directorPowers.test.ts b/reverse_survivors/src/core/directorPowers.test.ts new file mode 100644 index 00000000..a34a6935 --- /dev/null +++ b/reverse_survivors/src/core/directorPowers.test.ts @@ -0,0 +1,153 @@ +/** Director powers and difficulty economy. */ + +import { describe, expect, it } from 'vitest' +import { applyAction, edgeSpawnPos, regenRate, tickDirector, } from './director' +import { createInitialState } from './sim' +import type { GameState } from './types' +import { + AMBUSH_RADIUS, ARENA, ENEMY_SPECS, FRENZY_DURATION, + POWER_COOLDOWN, POWER_COST, RIFT_DURATION, +} from './types' + +const fresh = (seed = 1): GameState => createInitialState(seed) + +describe('director powers', () => { + it('power cooldowns drain and floor at zero', () => { + const s = fresh() + s.director.powerCooldowns.ambush = 0.3 + s.director.powerCooldowns.frenzy = 2 + tickDirector(s, 0.5) + expect(s.director.powerCooldowns.ambush).toBe(0) + expect(s.director.powerCooldowns.frenzy).toBeCloseTo(1.5) + }) + + it('frenzy and rift timers drain and floor at zero', () => { + const s = fresh() + s.director.frenzyTimer = 0.2 + s.director.riftTimer = 5 + tickDirector(s, 0.5) + expect(s.director.frenzyTimer).toBe(0) + expect(s.director.riftTimer).toBeCloseTo(4.5) + }) + + it('ambush lands a unit near the survivor and debits both costs', () => { + const s = fresh() + s.director.energy = 400 + expect(applyAction(s, { type: 'ambush', kind: 'rusher' })).toBe(true) + expect(s.enemies).toHaveLength(1) + const e = s.enemies[0] + const d = Math.hypot( + (e?.pos.x ?? 0) - s.survivor.pos.x, + (e?.pos.y ?? 0) - s.survivor.pos.y, + ) + expect(d).toBeCloseTo(AMBUSH_RADIUS, 5) + expect(s.director.energy).toBe(400 - POWER_COST.ambush - ENEMY_SPECS.rusher.cost) + expect(s.director.powerCooldowns.ambush).toBe(POWER_COOLDOWN.ambush) + }) + + it('ambush is rejected when it cannot be afforded', () => { + const s = fresh() + s.director.energy = 5 + expect(applyAction(s, { type: 'ambush', kind: 'rusher' })).toBe(false) + expect(s.enemies).toHaveLength(0) + expect(s.director.energy).toBe(5) + }) + + it('ambush is rejected while cooling', () => { + const s = fresh() + s.director.energy = 400 + expect(applyAction(s, { type: 'ambush', kind: 'rusher' })).toBe(true) + expect(applyAction(s, { type: 'ambush', kind: 'rusher' })).toBe(false) + expect(s.enemies).toHaveLength(1) + }) + + it('ambush lanes never repeat, even past a full turn', () => { + // Regression: a 2*PI/7 step cycled silently, so index 7 reused index 0. + const s = fresh() + const seen: string[] = [] + for (let i = 0; i < 24; i += 1) { + s.director.powerCooldowns.ambush = 0 + s.director.energy = 400 + s.enemies = [] + applyAction(s, { type: 'ambush', kind: 'rusher' }) + const e = s.enemies[0] + seen.push(`${(e?.pos.x ?? 0).toFixed(6)},${(e?.pos.y ?? 0).toFixed(6)}`) + } + expect(new Set(seen).size).toBe(24) + }) + + it('successive ambushes rotate to a different lane, deterministically', () => { + const angles = (): number[] => { + const s = fresh() + s.director.energy = 400 + const out: number[] = [] + for (let i = 0; i < 3; i += 1) { + s.director.powerCooldowns.ambush = 0 + s.director.energy = 400 + applyAction(s, { type: 'ambush', kind: 'rusher' }) + } + for (const e of s.enemies) { + out.push(Math.atan2(e.pos.y - s.survivor.pos.y, e.pos.x - s.survivor.pos.x)) + } + return out + } + const first = angles() + expect(new Set(first).size).toBe(3) + expect(angles()).toEqual(first) + }) + + it('frenzy sets its timer and debits the cost', () => { + const s = fresh() + s.director.energy = 400 + expect(applyAction(s, { type: 'frenzy' })).toBe(true) + expect(s.director.frenzyTimer).toBe(FRENZY_DURATION) + expect(s.director.energy).toBe(400 - POWER_COST.frenzy) + }) + + it('frenzy is rejected when unaffordable', () => { + const s = fresh() + s.director.energy = 10 + expect(applyAction(s, { type: 'frenzy' })).toBe(false) + expect(s.director.frenzyTimer).toBe(0) + }) + + it('a rift pins every spawn to the chosen edge', () => { + const s = fresh() + s.director.energy = 400 + expect(applyAction(s, { type: 'rift', edge: 2 })).toBe(true) + expect(s.director.riftTimer).toBe(RIFT_DURATION) + for (let i = 0; i < 20; i += 1) { + expect(edgeSpawnPos(s).x).toBe(0) + } + }) + + it('once the rift closes, all four edges return', () => { + const s = fresh(7) + s.director.riftTimer = 0 + const seen = new Set() + for (let i = 0; i < 40; i += 1) { + const p = edgeSpawnPos(s) + seen.add(`${String(p.x === 0 || p.x === ARENA.w)}-${String(p.y === 0 || p.y === ARENA.h)}`) + } + expect(seen.size).toBeGreaterThan(1) + }) + + it('a rift is rejected when unaffordable', () => { + const s = fresh() + s.director.energy = 1 + expect(applyAction(s, { type: 'rift', edge: 1 })).toBe(false) + expect(s.director.riftTimer).toBe(0) + }) +}) + +describe('difficulty economy', () => { + it('regen defaults to a rate of 1', () => { + expect(regenRate(0)).toBe(6) + expect(regenRate(30)).toBe(8) + }) + + it('regen scales with the tier rate', () => { + expect(regenRate(0, 1.25)).toBeCloseTo(7.5) + expect(regenRate(0, 0.85)).toBeCloseTo(5.1) + }) +}) diff --git a/reverse_survivors/src/core/sim.test.ts b/reverse_survivors/src/core/sim.test.ts index 0235605a..b2ab0841 100644 --- a/reverse_survivors/src/core/sim.test.ts +++ b/reverse_survivors/src/core/sim.test.ts @@ -1,34 +1,17 @@ import { describe, expect, it } from 'vitest' import { - applyUpgrade, createInitialState, DANGER_RADIUS, grantXp, makeEnemy, SPLIT_RADIUS, step, + applyUpgrade, createInitialState, DANGER_RADIUS, grantXp, makeEnemy, step, xpForLevel, } from './sim' -import type { GameState, Projectile } from './types' +import type { GameState, } from './types' import { - ARENA, DIFFICULTIES, FRENZY_DAMAGE, FRENZY_SPEED, GAME_DURATION, STATUS_POWER, -} from './types' + ARENA, GAME_DURATION, } from './types' const CENTER = { x: ARENA.w / 2, y: ARENA.h / 2 } const fresh = (seed = 1, duration?: number): GameState => duration === undefined ? createInitialState(seed) : createInitialState(seed, duration) -const shot = (state: GameState, x: number, y: number, damage: number): Projectile => { - const p: Projectile = { - id: state.nextId, - from: 'survivor', - pos: { x, y }, - vel: { x: 0, y: 0 }, - radius: 4, - damage, - ttl: 1, - debuff: 'slow', - debuffDuration: 0, - } - state.nextId += 1 - state.projectiles.push(p) - return p -} describe('createInitialState', () => { it('uses the default duration when omitted', () => { @@ -170,529 +153,3 @@ describe('survivor firing', () => { expect(s.projectiles).toHaveLength(3) }) }) - -describe('enemy behaviors', () => { - it('chasers close the distance', () => { - const s = fresh() - const e = makeEnemy(s, 'rusher', { x: CENTER.x + 300, y: CENTER.y }) - step(s, [], 0.1) - expect(e.pos.x).toBeLessThan(CENTER.x + 300) - }) - - it('melee contact damages the survivor once per cooldown window', () => { - const s = fresh() - makeEnemy(s, 'rusher', { x: CENTER.x, y: CENTER.y }) - step(s, [], 0.01) - expect(s.survivor.hp).toBe(100 - 6) - step(s, [], 0.01) - expect(s.survivor.hp).toBe(100 - 6) - }) - - it('tank hits harder', () => { - const s = fresh() - makeEnemy(s, 'tank', { x: CENTER.x + 5, y: CENTER.y }) - step(s, [], 0.01) - expect(s.survivor.hp).toBe(100 - 14) - }) - - it('stalker approaches when too far', () => { - const s = fresh() - const e = makeEnemy(s, 'stalker', { x: CENTER.x + 300, y: CENTER.y }) - step(s, [], 0.05) - expect(e.pos.x).toBeLessThan(CENTER.x + 300) - }) - - it('stalker retreats when too close', () => { - const s = fresh() - const e = makeEnemy(s, 'stalker', { x: CENTER.x + 100, y: CENTER.y }) - step(s, [], 0.05) - expect(e.pos.x).toBeGreaterThan(CENTER.x + 100) - }) - - it('stalker holds position inside its comfort band', () => { - const s = fresh() - const e = makeEnemy(s, 'stalker', { x: CENTER.x + 160, y: CENTER.y }) - const before = e.pos.x - step(s, [], 0.05) - expect(e.pos.x).toBe(before) - }) - - it('stalker fires when in range and ready, then cools down', () => { - const s = fresh() - makeEnemy(s, 'stalker', { x: CENTER.x + 220, y: CENTER.y }) - step(s, [], 0.01) - expect(s.projectiles.filter((p) => p.from === 'enemy')).toHaveLength(1) - step(s, [], 0.01) - expect(s.projectiles.filter((p) => p.from === 'enemy')).toHaveLength(1) - }) - - it('stalker does not fire from beyond its range', () => { - const s = fresh() - makeEnemy(s, 'stalker', { x: CENTER.x + 400, y: CENTER.y }) - step(s, [], 0.01) - expect(s.projectiles.filter((p) => p.from === 'enemy')).toHaveLength(0) - }) - - it('hivemind emits a rusher on its spawn cadence', () => { - const s = fresh() - const hive = makeEnemy(s, 'hivemind', { x: 900, y: 560 }) - step(s, [], 2.5) - expect(s.enemies.filter((e) => e.kind === 'rusher')).toHaveLength(1) - expect(hive.spawnTimer).toBe(2.5) - step(s, [], 0.1) - expect(s.enemies.filter((e) => e.kind === 'rusher')).toHaveLength(1) - }) -}) - -describe('bomber', () => { - it('detonates on contact: damages the survivor, dies, grants nothing', () => { - const s = fresh() - makeEnemy(s, 'bomber', { x: CENTER.x, y: CENTER.y }) - step(s, [], 0.01) - expect(s.survivor.hp).toBe(100 - 22) - expect(s.enemies).toHaveLength(0) - expect(s.survivor.kills).toBe(0) - expect(s.survivor.xp).toBe(0) - }) - - it('sniped from afar: grants xp, blast misses the survivor', () => { - const s = fresh() - makeEnemy(s, 'bomber', { x: CENTER.x + 300, y: CENTER.y }) - shot(s, CENTER.x + 300, CENTER.y, 99) - step(s, [], 0.001) - expect(s.survivor.kills).toBe(1) - expect(s.survivor.xp).toBe(10) - expect(s.survivor.hp).toBe(100) - expect(s.enemies).toHaveLength(0) - }) - - it('shot inside blast range: grants xp and still burns the survivor', () => { - const s = fresh() - makeEnemy(s, 'bomber', { x: CENTER.x + 30, y: CENTER.y }) - shot(s, CENTER.x + 30, CENTER.y, 99) - step(s, [], 0.001) - expect(s.survivor.kills).toBe(1) - expect(s.survivor.hp).toBeCloseTo(100 - 22, 5) - }) -}) - -describe('projectiles', () => { - it('expire by ttl', () => { - const s = fresh() - shot(s, 50, 50, 1) - step(s, [], 2) - expect(s.projectiles).toHaveLength(0) - }) - - it('a miss keeps flying', () => { - const s = fresh() - shot(s, 50, 50, 1) - step(s, [], 0.01) - expect(s.projectiles).toHaveLength(1) - }) - - it('damages without killing when the target outlasts the hit', () => { - const s = fresh() - const tank = makeEnemy(s, 'tank', { x: CENTER.x + 400, y: CENTER.y }) - shot(s, CENTER.x + 400, CENTER.y, 8) - step(s, [], 0.001) - expect(tank.hp).toBe(112) - expect(s.survivor.kills).toBe(0) - expect(s.enemies).toHaveLength(1) - }) - - it('a second projectile in the same tick passes through an already-dead target', () => { - const s = fresh() - makeEnemy(s, 'rusher', { x: CENTER.x + 400, y: CENTER.y }) - shot(s, CENTER.x + 400, CENTER.y, 20) - shot(s, CENTER.x + 400, CENTER.y, 20) - step(s, [], 0.001) - expect(s.survivor.kills).toBe(1) - expect(s.projectiles).toHaveLength(1) - }) - - it('enemy fire hurts the survivor', () => { - const s = fresh() - s.projectiles.push({ - id: 500, from: 'enemy', pos: { x: CENTER.x, y: CENTER.y }, - vel: { x: 0, y: 0 }, radius: 5, damage: 7, ttl: 1, - debuff: 'slow', debuffDuration: 0, - }) - step(s, [], 0.001) - expect(s.survivor.hp).toBe(93) - expect(s.projectiles).toHaveLength(0) - }) - - it('enemy fire that misses keeps flying', () => { - const s = fresh() - s.projectiles.push({ - id: 501, from: 'enemy', pos: { x: 30, y: 30 }, - vel: { x: 0, y: 0 }, radius: 5, damage: 7, ttl: 1, - debuff: 'slow', debuffDuration: 0, - }) - step(s, [], 0.001) - expect(s.survivor.hp).toBe(100) - expect(s.projectiles).toHaveLength(1) - }) -}) - -describe('game end', () => { - it('director wins when the survivor drops to zero', () => { - const s = fresh() - s.survivor.hp = 5 - makeEnemy(s, 'rusher', { x: CENTER.x, y: CENTER.y }) - step(s, [], 0.01) - expect(s.status).toBe('directorWon') - expect(s.outcomeTime).toBeCloseTo(0.01) - }) - - it('survivor wins when the clock runs out', () => { - const s = fresh(1, 0.05) - step(s, [], 0.1) - expect(s.status).toBe('survivorWon') - }) - - it('a kill on the final tick still counts as a director win', () => { - const s = fresh(1, 0.05) - s.survivor.hp = 5 - makeEnemy(s, 'rusher', { x: CENTER.x, y: CENTER.y }) - step(s, [], 0.1) - expect(s.status).toBe('directorWon') - }) - - it('a finished game is inert', () => { - const s = fresh(1, 0.05) - step(s, [], 0.1) - const t = s.t - step(s, [], 0.1) - expect(s.t).toBe(t) - }) - - it('director actions flow through step', () => { - const s = fresh() - step(s, [{ type: 'spawn', kind: 'rusher' }], 0.001) - expect(s.enemies).toHaveLength(1) - expect(s.director.energy).toBeLessThan(30) - }) -}) - -describe('splitter', () => { - it('dies into three rushers at fixed offsets', () => { - const s = fresh() - const sp = makeEnemy(s, 'splitter', { x: 300, y: 300 }) - shot(s, 300, 300, 999) - step(s, [], 0.001) - const children = s.enemies.filter((e) => e.id !== sp.id) - expect(children).toHaveLength(3) - expect(children.every((c) => c.kind === 'rusher')).toBe(true) - // Children drift toward the survivor within the same tick, so assert the - // spawn ring loosely — the exact geometry is pinned by the test below. - for (const c of children) { - expect(Math.hypot(c.pos.x - 300, c.pos.y - 300)).toBeLessThan(SPLIT_RADIUS + 5) - } - }) - - it('spawns children on a ring at 120-degree offsets', () => { - const s = fresh() - makeEnemy(s, 'splitter', { x: 300, y: 300 }) - shot(s, 300, 300, 999) - // dt of 0 isolates the spawn geometry from the movement that follows it. - step(s, [], 0) - const angles = s.enemies - .map((e) => Math.atan2(e.pos.y - 300, e.pos.x - 300)) - .sort((a, b) => a - b) - expect(angles).toHaveLength(3) - // Pairwise gaps without indexing (noUncheckedIndexedAccess would widen those). - const gaps: number[] = [] - let prev: number | null = null - for (const a of angles) { - if (prev !== null) { - gaps.push(a - prev) - } - prev = a - } - expect(gaps).toHaveLength(2) - for (const g of gaps) { - expect(g).toBeCloseTo((Math.PI * 2) / 3, 5) - } - }) - - it('children are deterministic and draw no RNG', () => { - const run = (): { x: number; y: number }[] => { - const s = fresh(42) - makeEnemy(s, 'splitter', { x: 300, y: 300 }) - shot(s, 300, 300, 999) - step(s, [], 0.001) - return s.enemies.map((e) => ({ x: e.pos.x, y: e.pos.y })) - } - expect(run()).toEqual(run()) - }) - - it('clamps children into the arena when it dies in a corner', () => { - const s = fresh() - makeEnemy(s, 'splitter', { x: 2, y: 2 }) - shot(s, 2, 2, 999) - step(s, [], 0.001) - for (const e of s.enemies) { - expect(e.pos.x).toBeGreaterThanOrEqual(10) - expect(e.pos.y).toBeGreaterThanOrEqual(10) - } - }) - - it('a later projectile in the same tick can hit a fresh child', () => { - const s = fresh() - makeEnemy(s, 'splitter', { x: 300, y: 300 }) - shot(s, 300, 300, 999) - shot(s, 300 + SPLIT_RADIUS, 300, 999) - step(s, [], 0.001) - expect(s.survivor.kills).toBe(2) - }) - - it('non-splitting kinds spawn no children', () => { - const s = fresh() - makeEnemy(s, 'rusher', { x: 300, y: 300 }) - shot(s, 300, 300, 999) - step(s, [], 0.001) - expect(s.enemies).toHaveLength(0) - }) -}) - -describe('new units', () => { - it('artillery outranges the survivor and fires while it cannot', () => { - const s = fresh() - const a = makeEnemy(s, 'artillery', { x: CENTER.x + 400, y: CENTER.y }) - a.fireTimer = 0 - step(s, [], 0.001) - expect(s.projectiles.some((p) => p.from === 'enemy')).toBe(true) - expect(s.projectiles.some((p) => p.from === 'survivor')).toBe(false) - }) - - it('artillery holds station at its keep distance', () => { - const s = fresh() - const a = makeEnemy(s, 'artillery', { x: CENTER.x + 420, y: CENTER.y }) - const before = a.pos.x - step(s, [], 0.016) - expect(a.pos.x).toBeCloseTo(before, 5) - }) - - it('warden closes faster than a rusher', () => { - const s = fresh() - const w = makeEnemy(s, 'warden', { x: CENTER.x + 300, y: CENTER.y }) - const r = makeEnemy(s, 'rusher', { x: CENTER.x + 300, y: CENTER.y }) - step(s, [], 0.016) - expect(CENTER.x + 300 - w.pos.x).toBeGreaterThan(CENTER.x + 300 - r.pos.x) - }) - - it('the hivemind still births rushers via spawnKind', () => { - const s = fresh() - const h = makeEnemy(s, 'hivemind', { x: 40, y: 40 }) - h.spawnTimer = 0 - step(s, [], 0.001) - expect(s.enemies.some((e) => e.kind === 'rusher')).toBe(true) - }) -}) - -describe('status effects', () => { - it('a warden contact mires the survivor and halves its movement', () => { - const slowed = fresh() - slowed.survivor.pos = { x: 100, y: CENTER.y } - makeEnemy(slowed, 'warden', { x: 100, y: CENTER.y }) - step(slowed, [], 0.01) - // Statuses tick in survivorStep, which runs before enemiesStep applies - // contact — so a status landing this tick starts at its full duration. - expect(slowed.survivor.statuses.slow).toBe(2.0) - - // Control: same displacement, an enemy that inflicts nothing. - const control = fresh() - control.survivor.pos = { x: 100, y: CENTER.y } - makeEnemy(control, 'rusher', { x: 100, y: CENTER.y }) - step(control, [], 0.01) - expect(control.survivor.statuses.slow).toBe(0) - - const before = { slowed: slowed.survivor.pos.x, control: control.survivor.pos.x } - slowed.enemies = [] - control.enemies = [] - step(slowed, [], 0.05) - step(control, [], 0.05) - const movedSlowed = slowed.survivor.pos.x - before.slowed - const movedControl = control.survivor.pos.x - before.control - expect(movedSlowed).toBeCloseTo(movedControl * STATUS_POWER.slow, 5) - }) - - it('slow expires and full speed returns', () => { - const s = fresh() - s.survivor.pos = { x: 100, y: CENTER.y } - s.survivor.statuses.slow = 0.02 - step(s, [], 0.05) - expect(s.survivor.statuses.slow).toBe(0) - }) - - it('an artillery shell stifles the survivor and lengthens its reload', () => { - const s = fresh() - s.projectiles.push({ - id: 900, from: 'enemy', pos: { x: CENTER.x, y: CENTER.y }, - vel: { x: 0, y: 0 }, radius: 5, damage: 13, ttl: 1, - debuff: 'suppress', debuffDuration: 2.5, - }) - makeEnemy(s, 'tank', { x: CENTER.x + 200, y: CENTER.y }) - step(s, [], 0.001) - expect(s.survivor.statuses.suppress).toBeGreaterThan(0) - // The shot fired this tick reloaded before the status landed; the next one pays. - s.survivor.fireTimer = 0 - step(s, [], 0.001) - expect(s.survivor.fireTimer).toBeCloseTo(0.55 * STATUS_POWER.suppress, 5) - }) - - it('a leech contact blocks regeneration until it expires', () => { - const s = fresh() - s.survivor.regen = 5 - s.survivor.hp = 50 - makeEnemy(s, 'leech', { x: CENTER.x, y: CENTER.y }) - const hp0 = s.survivor.hp - step(s, [], 0.1) - // Contact damage lands and regen is suppressed, so hp cannot have risen. - expect(s.survivor.hp).toBeLessThan(hp0) - expect(s.survivor.statuses.bleed).toBeGreaterThan(0) - - s.enemies = [] - s.survivor.statuses.bleed = 0 - const hp1 = s.survivor.hp - step(s, [], 0.1) - expect(s.survivor.hp).toBeGreaterThan(hp1) - }) - - it('repeated warden contacts refresh rather than stack', () => { - const s = fresh() - makeEnemy(s, 'warden', { x: CENTER.x, y: CENTER.y }) - makeEnemy(s, 'warden', { x: CENTER.x, y: CENTER.y }) - step(s, [], 0.01) - expect(s.survivor.statuses.slow).toBeLessThanOrEqual(2.0) - }) - - it('a plain rusher inflicts no status at all', () => { - const s = fresh() - makeEnemy(s, 'rusher', { x: CENTER.x, y: CENTER.y }) - step(s, [], 0.01) - expect(s.survivor.statuses).toEqual({ slow: 0, suppress: 0, bleed: 0 }) - }) - - it('survivor fire never debuffs the survivor', () => { - const s = fresh() - makeEnemy(s, 'tank', { x: CENTER.x + 200, y: CENTER.y }) - step(s, [], 0.001) - const own = s.projectiles.find((p) => p.from === 'survivor') - expect(own?.debuffDuration).toBe(0) - }) -}) - -describe('frenzy', () => { - it('speeds the horde while it lasts', () => { - const base = fresh() - const b = makeEnemy(base, 'rusher', { x: CENTER.x + 300, y: CENTER.y }) - step(base, [], 0.05) - const normal = CENTER.x + 300 - b.pos.x - - const hyped = fresh() - hyped.director.frenzyTimer = 5 - const h = makeEnemy(hyped, 'rusher', { x: CENTER.x + 300, y: CENTER.y }) - step(hyped, [], 0.05) - const rushed = CENTER.x + 300 - h.pos.x - - expect(rushed).toBeCloseTo(normal * FRENZY_SPEED, 5) - }) - - it('raises contact damage while it lasts', () => { - const s = fresh() - s.director.frenzyTimer = 5 - makeEnemy(s, 'rusher', { x: CENTER.x, y: CENTER.y }) - step(s, [], 0.01) - expect(s.survivor.hp).toBeCloseTo(100 - 6 * FRENZY_DAMAGE, 5) - }) - - it('expires back to baseline speed', () => { - const s = fresh() - s.director.frenzyTimer = 0.01 - const e = makeEnemy(s, 'rusher', { x: CENTER.x + 300, y: CENTER.y }) - step(s, [], 0.5) - expect(s.director.frenzyTimer).toBe(0) - const after = e.pos.x - step(s, [], 0.05) - const moved = after - e.pos.x - expect(moved).toBeCloseTo(170 * 0.05, 5) - }) -}) - -describe('difficulty tiers', () => { - it('defaults to normal, the identity tier', () => { - const s = fresh() - expect(s.difficulty).toBe(DIFFICULTIES.normal) - expect(s.survivor.maxHp).toBe(100) - }) - - it('accepts an explicit tier', () => { - const s = createInitialState(1, undefined, 'crusade') - expect(s.difficulty).toBe(DIFFICULTIES.crusade) - }) - - it('takes its duration from the tier when none is given', () => { - expect(createInitialState(1, undefined, 'haunting').duration) - .toBe(DIFFICULTIES.haunting.duration) - }) - - it('an explicit duration still wins over the tier', () => { - expect(createInitialState(1, 42, 'haunting').duration).toBe(42) - }) - - it('scales enemy hp', () => { - const s = createInitialState(1, undefined, 'crusade') - const e = makeEnemy(s, 'rusher', { x: 0, y: 0 }) - expect(e.hp).toBeCloseTo(14 * DIFFICULTIES.crusade.enemyHp, 5) - }) - - it('scales survivor hp', () => { - const s = createInitialState(1, undefined, 'haunting') - expect(s.survivor.maxHp).toBe(125) - }) - - it('scales enemy speed', () => { - const s = createInitialState(1, undefined, 'crusade') - const e = makeEnemy(s, 'rusher', { x: CENTER.x + 300, y: CENTER.y }) - step(s, [], 0.05) - const moved = CENTER.x + 300 - e.pos.x - expect(moved).toBeCloseTo(170 * DIFFICULTIES.crusade.enemySpeed * 0.05, 5) - }) -}) - -describe('frenzy damage reaches every channel', () => { - // Regression: frenzy multiplied contact damage only, so ranged and blast - // units — including the bomber, whose whole purpose is damage — ignored it. - const dealt = (frenzied: boolean, kind: 'warden' | 'artillery' | 'bomber', at: number): number => { - const s = fresh(3) - s.survivor.hp = 1e6 - s.survivor.maxHp = 1e6 - s.survivor.speed = 0 - s.duration = 1e6 - if (frenzied) { - s.director.frenzyTimer = 1e6 - } - makeEnemy(s, kind, { x: CENTER.x + at, y: CENTER.y }) - const before = s.survivor.hp - for (let i = 0; i < 360; i += 1) { - step(s, [], 1 / 60) - } - return before - s.survivor.hp - } - - it('scales contact damage', () => { - expect(dealt(true, 'warden', 0)).toBeCloseTo(dealt(false, 'warden', 0) * FRENZY_DAMAGE, 5) - }) - - it('scales ranged damage', () => { - expect(dealt(true, 'artillery', 300)) - .toBeCloseTo(dealt(false, 'artillery', 300) * FRENZY_DAMAGE, 5) - }) - - it('scales blast damage', () => { - expect(dealt(true, 'bomber', 0)).toBeCloseTo(dealt(false, 'bomber', 0) * FRENZY_DAMAGE, 5) - }) -}) diff --git a/reverse_survivors/src/core/simEnemies.test.ts b/reverse_survivors/src/core/simEnemies.test.ts new file mode 100644 index 00000000..44ebb50d --- /dev/null +++ b/reverse_survivors/src/core/simEnemies.test.ts @@ -0,0 +1,236 @@ +/** Enemy behaviour, bombers and projectiles. */ + +import { describe, expect, it } from 'vitest' +import { + createInitialState, makeEnemy, step, + } from './sim' +import type { GameState, Projectile } from './types' +import { + ARENA, } from './types' + +const CENTER = { x: ARENA.w / 2, y: ARENA.h / 2 } + +const fresh = (seed = 1, duration?: number): GameState => + duration === undefined ? createInitialState(seed) : createInitialState(seed, duration) + +const shot = (state: GameState, x: number, y: number, damage: number): Projectile => { + const p: Projectile = { + id: state.nextId, + from: 'survivor', + pos: { x, y }, + vel: { x: 0, y: 0 }, + radius: 4, + damage, + ttl: 1, + debuff: 'slow', + debuffDuration: 0, + } + state.nextId += 1 + state.projectiles.push(p) + return p +} + +describe('enemy behaviors', () => { + it('chasers close the distance', () => { + const s = fresh() + const e = makeEnemy(s, 'rusher', { x: CENTER.x + 300, y: CENTER.y }) + step(s, [], 0.1) + expect(e.pos.x).toBeLessThan(CENTER.x + 300) + }) + + it('melee contact damages the survivor once per cooldown window', () => { + const s = fresh() + makeEnemy(s, 'rusher', { x: CENTER.x, y: CENTER.y }) + step(s, [], 0.01) + expect(s.survivor.hp).toBe(100 - 6) + step(s, [], 0.01) + expect(s.survivor.hp).toBe(100 - 6) + }) + + it('tank hits harder', () => { + const s = fresh() + makeEnemy(s, 'tank', { x: CENTER.x + 5, y: CENTER.y }) + step(s, [], 0.01) + expect(s.survivor.hp).toBe(100 - 14) + }) + + it('stalker approaches when too far', () => { + const s = fresh() + const e = makeEnemy(s, 'stalker', { x: CENTER.x + 300, y: CENTER.y }) + step(s, [], 0.05) + expect(e.pos.x).toBeLessThan(CENTER.x + 300) + }) + + it('stalker retreats when too close', () => { + const s = fresh() + const e = makeEnemy(s, 'stalker', { x: CENTER.x + 100, y: CENTER.y }) + step(s, [], 0.05) + expect(e.pos.x).toBeGreaterThan(CENTER.x + 100) + }) + + it('stalker holds position inside its comfort band', () => { + const s = fresh() + const e = makeEnemy(s, 'stalker', { x: CENTER.x + 160, y: CENTER.y }) + const before = e.pos.x + step(s, [], 0.05) + expect(e.pos.x).toBe(before) + }) + + it('stalker fires when in range and ready, then cools down', () => { + const s = fresh() + makeEnemy(s, 'stalker', { x: CENTER.x + 220, y: CENTER.y }) + step(s, [], 0.01) + expect(s.projectiles.filter((p) => p.from === 'enemy')).toHaveLength(1) + step(s, [], 0.01) + expect(s.projectiles.filter((p) => p.from === 'enemy')).toHaveLength(1) + }) + + it('stalker does not fire from beyond its range', () => { + const s = fresh() + makeEnemy(s, 'stalker', { x: CENTER.x + 400, y: CENTER.y }) + step(s, [], 0.01) + expect(s.projectiles.filter((p) => p.from === 'enemy')).toHaveLength(0) + }) + + it('hivemind emits a rusher on its spawn cadence', () => { + const s = fresh() + const hive = makeEnemy(s, 'hivemind', { x: 900, y: 560 }) + step(s, [], 2.5) + expect(s.enemies.filter((e) => e.kind === 'rusher')).toHaveLength(1) + expect(hive.spawnTimer).toBe(2.5) + step(s, [], 0.1) + expect(s.enemies.filter((e) => e.kind === 'rusher')).toHaveLength(1) + }) +}) + +describe('bomber', () => { + it('detonates on contact: damages the survivor, dies, grants nothing', () => { + const s = fresh() + makeEnemy(s, 'bomber', { x: CENTER.x, y: CENTER.y }) + step(s, [], 0.01) + expect(s.survivor.hp).toBe(100 - 22) + expect(s.enemies).toHaveLength(0) + expect(s.survivor.kills).toBe(0) + expect(s.survivor.xp).toBe(0) + }) + + it('sniped from afar: grants xp, blast misses the survivor', () => { + const s = fresh() + makeEnemy(s, 'bomber', { x: CENTER.x + 300, y: CENTER.y }) + shot(s, CENTER.x + 300, CENTER.y, 99) + step(s, [], 0.001) + expect(s.survivor.kills).toBe(1) + expect(s.survivor.xp).toBe(10) + expect(s.survivor.hp).toBe(100) + expect(s.enemies).toHaveLength(0) + }) + + it('shot inside blast range: grants xp and still burns the survivor', () => { + const s = fresh() + makeEnemy(s, 'bomber', { x: CENTER.x + 30, y: CENTER.y }) + shot(s, CENTER.x + 30, CENTER.y, 99) + step(s, [], 0.001) + expect(s.survivor.kills).toBe(1) + expect(s.survivor.hp).toBeCloseTo(100 - 22, 5) + }) +}) + +describe('projectiles', () => { + it('expire by ttl', () => { + const s = fresh() + shot(s, 50, 50, 1) + step(s, [], 2) + expect(s.projectiles).toHaveLength(0) + }) + + it('a miss keeps flying', () => { + const s = fresh() + shot(s, 50, 50, 1) + step(s, [], 0.01) + expect(s.projectiles).toHaveLength(1) + }) + + it('damages without killing when the target outlasts the hit', () => { + const s = fresh() + const tank = makeEnemy(s, 'tank', { x: CENTER.x + 400, y: CENTER.y }) + shot(s, CENTER.x + 400, CENTER.y, 8) + step(s, [], 0.001) + expect(tank.hp).toBe(112) + expect(s.survivor.kills).toBe(0) + expect(s.enemies).toHaveLength(1) + }) + + it('a second projectile in the same tick passes through an already-dead target', () => { + const s = fresh() + makeEnemy(s, 'rusher', { x: CENTER.x + 400, y: CENTER.y }) + shot(s, CENTER.x + 400, CENTER.y, 20) + shot(s, CENTER.x + 400, CENTER.y, 20) + step(s, [], 0.001) + expect(s.survivor.kills).toBe(1) + expect(s.projectiles).toHaveLength(1) + }) + + it('enemy fire hurts the survivor', () => { + const s = fresh() + s.projectiles.push({ + id: 500, from: 'enemy', pos: { x: CENTER.x, y: CENTER.y }, + vel: { x: 0, y: 0 }, radius: 5, damage: 7, ttl: 1, + debuff: 'slow', debuffDuration: 0, + }) + step(s, [], 0.001) + expect(s.survivor.hp).toBe(93) + expect(s.projectiles).toHaveLength(0) + }) + + it('enemy fire that misses keeps flying', () => { + const s = fresh() + s.projectiles.push({ + id: 501, from: 'enemy', pos: { x: 30, y: 30 }, + vel: { x: 0, y: 0 }, radius: 5, damage: 7, ttl: 1, + debuff: 'slow', debuffDuration: 0, + }) + step(s, [], 0.001) + expect(s.survivor.hp).toBe(100) + expect(s.projectiles).toHaveLength(1) + }) +}) + +describe('game end', () => { + it('director wins when the survivor drops to zero', () => { + const s = fresh() + s.survivor.hp = 5 + makeEnemy(s, 'rusher', { x: CENTER.x, y: CENTER.y }) + step(s, [], 0.01) + expect(s.status).toBe('directorWon') + expect(s.outcomeTime).toBeCloseTo(0.01) + }) + + it('survivor wins when the clock runs out', () => { + const s = fresh(1, 0.05) + step(s, [], 0.1) + expect(s.status).toBe('survivorWon') + }) + + it('a kill on the final tick still counts as a director win', () => { + const s = fresh(1, 0.05) + s.survivor.hp = 5 + makeEnemy(s, 'rusher', { x: CENTER.x, y: CENTER.y }) + step(s, [], 0.1) + expect(s.status).toBe('directorWon') + }) + + it('a finished game is inert', () => { + const s = fresh(1, 0.05) + step(s, [], 0.1) + const t = s.t + step(s, [], 0.1) + expect(s.t).toBe(t) + }) + + it('director actions flow through step', () => { + const s = fresh() + step(s, [{ type: 'spawn', kind: 'rusher' }], 0.001) + expect(s.enemies).toHaveLength(1) + expect(s.director.energy).toBeLessThan(30) + }) +}) diff --git a/reverse_survivors/src/core/simFrenzy.test.ts b/reverse_survivors/src/core/simFrenzy.test.ts new file mode 100644 index 00000000..84b56793 --- /dev/null +++ b/reverse_survivors/src/core/simFrenzy.test.ts @@ -0,0 +1,131 @@ +/** Frenzy, difficulty tiers and damage channels. */ + +/** Splitters, new units and status effects. */ + +/** Enemy behaviour, bombers and projectiles. */ + +import { describe, expect, it } from 'vitest' +import { + createInitialState, makeEnemy, step, + } from './sim' +import type { GameState, } from './types' +import { + ARENA, DIFFICULTIES, FRENZY_DAMAGE, FRENZY_SPEED, } from './types' + +const CENTER = { x: ARENA.w / 2, y: ARENA.h / 2 } + +const fresh = (seed = 1, duration?: number): GameState => + duration === undefined ? createInitialState(seed) : createInitialState(seed, duration) + + +describe('frenzy', () => { + it('speeds the horde while it lasts', () => { + const base = fresh() + const b = makeEnemy(base, 'rusher', { x: CENTER.x + 300, y: CENTER.y }) + step(base, [], 0.05) + const normal = CENTER.x + 300 - b.pos.x + + const hyped = fresh() + hyped.director.frenzyTimer = 5 + const h = makeEnemy(hyped, 'rusher', { x: CENTER.x + 300, y: CENTER.y }) + step(hyped, [], 0.05) + const rushed = CENTER.x + 300 - h.pos.x + + expect(rushed).toBeCloseTo(normal * FRENZY_SPEED, 5) + }) + + it('raises contact damage while it lasts', () => { + const s = fresh() + s.director.frenzyTimer = 5 + makeEnemy(s, 'rusher', { x: CENTER.x, y: CENTER.y }) + step(s, [], 0.01) + expect(s.survivor.hp).toBeCloseTo(100 - 6 * FRENZY_DAMAGE, 5) + }) + + it('expires back to baseline speed', () => { + const s = fresh() + s.director.frenzyTimer = 0.01 + const e = makeEnemy(s, 'rusher', { x: CENTER.x + 300, y: CENTER.y }) + step(s, [], 0.5) + expect(s.director.frenzyTimer).toBe(0) + const after = e.pos.x + step(s, [], 0.05) + const moved = after - e.pos.x + expect(moved).toBeCloseTo(170 * 0.05, 5) + }) +}) + +describe('difficulty tiers', () => { + it('defaults to normal, the identity tier', () => { + const s = fresh() + expect(s.difficulty).toBe(DIFFICULTIES.normal) + expect(s.survivor.maxHp).toBe(100) + }) + + it('accepts an explicit tier', () => { + const s = createInitialState(1, undefined, 'crusade') + expect(s.difficulty).toBe(DIFFICULTIES.crusade) + }) + + it('takes its duration from the tier when none is given', () => { + expect(createInitialState(1, undefined, 'haunting').duration) + .toBe(DIFFICULTIES.haunting.duration) + }) + + it('an explicit duration still wins over the tier', () => { + expect(createInitialState(1, 42, 'haunting').duration).toBe(42) + }) + + it('scales enemy hp', () => { + const s = createInitialState(1, undefined, 'crusade') + const e = makeEnemy(s, 'rusher', { x: 0, y: 0 }) + expect(e.hp).toBeCloseTo(14 * DIFFICULTIES.crusade.enemyHp, 5) + }) + + it('scales survivor hp', () => { + const s = createInitialState(1, undefined, 'haunting') + expect(s.survivor.maxHp).toBe(125) + }) + + it('scales enemy speed', () => { + const s = createInitialState(1, undefined, 'crusade') + const e = makeEnemy(s, 'rusher', { x: CENTER.x + 300, y: CENTER.y }) + step(s, [], 0.05) + const moved = CENTER.x + 300 - e.pos.x + expect(moved).toBeCloseTo(170 * DIFFICULTIES.crusade.enemySpeed * 0.05, 5) + }) +}) + +describe('frenzy damage reaches every channel', () => { + // Regression: frenzy multiplied contact damage only, so ranged and blast + // units — including the bomber, whose whole purpose is damage — ignored it. + const dealt = (frenzied: boolean, kind: 'warden' | 'artillery' | 'bomber', at: number): number => { + const s = fresh(3) + s.survivor.hp = 1e6 + s.survivor.maxHp = 1e6 + s.survivor.speed = 0 + s.duration = 1e6 + if (frenzied) { + s.director.frenzyTimer = 1e6 + } + makeEnemy(s, kind, { x: CENTER.x + at, y: CENTER.y }) + const before = s.survivor.hp + for (let i = 0; i < 360; i += 1) { + step(s, [], 1 / 60) + } + return before - s.survivor.hp + } + + it('scales contact damage', () => { + expect(dealt(true, 'warden', 0)).toBeCloseTo(dealt(false, 'warden', 0) * FRENZY_DAMAGE, 5) + }) + + it('scales ranged damage', () => { + expect(dealt(true, 'artillery', 300)) + .toBeCloseTo(dealt(false, 'artillery', 300) * FRENZY_DAMAGE, 5) + }) + + it('scales blast damage', () => { + expect(dealt(true, 'bomber', 0)).toBeCloseTo(dealt(false, 'bomber', 0) * FRENZY_DAMAGE, 5) + }) +}) diff --git a/reverse_survivors/src/core/simUnits.test.ts b/reverse_survivors/src/core/simUnits.test.ts new file mode 100644 index 00000000..0356680f --- /dev/null +++ b/reverse_survivors/src/core/simUnits.test.ts @@ -0,0 +1,243 @@ +/** Splitters, new units and status effects. */ + +/** Enemy behaviour, bombers and projectiles. */ + +import { describe, expect, it } from 'vitest' +import { + createInitialState, makeEnemy, SPLIT_RADIUS, step, + } from './sim' +import type { GameState, Projectile } from './types' +import { + ARENA, STATUS_POWER, +} from './types' + +const CENTER = { x: ARENA.w / 2, y: ARENA.h / 2 } + +const fresh = (seed = 1, duration?: number): GameState => + duration === undefined ? createInitialState(seed) : createInitialState(seed, duration) + +const shot = (state: GameState, x: number, y: number, damage: number): Projectile => { + const p: Projectile = { + id: state.nextId, + from: 'survivor', + pos: { x, y }, + vel: { x: 0, y: 0 }, + radius: 4, + damage, + ttl: 1, + debuff: 'slow', + debuffDuration: 0, + } + state.nextId += 1 + state.projectiles.push(p) + return p +} + +describe('splitter', () => { + it('dies into three rushers at fixed offsets', () => { + const s = fresh() + const sp = makeEnemy(s, 'splitter', { x: 300, y: 300 }) + shot(s, 300, 300, 999) + step(s, [], 0.001) + const children = s.enemies.filter((e) => e.id !== sp.id) + expect(children).toHaveLength(3) + expect(children.every((c) => c.kind === 'rusher')).toBe(true) + // Children drift toward the survivor within the same tick, so assert the + // spawn ring loosely — the exact geometry is pinned by the test below. + for (const c of children) { + expect(Math.hypot(c.pos.x - 300, c.pos.y - 300)).toBeLessThan(SPLIT_RADIUS + 5) + } + }) + + it('spawns children on a ring at 120-degree offsets', () => { + const s = fresh() + makeEnemy(s, 'splitter', { x: 300, y: 300 }) + shot(s, 300, 300, 999) + // dt of 0 isolates the spawn geometry from the movement that follows it. + step(s, [], 0) + const angles = s.enemies + .map((e) => Math.atan2(e.pos.y - 300, e.pos.x - 300)) + .sort((a, b) => a - b) + expect(angles).toHaveLength(3) + // Pairwise gaps without indexing (noUncheckedIndexedAccess would widen those). + const gaps: number[] = [] + let prev: number | null = null + for (const a of angles) { + if (prev !== null) { + gaps.push(a - prev) + } + prev = a + } + expect(gaps).toHaveLength(2) + for (const g of gaps) { + expect(g).toBeCloseTo((Math.PI * 2) / 3, 5) + } + }) + + it('children are deterministic and draw no RNG', () => { + const run = (): { x: number; y: number }[] => { + const s = fresh(42) + makeEnemy(s, 'splitter', { x: 300, y: 300 }) + shot(s, 300, 300, 999) + step(s, [], 0.001) + return s.enemies.map((e) => ({ x: e.pos.x, y: e.pos.y })) + } + expect(run()).toEqual(run()) + }) + + it('clamps children into the arena when it dies in a corner', () => { + const s = fresh() + makeEnemy(s, 'splitter', { x: 2, y: 2 }) + shot(s, 2, 2, 999) + step(s, [], 0.001) + for (const e of s.enemies) { + expect(e.pos.x).toBeGreaterThanOrEqual(10) + expect(e.pos.y).toBeGreaterThanOrEqual(10) + } + }) + + it('a later projectile in the same tick can hit a fresh child', () => { + const s = fresh() + makeEnemy(s, 'splitter', { x: 300, y: 300 }) + shot(s, 300, 300, 999) + shot(s, 300 + SPLIT_RADIUS, 300, 999) + step(s, [], 0.001) + expect(s.survivor.kills).toBe(2) + }) + + it('non-splitting kinds spawn no children', () => { + const s = fresh() + makeEnemy(s, 'rusher', { x: 300, y: 300 }) + shot(s, 300, 300, 999) + step(s, [], 0.001) + expect(s.enemies).toHaveLength(0) + }) +}) + +describe('new units', () => { + it('artillery outranges the survivor and fires while it cannot', () => { + const s = fresh() + const a = makeEnemy(s, 'artillery', { x: CENTER.x + 400, y: CENTER.y }) + a.fireTimer = 0 + step(s, [], 0.001) + expect(s.projectiles.some((p) => p.from === 'enemy')).toBe(true) + expect(s.projectiles.some((p) => p.from === 'survivor')).toBe(false) + }) + + it('artillery holds station at its keep distance', () => { + const s = fresh() + const a = makeEnemy(s, 'artillery', { x: CENTER.x + 420, y: CENTER.y }) + const before = a.pos.x + step(s, [], 0.016) + expect(a.pos.x).toBeCloseTo(before, 5) + }) + + it('warden closes faster than a rusher', () => { + const s = fresh() + const w = makeEnemy(s, 'warden', { x: CENTER.x + 300, y: CENTER.y }) + const r = makeEnemy(s, 'rusher', { x: CENTER.x + 300, y: CENTER.y }) + step(s, [], 0.016) + expect(CENTER.x + 300 - w.pos.x).toBeGreaterThan(CENTER.x + 300 - r.pos.x) + }) + + it('the hivemind still births rushers via spawnKind', () => { + const s = fresh() + const h = makeEnemy(s, 'hivemind', { x: 40, y: 40 }) + h.spawnTimer = 0 + step(s, [], 0.001) + expect(s.enemies.some((e) => e.kind === 'rusher')).toBe(true) + }) +}) + +describe('status effects', () => { + it('a warden contact mires the survivor and halves its movement', () => { + const slowed = fresh() + slowed.survivor.pos = { x: 100, y: CENTER.y } + makeEnemy(slowed, 'warden', { x: 100, y: CENTER.y }) + step(slowed, [], 0.01) + // Statuses tick in survivorStep, which runs before enemiesStep applies + // contact — so a status landing this tick starts at its full duration. + expect(slowed.survivor.statuses.slow).toBe(2.0) + + // Control: same displacement, an enemy that inflicts nothing. + const control = fresh() + control.survivor.pos = { x: 100, y: CENTER.y } + makeEnemy(control, 'rusher', { x: 100, y: CENTER.y }) + step(control, [], 0.01) + expect(control.survivor.statuses.slow).toBe(0) + + const before = { slowed: slowed.survivor.pos.x, control: control.survivor.pos.x } + slowed.enemies = [] + control.enemies = [] + step(slowed, [], 0.05) + step(control, [], 0.05) + const movedSlowed = slowed.survivor.pos.x - before.slowed + const movedControl = control.survivor.pos.x - before.control + expect(movedSlowed).toBeCloseTo(movedControl * STATUS_POWER.slow, 5) + }) + + it('slow expires and full speed returns', () => { + const s = fresh() + s.survivor.pos = { x: 100, y: CENTER.y } + s.survivor.statuses.slow = 0.02 + step(s, [], 0.05) + expect(s.survivor.statuses.slow).toBe(0) + }) + + it('an artillery shell stifles the survivor and lengthens its reload', () => { + const s = fresh() + s.projectiles.push({ + id: 900, from: 'enemy', pos: { x: CENTER.x, y: CENTER.y }, + vel: { x: 0, y: 0 }, radius: 5, damage: 13, ttl: 1, + debuff: 'suppress', debuffDuration: 2.5, + }) + makeEnemy(s, 'tank', { x: CENTER.x + 200, y: CENTER.y }) + step(s, [], 0.001) + expect(s.survivor.statuses.suppress).toBeGreaterThan(0) + // The shot fired this tick reloaded before the status landed; the next one pays. + s.survivor.fireTimer = 0 + step(s, [], 0.001) + expect(s.survivor.fireTimer).toBeCloseTo(0.55 * STATUS_POWER.suppress, 5) + }) + + it('a leech contact blocks regeneration until it expires', () => { + const s = fresh() + s.survivor.regen = 5 + s.survivor.hp = 50 + makeEnemy(s, 'leech', { x: CENTER.x, y: CENTER.y }) + const hp0 = s.survivor.hp + step(s, [], 0.1) + // Contact damage lands and regen is suppressed, so hp cannot have risen. + expect(s.survivor.hp).toBeLessThan(hp0) + expect(s.survivor.statuses.bleed).toBeGreaterThan(0) + + s.enemies = [] + s.survivor.statuses.bleed = 0 + const hp1 = s.survivor.hp + step(s, [], 0.1) + expect(s.survivor.hp).toBeGreaterThan(hp1) + }) + + it('repeated warden contacts refresh rather than stack', () => { + const s = fresh() + makeEnemy(s, 'warden', { x: CENTER.x, y: CENTER.y }) + makeEnemy(s, 'warden', { x: CENTER.x, y: CENTER.y }) + step(s, [], 0.01) + expect(s.survivor.statuses.slow).toBeLessThanOrEqual(2.0) + }) + + it('a plain rusher inflicts no status at all', () => { + const s = fresh() + makeEnemy(s, 'rusher', { x: CENTER.x, y: CENTER.y }) + step(s, [], 0.01) + expect(s.survivor.statuses).toEqual({ slow: 0, suppress: 0, bleed: 0 }) + }) + + it('survivor fire never debuffs the survivor', () => { + const s = fresh() + makeEnemy(s, 'tank', { x: CENTER.x + 200, y: CENTER.y }) + step(s, [], 0.001) + const own = s.projectiles.find((p) => p.from === 'survivor') + expect(own?.debuffDuration).toBe(0) + }) +})