fix(battle): assign combat presets to AI aircraft

Read stock weapon triggers and prerequisite modifications. Supply a suitable native preset for aircraft whose default lacks the relevant armament and preserve repeated modification names in the launch block.
This commit is contained in:
N0VAN0M4LY committed 2026-12-01 13:50:04 +08:00
1 parent 44e64fa414
commit 70c4ee4880
8 files changed
+190 -9

No files matched your search

@@ -0,0 +1,95 @@
/** Stock flight-model presets and the weapon triggers and modifications of their payloads. */
import type { BlkBlock } from '@wto/blk';
import { blockAt, boolField, numberField, stringField, stringList } from './blk-fields.ts';
import type { Dataset } from './dataset.ts';
const PACK = 'aces.vromfs.bin';
const MODEL_FILE = /^gamedata\/flightmodels\/([^/]+)\.blk$/;
export interface FlightModelPreset {
readonly name: string;
readonly auxiliary: boolean;
readonly triggers: readonly string[];
readonly requires: readonly string[];
}
export type FlightModelPresets = Readonly<Record<string, readonly FlightModelPreset[]>>;
type MountsBySlot = ReadonlyMap<number, ReadonlyMap<string, BlkBlock>>;
export const flightModelPresets: Dataset<FlightModelPresets> = {
id: 'flight-model-presets',
version: 1,
extract(source) {
const files = new Set(source.listFiles(PACK));
const payloads = new Map<string, BlkBlock>();
const entries: [string, FlightModelPreset[]][] = [];
for (const file of files) {
const unit = MODEL_FILE.exec(file)?.[1];
if (unit === undefined) continue;
const model = source.readBlk(PACK, file);
const presets = blockAt(model, 'weapon_presets');
if (!presets?.blocks.length) continue;
const mounts = readMounts(model);
entries.push([unit, presets.blocks.flatMap((preset) => {
const name = stringField(preset, 'name');
if (!name) return [];
const path = stringField(preset, 'blk')?.toLowerCase();
let payload = path === undefined ? undefined : payloads.get(path);
if (path !== undefined && payload === undefined && files.has(path)) {
payload = source.readBlk(PACK, path);
payloads.set(path, payload);
}
// Some stock presets point to absent files. Keep their place in the list without
// treating their unverified payload as a candidate.
return [readPreset(name, preset, payload, mounts)];
})]);
}
return Object.fromEntries(entries);
},
};
function readMounts(model: BlkBlock): MountsBySlot {
const slots = new Map<number, Map<string, BlkBlock>>();
for (const slot of blockAt(model, 'WeaponSlots')?.blocks ?? []) {
const index = numberField(slot, 'index');
if (slot.name !== 'WeaponSlot' || index === undefined) continue;
const choices = new Map<string, BlkBlock>();
for (const mount of slot.blocks) {
const name = stringField(mount, 'name');
if (name !== undefined) choices.set(name, mount);
}
slots.set(index, choices);
}
return slots;
}
function readPreset(name: string, definition: BlkBlock, payload: BlkBlock | undefined, mounts: MountsBySlot): FlightModelPreset {
const requires = new Set(stringList(definition, 'reqModification'));
const triggers = new Set<string>();
const visitedMounts = new Set<BlkBlock>();
function readWeapons(block: BlkBlock): void {
for (const weapon of block.blocks) {
if (weapon.name !== 'Weapon' && weapon.name !== 'DependentWeaponPreset') continue;
const trigger = stringField(weapon, 'trigger');
if (trigger !== undefined) triggers.add(trigger);
const slot = numberField(weapon, 'slot');
const preset = stringField(weapon, 'preset');
if (slot === undefined || preset === undefined) continue;
const mount = mounts.get(slot)?.get(preset);
if (mount === undefined || visitedMounts.has(mount)) continue;
visitedMounts.add(mount);
for (const requirement of stringList(mount, 'reqModification')) requires.add(requirement);
readWeapons(mount);
}
}
if (payload !== undefined) readWeapons(payload);
return {
name,
auxiliary: boolField(blockAt(definition, 'tags'), 'aux') === true,
triggers: [...triggers],
requires: [...requires],
};
}