/** * Offline AI tank spawn ammunition selector: * - Total carried rounds follow the vehicle's first-stage / autoloader ready rack, topped up * when below the configured threshold (clamped to main gun capacity). * - Primary shell prefers legal APFSDS, otherwise ranks anti-armor shells by 500 m penetration. * - Secondary shell carries a small share of HE / HEAT / HESH when available for soft targets. */ import type { GroundAiLoadoutConfig } from '@wto/patcher'; import type { TankAmmoProfile, TankBotAmmoData, TankShellChoice } from '../game-data/datasets/tank-bot-ammo.ts'; import type { CatalogueVehicle } from '../game-data/datasets/vehicle-catalogue.ts'; import { vehicleTypeOfClass } from '../research/vehicle-types.ts'; export interface BotTankBulletSlot { readonly modName: string; readonly count: number; } export interface BotTankLoadout { readonly weaponName: string; readonly bullets: readonly BotTankBulletSlot[]; readonly modifications: readonly string[]; } export function selectBotTankLoadouts( vehicles: readonly CatalogueVehicle[], ammoData: TankBotAmmoData, config: GroundAiLoadoutConfig, ): ReadonlyMap { const result = new Map(); for (const vehicle of vehicles) { if (vehicleTypeOfClass(vehicle.unitClass) !== 'Tank' || vehicle.unitClass === 'exp_SPAA') continue; const profile = ammoData[vehicle.name]; if (!profile || profile.isBelt || profile.maxBullets <= 0) continue; const loadout = chooseTankLoadout(profile, config); if (loadout !== undefined) result.set(vehicle.name, loadout); } return result; } function chooseTankLoadout(profile: TankAmmoProfile, config: GroundAiLoadoutConfig): BotTankLoadout | undefined { const combatShells = profile.shells.filter((shell) => !isNonCombatShell(shell.bulletType)); if (combatShells.length === 0) return undefined; const primary = pickPrimaryShell(combatShells); if (!primary) return undefined; const totalRounds = computeCarriedRounds(profile, config); if (totalRounds <= 0) return undefined; const secondary = pickSecondaryShell(combatShells, primary); const bullets = splitRounds(primary, secondary, totalRounds, config); const modSet = new Set(); for (const chosen of [primary, ...(bullets.length > 1 && secondary ? [secondary] : [])]) { if (chosen.modName) modSet.add(chosen.modName); for (const req of chosen.requires) modSet.add(req); } return { weaponName: profile.weaponName, bullets, modifications: [...modSet].sort(), }; } function computeCarriedRounds(profile: TankAmmoProfile, config: GroundAiLoadoutConfig): number { const want = profile.readyRack >= config.lowReadyRackThreshold ? profile.readyRack : config.topUpAmmoCount; return Math.max(1, Math.min(want, profile.maxBullets)); } function pickPrimaryShell(shells: readonly TankShellChoice[]): TankShellChoice | undefined { const ranked = [...shells].sort((a, b) => compareAntiArmor(a, b)); return ranked[0]; } function compareAntiArmor(a: TankShellChoice, b: TankShellChoice): number { const tierA = antiArmorTier(a.bulletType); const tierB = antiArmorTier(b.bulletType); if (tierA !== tierB) return tierB - tierA; const scoreA = a.pen500Mm + (isApheFamily(a.bulletType) ? 12 : 0); const scoreB = b.pen500Mm + (isApheFamily(b.bulletType) ? 12 : 0); if (scoreA !== scoreB) return scoreB - scoreA; if (a.speedMps !== b.speedMps) return b.speedMps - a.speedMps; return a.modName.localeCompare(b.modName); } function antiArmorTier(bulletType: string): number { if (bulletType.includes('apds_fs')) return 5; if (bulletType.includes('atgm')) return 4; if (bulletType.includes('apds')) return 3; if (bulletType.includes('heat') || isApheFamily(bulletType) || bulletType.startsWith('ap')) return 2; if (bulletType.includes('hesh')) return 1; return 0; } function pickSecondaryShell(shells: readonly TankShellChoice[], primary: TankShellChoice): TankShellChoice | undefined { const candidates = shells.filter((shell) => shell.modName !== primary.modName && isSoftTargetShell(shell.bulletType)); if (candidates.length === 0) return undefined; return [...candidates].sort((a, b) => { const heA = a.bulletType.includes('he_frag') ? 2 : (a.bulletType.includes('hesh') ? 1 : 0); const heB = b.bulletType.includes('he_frag') ? 2 : (b.bulletType.includes('hesh') ? 1 : 0); if (heA !== heB) return heB - heA; if (a.speedMps !== b.speedMps) return b.speedMps - a.speedMps; return a.modName.localeCompare(b.modName); })[0]; } function splitRounds( primary: TankShellChoice, secondary: TankShellChoice | undefined, totalRounds: number, config: GroundAiLoadoutConfig, ): BotTankBulletSlot[] { if (!secondary || config.primaryAmmoPct >= 100 || config.secondaryMinRounds <= 0 || totalRounds < config.secondaryMinRounds * 2 + 2) { return [{ modName: primary.modName, count: totalRounds }]; } const wantSec = Math.round((totalRounds * (100 - config.primaryAmmoPct)) / 100); const secCount = Math.min(totalRounds - 1, Math.max(config.secondaryMinRounds, wantSec)); const priCount = totalRounds - secCount; return [ { modName: primary.modName, count: priCount }, { modName: secondary.modName, count: secCount }, ]; } function isNonCombatShell(bulletType: string): boolean { return bulletType.includes('smoke') || bulletType.includes('practice') || bulletType.includes('shrapnel'); } function isSoftTargetShell(bulletType: string): boolean { return bulletType.includes('he_frag') || bulletType.includes('hesh') || bulletType.includes('heat'); } function isApheFamily(bulletType: string): boolean { return bulletType.includes('aphe') || bulletType.includes('apcbc') || bulletType.includes('apbc'); }