/** * The extra vehicles a battle adds to the player's lineup (online the battle server adds them): * * - Support plane (the nuclear carrier): gameplay.blk missionSupportPlane, keyed by the game mode * name; the preset whose rankRange holds the session's top rank, the unit of the player's * country, with its fixed weapon and its spawnScoreToUse as a fixed spawn cost. * - Strike UCAV: wpcost.blk ucav_*_killstreak of the player's country. The game files have no rule * for when it is offered or what it costs; both are in tuning/battle.jsonc (the cost as seen in * the live game, the offer rule offline design). * - Extra vehicles from gameplay.blk RandomSpawnUnits, keyed by the event economic name: one * vehicle per group of the player's country (ground RB: a tank and an SPAA), drawn at launch and * shown in the lineup as a plain vehicle, as the live game does (a 12.7 US lineup received an * M1A1 12.0 and an ADATS 12.0, no dice; screenshot of the live game). Rules from the official * changelog ("Line of Contact", 2025-12-16): offered to lineups of BR 9.3 and higher (the * groups' minPlayerMRank), with no condition on what the lineup holds; the vehicle depends on the * player's BR and the session's BR and is never of the session's top BR; when no vehicle fits, * lower BR vehicles are used. Worked examples there: in a 9.7-10.7 session a player at 10.7 gets * 9.7-10.3 and a player at 9.7 gets 9.3-10.0. The window formula is not published; * `randomWindow` is fitted to those examples with the group's own parameters (inferred). Their * spawn cost comes from the stock spawn score formula (the live screenshot's 110 and 80 match it). */ import { randomInt } from 'node:crypto'; import type { BattleSpawnRulesData, NuclearCarrierUnit, RandomSpawnGroup, RandomSpawnUnit, SupportPlanePreset, } from '../game-data/datasets/battle-spawn-rules.ts'; import type { CatalogueVehicle } from '../game-data/datasets/vehicle-catalogue.ts'; import type { GameModeDeclaration } from '../game-modes/catalogue/game-mode-declaration.ts'; import type { BattleTuning } from './battle-tuning.ts'; import { vehicleMrank } from './battle-ranks.ts'; /** The LocalMP host tracks crew slots in a 32-bit mask (native LMP_MP_OFF_BROKEN_SLOTS_MASK). */ export const MAX_BATTLE_CREW_SLOTS = 32; export interface ExtraBattleSlot { readonly unitName: string; /** Fixed spawn cost (mission customSpawnScore.persistent_costs); absent = the stock formula. */ readonly persistentCost?: number; /** Every modification of the unit is enabled. */ readonly allModifications: boolean; /** The modifications enabled when not `allModifications`; the unit is otherwise stock. */ readonly modifications: readonly string[]; readonly forceWeapon?: string; /** Restricts the respawn weapon dropdown to these presets when multiple mode presets exist. */ readonly allowedWeapons?: readonly string[]; /** The RandomSpawnUnits group the vehicle was drawn from. */ readonly groupName?: string; } export interface ExtraBattleSlotsInput { readonly country: string; readonly slotVehicles: readonly string[]; readonly declaration: GameModeDeclaration; /** The lineup's mrank the session was matched on. */ readonly playerMrank: number; readonly sessionMaxMrank: number; readonly vehiclesByName: ReadonlyMap; readonly rules: BattleSpawnRulesData; readonly tuning: Pick< BattleTuning, 'strikeUcavModes' | 'strikeUcavSessionMrankAllowance' | 'strikeUcavSpawnScore' | 'nuclearEscalationStrategicBombers' >; } export function selectExtraBattleSlots(input: ExtraBattleSlotsInput): readonly ExtraBattleSlot[] { const room = MAX_BATTLE_CREW_SLOTS - input.slotVehicles.length; if (input.declaration.multiSlot === false || room <= 0) return []; const supportSlots: ExtraBattleSlot[] = []; const supportPlane = supportPlaneSlot(input); if (supportPlane) supportSlots.push(supportPlane); const ucav = strikeUcavSlot(input); if (ucav) supportSlots.push(ucav); supportSlots.push(...nuclearEscalationSlots(input)); // Keep room for fixed support vehicles while displaying random vehicle groups first. const slots: ExtraBattleSlot[] = []; for (const group of input.rules.randomSpawnUnits[input.declaration.economicName]?.[input.country] ?? []) { if (slots.length + supportSlots.length >= room) break; const options = randomGroupOptions(group, input); if (options.length === 0) continue; const chosen = options[randomInt(options.length)]!; slots.push({ unitName: chosen.unitName, allModifications: false, modifications: chosen.addModifications, ...(chosen.forceWeapon ? { forceWeapon: chosen.forceWeapon } : {}), groupName: group.name, }); } return [...slots, ...supportSlots].slice(0, room); } function supportPlaneSlot(input: ExtraBattleSlotsInput): ExtraBattleSlot | undefined { const presets: readonly SupportPlanePreset[] = input.rules.supportPlanes[input.declaration.name] ?? []; const preset = presets.find((p) => input.sessionMaxMrank >= p.minMrank && input.sessionMaxMrank <= p.maxMrank); const unit = preset?.units[input.country]; if (!preset || !unit) return undefined; return { unitName: unit.unitName, persistentCost: preset.spawnScoreToUse, allModifications: true, modifications: [], forceWeapon: unit.weapon, }; } function strikeUcavSlot(input: ExtraBattleSlotsInput): ExtraBattleSlot | undefined { if (!input.tuning.strikeUcavModes.includes(input.declaration.name)) return undefined; const ucav = input.rules.strikeUcavs.find((entry) => entry.country === input.country); const vehicle = ucav && input.vehiclesByName.get(ucav.unitName); if (!ucav || !vehicle) return undefined; const ucavMrank = vehicleMrank(vehicle, input.declaration.difficulty, input.declaration.requiredClass); if (input.sessionMaxMrank < ucavMrank - input.tuning.strikeUcavSessionMrankAllowance) return undefined; // Offline design: the UCAV comes fully upgraded and keeps its weapon choice (the game files force no preset). return { unitName: ucav.unitName, persistentCost: input.tuning.strikeUcavSpawnScore, allModifications: true, modifications: [], }; } const NUCLEAR_ESCALATION_TAG_PREFIX = 'nuclear_escalation_'; const GROUND_RB_SUPPORT_MODE = 'tank_event_in_random_battles_historical'; const EASTERN_STRATEGIC_BOMBER_COUNTRIES = new Set(['country_ussr', 'country_china']); function nuclearEscalationSlots(input: ExtraBattleSlotsInput): ExtraBattleSlot[] { if (!input.declaration.tag?.startsWith(NUCLEAR_ESCALATION_TAG_PREFIX)) return []; const slots: ExtraBattleSlot[] = []; const tactical = selectTacticalNuclearCarrier(input); if (tactical) { const weaponNames = tactical.weapons.map((weapon) => weapon.name); slots.push({ unitName: tactical.unitName, allModifications: true, modifications: [], ...(weaponNames.length === 1 ? { forceWeapon: weaponNames[0]! } : { allowedWeapons: weaponNames }), }); } if (input.tuning.nuclearEscalationStrategicBombers) { const strategic = selectStrategicNuclearBomber(input.country, input.rules.nuclearEscalationStrategicBombers); if (strategic) { const weapon = strategic.weapons[0]; slots.push({ unitName: strategic.unitName, allModifications: true, modifications: [], ...(weapon ? { forceWeapon: weapon.name } : {}), }); } } return slots; } function selectTacticalNuclearCarrier(input: ExtraBattleSlotsInput): NuclearCarrierUnit | undefined { const byCountry = input.rules.nuclearEscalationTacticalCarriers.filter((entry) => entry.country === input.country); if (byCountry.length === 0) return undefined; // Prefer the stock Ground RB nuclear carrier for the matching rank bracket when it carries // Nuclear Escalation 5 kt / 30 kt presets; otherwise pick the country's closest-mrank carrier. const groundPresets = input.rules.supportPlanes[GROUND_RB_SUPPORT_MODE] ?? []; const bracket = groundPresets.find((p) => input.sessionMaxMrank >= p.minMrank && input.sessionMaxMrank <= p.maxMrank); const preferredUnit = bracket?.units[input.country]?.unitName; if (preferredUnit) { const matched = byCountry.find((entry) => entry.unitName === preferredUnit); if (matched) return matched; } return [...byCountry].sort((a, b) => { const distA = Math.abs(a.mrank - input.sessionMaxMrank); const distB = Math.abs(b.mrank - input.sessionMaxMrank); if (distA !== distB) return distA - distB; const underA = a.mrank <= input.sessionMaxMrank ? 0 : 1; const underB = b.mrank <= input.sessionMaxMrank ? 0 : 1; if (underA !== underB) return underA - underB; return a.mrank - b.mrank || a.unitName.localeCompare(b.unitName); })[0]; } function selectStrategicNuclearBomber( country: string, bombers: readonly NuclearCarrierUnit[], ): NuclearCarrierUnit | undefined { if (bombers.length === 0) return undefined; const preferredCountry = EASTERN_STRATEGIC_BOMBER_COUNTRIES.has(country) ? 'country_ussr' : 'country_usa'; return bombers.find((entry) => entry.country === preferredCountry) ?? bombers[0]; } /** * The options of one random group: the group's vehicles inside `randomWindow`, or, when none fits, * those of the highest rank below it. Vehicles already in the lineup are left out (offline * design: the lineup does not hold the same vehicle twice). */ function randomGroupOptions(group: RandomSpawnGroup, input: ExtraBattleSlotsInput): RandomSpawnUnit[] { if (input.playerMrank < group.minPlayerMrank) return []; const { low, high } = randomWindow(group, input.playerMrank, input.sessionMaxMrank); const lineup = new Set(input.slotVehicles); const ranked = group.units.flatMap((unit) => { const vehicle = input.vehiclesByName.get(unit.unitName); if (!vehicle || lineup.has(unit.unitName)) return []; const mrank = vehicleMrank(vehicle, input.declaration.difficulty, input.declaration.requiredClass); return mrank <= high ? [{ unit, mrank }] : []; }); const inWindow = ranked.filter((entry) => entry.mrank >= low); if (inWindow.length > 0) return inWindow.map((entry) => entry.unit); const best = Math.max(...ranked.map((entry) => entry.mrank)); return ranked.filter((entry) => entry.mrank === best).map((entry) => entry.unit); } /** * Inferred window (the formula is not published): the top option is the player's rank, raised to * at least `maxDiffBetweenTopUnitAndSessionRank` below the session's top rank and capped one rank * below it; the window spans `mRanksCount` ranks under the top option. With the stock values * (2 and 2) this gives both official examples: session 10.7 (mrank 29), player 10.7 (29) -> * mranks 26-28 = BR 9.7-10.3; player 9.7 (26) -> mranks 25-27 = BR 9.3-10.0. */ function randomWindow(group: RandomSpawnGroup, playerMrank: number, sessionMaxMrank: number) { const high = Math.min(Math.max(playerMrank, sessionMaxMrank - group.maxDiffBetweenTopUnitAndSessionRank), sessionMaxMrank - 1); return { low: Math.max(0, high - group.mranksCount), high }; }