Carry the queued aircraft and crew index into LocalMP, apply the native single-spawn limits to players and aircraft bots, and disable spares for single-slot modes. Verified with root and launcher type checks, check-patches and check-gui for 2.61.0.37 and 2.61.0.43, and an isolated launch-payload check.
210 lines
8.2 KiB
TypeScript
210 lines
8.2 KiB
TypeScript
/**
|
|
* Pure matchmaking rank (`mrank`) calculation and deterministic BR-matched `customBots`
|
|
* vehicle-pool selection (`scripts/slotbar/slotbarpresets/calcranks.nut`).
|
|
*
|
|
* `BotSystem` (`aces.exe` `0x11af3f0`) reads `MissionSettings.customBots` using bare country
|
|
* names (`"usa"`, `"germany"`, ...) and three sub-blocks: `allowedTanks` (`tank`),
|
|
* `allowedAircrafts` (`aircraft`), and `allowedShips` (`ship`).
|
|
*/
|
|
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';
|
|
|
|
const COUNTRY_PREFIX = 'country_';
|
|
|
|
export interface CraftRankEntry {
|
|
readonly unitClass: UnitClass | undefined;
|
|
readonly mrank: number;
|
|
}
|
|
|
|
export interface BotPoolEntry {
|
|
readonly name: string;
|
|
readonly country: string;
|
|
readonly weight: number;
|
|
}
|
|
|
|
export interface CustomBotsPool {
|
|
readonly tanks: readonly BotPoolEntry[];
|
|
readonly aircrafts: readonly BotPoolEntry[];
|
|
readonly ships: readonly BotPoolEntry[];
|
|
}
|
|
|
|
export interface BotPoolQuery {
|
|
readonly difficulty: ModeDifficulty;
|
|
readonly requiredClass: UnitClass;
|
|
readonly allowedClasses: readonly UnitClass[];
|
|
readonly countries: readonly string[];
|
|
readonly centerMrank: number;
|
|
readonly minMrank: number;
|
|
readonly maxMrank: number;
|
|
readonly limitPerType: number;
|
|
}
|
|
|
|
export function stripCountryPrefix(country: string): string {
|
|
return country.startsWith(COUNTRY_PREFIX) ? country.slice(COUNTRY_PREFIX.length) : country;
|
|
}
|
|
|
|
export function unitClassOfExpClass(expClass: string): UnitClass | undefined {
|
|
return unitClassOfVehicleType(vehicleTypeOfClass(expClass));
|
|
}
|
|
|
|
export function unitClassOfTypeCode(unitTypeCode: number): UnitClass | undefined {
|
|
return unitClassOfVehicleType(vehicleTypeById(unitTypeCode));
|
|
}
|
|
|
|
export function vehicleMrank(vehicle: CatalogueVehicle, difficulty: ModeDifficulty, requiredClass: UnitClass): number {
|
|
if (difficulty === 'arcade') return vehicle.economicRankArcade;
|
|
if (difficulty === 'simulator') {
|
|
return requiredClass === 'tank' && vehicle.economicRankTankSimulation !== undefined
|
|
? vehicle.economicRankTankSimulation
|
|
: vehicle.economicRankSimulation;
|
|
}
|
|
return requiredClass === 'tank' && vehicle.economicRankTankHistorical !== undefined
|
|
? vehicle.economicRankTankHistorical
|
|
: vehicle.economicRankHistorical;
|
|
}
|
|
|
|
/**
|
|
* Maximum `mrank` among crafts allowed in the mode (`calcranks.nut` `filterCrafts` +
|
|
* `specialShipsFilter`: in naval modes, aircraft above `maxShipMrank + 1` are ignored).
|
|
*/
|
|
export function maxModeMrank(
|
|
crafts: readonly CraftRankEntry[],
|
|
requiredClass: UnitClass,
|
|
allowedClasses: readonly UnitClass[],
|
|
): number {
|
|
const allowedSet = new Set(allowedClasses);
|
|
const matching = crafts.filter((craft) => craft.unitClass !== undefined && allowedSet.has(craft.unitClass));
|
|
if (!matching.some((craft) => craft.unitClass === requiredClass)) return 0;
|
|
|
|
let filtered = matching;
|
|
if (requiredClass === 'ship') {
|
|
const maxShip = matching
|
|
.filter((craft) => craft.unitClass === 'ship')
|
|
.reduce((max, craft) => Math.max(max, craft.mrank), 0);
|
|
filtered = matching.filter((craft) => craft.unitClass !== 'aircraft' || craft.mrank <= maxShip + 1);
|
|
}
|
|
return filtered.reduce((max, craft) => Math.max(max, Math.max(0, Math.trunc(craft.mrank))), 0);
|
|
}
|
|
|
|
export function lineupMrank(
|
|
vehicleNames: readonly string[],
|
|
byName: ReadonlyMap<string, CatalogueVehicle>,
|
|
difficulty: ModeDifficulty,
|
|
requiredClass: UnitClass,
|
|
allowedClasses: readonly UnitClass[],
|
|
): number {
|
|
const crafts: CraftRankEntry[] = vehicleNames.flatMap((name) => {
|
|
const vehicle = byName.get(name);
|
|
if (!vehicle) return [];
|
|
return [{
|
|
unitClass: unitClassOfExpClass(vehicle.unitClass),
|
|
mrank: vehicleMrank(vehicle, difficulty, requiredClass),
|
|
}];
|
|
});
|
|
return maxModeMrank(crafts, requiredClass, allowedClasses);
|
|
}
|
|
|
|
export function selectCustomBots(vehicles: readonly CatalogueVehicle[], query: BotPoolQuery): CustomBotsPool {
|
|
const allowed = new Set(query.allowedClasses);
|
|
const orderedCountries = [...new Set(query.countries.map((c) => (c.startsWith(COUNTRY_PREFIX) ? c : `${COUNTRY_PREFIX}${c}`)))];
|
|
return {
|
|
tanks: allowed.has('tank') ? pickCategoryPool(vehicles, orderedCountries, 'tank', query) : [],
|
|
aircrafts: allowed.has('aircraft') ? pickCategoryPool(vehicles, orderedCountries, 'aircraft', query) : [],
|
|
ships: allowed.has('ship') ? pickCategoryPool(vehicles, orderedCountries, 'ship', query) : [],
|
|
};
|
|
}
|
|
|
|
function pickCategoryPool(
|
|
vehicles: readonly CatalogueVehicle[],
|
|
orderedCountries: readonly string[],
|
|
category: 'tank' | 'aircraft' | 'ship',
|
|
query: BotPoolQuery,
|
|
): BotPoolEntry[] {
|
|
const countrySet = new Set(orderedCountries);
|
|
const eligible = vehicles.filter((vehicle) =>
|
|
vehicle.inTechTree
|
|
&& !vehicle.onlyShownWhenOwned
|
|
&& !vehicle.premiumPack
|
|
&& !vehicle.gift
|
|
&& !vehicle.event
|
|
&& vehicle.researchType === undefined
|
|
&& countrySet.has(vehicle.country)
|
|
&& unitClassOfExpClass(vehicle.unitClass) === category);
|
|
if (eligible.length === 0) return [];
|
|
|
|
const sortedByCloseness = [...eligible].sort((a, b) => {
|
|
const distA = Math.abs(vehicleMrank(a, query.difficulty, query.requiredClass) - query.centerMrank);
|
|
const distB = Math.abs(vehicleMrank(b, query.difficulty, query.requiredClass) - query.centerMrank);
|
|
return distA !== distB ? distA - distB : a.name.localeCompare(b.name);
|
|
});
|
|
const inWindow = sortedByCloseness.filter((vehicle) => {
|
|
const mrank = vehicleMrank(vehicle, query.difficulty, query.requiredClass);
|
|
return mrank >= query.minMrank && mrank <= query.maxMrank;
|
|
});
|
|
const source = inWindow.length > 0 ? inWindow : sortedByCloseness.slice(0, query.limitPerType);
|
|
return roundRobinByCountry(source, orderedCountries, query.limitPerType);
|
|
}
|
|
|
|
function roundRobinByCountry(
|
|
vehicles: readonly CatalogueVehicle[],
|
|
orderedCountries: readonly string[],
|
|
limit: number,
|
|
): BotPoolEntry[] {
|
|
const buckets = new Map<string, CatalogueVehicle[]>(orderedCountries.map((country) => [country, []]));
|
|
for (const vehicle of vehicles) buckets.get(vehicle.country)?.push(vehicle);
|
|
const out: BotPoolEntry[] = [];
|
|
for (let index = 0; out.length < limit; index++) {
|
|
let added = false;
|
|
for (const country of orderedCountries) {
|
|
const vehicle = buckets.get(country)?.[index];
|
|
if (!vehicle) continue;
|
|
out.push({ name: vehicle.name, country: stripCountryPrefix(country), weight: 1 });
|
|
added = true;
|
|
if (out.length >= limit) break;
|
|
}
|
|
if (!added) break;
|
|
}
|
|
return out;
|
|
}
|
|
|
|
function unitClassOfVehicleType(type: VehicleType | undefined): UnitClass | undefined {
|
|
if (type === 'Tank') return 'tank';
|
|
if (type === 'Aircraft') return 'aircraft';
|
|
if (type === 'Helicopter') return 'helicopter';
|
|
if (type === 'Ship' || type === 'Boat') return 'ship';
|
|
return undefined;
|
|
}
|
|
|
|
export function activeVehiclesForMode(
|
|
declaration: GameModeDeclaration,
|
|
slotVehicles: readonly string[],
|
|
selectedSlotVehicle: string | undefined,
|
|
byName: ReadonlyMap<string, CatalogueVehicle>,
|
|
): readonly string[] {
|
|
if (declaration.multiSlot !== false) return slotVehicles;
|
|
if (!selectedSlotVehicle) return [];
|
|
const selected = byName.get(selectedSlotVehicle);
|
|
return selected && unitClassOfExpClass(selected.unitClass) === declaration.requiredClass
|
|
? [selectedSlotVehicle]
|
|
: [];
|
|
}
|
|
|
|
export function isModeEligibleForLineup(
|
|
declaration: GameModeDeclaration,
|
|
slotVehicles: readonly string[],
|
|
selectedSlotVehicle: string | undefined,
|
|
byName: ReadonlyMap<string, CatalogueVehicle>,
|
|
): boolean {
|
|
if (!declaration.tag && declaration.multiSlot !== false) return true;
|
|
const active = activeVehiclesForMode(declaration, slotVehicles, selectedSlotVehicle, byName);
|
|
const minMrank = declaration.minQueueMrank ?? 0;
|
|
const minCrafts = declaration.minCraftsToPlay ?? 1;
|
|
const matchingCount = active.filter((name) => {
|
|
const vehicle = byName.get(name);
|
|
if (!vehicle || unitClassOfExpClass(vehicle.unitClass) !== declaration.requiredClass) return false;
|
|
return vehicleMrank(vehicle, declaration.difficulty, declaration.requiredClass) >= minMrank;
|
|
}).length;
|
|
return matchingCount >= minCrafts;
|
|
}
|