diff --git a/apps/server/src/battle/battle-matchmaker.ts b/apps/server/src/battle/battle-matchmaker.ts index a9eacd7..ceacd1b 100644 --- a/apps/server/src/battle/battle-matchmaker.ts +++ b/apps/server/src/battle/battle-matchmaker.ts @@ -122,8 +122,11 @@ export class BattleMatchmaker { activeVehicles, this.#vehiclesByName, declaration.difficulty, declaration.requiredClass, declaration.allowedClasses, ); const minMrank = Math.max(declaration.minQueueMrank ?? 0, playerMrank - this.#tuning.brSpreadMrank); - const maxMrank = Math.max(minMrank, playerMrank + this.#tuning.brSpreadMrank); - const mission = pickMission(twoSidedMissionsOf(chosenMode), input.bannedMissions, input.dislikedMissions); + const roomCeiling = chosenMode.missionRanks?.maxMrank ?? Infinity; + const maxMrank = Math.max(minMrank, playerMrank, Math.min(roomCeiling, playerMrank + this.#tuning.brSpreadMrank)); + const missions = twoSidedMissionsOf(chosenMode, maxMrank); + if (missions.length === 0) return { ok: false, error: 'NO_MISSION_FOR_BR' }; + const mission = pickMission(missions, input.bannedMissions, input.dislikedMissions); const coalitions = selectBattleCoalitions({ vehicles: this.#vehicles, knownCountries, @@ -250,8 +253,13 @@ export class BattleMatchmaker { } } -function twoSidedMissionsOf(mode: OfferedGameMode): MissionHeader[] { - return mode.missions.filter((m) => m.playerSlots.teamA > 0 && m.playerSlots.teamB > 0); +function twoSidedMissionsOf(mode: OfferedGameMode, roomMrank?: number): MissionHeader[] { + return mode.missions.filter((mission) => { + if (mission.playerSlots.teamA === 0 || mission.playerSlots.teamB === 0) return false; + if (!mode.missionRanks || roomMrank === undefined) return true; + return mode.missionRanks.byMission.get(mission.name)?.some(({ minMRank, maxMRank }) => + roomMrank >= minMRank && roomMrank <= maxMRank); + }); } function pickMission(missions: readonly MissionHeader[], banned: ReadonlySet, disliked: ReadonlySet): MissionHeader { diff --git a/apps/server/src/battle/battle-tuning.ts b/apps/server/src/battle/battle-tuning.ts index e4cac27..6bd8c0e 100644 --- a/apps/server/src/battle/battle-tuning.ts +++ b/apps/server/src/battle/battle-tuning.ts @@ -13,6 +13,7 @@ export interface BattleTuning { readonly smallTeamsMaxPlayers: number; readonly subModeChance: number; readonly brSpreadMrank: number; + readonly fixedAirRealisticMaxMrank: number; readonly botPoolLimitPerType: number; readonly rbCoalitionMinCountries: number; readonly rbCoalitionMaxCountries: number; @@ -52,6 +53,7 @@ export function readBattleTuning(reader: TuningReader): BattleTuning { smallTeamsMaxPlayers, subModeChance: reader.number('subModeChance', { min: 0, max: 1 }), brSpreadMrank: reader.int('brSpreadMrank', { min: 0, max: 12 }), + fixedAirRealisticMaxMrank: reader.int('fixedAirRealisticMaxMrank', { min: 0 }), botPoolLimitPerType: reader.int('botPoolLimitPerType', { min: 4, max: 200 }), rbCoalitionMinCountries, rbCoalitionMaxCountries, diff --git a/apps/server/src/game-data/datasets/multiplayer-missions.ts b/apps/server/src/game-data/datasets/multiplayer-missions.ts index 3f746cf..97ea389 100644 --- a/apps/server/src/game-data/datasets/multiplayer-missions.ts +++ b/apps/server/src/game-data/datasets/multiplayer-missions.ts @@ -13,13 +13,15 @@ * * 2.61.0.37: 1315 missions, each name matching exactly one mission file. */ -import type { BlkBlock, BlkParam } from '@wto/blk'; +import { findParam, type BlkBlock, type BlkParam } from '@wto/blk'; import type { Dataset, DatasetSource } from './dataset.ts'; import { blockAt, boolFields, numberField, stringField, stringList } from './blk-fields.ts'; const MISSION_LIST = 'config/mislist.blk'; const MULTIPLAYER_SECTION = 'skirmish'; const MISSION_FOLDER = 'gamedata/missions/'; +export const AIR_HISTORICAL_MISSION_FOLDER = 'gamedata/missions/cta/planes/historical/'; +const AIRFIELD_LAYOUT = /\/template_airfields_(low|mid|high|top)_ranks\.blk$/; export interface MissionCategory { readonly name: string; @@ -27,7 +29,19 @@ export interface MissionCategory { readonly missions: readonly string[]; } -export interface MissionHeader { +export interface AirfieldRankLayout { + readonly tier: string; + readonly minMRank: number; + readonly maxMRank: number; +} + +interface AirfieldLayoutData { + /** Ranked airfield imports within the mission's airfields_part.blk, as declared by the game. */ + readonly airfieldLayouts: readonly AirfieldRankLayout[]; + readonly airfieldLayoutProblem?: string; +} + +export interface MissionHeader extends AirfieldLayoutData { readonly name: string; readonly path: string; readonly level?: string; @@ -58,7 +72,7 @@ export interface MultiplayerMissions { export const multiplayerMissions: Dataset = { id: 'multiplayer-missions', - version: 3, + version: 4, extract(source: DatasetSource): MultiplayerMissions { const section = blockAt(source.readBlk('char.vromfs.bin', MISSION_LIST), MULTIPLAYER_SECTION); if (!section) throw new Error(`${MISSION_LIST} has no "${MULTIPLAYER_SECTION}" block`); @@ -68,13 +82,19 @@ export const multiplayerMissions: Dataset = { missions: stringList(block, 'm'), })); - const pathByName = missionPathsByLowerName(source.listFiles('mis.vromfs.bin')); + const paths = source.listFiles('mis.vromfs.bin'); + const pathByName = missionPathsByLowerName(paths); + const knownPaths = new Set(paths); + const airfieldsByPath = new Map(); const missions: Record = {}; for (const name of categories.flatMap((category) => category.missions)) { if (name in missions) continue; const path = pathByName.get(name.toLowerCase()); if (!path) throw new Error(`mission ${name} (${MISSION_LIST}) has no file in mis.vromfs.bin`); - missions[name] = readHeader(name, path, source.readBlk('mis.vromfs.bin', path)); + const document = source.readBlk('mis.vromfs.bin', path); + const airfields = path.startsWith(AIR_HISTORICAL_MISSION_FOLDER) + ? readAirfieldLayouts(source, document, knownPaths, airfieldsByPath) : { airfieldLayouts: [] }; + missions[name] = readHeader(name, path, document, airfields); } return { categories, missions }; }, @@ -93,7 +113,9 @@ function missionPathsByLowerName(paths: readonly string[]): Map return byName; } -function readHeader(name: string, path: string, document: BlkBlock): MissionHeader { +function readHeader( + name: string, path: string, document: BlkBlock, airfields: AirfieldLayoutData, +): MissionHeader { const settings = blockAt(document, 'mission_settings'); const mission = blockAt(settings, 'mission'); const optional = { @@ -110,6 +132,7 @@ function readHeader(name: string, path: string, document: BlkBlock): MissionHead return { name, path, + ...airfields, ...Object.fromEntries(Object.entries(optional).filter(([, value]) => value !== undefined)), allowedUnitTypes: boolFields(blockAt(mission, 'allowedUnitTypes')), difficultyOverrides: (mission?.blocks ?? []) @@ -129,6 +152,40 @@ function readHeader(name: string, path: string, document: BlkBlock): MissionHead }; } +function readAirfieldLayouts( + source: DatasetSource, mission: BlkBlock, knownPaths: ReadonlySet, cache: Map, +): AirfieldLayoutData { + const airfieldLayouts: AirfieldRankLayout[] = []; + for (const entry of blockAt(mission, 'imports')?.blocks ?? []) { + const path = stringField(entry, 'file')?.toLowerCase(); + if (!path?.endsWith('/airfields_part.blk')) continue; + const part = cache.get(path) ?? readAirfieldPart(source, path, knownPaths); + cache.set(path, part); + if (part.airfieldLayoutProblem) return part; + airfieldLayouts.push(...part.airfieldLayouts); + } + return { airfieldLayouts }; +} + +function readAirfieldPart(source: DatasetSource, path: string, knownPaths: ReadonlySet): AirfieldLayoutData { + if (!knownPaths.has(path)) return { airfieldLayouts: [], airfieldLayoutProblem: `missing ${path}` }; + const part = source.readBlk('mis.vromfs.bin', path); + const airfieldLayouts: AirfieldRankLayout[] = []; + for (const layout of blockAt(part, 'imports')?.blocks ?? []) { + const file = stringField(layout, 'file')?.toLowerCase(); + if (!file) continue; + const tier = AIRFIELD_LAYOUT.exec(file)?.[1]; + if (!tier) continue; + if (!knownPaths.has(file)) return { airfieldLayouts: [], airfieldLayoutProblem: `missing ${file}` }; + const range = findParam(layout, 'rankRange')?.value; + if (range?.type !== 'ipoint2' || range.value[0] < 0 || range.value[1] < range.value[0]) { + return { airfieldLayouts: [], airfieldLayoutProblem: `invalid rankRange for ${file} in ${path}` }; + } + airfieldLayouts.push({ tier, minMRank: range.value[0], maxMRank: range.value[1] }); + } + return { airfieldLayouts }; +} + const SCALAR_TYPES = new Set(['string', 'int', 'real', 'bool']); /** The parameters whose values survive the dataset cache's JSON unchanged. */ diff --git a/apps/server/src/game-modes/air-realistic-missions.ts b/apps/server/src/game-modes/air-realistic-missions.ts new file mode 100644 index 0000000..1aa0b6b --- /dev/null +++ b/apps/server/src/game-modes/air-realistic-missions.ts @@ -0,0 +1,51 @@ +/** Build the offline Air Realistic rotation from the stock mission variants and ranked airfield imports. */ +import { AIR_HISTORICAL_MISSION_FOLDER, type MissionHeader } from '../game-data/datasets/multiplayer-missions.ts'; + +interface MissionRankRange { + readonly minMRank: number; + readonly maxMRank: number; +} + +export interface MissionRotationRanks { + readonly byMission: ReadonlyMap; + readonly maxMrank: number; +} + +export function airRealisticMissionRanks(selected: readonly MissionHeader[], fixedLayoutMaxMrank: number): MissionRotationRanks { + const historical = selected.filter((mission) => mission.path.startsWith(AIR_HISTORICAL_MISSION_FOLDER) + && !mission.airfieldLayoutProblem && !mission.locName?.split(';').includes('missions/air_newbie')); + const byMission = new Map(); + + const topRankStarts = historical.flatMap((mission) => mission.airfieldLayouts.flatMap((layout) => + layout.tier === 'top' ? [layout.minMRank] : [])); + if (topRankStarts.length === 0) return { byMission, maxMrank: 0 }; + const belowTopMax = Math.min(...topRankStarts) - 1; + let maxMrank = 0; + for (const mission of historical) { + const ranges = mission.airfieldLayouts.length === 0 + ? [{ minMRank: 0, maxMRank: Math.min(fixedLayoutMaxMrank, belowTopMax) }] + : mission.airfieldLayouts.map(({ tier, minMRank, maxMRank }) => ({ + minMRank, + maxMRank: tier === 'top' ? maxMRank : Math.min(maxMRank, belowTopMax), + })); + const supported = mergeRankRanges(ranges); + if (supported.length === 0) continue; + byMission.set(mission.name, supported); + maxMrank = Math.max(maxMrank, supported.at(-1)!.maxMRank); + } + return { byMission, maxMrank }; +} + +function mergeRankRanges(ranges: readonly MissionRankRange[]): MissionRankRange[] { + const sorted = ranges.filter(({ minMRank, maxMRank }) => minMRank <= maxMRank).sort((a, b) => a.minMRank - b.minMRank); + const merged: MissionRankRange[] = []; + for (const range of sorted) { + const previous = merged.at(-1); + if (previous && range.minMRank <= previous.maxMRank + 1) { + merged[merged.length - 1] = { minMRank: previous.minMRank, maxMRank: Math.max(previous.maxMRank, range.maxMRank) }; + } else { + merged.push(range); + } + } + return merged; +} diff --git a/apps/server/src/game-modes/catalogue/game-mode-declaration.ts b/apps/server/src/game-modes/catalogue/game-mode-declaration.ts index 0d0d2ce..fd93c8c 100644 --- a/apps/server/src/game-modes/catalogue/game-mode-declaration.ts +++ b/apps/server/src/game-modes/catalogue/game-mode-declaration.ts @@ -23,6 +23,8 @@ export interface MissionSelection { readonly include?: RegExp; /** Drop missions whose name matches. */ readonly exclude?: RegExp; + /** Use the stock historical mission and airfield layout data to limit the rotation by BR. */ + readonly rankByAirfieldLayout?: boolean; } export interface GameModeDeclaration { diff --git a/apps/server/src/game-modes/catalogue/mission-pools.ts b/apps/server/src/game-modes/catalogue/mission-pools.ts index b8d7fa0..21098f9 100644 --- a/apps/server/src/game-modes/catalogue/mission-pools.ts +++ b/apps/server/src/game-modes/catalogue/mission-pools.ts @@ -16,6 +16,13 @@ export const AIR_BATTLE_MISSIONS: MissionSelection = { exclude: NEWBIE_MISSIONS, }; +export const AIR_REALISTIC_MISSIONS: MissionSelection = { + categories: ['base_dom'], + requireAllowed: ['aircraft'], + exclude: NEWBIE_MISSIONS, + rankByAirfieldLayout: true, +}; + export const GROUND_BATTLE_MISSIONS: MissionSelection = { categories: ['air_ground_Dom', 'air_ground_Conq', 'air_ground_Bttl'], requireAllowed: ['tank'], diff --git a/apps/server/src/game-modes/catalogue/random-battles.ts b/apps/server/src/game-modes/catalogue/random-battles.ts index 038ba55..18bd8f9 100644 --- a/apps/server/src/game-modes/catalogue/random-battles.ts +++ b/apps/server/src/game-modes/catalogue/random-battles.ts @@ -13,7 +13,7 @@ * `scripts/unlocks/personalunlocks.nut:12`, and `config/unlocks.blk` (`title_prowler_in_the_night`). */ import type { GameModeDeclaration } from './game-mode-declaration.ts'; -import { AIR_BATTLE_MISSIONS, GROUND_BATTLE_MISSIONS, NAVAL_BATTLE_MISSIONS } from './mission-pools.ts'; +import { AIR_BATTLE_MISSIONS, AIR_REALISTIC_MISSIONS, GROUND_BATTLE_MISSIONS, NAVAL_BATTLE_MISSIONS } from './mission-pools.ts'; const AIR_REALISTIC_SMALL_TEAMS_MIN_MRANK = 27; const GROUND_REALISTIC_NIGHT_MIN_MRANK = 27; @@ -30,7 +30,7 @@ export const RANDOM_BATTLES: readonly GameModeDeclaration[] = [ { gameModeId: 2, name: 'air_realistic', economicName: 'air_realistic', locName: 'missions/air_event_historical', difficulty: 'realistic', displayType: 'random_battle', multiSlot: false, - minMRankForSmallTeamsBattles: AIR_REALISTIC_SMALL_TEAMS_MIN_MRANK, ...AIR, + ...AIR, missions: AIR_REALISTIC_MISSIONS, minMRankForSmallTeamsBattles: AIR_REALISTIC_SMALL_TEAMS_MIN_MRANK, }, { gameModeId: 3, name: 'tank_event_in_random_battles_arcade', economicName: 'tank_event_in_random_battles_arcade', diff --git a/apps/server/src/game-modes/catalogue/sub-game-modes.ts b/apps/server/src/game-modes/catalogue/sub-game-modes.ts index 70ce64b..834f8b1 100644 --- a/apps/server/src/game-modes/catalogue/sub-game-modes.ts +++ b/apps/server/src/game-modes/catalogue/sub-game-modes.ts @@ -13,7 +13,7 @@ */ import type { GameModeDeclaration } from './game-mode-declaration.ts'; import { - AIR_BATTLE_MISSIONS, + AIR_REALISTIC_MISSIONS, BULLET_HELL_MISSIONS, GROUND_BATTLE_MISSIONS, NAVAL_EC_MISSIONS, @@ -57,7 +57,7 @@ export const SUB_GAME_MODES: readonly GameModeDeclaration[] = [ isSmallTeams: true, requiredClass: 'aircraft', allowedClasses: ['aircraft'], - missions: AIR_BATTLE_MISSIONS, + missions: AIR_REALISTIC_MISSIONS, }, { gameModeId: 103, diff --git a/apps/server/src/game-modes/game-modes.ts b/apps/server/src/game-modes/game-modes.ts index fd3688d..9544ce1 100644 --- a/apps/server/src/game-modes/game-modes.ts +++ b/apps/server/src/game-modes/game-modes.ts @@ -7,6 +7,7 @@ * and team rules a chosen mode has. */ import type { MissionHeader, MultiplayerMissions } from '../game-data/datasets/multiplayer-missions.ts'; +import { airRealisticMissionRanks, type MissionRotationRanks } from './air-realistic-missions.ts'; import { GAME_MODE_CATALOGUE } from './catalogue/catalogue.ts'; import type { GameModeDeclaration } from './catalogue/game-mode-declaration.ts'; import { selectMissions } from './mission-pool.ts'; @@ -15,6 +16,7 @@ import { buildModeDefinition, type ModeDefinition } from './mode-definition.ts'; export interface OfferedGameMode { readonly declaration: GameModeDeclaration; readonly missions: readonly MissionHeader[]; + readonly missionRanks?: MissionRotationRanks; readonly definition: ModeDefinition; } @@ -25,18 +27,29 @@ export class GameModes { readonly #countries: readonly string[]; constructor(missions: MultiplayerMissions, countries: readonly string[], report: (line: string) => void, - catalogue: readonly GameModeDeclaration[] = GAME_MODE_CATALOGUE) { + fixedAirRealisticMaxMrank: number, catalogue: readonly GameModeDeclaration[] = GAME_MODE_CATALOGUE) { this.#countries = [...countries]; for (const mode of catalogue) { const selected = selectMissions(mode.missions, missions); - if (selected.length === 0) { - report(`game mode ${mode.name}${mode.tag ? `.${mode.tag}` : ''}: no missions in the game files, left out`); + const name = mode.tag ?? mode.name; + const missionRanks = mode.missions.rankByAirfieldLayout + ? airRealisticMissionRanks(selected, fixedAirRealisticMaxMrank) : undefined; + if (missionRanks) { + for (const mission of selected) { + if (mission.airfieldLayoutProblem) report(`game mode ${name}: ${mission.name} omitted: ${mission.airfieldLayoutProblem}`); + } + } + const available = missionRanks ? selected.filter((mission) => missionRanks.byMission.has(mission.name)) : selected; + if (available.length === 0) { + const reason = missionRanks ? 'no usable top-rank airfield layouts' : 'no missions in the game files'; + report(`game mode ${name}: ${reason}, left out`); continue; } const offered: OfferedGameMode = { declaration: mode, - missions: selected, - definition: buildModeDefinition(mode, selected, countries), + missions: available, + ...(missionRanks ? { missionRanks } : {}), + definition: buildModeDefinition(mode, available, countries, missionRanks), }; this.#byId.set(mode.gameModeId, offered); if (!mode.tag) this.#byName.set(mode.name, offered); diff --git a/apps/server/src/game-modes/mode-definition.ts b/apps/server/src/game-modes/mode-definition.ts index 37725de..27ac950 100644 --- a/apps/server/src/game-modes/mode-definition.ts +++ b/apps/server/src/game-modes/mode-definition.ts @@ -10,8 +10,8 @@ * playable and mark the mode invalid. Craft rules name a vehicle class; requiredCrafts * is what greys the mode out for a player without that class (gameModeManagerState.nut * createEventGameMode). - * - mission_decl.missions_list: mission name -> {} (a value may carry "enableIf" rank - * limits, scripts/missions/mapPreferencesParams.nut; none are set). + * - mission_decl.missions_list: mission name -> settings; "enableIf" carries the rank + * limits of ranked missions (scripts/missions/mapPreferencesParams.nut). * - tag: required, the script reads it unguarded (scripts/matching/matchingGameModes.nut, * onGameModesUpdated). Tag prefixes mark sub-modes * (night battles, small teams, bullet hell, nuclear escalation, naval EC; @@ -26,6 +26,7 @@ * missionsBanMode (map preferences; absent means none, mapPreferencesParams.nut). */ import type { MissionHeader } from '../game-data/datasets/multiplayer-missions.ts'; +import type { MissionRotationRanks } from './air-realistic-missions.ts'; import type { GameModeDeclaration, ModeDifficulty } from './catalogue/game-mode-declaration.ts'; const MATCHING_DIFFICULTY: Readonly> = { @@ -46,7 +47,7 @@ export const SINGLE_SPAWN = 1; export type ModeDefinition = Readonly>; export function buildModeDefinition(mode: GameModeDeclaration, missions: readonly MissionHeader[], - countries: readonly string[]): ModeDefinition { + countries: readonly string[], missionRanks?: MissionRotationRanks): ModeDefinition { const minTeamSize = mode.teamSizeOverride?.min ?? 1; const maxTeamSize = mode.teamSizeOverride?.max ?? teamSize(mode, missions); const team = { @@ -83,7 +84,10 @@ export function buildModeDefinition(mode: GameModeDeclaration, missions: readonl mission_decl: { difficulty: MISSION_DIFFICULTY[mode.difficulty], ...(mode.multiSlot === false ? { maxRespawns: SINGLE_SPAWN } : {}), - missions_list: Object.fromEntries(missions.map((mission) => [mission.name, {}])), + missions_list: Object.fromEntries(missions.map((mission) => { + const ranges = missionRanks?.byMission.get(mission.name); + return [mission.name, ranges?.length === 1 ? { enableIf: ranges[0] } : {}]; + })), }, }; } diff --git a/apps/server/src/server.ts b/apps/server/src/server.ts index a1ef35d..060d314 100644 --- a/apps/server/src/server.ts +++ b/apps/server/src/server.ts @@ -81,9 +81,11 @@ export async function startServer(options: ServerOptions): Promise