Files
mfyheart/tools/check_enemy_ai.mjs

76 lines
3.3 KiB
JavaScript

import { readFileSync } from 'node:fs';
const source = readFileSync(new URL('../game.js', import.meta.url), 'utf8');
function arrayLiteral(name) {
const marker = `const ${name} = [`;
const start = source.indexOf(marker);
if (start < 0) throw new Error(`Missing ${name}`);
const bodyStart = start + marker.length - 1;
let depth = 0;
for (let index = bodyStart; index < source.length; index++) {
if (source[index] === '[') depth++;
if (source[index] === ']' && --depth === 0) return source.slice(bodyStart, index + 1);
}
throw new Error(`Unclosed ${name}`);
}
const roomLayouts = Function(`return ${arrayLiteral('ROOM_LAYOUTS')}`)();
const roomWidth = Number(source.match(/ROOM_W = (\d+)/)?.[1]);
const platforms = roomLayouts.flatMap((layout, index) => {
const start = index * 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 safeZones = Function(`return ${arrayLiteral('safeZones')}`)();
const sizes = { shade: { w: 46, h: 55, speed: 112 }, crawler: { w: 54, h: 34, speed: 178 } };
const spawns = [...source.matchAll(/makeEnemy\((\d+),(\d+),'(shade|crawler)'\)/g)].map(match => ({
x: Number(match[1]), y: Number(match[2]), type: match[3], ...sizes[match[3]]
}));
const supportBelow = enemy => platforms
.filter(platform => enemy.x + enemy.w / 2 >= platform.x && enemy.x + enemy.w / 2 <= platform.x + platform.w && platform.y >= enemy.y + enemy.h - 8)
.sort((a, b) => a.y - b.y)[0];
function groundAhead(enemy, direction, distance = 28, drop = 72) {
const foot = enemy.y + enemy.h;
const probeX = direction > 0 ? enemy.x + enemy.w + distance : enemy.x - distance;
const roomStart = Math.floor(enemy.x / roomWidth) * roomWidth;
if (probeX <= roomStart + 28 || probeX >= roomStart + roomWidth - 28) return false;
if (safeZones.some(zone => probeX >= zone.x && probeX <= zone.x + zone.w && foot >= zone.y)) return false;
return platforms.some(platform => probeX >= platform.x + 3 && probeX <= platform.x + platform.w - 3 && platform.y >= foot - 10 && platform.y <= foot + drop);
}
function hasFooting(enemy) {
const center = enemy.x + enemy.w / 2;
const foot = enemy.y + enemy.h;
return platforms.some(platform => center >= platform.x && center <= platform.x + platform.w && platform.y >= foot - 10 && platform.y <= foot + 72);
}
const failures = [];
for (const spawn of spawns) {
if (safeZones.some(zone => spawn.x + spawn.w > zone.x && spawn.x < zone.x + zone.w)) {
failures.push(`${spawn.type}@${spawn.x}: spawned inside safe zone`);
continue;
}
const support = supportBelow(spawn);
if (!support) { failures.push(`${spawn.type}@${spawn.x}: no spawn support`); continue; }
const enemy = { ...spawn, y: support.y - spawn.h };
let direction = 1;
for (let frame = 0; frame < 900; frame++) {
if (!groundAhead(enemy, direction)) direction *= -1;
enemy.x += direction * enemy.speed / 60;
if (!hasFooting(enemy)) { failures.push(`${spawn.type}@${spawn.x}: left safe ground on frame ${frame}`); break; }
}
}
console.log(`Ground enemies simulated: ${spawns.length}`);
console.log(`Patrol frames simulated: ${spawns.length * 900}`);
console.log(`Unsafe ledge exits: ${failures.length}`);
if (failures.length) {
failures.forEach(failure => console.error(failure));
process.exitCode = 1;
}