/** * Typed, range-validated reader for `tuning/ground-ai.jsonc`. */ import type { DiscoveredMod } from '@wto/mods'; import { GROUND_AI_DRIVE_MODES, GROUND_AI_MODES, type GroundAiConfig, type GroundAiDriveMode, 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 interface GroundAiTuning extends GroundAiConfig { /** Tank pool weights by class, used only when a custom ground AI mode is active. */ readonly lineupWeights: TankLineupWeights; } export function loadGroundAiTuning( dataFolder: string | null, report: (line: string) => void, mods: readonly DiscoveredMod[] = [], ): GroundAiTuning { return readGroundAiTuning(tuningFiles(dataFolder, report, undefined, mods).read('ground-ai')); } export function readGroundAiTuning(reader: TuningReader): GroundAiTuning { const cadence = reader.object('cadence'); const diag = reader.object('diagnostics'); return { mode: readMode(reader), loadout: readLoadoutSection(reader.object('loadout')), lineupWeights: readLineupWeights(reader.object('lineupWeights')), cadence: { maxBots: cadence.int('maxBots', { min: 1, max: 64 }), macroTickSec: cadence.number('macroTickSec', { min: 0.1, max: 5 }), tacticsTickSec: cadence.number('tacticsTickSec', { min: 0.05, max: 2 }), navTickSec: cadence.number('navTickSec', { min: 0.05, max: 2 }), }, commander: readCommanderSection(reader.object('commander')), macro: readMacroSection(reader.object('macro')), crowd: readCrowdSection(reader.object('crowd')), terrain: readTerrainSection(reader.object('terrain')), navigation: readNavigationSection(reader.object('navigation')), perception: readPerceptionSection(reader.object('perception')), stateMachine: readStateMachineSection(reader.object('stateMachine')), tactics: readTacticsSection(reader.object('tactics')), estimation: readEstimationSection(reader.object('estimation')), fireControl: readFireControlSection(reader.object('fireControl')), diagnostics: { telemetry: diag.bool('telemetry'), telemetryIntervalSec: diag.number('telemetryIntervalSec', { min: 0.5, max: 60 }), }, }; } 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 readLoadoutSection(loadout: TuningReader): GroundAiTuning['loadout'] { return { lowReadyRackThreshold: loadout.int('lowReadyRackThreshold', { min: 1, max: 60 }), topUpAmmoCount: loadout.int('topUpAmmoCount', { min: 4, max: 80 }), primaryAmmoPct: loadout.int('primaryAmmoPct', { min: 50, max: 100 }), secondaryMinRounds: loadout.int('secondaryMinRounds', { min: 0, max: 20 }), penetrationCompareDistM: loadout.int('penetrationCompareDistM', { min: 100, max: 2000 }), }; } 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 readCommanderSection(cmd: TuningReader): GroundAiTuning['commander'] { const pct = { min: 0, max: 100 }; const bttlAssaultPct = cmd.int('bttlAssaultPct', pct); const bttlFlankPct = cmd.int('bttlFlankPct', pct); const bttlOverwatchPct = cmd.int('bttlOverwatchPct', pct); if (bttlAssaultPct + bttlFlankPct + bttlOverwatchPct > 100) { throw new TuningError('tuning file ground-ai.jsonc: commander Bttl percentages must sum to <= 100'); } const conqAssaultPct = cmd.int('conqAssaultPct', pct); const conqFlankPct = cmd.int('conqFlankPct', pct); if (conqAssaultPct + conqFlankPct > 100) { throw new TuningError('tuning file ground-ai.jsonc: commander Conq assault + flank percentages must sum to <= 100'); } const conqHoldDefendPct = cmd.int('conqHoldDefendPct', pct); const conqHoldOverwatchPct = cmd.int('conqHoldOverwatchPct', pct); if (conqHoldDefendPct + conqHoldOverwatchPct > 100) { throw new TuningError('tuning file ground-ai.jsonc: commander Conq hold percentages must sum to <= 100'); } return { rebalanceSec: cmd.number('rebalanceSec', { min: 1.0, max: 30.0 }), roleHoldSec: cmd.number('roleHoldSec', { min: 0.0, max: 120.0 }), bttlAssaultPct, bttlFlankPct, bttlOverwatchPct, conqAssaultPct, conqFlankPct, conqHoldDefendPct, conqHoldOverwatchPct, domAssaultPct: cmd.int('domAssaultPct', pct), domMaxDefendersPerCap: cmd.int('domMaxDefendersPerCap', { min: 0, max: 4 }), overwatchStandOffM: cmd.number('overwatchStandOffM', { min: 25.0, max: 350.0 }), flankStandOffM: cmd.number('flankStandOffM', { min: 25.0, max: 300.0 }), spaaFollowOffsetM: cmd.number('spaaFollowOffsetM', { min: 40.0, max: 500.0 }), flankMaxLengthRatio: cmd.number('flankMaxLengthRatio', { min: 1.05, max: 3.0 }), flankMinSeparationM: cmd.number('flankMinSeparationM', { min: 15.0, max: 200.0 }), rallyProgressFrac: cmd.number('rallyProgressFrac', { min: 0.1, max: 0.75 }), rallyWaitSec: cmd.number('rallyWaitSec', { min: 3.0, max: 60.0 }), rallyMinFrac: cmd.number('rallyMinFrac', { min: 0.25, max: 1.0 }), rallyRadiusM: cmd.number('rallyRadiusM', { min: 15.0, max: 140.0 }), recallSafeHoldSec: cmd.number('recallSafeHoldSec', { min: 3.0, max: 60.0 }), }; } function readMacroSection(macro: TuningReader): GroundAiTuning['macro'] { const recaptureMinBots = macro.int('recaptureMinBots', { min: 1, max: 8 }); const recaptureMaxBots = macro.int('recaptureMaxBots', { min: 1, max: 12 }); if (recaptureMaxBots < recaptureMinBots) { throw new TuningError('tuning file ground-ai.jsonc: macro.recaptureMaxBots must be >= macro.recaptureMinBots'); } return { openingSplitSec: macro.number('openingSplitSec', { min: 0, max: 180 }), capMaxInsiders: macro.int('capMaxInsiders', { min: 1, max: 8 }), recaptureMinBots, recaptureMaxBots, capStickyBonusM: macro.number('capStickyBonusM', { min: 0, max: 800 }), deathZoneRadiusM: macro.number('deathZoneRadiusM', { min: 10, max: 250 }), deathZoneTtlSec: macro.number('deathZoneTtlSec', { min: 5, max: 300 }), deathZoneMinKills: macro.int('deathZoneMinKills', { min: 1, max: 16 }), spaaRearOffsetM: macro.number('spaaRearOffsetM', { min: 40, max: 600 }), spaaRelocateSec: macro.number('spaaRelocateSec', { min: 5, max: 180 }), enemySpawnAvoidM: macro.number('enemySpawnAvoidM', { min: 60, max: 600 }), }; } function readCrowdSection(crowd: TuningReader): GroundAiTuning['crowd'] { return { personalSpaceM: crowd.number('personalSpaceM', { min: 6, max: 40 }), clusterRadiusM: crowd.number('clusterRadiusM', { min: 12, max: 80 }), clusterMaxAllies: crowd.int('clusterMaxAllies', { min: 1, max: 10 }), disperseStepM: crowd.number('disperseStepM', { min: 8, max: 65 }), disperseHoldSec: crowd.number('disperseHoldSec', { min: 1, max: 15 }), }; } function readTerrainSection(terrain: TuningReader): GroundAiTuning['terrain'] { return { maxClimbSlopeDeg: terrain.number('maxClimbSlopeDeg', { min: 10, max: 42 }), maxDropSlopeDeg: terrain.number('maxDropSlopeDeg', { min: 8, max: 40 }), maxWaterDepthM: terrain.number('maxWaterDepthM', { min: 0.2, max: 2.5 }), collisionMarginM: terrain.number('collisionMarginM', { min: 0.05, max: 2.0 }), crestLookaheadM: terrain.number('crestLookaheadM', { min: 8, max: 80 }), ridgeMaxDwellSec: terrain.number('ridgeMaxDwellSec', { min: 2, max: 60 }), ridgeMaxShots: terrain.int('ridgeMaxShots', { min: 1, max: 10 }), }; } function readNavigationSection(nav: TuningReader): GroundAiTuning['navigation'] { const driveModeRaw = nav.string('driveMode'); const driveMode = GROUND_AI_DRIVE_MODES.find((candidate): candidate is GroundAiDriveMode => candidate === driveModeRaw); if (driveMode === undefined) { throw new TuningError(`tuning file ground-ai.jsonc: navigation.driveMode must be "vehicle_phys" or "movesys" (found "${driveModeRaw}")`); } const steerSign = nav.number('steerSign', { min: -1, max: 1 }); if (steerSign !== 1 && steerSign !== -1) { throw new TuningError('tuning file ground-ai.jsonc: navigation.steerSign must be 1 or -1'); } return { driveMode, maxPhysBots: nav.int('maxPhysBots', { min: 0, max: 64 }), steerSign, deadEchoSec: nav.number('deadEchoSec', { min: 0.3, max: 10 }), controlTimeoutSec: nav.number('controlTimeoutSec', { min: 0.3, max: 10 }), cornerBrakeAccel: nav.number('cornerBrakeAccel', { min: 0.5, max: 8 }), pivotAngleDeg: nav.number('pivotAngleDeg', { min: 25, max: 150 }), stuckWindowSec: nav.number('stuckWindowSec', { min: 1, max: 15 }), stuckMinMoveM: nav.number('stuckMinMoveM', { min: 0.5, max: 10 }), unstickReverseSec: nav.number('unstickReverseSec', { min: 0.8, max: 10 }), tacticalReverseDistM: nav.number('tacticalReverseDistM', { min: 10, max: 90 }), tacticalReverseMaxSec: nav.number('tacticalReverseMaxSec', { min: 2, max: 25 }), }; } function readPerceptionSection(perc: TuningReader): GroundAiTuning['perception'] { return { foliageTransparency: perc.number('foliageTransparency', { min: 0.001, max: 1.0 }), nearSpotRangeM: perc.number('nearSpotRangeM', { min: 20, max: 250 }), firingSpotRangeM: perc.number('firingSpotRangeM', { min: 150, max: 1500 }), fastSpotSec: perc.number('fastSpotSec', { min: 0.05, max: 1.5 }), normalSpotSec: perc.number('normalSpotSec', { min: 0.1, max: 3.0 }), farSpotSec: perc.number('farSpotSec', { min: 0.3, max: 6.0 }), decayPerSec: perc.number('decayPerSec', { min: 0.2, max: 10.0 }), frontConeHalfDeg: perc.number('frontConeHalfDeg', { min: 10, max: 90 }), peripheralCloseM: perc.number('peripheralCloseM', { min: 30, max: 250 }), }; } function readStateMachineSection(sm: TuningReader): GroundAiTuning['stateMachine'] { const coverMinDistM = sm.number('coverMinDistM', { min: 2.0, max: 30.0 }); const coverMaxDistM = sm.number('coverMaxDistM', { min: 6.0, max: 80.0 }); if (coverMaxDistM <= coverMinDistM) { throw new TuningError('tuning file ground-ai.jsonc: stateMachine.coverMaxDistM must be > coverMinDistM'); } return { preaimAngleDeg: sm.number('preaimAngleDeg', { min: 1.0, max: 30.0 }), shortHaltMaxSec: sm.number('shortHaltMaxSec', { min: 0.5, max: 12.0 }), shortHaltCooldownSec: sm.number('shortHaltCooldownSec', { min: 0.5, max: 20.0 }), turnLaunchSpeedMps: sm.number('turnLaunchSpeedMps', { min: 0.5, max: 6.0 }), coverMinDistM, coverMaxDistM, coverRetreatBudgetSec: sm.number('coverRetreatBudgetSec', { min: 1.5, max: 20.0 }), reloadAdvanceAngleDeg: sm.number('reloadAdvanceAngleDeg', { min: 0.0, max: 45.0 }), hullAngleTriggerDeg: sm.number('hullAngleTriggerDeg', { min: 15.0, max: 75.0 }), hullAngleTargetDeg: sm.number('hullAngleTargetDeg', { min: 0.0, max: 35.0 }), maxTotalHaltSec: sm.number('maxTotalHaltSec', { min: 3.0, max: 40.0 }), stalemateMissShots: sm.int('stalemateMissShots', { min: 1, max: 8 }), stalemateNoShotHalts: sm.int('stalemateNoShotHalts', { min: 1, max: 6 }), repositionMinMoveM: sm.number('repositionMinMoveM', { min: 5.0, max: 80.0 }), repositionStepM: sm.number('repositionStepM', { min: 15.0, max: 120.0 }), repairSafeEnemyDistM: sm.number('repairSafeEnemyDistM', { min: 30.0, max: 400.0 }), }; } function readTacticsSection(tactics: TuningReader): GroundAiTuning['tactics'] { return { visionRangeM: tactics.number('visionRangeM', { min: 200, max: 3500 }), visionFovDeg: tactics.number('visionFovDeg', { min: 40, max: 180 }), peripheralRangeM: tactics.number('peripheralRangeM', { min: 30, max: 800 }), engineAudioRangeM: tactics.number('engineAudioRangeM', { min: 10, max: 250 }), shotAudioRangeM: tactics.number('shotAudioRangeM', { min: 50, max: 1200 }), radioShareRangeM: tactics.number('radioShareRangeM', { min: 50, max: 1500 }), memoryTtlSec: tactics.number('memoryTtlSec', { min: 1, max: 45 }), targetStickinessSec: tactics.number('targetStickinessSec', { min: 0.5, max: 20 }), peekCooldownSec: tactics.number('peekCooldownSec', { min: 1, max: 30 }), }; } function readEstimationSection(est: TuningReader): GroundAiTuning['estimation'] { const watchMinSec = est.number('watchMinSec', { min: 0.5, max: 10.0 }); const watchMaxSec = est.number('watchMaxSec', { min: 1.0, max: 15.0 }); if (watchMaxSec < watchMinSec) { throw new TuningError('tuning file ground-ai.jsonc: estimation.watchMaxSec must be >= watchMinSec'); } const velSampleMinSpanSec = est.number('velSampleMinSpanSec', { min: 0.05, max: 2.0 }); const velStableSpanSec = est.number('velStableSpanSec', { min: 0.2, max: 6.0 }); if (velStableSpanSec <= velSampleMinSpanSec) { throw new TuningError('tuning file ground-ai.jsonc: estimation.velStableSpanSec must be > velSampleMinSpanSec'); } return { watchMinSec, watchMaxSec, velSampleMinSpanSec, velStableSpanSec, velJumpResetMps: est.number('velJumpResetMps', { min: 1.0, max: 20.0 }), baseAimSigmaM: est.number('baseAimSigmaM', { min: 0.0, max: 2.0 }), leadErrorFrac: est.number('leadErrorFrac', { min: 0.0, max: 1.0 }), maxBiasM: est.number('maxBiasM', { min: 0.0, max: 6.0 }), }; } function readFireControlSection(fc: TuningReader): GroundAiTuning['fireControl'] { const closeRangeM = fc.number('closeRangeM', { min: 30, max: 250 }); const midRangeM = fc.number('midRangeM', { min: 100, max: 600 }); const longRangeM = fc.number('longRangeM', { min: 300, max: 1500 }); if (midRangeM <= closeRangeM || longRangeM <= midRangeM) { throw new TuningError('tuning file ground-ai.jsonc: fireControl ranges must satisfy closeRangeM < midRangeM < longRangeM'); } return { maxEngageRangeM: fc.number('maxEngageRangeM', { min: 150, max: 3000 }), closeRangeM, midRangeM, longRangeM, midSlowSpeedRatio: fc.number('midSlowSpeedRatio', { min: 0.1, max: 1.0 }), midSlowMaxMps: fc.number('midSlowMaxMps', { min: 1.0, max: 15.0 }), stopSpeedMaxMps: fc.number('stopSpeedMaxMps', { min: 0.1, max: 2.0 }), longRangeWindowSec: fc.number('longRangeWindowSec', { min: 1.0, max: 15.0 }), baseAlignDeg: fc.number('baseAlignDeg', { min: 0.1, max: 3.0 }), closeHitRadiusM: fc.number('closeHitRadiusM', { min: 0.25, max: 2.2 }), barrelSettleDegSec: fc.number('barrelSettleDegSec', { min: 0.2, max: 12.0 }), closeSettleHoldSec: fc.number('closeSettleHoldSec', { min: 0.02, max: 0.5 }), settleHoldSec: fc.number('settleHoldSec', { min: 0.05, max: 1.5 }), unstabilizedStopSpeedMps: fc.number('unstabilizedStopSpeedMps', { min: 0.1, max: 3.0 }), stabilizedCreepSpeedMps: fc.number('stabilizedCreepSpeedMps', { min: 1.5, max: 15.0 }), friendlyCorridorM: fc.number('friendlyCorridorM', { min: 1.0, max: 10.0 }), ballisticStepMaxM: fc.number('ballisticStepMaxM', { min: 5.0, max: 80.0 }), ballisticSweepRadiusM: fc.number('ballisticSweepRadiusM', { min: 0.02, max: 0.5 }), maxPreFireChecksPerTick: fc.int('maxPreFireChecksPerTick', { min: 1, max: 16 }), }; }