Files
WT-Offline/apps/server/src/battle/battle-extra-slots.ts
T
N0VAN0M4LY 27fc535301 Split the repository into apps, packages and patches
Everything sat in packages/: two programs next to the small libraries
they use, and the patch data loose at the root. The programs now live in
apps/ and the shared libraries in packages/:

  apps/server        the local server (was packages/local-service),
                     package @wto/server
  packages/patcher   builds the game copy (was packages/build),
                     package @wto/patcher
  patches/exe        exe patch definitions (was exe-patches)
  patches/gui        GUI script patches (was gui-patches)

Inside the server, the network layer folder becomes transport/ so it is
not server/server/, the entry module is server.ts, and startLocalService
and its types become startServer, ServerOptions and RunningServer. The
health answer names the app wto-server, and the TLS files in the data
folder are server.key and server.crt (made again on the next start).
Comments, logs and tool texts say "local server".

The patcher no longer reaches into the server's folder. It read the
default tuning/store.jsonc by a relative path to register bundle GUIDs
for the multi-vehicle packs, which also ignored the player's overrides
in <data>/tuning/. It now takes the pack ids from its caller; the
server exports multiVehiclePackIds(dataFolder), which reads them the way
the running server does. rebuildGuiPack takes the patch folder from its
caller as well.

The development build moves out of the patcher to
scripts/build-game-copy.ts, since it uses both the server and the
patcher. It needs --data (the folder the server will run with) and
defaults the patch folders and the hook library to this repository.


Checked with the root typecheck, check-patches, check-gui,
check-offsets, the native build, a run of build-game-copy.ts into a
scratch folder (12 exe patches, 35 GUI patches, 496 bundle GUIDs as
before) and a server start from apps/server answering wto-server.
2026-11-29 03:20:54 +08:00

153 lines
7.7 KiB
TypeScript

/**
* 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,
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). */
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;
/** 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<string, CatalogueVehicle>;
readonly rules: BattleSpawnRulesData;
readonly tuning: Pick<BattleTuning, 'strikeUcavModes' | 'strikeUcavSessionMrankAllowance' | 'strikeUcavSpawnScore'>;
}
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);
// 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: [],
};
}
/**
* 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 };
}