Files
WT-Offline/apps/server/src/battle/battle-mission-settings.ts
T
N0VAN0M4LY 5c09c4e47a fix(battle): enable limited fuel in LocalMP versus battles
MissionSettings initializes isLimitedFuel to false by default (2.61.0.37
0x12620af), leaving LocalMP versus battles with infinite fuel, no fuel
burn weight reduction, and no HUD fuel timer. Set isLimitedFuel=true in
battleMissionSettings across all versus modes to match live behavior.

Verified with root npm run typecheck. In-game acceptance pending on an
isolated 2.61.0.58 client copy.
2026-12-03 00:31:35 +08:00

176 lines
9.0 KiB
TypeScript

/**
* Mission settings for a local battle: the block the client hands to the native
* `select_training_mission` (guiMission) to host the battle as an in-process LocalMP session.
*
* The client loads the mission file itself, but does not copy the localization keys or the
* ticket and capture rules of `mission_settings.mission` into its MissionSettings; they are
* copied here from the mission file, base values first and then the sub-block for the battle's
* difficulty. `_gameMode` is left to the client, which takes it from its own native constants.
*/
import { emptyBlock, ensureBlock, findParam, setParam, appendParam, type BlkBlock, type BlkParam, type BlkValue } from '@wto/blk';
import { UNLIMITED_RESPAWNS } from '../game-modes/mode-definition.ts';
import type { ExtraBattleSlot } from './battle-extra-slots.ts';
import type { BattleLaunchPlan } from './battle-matchmaker.ts';
import type { CustomBotsPool } from './battle-ranks.ts';
/** Mission localization keys the client reads from MissionSettings (missionsText.nut). */
const LOCALIZATION_KEYS: readonly string[] = [
'locName', 'locDesc', 'locNameTeamA', 'locNameTeamB', 'locDescTeamA', 'locDescTeamB', 'loc_name', 'postfix', 'restoreType',
];
/** Ticket, capture and time rules of the mission. */
const RULE_KEYS: readonly string[] = [
'scoreLimit', 'timeLimit', 'killLimit', 'deathPenaltyMul',
'ctaCaptureZoneEqualPenaltyMul', 'ctaCaptureZonePenaltyMul', 'captureZonePenaltyMul', 'scorePerSec',
'ctaInstantDecreaseTicketsWhenNoRespawn', 'randomSpawnTeams', 'remapAiTankModels', 'useAlternativeMapCoord',
'allowedKillStreaks',
];
/** Game type switches of a versus battle with tickets, capture zones and session rewards. */
const VERSUS_SWITCHES: readonly string[] = [
'gt_versus', 'gt_mp_tickets', 'gt_mp_capture', 'gt_use_wp', 'gt_use_xp', 'gt_use_stats', 'gt_use_unlocks',
'allowUseArtillerySupport', 'isBotsAllowed', 'autoBalance',
];
/**
* `scores`: the scoreboard score table (BattleEconomy.missionScores) for the native LocalMP bridge,
* written into the stock mission block customScoreParams, which the client copies into its
* MissionSettings without reading it (native/wto-net/src/local_mp_scores.c).
*/
export function battleMissionSettings(plan: BattleLaunchPlan, scores: Readonly<Record<string, number>>): BlkBlock {
const settings = emptyBlock();
copyMissionParams(settings, plan);
// Enable Ground Arcade air events when the mission allows killstreaks; the LocalMP host preserves
// MissionSettings.useKillStreaks across prepare_local_mission (local_mp_air_events.c).
const allowedKillStreaks = findParam(settings, 'allowedKillStreaks')?.value;
if (plan.modeName === 'tank_event_in_random_battles_arcade'
&& allowedKillStreaks?.type === 'bool' && allowedKillStreaks.value) {
setParam(settings, 'useKillStreaks', flag(true));
}
if (plan.level !== undefined) setParam(settings, 'level', text(plan.level));
if (plan.environment !== undefined) setParam(settings, 'environment', text(plan.environment));
if (plan.weather !== undefined) setParam(settings, 'weather', text(plan.weather));
setParam(settings, 'localMP', flag(true));
setParam(settings, 'name', text(plan.missionName));
setParam(settings, 'economicName', text(plan.economicName));
// The 2.61.0.37 mission loader keeps gameModeName (MissionSettings+0x48) and looks up the
// gameplay.blk missionSupportPlane preset by it (0x1269aff), which drives the native nuclear
// carrier: assignment at the show threshold, the action bar item and the support unit spawn.
setParam(settings, 'gameModeName', text(plan.modeName));
setParam(settings, 'mis_file', text(plan.missionFile));
setParam(settings, 'chapter', text('skirmish'));
setParam(settings, 'type', text(plan.missionType));
setParam(settings, 'difficulty', text(plan.difficulty));
setParam(settings, 'country_allies', text(plan.countryAllies));
setParam(settings, 'country_axis', text(plan.countryAxis));
setParam(settings, 'arcadeCountry', flag(true));
setParam(settings, 'symmetricTeams', flag(plan.symmetricTeams));
setParam(settings, 'gt_mp_solo', flag(false));
for (const name of VERSUS_SWITCHES) setParam(settings, name, flag(true));
// The native in-flight rearm request and its receiver both require unlimited reserve ammunition.
setParam(settings, 'isLimitedAmmo', flag(plan.modeName !== 'air_arcade'));
// MissionSettings defaults isLimitedFuel to false, which disables fuel consumption and HUD timers.
setParam(settings, 'isLimitedFuel', flag(true));
setParam(settings, 'allowSpare', flag(!plan.selectedSlot && plan.maxRespawns !== UNLIMITED_RESPAWNS));
if (plan.maxRespawns !== undefined) setParam(settings, 'maxRespawns', integer(plan.maxRespawns));
if (plan.selectedSlot) {
setParam(settings, 'ctaInstantDecreaseTicketsWhenNoRespawn', flag(true));
// The native mission loader reads the AI limit at the top level, after its bots.blk default.
setParam(settings, 'numBotRespawns', integer(plan.airBotRespawns));
}
setParam(settings, 'useSpawnScore', flag(plan.useSpawnScore));
setParam(settings, 'useTankBots', flag(plan.useTankBots));
setParam(settings, 'useShipBots', flag(plan.useShipBots));
setParam(settings, 'maxPlayers', integer(plan.maxPlayers));
setParam(settings, 'maxBots', integer(plan.maxBots));
addPersistentSpawnCosts(settings, plan.extraBattleSlots);
const scoreParams = ensureBlock(settings, 'customScoreParams');
for (const [name, value] of Object.entries(scores)) setParam(scoreParams, name, real(value));
for (const name of ['ranks', 'forcedRanks']) {
const ranks = ensureBlock(settings, name);
setParam(ranks, 'min', integer(plan.minMrank));
setParam(ranks, 'max', integer(plan.maxMrank));
}
// The 2.61.0.37 mission loader copies customRules at 0x126db82 and reads customBots
// from that block at 0x126f074.
ensureBlock(settings, 'customRules').blocks.push(customBotsBlock(plan));
return settings;
}
function copyMissionParams(settings: BlkBlock, plan: BattleLaunchPlan): void {
const ruleKeys = new Set([...RULE_KEYS, ...plan.missionRuleKeys]);
const wanted = new Set([...LOCALIZATION_KEYS, ...ruleKeys]);
const copy = (params: readonly BlkParam[] | undefined, keys: ReadonlySet<string>): void => {
for (const param of params ?? []) {
if (keys.has(param.name)) setParam(settings, param.name, param.value);
}
};
copy(plan.mission.missionParams, wanted);
copy(plan.mission.difficultyParams[plan.difficulty], ruleKeys);
}
/** Fixed spawn costs of extra slots, read back by the spawn score respondents (customSpawnScore). */
function addPersistentSpawnCosts(settings: BlkBlock, slots: readonly ExtraBattleSlot[]): void {
const costed = slots.flatMap((slot) =>
slot.persistentCost !== undefined && slot.persistentCost > 0 && slot.unitName !== ''
? [{ unitName: slot.unitName, cost: slot.persistentCost }]
: []);
if (costed.length === 0) return;
const costs = ensureBlock(ensureBlock(settings, 'customSpawnScore'), 'persistent_costs');
for (const slot of costed) setParam(costs, slot.unitName, integer(slot.cost));
}
/** AI vehicle pool in the layout of config/bots.blk; repeated entries are separate blocks. */
function customBotsBlock(plan: BattleLaunchPlan): BlkBlock {
const bots = emptyBlock('customBots');
setParam(bots, 'wtoPlayerTeam', text(String(plan.playerTeam)));
setParam(bots, 'wtoTeamACountries', text(plan.countriesAllies.join(';')));
setParam(bots, 'wtoTeamBCountries', text(plan.countriesAxis.join(';')));
if (plan.maxRespawns === UNLIMITED_RESPAWNS) setParam(bots, 'wtoUnlimitedRespawns', text('1'));
setParam(bots, 'numBotRespawnsInMultiSlot', integer(plan.airBotRespawns));
setParam(bots, 'numTankBotRespawnsInMultiSlot', integer(plan.tankBotRespawns));
setParam(bots, 'numBotRespawnsInVersus', integer(plan.airBotRespawns));
setParam(bots, 'numTankBotRespawnsInVersus', integer(plan.tankBotRespawns));
bots.blocks.push(
poolBlock('allowedTanks', 'tank', plan.customBots.tanks),
poolBlock('allowedAircrafts', 'aircraft', plan.customBots.aircrafts),
poolBlock('allowedShips', 'ship', plan.customBots.ships),
);
return bots;
}
function poolBlock(name: string, entryName: string, entries: CustomBotsPool['tanks']): BlkBlock {
const pool = emptyBlock(name);
for (const entry of entries) {
const block = emptyBlock(entryName);
appendParam(block, 'name', text(entry.name));
appendParam(block, 'weight', { type: 'real', value: entry.weight });
appendParam(block, 'country', text(entry.country));
const loadout = entry.aircraftLoadout;
if (loadout !== undefined) {
setParam(ensureBlock(block, 'preset'), 'name', text(loadout.preset));
for (const modification of loadout.modifications) appendParam(block, 'addModification', text(modification));
}
pool.blocks.push(block);
}
return pool;
}
function text(value: string): BlkValue {
return { type: 'string', value };
}
function integer(value: number): BlkValue {
return { type: 'int', value: Math.trunc(value) };
}
function flag(value: boolean): BlkValue {
return { type: 'bool', value };
}
function real(value: number): BlkValue {
return { type: 'real', value };
}