50 lines
2.1 KiB
JavaScript
50 lines
2.1 KiB
JavaScript
import { readFileSync } from 'node:fs';
|
|
import { performance } from 'node:perf_hooks';
|
|
|
|
const source = readFileSync(new URL('../game.js', import.meta.url), 'utf8');
|
|
const literal = name => {
|
|
const marker = `const ${name} = [`;
|
|
const start = source.indexOf(marker);
|
|
if (start < 0) throw new Error(`Missing ${name}`);
|
|
const body = start + marker.length - 1;
|
|
let depth = 0;
|
|
for (let index = body; index < source.length; index++) {
|
|
if (source[index] === '[') depth++;
|
|
if (source[index] === ']' && --depth === 0) return source.slice(body, index + 1);
|
|
}
|
|
throw new Error(`Unclosed ${name}`);
|
|
};
|
|
|
|
const roomWidth = Number(source.match(/ROOM_W = (\d+)/)?.[1]);
|
|
const layouts = Function(`return ${literal('ROOM_LAYOUTS')}`)();
|
|
const platforms = layouts.flatMap((layout, room) => {
|
|
const start = room * roomWidth;
|
|
return [
|
|
...layout.ground.map(([x, w]) => ({ x: start + x, y: 610, w, h: 130 })),
|
|
...layout.ledges.map(([x, y, w]) => ({ x: start + x, y, w, h: 24 }))
|
|
];
|
|
});
|
|
const byRoom = layouts.map((_, room) => platforms.filter(platform => Math.floor(platform.x / roomWidth) === room));
|
|
const entity = { x: 180, y: 505, w: 38, h: 78 };
|
|
const overlap = (a, b) => a.x < b.x + b.w && a.x + a.w > b.x && a.y < b.y + b.h && a.y + a.h > b.y;
|
|
const iterations = 180_000;
|
|
|
|
const run = candidates => {
|
|
let hits = 0;
|
|
const started = performance.now();
|
|
for (let iteration = 0; iteration < iterations; iteration++) {
|
|
entity.x = (iteration * 37) % roomWidth;
|
|
for (const platform of candidates) if (overlap(entity, platform)) hits++;
|
|
}
|
|
return { ms: performance.now() - started, hits };
|
|
};
|
|
|
|
const globalScan = run(platforms);
|
|
const localScan = run(byRoom[0]);
|
|
const ratio = platforms.length / byRoom[0].length;
|
|
console.log(`Global candidates per pass: ${platforms.length}`);
|
|
console.log(`Room-local candidates per pass: ${byRoom[0].length}`);
|
|
console.log(`Candidate reduction: ${ratio.toFixed(2)}x`);
|
|
console.log(`Synthetic ${iterations.toLocaleString()}-pass scan: ${globalScan.ms.toFixed(2)}ms -> ${localScan.ms.toFixed(2)}ms`);
|
|
if (globalScan.hits !== localScan.hits || ratio < 8) process.exitCode = 1;
|