testsAndMisc/reverse_survivors/src/core/simFrenzy.test.ts
Krzysztof kuhy Rudnicki 1342a60a65
Some checks are pending
Pre-commit checks / pre-commit (push) Waiting to run
reverse-survivors CI / check (push) Waiting to run
Shell tests / shell-tests (push) Waiting to run
Shell tests / arch-tests (push) Waiting to run
Split reverse_survivors sim.ts 361 -> 69 + three modules
Split along actor lines, but not the obvious way. A plain sim/enemies cut
needs makeEnemy and grantXp going out and enemiesStep coming back -- a cycle.
Nothing here would have caught it: no eslint-plugin-import, and tsc and vitest
both pass on cyclic ES modules. It shows up as a TDZ error in the bundle.

So the graph is a tree instead:

  simUnit.ts (42)      leaf -- geometry, projectile constants, makeEnemy,
                       enemySpeed; imports only types and vec
  simSurvivor.ts (132) createSurvivor, xp/upgrades, survivorStep
  simThreats.ts (168)  enemiesStep, projectilesStep, and enemy death
  sim.ts (69)          createInitialState and step, the only module that
                       reaches all three

Repointing director.ts's makeEnemy at the leaf also removes the sim <->
director cycle that was already there, surviving on const-arrow late binding.

npm run check and npm run build both pass -- 160 tests, coverage identical at
531/159/101/514, 32 modules bundled at 210.09 kB against 210.11 kB before.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011w2Pj4Ek3ZNMQVYcFHgXDY
2026-08-17 09:52:19 +02:00

131 lines
4.2 KiB
TypeScript

/** 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, step } from './sim'
import { makeEnemy } from './simUnit'
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)
})
})