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

+5 -1
View File
@@ -23,6 +23,7 @@ import {
} from './battle-ranks.ts';
import type { BattleSessionStore } from './battle-session-store.ts';
import { battleSpawnScore, type BattleSpawnScore } from './battle-spawn-score.ts';
import type { BotAircraftLoadout } from './bot-aircraft-loadouts.ts';
import type { BattleTuning } from './battle-tuning.ts';
export interface BattleCrewSlot {
@@ -88,6 +89,7 @@ export class BattleMatchmaker {
readonly #sessions: BattleSessionStore;
readonly #tuning: BattleTuning;
readonly #spawnRules: BattleSpawnRulesData;
readonly #aircraftLoadouts: ReadonlyMap<string, BotAircraftLoadout>;
constructor(
gameModes: GameModes,
@@ -95,6 +97,7 @@ export class BattleMatchmaker {
sessions: BattleSessionStore,
tuning: BattleTuning,
spawnRules: BattleSpawnRulesData,
aircraftLoadouts: ReadonlyMap<string, BotAircraftLoadout>,
) {
this.#gameModes = gameModes;
this.#vehicles = vehicles;
@@ -102,6 +105,7 @@ export class BattleMatchmaker {
this.#sessions = sessions;
this.#tuning = tuning;
this.#spawnRules = spawnRules;
this.#aircraftLoadouts = aircraftLoadouts;
}
enqueue(input: EnqueueBattleInput): MatchmakeOutcome {
@@ -147,7 +151,7 @@ export class BattleMatchmaker {
minMrank,
maxMrank,
limitPerType: this.#tuning.botPoolLimitPerType,
});
}, this.#aircraftLoadouts);
return {
ok: true,
launch: this.#buildLaunchPlan(input, chosenMode, mission, coalitions, playerMrank, minMrank, maxMrank, customBots),
@@ -134,6 +134,11 @@ function poolBlock(name: string, entryName: string, entries: CustomBotsPool['tan
appendParam(block, 'name', text(entry.name));
appendParam(block, 'weight', { type: 'real', value: entry.weight });
appendParam(block, 'country', text(entry.country));
const loadout = entry.aircraftLoadout;
if (loadout !== undefined) {
setParam(ensureBlock(block, 'preset'), 'name', text(loadout.preset));
for (const modification of loadout.modifications) appendParam(block, 'addModification', text(modification));
}
pool.blocks.push(block);
}
return pool;
+10 -2
View File
@@ -9,6 +9,7 @@
import type { CatalogueVehicle } from '../game-data/datasets/vehicle-catalogue.ts';
import type { GameModeDeclaration, ModeDifficulty, UnitClass } from '../game-modes/catalogue/game-mode-declaration.ts';
import { vehicleTypeById, vehicleTypeOfClass, type VehicleType } from '../research/vehicle-types.ts';
import type { BotAircraftLoadout } from './bot-aircraft-loadouts.ts';
const COUNTRY_PREFIX = 'country_';
@@ -21,6 +22,7 @@ export interface BotPoolEntry {
readonly name: string;
readonly country: string;
readonly weight: number;
readonly aircraftLoadout?: BotAircraftLoadout;
}
export interface CustomBotsPool {
@@ -105,12 +107,18 @@ export function lineupMrank(
return maxModeMrank(crafts, requiredClass, allowedClasses);
}
export function selectCustomBots(vehicles: readonly CatalogueVehicle[], query: BotPoolQuery): CustomBotsPool {
export function selectCustomBots(
vehicles: readonly CatalogueVehicle[], query: BotPoolQuery, aircraftLoadouts: ReadonlyMap<string, BotAircraftLoadout>,
): CustomBotsPool {
const allowed = new Set(query.allowedClasses);
const orderedCountries = [...new Set(query.countries.map((c) => (c.startsWith(COUNTRY_PREFIX) ? c : `${COUNTRY_PREFIX}${c}`)))];
const aircrafts = allowed.has('aircraft') ? pickCategoryPool(vehicles, orderedCountries, 'aircraft', query) : [];
return {
tanks: allowed.has('tank') ? pickCategoryPool(vehicles, orderedCountries, 'tank', query) : [],
aircrafts: allowed.has('aircraft') ? pickCategoryPool(vehicles, orderedCountries, 'aircraft', query) : [],
aircrafts: query.requiredClass === 'aircraft' ? aircrafts.map((entry) => {
const aircraftLoadout = aircraftLoadouts.get(entry.name);
return aircraftLoadout === undefined ? entry : { ...entry, aircraftLoadout };
}) : aircrafts,
ships: allowed.has('ship') ? pickCategoryPool(vehicles, orderedCountries, 'ship', query) : [],
};
}
@@ -0,0 +1,62 @@
/** Choose a stock combat preset when the native default carries no suitable armament. */
import type { FlightModelPreset, FlightModelPresets } from '../game-data/datasets/flight-model-presets.ts';
import type { CatalogueVehicle } from '../game-data/datasets/vehicle-catalogue.ts';
import type { EquipmentCatalogue } from '../modifications/equipment-catalogue.ts';
import { vehicleTypeOfClass } from '../research/vehicle-types.ts';
const AIR_WEAPONS = new Set(['aam', 'additional gun', 'cannon', 'machine gun']);
const STRIKE_WEAPONS = new Set(['bombs', 'guided bombs', 'rockets', 'atgm', 'torpedoes']);
export interface BotAircraftLoadout {
readonly preset: string;
readonly modifications: readonly string[];
}
export function selectBotAircraftLoadouts(
vehicles: readonly CatalogueVehicle[], models: FlightModelPresets, equipment: EquipmentCatalogue,
): ReadonlyMap<string, BotAircraftLoadout> {
const result = new Map<string, BotAircraftLoadout>();
for (const vehicle of vehicles) {
if (vehicleTypeOfClass(vehicle.unitClass) !== 'Aircraft') continue;
const presets = models[vehicle.name.toLowerCase()]?.filter((preset) => !preset.auxiliary);
if (!presets?.length) continue;
const loadout = chooseLoadout(vehicle, presets, equipment);
if (loadout !== undefined) result.set(vehicle.name, loadout);
}
return result;
}
function chooseLoadout(
vehicle: CatalogueVehicle, presets: readonly FlightModelPreset[], equipment: EquipmentCatalogue,
): BotAircraftLoadout | undefined {
const priorities = vehicle.unitClass === 'exp_fighter' ? [AIR_WEAPONS] : [STRIKE_WEAPONS, AIR_WEAPONS];
const stockDefault = presets[0];
if (!stockDefault || priorities.some((group) => carriesWeapons(stockDefault, group))) return undefined;
for (const group of priorities) {
const candidates = presets.filter((preset) => carriesWeapons(preset, group)).map((preset) => ({
preset: preset.name,
modifications: requiredModifications(vehicle.name, preset, equipment),
}));
if (candidates.length > 0) {
return candidates.find((candidate) => candidate.modifications.length === 0) ?? candidates[0];
}
}
return undefined;
}
function carriesWeapons(preset: FlightModelPreset, group: ReadonlySet<string>): boolean {
return preset.triggers.some((trigger) => group.has(trigger));
}
function requiredModifications(
vehicle: string, preset: FlightModelPreset, equipment: EquipmentCatalogue,
): string[] {
const requirements = new Set([...preset.requires, ...(equipment.weapon(vehicle, preset.name)?.requires ?? [])]);
for (const name of requirements) {
for (const prerequisite of equipment.modification(vehicle, name)?.requires ?? []) {
requirements.add(prerequisite);
}
}
return [...requirements];
}