refactor: tighten core seams and spawning
This commit is contained in:
@@ -41,4 +41,21 @@ describe('Rng', () => {
|
||||
const shuffled = r.shuffle([...arr]);
|
||||
expect([...shuffled].sort()).toEqual(arr);
|
||||
});
|
||||
|
||||
it('авто-seed не вызывает Math.random()', () => {
|
||||
const math = Math as Math & { random: () => number };
|
||||
const original = math.random;
|
||||
math.random = () => { throw new Error('Math.random вызван'); };
|
||||
try {
|
||||
const r = new Rng();
|
||||
expect(r.next()).toBeGreaterThanOrEqual(0);
|
||||
expect(r.next()).toBeLessThan(1);
|
||||
} finally {
|
||||
math.random = original;
|
||||
}
|
||||
});
|
||||
|
||||
it('pick явно ругается на пустой массив', () => {
|
||||
expect(() => new Rng(1).pick([])).toThrow('пустой массив');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -4,6 +4,17 @@ import { Room } from '../src/core/world/Room';
|
||||
import { Rng } from '../src/core/rng';
|
||||
import { OX, OY, TILE, DOOR, SPAWN } from '../src/config';
|
||||
import { dist } from '../src/core/util';
|
||||
import { DEFAULT_RULES, type LevelRules } from '../src/core/rules';
|
||||
|
||||
const denseRules: LevelRules = {
|
||||
...DEFAULT_RULES,
|
||||
id: 'dense-test',
|
||||
name: 'Тестовая тесная комната',
|
||||
description: 'Много врагов, чтобы проверить отказ от плохих позиций спавна.',
|
||||
map: { ...DEFAULT_RULES.map },
|
||||
player: { ...DEFAULT_RULES.player },
|
||||
enemies: { ...DEFAULT_RULES.enemies, densityMul: 40 },
|
||||
};
|
||||
|
||||
describe('spawner', () => {
|
||||
it('в обычной комнате врагов в ожидаемом диапазоне', () => {
|
||||
@@ -40,4 +51,24 @@ describe('spawner', () => {
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
it('РЕГРЕССИЯ: при переполнении не ставит врага в последнюю плохую точку', () => {
|
||||
const room = new Room(1, 0, 'normal');
|
||||
const playerX = OX + 7 * TILE;
|
||||
const playerY = OY + 5 * TILE;
|
||||
const door = DOOR.left;
|
||||
const doorX = OX + door.cx * TILE + TILE / 2;
|
||||
const doorY = OY + door.cy * TILE + TILE / 2;
|
||||
const enemies = spawnEnemies(room, 'left', playerX, playerY, new Rng(12), denseRules);
|
||||
|
||||
expect(enemies.length).toBeGreaterThan(0);
|
||||
for (let i = 0; i < enemies.length; i++) {
|
||||
const e = enemies[i];
|
||||
expect(dist(e.x, e.y, doorX, doorY)).toBeGreaterThanOrEqual(SPAWN.minDistFromDoor);
|
||||
expect(dist(e.x, e.y, playerX, playerY)).toBeGreaterThanOrEqual(SPAWN.minDistFromPlayer);
|
||||
for (let j = i + 1; j < enemies.length; j++) {
|
||||
expect(dist(e.x, e.y, enemies[j].x, enemies[j].y)).toBeGreaterThanOrEqual(SPAWN.minDistBetween);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user