Files
WT-Offline/tools/navgen/bake-maps.mts
T

110 lines
4.9 KiB
TypeScript

/**
* Build the navigation and cover data for the ground mission pool of a local game copy.
* Compile the Rust extractor and C++ baker first.
*
* node tools/navgen/bake-maps.mts --game <client> --out <folder>
* [--levels <level,level,...>] [--extract <exe>] [--bake <exe>] [--jobs <count>]
*/
import { spawn } from 'node:child_process';
import { mkdir, mkdtemp, open, readdir, readFile, rename, stat } from 'node:fs/promises';
import { join, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { parseArgs } from 'node:util';
const repository = fileURLToPath(new URL('../../', import.meta.url));
const { values } = parseArgs({
options: {
game: { type: 'string' },
out: { type: 'string' },
levels: { type: 'string' },
extract: { type: 'string', default: join(repository, 'tools/navgen/extract/target/release/wto-navgen-extract.exe') },
bake: { type: 'string', default: join(repository, 'build/navgen/wto_navgen_bake.exe') },
jobs: { type: 'string', default: '1' },
},
});
async function run(executable: string, args: string[], log?: string): Promise<void> {
const output = log === undefined ? undefined : await open(log, 'w');
try {
await new Promise<void>((done, fail) => {
const child = spawn(executable, args, {
cwd: repository,
stdio: output ? ['ignore', output.fd, output.fd] : 'inherit',
windowsHide: true,
});
child.once('error', fail);
child.once('exit', (code, signal) => {
if (code === 0) done();
else fail(new Error(`${executable} exited with ${code ?? signal}${log ? ` (see ${log})` : ''}`));
});
});
} finally {
await output?.close();
}
}
async function main(): Promise<void> {
const jobs = Number(values.jobs);
if (!values.game || !values.out || !Number.isSafeInteger(jobs) || jobs < 1) {
throw new Error('Usage: --game <client> --out <folder> [--levels <list>] [--extract <exe>] [--bake <exe>] [--jobs <count>]');
}
const game = resolve(values.game);
const output = resolve(values.out);
const extract = resolve(values.extract);
const bake = resolve(values.bake);
const maps = join(output, 'maps');
await Promise.all([stat(extract), stat(bake), mkdir(maps, { recursive: true })]);
const work = await mkdtemp(join(output, 'bake-'));
const args = [join(repository, 'tools/navgen/mission-bounds.mts'), '--game', game, '--out', work];
if (values.levels !== undefined) args.push('--levels', values.levels);
await run(process.execPath, args, join(work, 'missions.log'));
const suffix = '.pois.json';
const levels = (await readdir(work)).filter((name) => /^[a-z0-9_]+\.pois\.json$/.test(name))
.map((name) => name.slice(0, -suffix.length));
if (levels.length === 0) throw new Error(`No ground levels found. See ${join(work, 'missions.log')}.`);
if (values.levels) {
const missing = values.levels.split(',').map((level) => level.trim().toLowerCase())
.filter((level) => !levels.includes(level));
if (missing.length) throw new Error(`Levels missing from the mission pool: ${missing.join(', ')}`);
}
console.log(`Baking ${levels.length} ground levels. Logs: ${work}`);
let next = 0;
let finished = 0;
const failures: string[] = [];
const worker = async (): Promise<void> => {
while (next < levels.length) {
const level = levels[next++]!;
const pois = join(work, level + suffix);
const geo = join(work, level + '.wtogeo');
const candidate = join(work, level + '.wtonav');
try {
await run(extract, ['--game', game, '--pois', pois, '--out', geo,
'--report', join(work, level + '.extract-report.json')], join(work, level + '.extract.log'));
const bakeLog = join(work, level + '.bake.log');
await run(bake, ['--geo', geo, '--pois', pois, '--out', candidate], bakeLog);
const log = await readFile(bakeLog, 'utf8');
const routes = [...log.matchAll(/Connectivity \S+: full=(\d+)\/(\d+)/g)];
if (routes.length !== 3 || routes.some(([, , total]) => Number(total) === 0)) {
throw new Error('Missing spawn-to-capture checks. See ' + bakeLog);
}
if (routes.some(([, full, total]) => full !== total)) {
console.warn(level + ': incomplete paths in ' + bakeLog);
}
const { size } = await stat(candidate);
if (size === 0) throw new Error('The baker wrote an empty map.');
await rename(candidate, join(maps, level + '.wtonav'));
console.log(`[${++finished}/${levels.length}] ${level} (${(size / 2 ** 20).toFixed(1)} MiB)`);
} catch (error) {
failures.push(level);
console.error(`[${++finished}/${levels.length}] ${level}: ${error instanceof Error ? error.message : error}`);
}
}
};
await Promise.all(Array.from({ length: Math.min(jobs, levels.length) }, worker));
if (failures.length) throw new Error(`Map generation failed for ${failures.join(', ')}.`);
}
await main();