diff --git a/apps/server/src/battle/battle-matchmaker.ts b/apps/server/src/battle/battle-matchmaker.ts index bfcc421..0d2dfaa 100644 --- a/apps/server/src/battle/battle-matchmaker.ts +++ b/apps/server/src/battle/battle-matchmaker.ts @@ -19,6 +19,7 @@ import { isModeEligibleForLineup, lineupMrank, selectCustomBots, + type TankLineupWeights, type CustomBotsPool, } from './battle-ranks.ts'; import type { BattleSessionStore } from './battle-session-store.ts'; @@ -104,6 +105,7 @@ export class BattleMatchmaker { readonly #tuning: BattleTuning; readonly #spawnRules: BattleSpawnRulesData; readonly #aircraftLoadouts: ReadonlyMap; + readonly #tankWeights: TankLineupWeights | undefined; readonly #filterMissions: MissionPoolFilter | undefined; constructor( @@ -113,6 +115,7 @@ export class BattleMatchmaker { tuning: BattleTuning, spawnRules: BattleSpawnRulesData, aircraftLoadouts: ReadonlyMap, + tankWeights: TankLineupWeights | undefined, filterMissions?: MissionPoolFilter, ) { this.#gameModes = gameModes; @@ -122,6 +125,7 @@ export class BattleMatchmaker { this.#tuning = tuning; this.#spawnRules = spawnRules; this.#aircraftLoadouts = aircraftLoadouts; + this.#tankWeights = tankWeights; this.#filterMissions = filterMissions; } @@ -181,7 +185,7 @@ export class BattleMatchmaker { minMrank, maxMrank, limitPerType: this.#tuning.botPoolLimitPerType, - }, declaration.allowBotAircraftLoadouts === false ? undefined : this.#aircraftLoadouts); + }, declaration.allowBotAircraftLoadouts === false ? undefined : this.#aircraftLoadouts, this.#tankWeights); return { ok: true, launch: this.#buildLaunchPlan(input, chosenMode, mission, coalitions, playerMrank, minMrank, maxMrank, customBots), diff --git a/apps/server/src/battle/battle-ranks.ts b/apps/server/src/battle/battle-ranks.ts index 11991f0..d252695 100644 --- a/apps/server/src/battle/battle-ranks.ts +++ b/apps/server/src/battle/battle-ranks.ts @@ -31,6 +31,16 @@ export interface CustomBotsPool { readonly ships: readonly BotPoolEntry[]; } +/** Offline tank pool weights by vehicle class (`tuning/ground-ai.jsonc` `lineupWeights`). */ +export interface TankLineupWeights { + readonly spaa: number; + readonly lightTank: number; + readonly tankDestroyer: number; + readonly heavyTank: number; + readonly mediumTank: number; + readonly other: number; +} + export interface BotPoolQuery { readonly difficulty: ModeDifficulty; readonly requiredClass: UnitClass; @@ -117,14 +127,20 @@ function lineupVehicleMrank( return vehicleMrank(vehicle, difficulty, requiredClass) + (mrankDeltas.get(vehicle.name) ?? 0); } +/** + * Without `tankWeights` every pool entry keeps the stock weight of 1. + */ export function selectCustomBots( - vehicles: readonly CatalogueVehicle[], query: BotPoolQuery, aircraftLoadouts?: ReadonlyMap, + vehicles: readonly CatalogueVehicle[], + query: BotPoolQuery, + aircraftLoadouts?: ReadonlyMap, + tankWeights?: TankLineupWeights, ): 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) : [], + tanks: allowed.has('tank') ? pickCategoryPool(vehicles, orderedCountries, 'tank', query, tankWeights) : [], aircrafts: query.requiredClass === 'aircraft' && aircraftLoadouts ? aircrafts.map((entry) => { const aircraftLoadout = aircraftLoadouts.get(entry.name); return aircraftLoadout === undefined ? entry : { ...entry, aircraftLoadout }; @@ -138,6 +154,7 @@ function pickCategoryPool( orderedCountries: readonly string[], category: 'tank' | 'aircraft' | 'ship', query: BotPoolQuery, + tankWeights?: TankLineupWeights, ): BotPoolEntry[] { const countrySet = new Set(orderedCountries); const eligible = vehicles.filter((vehicle) => @@ -160,15 +177,15 @@ function pickCategoryPool( 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, category); + const source = inWindow.length > 0 ? inWindow : sortedByCloseness.slice(0, query.limitPerType); + return roundRobinByCountry(source, orderedCountries, query.limitPerType, (vehicle) => tankPoolWeight(vehicle, tankWeights)); } function roundRobinByCountry( vehicles: readonly CatalogueVehicle[], orderedCountries: readonly string[], limit: number, - category: 'tank' | 'aircraft' | 'ship', + weightOf: (vehicle: CatalogueVehicle) => number, ): BotPoolEntry[] { const buckets = new Map(orderedCountries.map((country) => [country, []])); for (const vehicle of vehicles) buckets.get(vehicle.country)?.push(vehicle); @@ -178,8 +195,7 @@ function roundRobinByCountry( for (const country of orderedCountries) { const vehicle = buckets.get(country)?.[index]; if (!vehicle) continue; - const weight = category === 'tank' ? tankBotPoolWeight(vehicle) : 1; - out.push({ name: vehicle.name, country: stripCountryPrefix(country), weight }); + out.push({ name: vehicle.name, country: stripCountryPrefix(country), weight: weightOf(vehicle) }); added = true; if (out.length >= limit) break; } @@ -188,13 +204,14 @@ function roundRobinByCountry( return out; } -function tankBotPoolWeight(vehicle: CatalogueVehicle): number { - if (vehicle.unitClass === 'exp_SPAA') return 0.12; - if (vehicle.isLightTank) return 0.45; - if (vehicle.unitClass === 'exp_tank_destroyer') return 0.55; - if (vehicle.unitClass === 'exp_heavy_tank') return 0.85; - if (vehicle.unitClass === 'exp_tank') return 1.15; - return 1; +function tankPoolWeight(vehicle: CatalogueVehicle, weights: TankLineupWeights | undefined): number { + if (weights === undefined) return 1; + if (vehicle.unitClass === 'exp_SPAA') return weights.spaa; + if (vehicle.isLightTank) return weights.lightTank; + if (vehicle.unitClass === 'exp_tank_destroyer') return weights.tankDestroyer; + if (vehicle.unitClass === 'exp_heavy_tank') return weights.heavyTank; + if (vehicle.unitClass === 'exp_tank') return weights.mediumTank; + return weights.other; } function unitClassOfVehicleType(type: VehicleType | undefined): UnitClass | undefined { diff --git a/apps/server/src/server.ts b/apps/server/src/server.ts index 1a76546..1dc0e60 100644 --- a/apps/server/src/server.ts +++ b/apps/server/src/server.ts @@ -188,6 +188,7 @@ export async function startServer(options: ServerOptions): Promise modRuntime.runBattleFilterMissions(ctx), ); const economy = new BattleEconomy( diff --git a/apps/server/src/tuning/ground-ai-tuning.ts b/apps/server/src/tuning/ground-ai-tuning.ts index 28a4747..20ed0de 100644 --- a/apps/server/src/tuning/ground-ai-tuning.ts +++ b/apps/server/src/tuning/ground-ai-tuning.ts @@ -2,11 +2,15 @@ * Typed, range-validated reader for `tuning/ground-ai.jsonc`. */ import type { DiscoveredMod } from '@wto/mods'; -import type { GroundAiConfig } from '@wto/patcher'; +import { GROUND_AI_MODES, type GroundAiConfig, type GroundAiMode } from '@wto/patcher'; +import type { TankLineupWeights } from '../battle/battle-ranks.ts'; import { tuningFiles } from './tuning-loader.ts'; import { TuningError, type TuningReader } from './tuning-reader.ts'; -export type GroundAiTuning = GroundAiConfig; +export interface GroundAiTuning extends GroundAiConfig { + /** Tank pool weights by class, used only when a custom ground AI mode is active. */ + readonly lineupWeights: TankLineupWeights; +} const DRIVE_MODES = new Set(['vehicle_phys', 'movesys']); @@ -19,17 +23,11 @@ export function loadGroundAiTuning( } export function readGroundAiTuning(reader: TuningReader): GroundAiTuning { - const layers = reader.object('layers'); const cadence = reader.object('cadence'); const diag = reader.object('diagnostics'); return { - enabled: reader.bool('enabled'), - layers: { - macro: layers.bool('macro'), - tactics: layers.bool('tactics'), - navigation: layers.bool('navigation'), - fireControl: layers.bool('fireControl'), - }, + mode: readMode(reader), + lineupWeights: readLineupWeights(reader.object('lineupWeights')), cadence: { maxBots: cadence.int('maxBots', { min: 1, max: 64 }), macroTickSec: cadence.number('macroTickSec', { min: 0.1, max: 5 }), @@ -49,6 +47,27 @@ export function readGroundAiTuning(reader: TuningReader): GroundAiTuning { }; } +function readMode(reader: TuningReader): GroundAiMode { + const mode = reader.string('mode'); + const known = GROUND_AI_MODES.find((candidate) => candidate === mode); + if (known === undefined) { + throw new TuningError(`tuning file ground-ai.jsonc: mode must be one of ${GROUND_AI_MODES.map((m) => `"${m}"`).join(', ')} (found "${mode}")`); + } + return known; +} + +function readLineupWeights(weights: TuningReader): TankLineupWeights { + const range = { min: 0.01, max: 10 }; + return { + spaa: weights.number('spaa', range), + lightTank: weights.number('lightTank', range), + tankDestroyer: weights.number('tankDestroyer', range), + heavyTank: weights.number('heavyTank', range), + mediumTank: weights.number('mediumTank', range), + other: weights.number('other', range), + }; +} + function readMacroSection(macro: TuningReader): GroundAiTuning['macro'] { const recaptureMinBots = macro.int('recaptureMinBots', { min: 1, max: 8 }); const recaptureMaxBots = macro.int('recaptureMaxBots', { min: 1, max: 12 }); diff --git a/apps/server/tuning/ground-ai.jsonc b/apps/server/tuning/ground-ai.jsonc index e6e851e..e0b9acc 100644 --- a/apps/server/tuning/ground-ai.jsonc +++ b/apps/server/tuning/ground-ai.jsonc @@ -1,14 +1,22 @@ -// Ground battle AI modular configuration. -// Shipped defaults keep takeover layers off; per-client overrides live in /tuning/ground-ai.jsonc -// and compile into game/wtoai/wto_ai_config_common.das when building the game copy. +// Ground battle AI configuration. +// Per-client overrides live in /tuning/ground-ai.jsonc. The game copy build compiles the +// layer settings into game/wtoai/wto_ai_config_common.das; the local server reads lineupWeights. { - "enabled": true, + // "stock": stock AI only, no scripts packed, stock lineup weights. + // "fire": stock movement and destinations, custom perception and fire permission. + // "drive": custom team orders and driving, stock aiming and firing (debugging). + // "full": every custom layer. + "mode": "stock", - "layers": { - "macro": false, - "tactics": false, - "navigation": false, - "fireControl": false + // Relative pick weights of AI tank classes in a battle's bot pool. Offline values; ignored in + // "stock" mode, where every vehicle keeps the stock weight of 1. + "lineupWeights": { + "spaa": 0.12, + "lightTank": 0.45, + "tankDestroyer": 0.55, + "heavyTank": 0.85, + "mediumTank": 1.15, + "other": 1.0 }, "cadence": { diff --git a/packages/patcher/src/build-game-copy.ts b/packages/patcher/src/build-game-copy.ts index ca7f12a..2666592 100644 --- a/packages/patcher/src/build-game-copy.ts +++ b/packages/patcher/src/build-game-copy.ts @@ -23,7 +23,7 @@ import { type GamePackResult, type GroundAiBuildInputs, } from './ground-ai/game-pack.ts'; -import { isGroundAiActive } from './ground-ai/ground-ai-config.ts'; +import { groundAiOwnsMovement, isGroundAiActive } from './ground-ai/ground-ai-config.ts'; import { rebuildGuiPack } from './gui-pack.ts'; import { rebuildMainPack } from './main-pack.ts'; import { collectModAddonMounts, type ModAddonMount } from './mods/addon-register.ts'; @@ -130,7 +130,7 @@ export function buildGameCopy(inputs: GameCopyInputs): GameCopyBuild { const executable = patchExecutableWithMods(inputs.stock.executable, folders.exe, enabledMods); const stockImage = new PeImage(inputs.stock.executable); - const customGroundDrive = groundAiActive && inputs.groundAi?.config.layers.navigation && + const customGroundDrive = groundAiActive && inputs.groundAi !== undefined && groundAiOwnsMovement(inputs.groundAi.config) && inputs.groundAi.config.navigation.driveMode === 'vehicle_phys'; const nativeDefinitions = loadNativeModules(folders.native) .filter((module) => customGroundDrive || module.module !== 'local_mp_ground_ai'); diff --git a/packages/patcher/src/ground-ai/ground-ai-config.ts b/packages/patcher/src/ground-ai/ground-ai-config.ts index 83fd400..59f20b9 100644 --- a/packages/patcher/src/ground-ai/ground-ai-config.ts +++ b/packages/patcher/src/ground-ai/ground-ai-config.ts @@ -3,14 +3,18 @@ * build-time daScript constant compilation (`game/wtoai/wto_ai_config_common.das`). */ +/** + * Which parts of the ground AI replace stock behavior. Each mode is a complete combination: + * - `stock`: stock tankBot moves, aims and fires; no scripts are packed. + * - `fire`: stock movement and destinations; custom perception and fire permission. + * - `drive`: custom team orders and driving; stock aiming and firing (debugging). + * - `full`: every custom layer. + */ +export const GROUND_AI_MODES = ['stock', 'fire', 'drive', 'full'] as const; +export type GroundAiMode = (typeof GROUND_AI_MODES)[number]; + export interface GroundAiConfig { - readonly enabled: boolean; - readonly layers: { - readonly macro: boolean; - readonly tactics: boolean; - readonly navigation: boolean; - readonly fireControl: boolean; - }; + readonly mode: GroundAiMode; readonly cadence: { readonly maxBots: number; readonly macroTickSec: number; @@ -88,9 +92,17 @@ export interface GroundAiConfig { } export function isGroundAiActive(config: GroundAiConfig | undefined): boolean { - if (!config || !config.enabled) return false; - const { macro, tactics, navigation, fireControl } = config.layers; - return macro || tactics || navigation || fireControl; + return config !== undefined && config.mode !== 'stock'; +} + +/** True when custom team orders and driving replace stock movement. */ +export function groundAiOwnsMovement(config: GroundAiConfig): boolean { + return config.mode === 'drive' || config.mode === 'full'; +} + +/** True when custom perception and fire permission replace stock aiming and firing. */ +export function groundAiOwnsFire(config: GroundAiConfig): boolean { + return config.mode === 'fire' || config.mode === 'full'; } type DasEntry = readonly [name: string, kind: 'bool' | 'int' | 'float' | 'string', value: boolean | number | string]; @@ -112,12 +124,15 @@ export function compileGroundAiConfigDas(config: GroundAiConfig): string { } function collectConfigEntries(c: GroundAiConfig): readonly DasEntry[] { + const movement = groundAiOwnsMovement(c); + const fire = groundAiOwnsFire(c); return [ - ['WTO_AI_ENABLED', 'bool', c.enabled], - ['WTO_AI_LAYER_MACRO', 'bool', c.layers.macro], - ['WTO_AI_LAYER_TACTICS', 'bool', c.layers.tactics], - ['WTO_AI_LAYER_NAVIGATION', 'bool', c.layers.navigation], - ['WTO_AI_LAYER_FIRE_CONTROL', 'bool', c.layers.fireControl], + ['WTO_AI_ENABLED', 'bool', isGroundAiActive(c)], + ['WTO_AI_LAYER_MACRO', 'bool', movement], + // Perception runs inside the tactics layer, so every custom mode keeps it on. + ['WTO_AI_LAYER_TACTICS', 'bool', movement || fire], + ['WTO_AI_LAYER_NAVIGATION', 'bool', movement], + ['WTO_AI_LAYER_FIRE_CONTROL', 'bool', fire], ['WTO_AI_MAX_TRACKED_BOTS', 'int', c.cadence.maxBots], ['WTO_AI_MACRO_TICK_SEC', 'float', c.cadence.macroTickSec], ['WTO_AI_TACTICS_TICK_SEC', 'float', c.cadence.tacticsTickSec], diff --git a/packages/patcher/src/index.ts b/packages/patcher/src/index.ts index ddc6806..61b1430 100644 --- a/packages/patcher/src/index.ts +++ b/packages/patcher/src/index.ts @@ -68,8 +68,10 @@ export { } from './ground-ai/game-pack.ts'; export { compileGroundAiConfigDas, + GROUND_AI_MODES, isGroundAiActive, type GroundAiConfig, + type GroundAiMode, } from './ground-ai/ground-ai-config.ts'; export { rebuildGuiPack, type AppliedGuiPatch, type GuiPackResult } from './gui-pack.ts'; export { loadGuiPatches, type GuiScriptEdit, type GuiScriptPatch, type GuiScriptVariant } from './gui-patch-definition.ts';