fix(battle): unblock LocalMP backup respawns and normalize unit names

Store the selected LocalMP backup token in a separate wtoLastBackup handler field instead of mutating requestData.wtoBackup. Mutating requestData caused respawn.nut checkReady() u.isEqual(lastRequestData, getSelectedRequestData()) to always evaluate false for backup spawns, preventing doRespawnPlayer() from ever running.

Also fix downstream ground and naval backup accounting where native UnitInfo::name carries folder prefixes (e.g. tankModels/cn_type_59) that mismatched bare unit IDs in session.spareSlots, by stripping path prefixes in local_mp_awards.c, record-local-battle-awards, battle-spares.ts, and battle-queue-requests.ts. Update hook_is_slot_ready_at_host to check USED_SPARES_MASK rather than BROKEN_SLOTS_MASK so unrepaired slots remain eligible for a backup spawn.

Verified root and launcher typecheck, 2.61.0.37 and 2.61.0.58 check-gui (50 patches / 37 .nut), check-patches (14/14), checkPatchSet, battle-spares runnable check, and MSVC temp build of wto_ws.dll (0 warnings in project C sources). Pending regular-terminal build of build/native/wto_ws.dll and in-game acceptance.
This commit is contained in:
N0VAN0M4LY committed 2026-12-04 23:56:13 +08:00
1 parent c3cf4e3707
commit 647eff35fe
11 files changed
+49 -16

No files matched your search

+11 -3
View File
@@ -16,12 +16,19 @@ export interface BackupSpawn {
readonly backup: string; readonly backup: string;
} }
/** Strip native model folder prefixes (e.g. "tankModels/cn_type_59" -> "cn_type_59"). */
export function normalizeBackupUnitName(unit: string): string {
const slash = unit.lastIndexOf('/');
return slash >= 0 ? unit.slice(slash + 1) : unit;
}
function receiptKey(session: BattleSession, slot: number): string { function receiptKey(session: BattleSession, slot: number): string {
return `${session.sessionId}_${slot}`; return `${session.sessionId}_${slot}`;
} }
export function recordDailyBackupSpawn(profile: BlkBlock, session: BattleSession, spawn: BackupSpawn): boolean { export function recordDailyBackupSpawn(profile: BlkBlock, session: BattleSession, spawn: BackupSpawn): boolean {
return spawn.backup === DAILY_BACKUP && session.spareSlots?.get(spawn.slot) === spawn.unit const unit = normalizeBackupUnitName(spawn.unit);
return spawn.backup === DAILY_BACKUP && session.spareSlots?.get(spawn.slot) === unit
&& recordDailySpare(profile, session.startedAtSec, receiptKey(session, spawn.slot)); && recordDailySpare(profile, session.startedAtSec, receiptKey(session, spawn.slot));
} }
@@ -32,11 +39,12 @@ export function consumeBattleSpares(
const seenSlots = new Set<number>(); const seenSlots = new Set<number>();
const consumed = new Map<string, number>(); const consumed = new Map<string, number>();
for (const spawn of spawns) { for (const spawn of spawns) {
const { slot, unit, backup } = spawn; const { slot, backup } = spawn;
const unit = normalizeBackupUnitName(spawn.unit);
if (session.spareSlots?.get(slot) !== unit || seenSlots.has(slot)) continue; if (session.spareSlots?.get(slot) !== unit || seenSlots.has(slot)) continue;
seenSlots.add(slot); seenSlots.add(slot);
if (backup === DAILY_BACKUP) { if (backup === DAILY_BACKUP) {
recordDailyBackupSpawn(profile, session, spawn); recordDailyBackupSpawn(profile, session, { slot, unit, backup });
continue; continue;
} }
if (dailySparesLeft(profile, session.startedAtSec) > 0 if (dailySparesLeft(profile, session.startedAtSec) > 0
@@ -12,7 +12,7 @@ import { economyDifficulty, type BattleEconomy } from '../battle/battle-economy.
import type { BattleMatchmaker } from '../battle/battle-matchmaker.ts'; import type { BattleMatchmaker } from '../battle/battle-matchmaker.ts';
import { maxModeMrank, unitClassOfExpClass, unitClassOfTypeCode, type CraftRankEntry } from '../battle/battle-ranks.ts'; import { maxModeMrank, unitClassOfExpClass, unitClassOfTypeCode, type CraftRankEntry } from '../battle/battle-ranks.ts';
import type { BattleSession, BattleSessionStore } from '../battle/battle-session-store.ts'; import type { BattleSession, BattleSessionStore } from '../battle/battle-session-store.ts';
import { DAILY_BACKUP, recordDailyBackupSpawn } from '../battle/battle-spares.ts'; import { DAILY_BACKUP, normalizeBackupUnitName, recordDailyBackupSpawn } from '../battle/battle-spares.ts';
import type { GameModes, OfferedGameMode } from '../game-modes/game-modes.ts'; import type { GameModes, OfferedGameMode } from '../game-modes/game-modes.ts';
import { crewSlotVehicleOfCountry, crewVehiclesOfCountry } from '../profile/profile-crews.ts'; import { crewSlotVehicleOfCountry, crewVehiclesOfCountry } from '../profile/profile-crews.ts';
import { dailySparesLeft } from '../profile/profile-daily-spares.ts'; import { dailySparesLeft } from '../profile/profile-daily-spares.ts';
@@ -50,8 +50,9 @@ export function battleQueueRequests(deps: BattleQueueDependencies): Array<[strin
return { error: 'BAD_PARAMS' }; return { error: 'BAD_PARAMS' };
} }
const battle = deps.sessions.findForUser(sessionId, userId); const battle = deps.sessions.findForUser(sessionId, userId);
if (!battle || battle.settled || battle.spareSlots?.get(slot) !== unit) return { error: 'BAD_PARAMS' }; const normalizedUnit = normalizeBackupUnitName(unit);
deps.profiles.updateWhen(userId, (profile) => recordDailyBackupSpawn(profile, battle, { slot, unit, backup: DAILY_BACKUP })); if (!battle || battle.settled || battle.spareSlots?.get(slot) !== normalizedUnit) return { error: 'BAD_PARAMS' };
deps.profiles.updateWhen(userId, (profile) => recordDailyBackupSpawn(profile, battle, { slot, unit: normalizedUnit, backup: DAILY_BACKUP }));
return {}; return {};
}], }],
]; ];
+4
View File
@@ -114,9 +114,13 @@ void local_mp_report_backup_spawn(const char *unit_name, int32_t slot_id, const
{ {
__declspec(align(16)) uint8_t blk[LMP_BLK_SIZE]; __declspec(align(16)) uint8_t blk[LMP_BLK_SIZE];
uint8_t *gsd = g_game_save_data ? *g_game_save_data : NULL; uint8_t *gsd = g_game_save_data ? *g_game_save_data : NULL;
const char *slash;
if (gsd == NULL || fn_hud_notify_script == NULL || unit_name == NULL) if (gsd == NULL || fn_hud_notify_script == NULL || unit_name == NULL)
return; return;
slash = strrchr(unit_name, '/');
if (slash != NULL && slash[1] != '\0')
unit_name = slash + 1;
memset(blk, 0, sizeof(blk)); memset(blk, 0, sizeof(blk));
fn_blk_ctor(blk); fn_blk_ctor(blk);
fn_blk_set_str(blk, "unit", unit_name); fn_blk_set_str(blk, "unit", unit_name);
+2 -1
View File
@@ -244,7 +244,8 @@ static uint8_t __fastcall hook_is_slot_ready_at_host(int32_t slot_id, const char
mplayer = g_local_mp_mplayer ? *g_local_mp_mplayer : NULL; mplayer = g_local_mp_mplayer ? *g_local_mp_mplayer : NULL;
if (mplayer == NULL) if (mplayer == NULL)
return 1; return 1;
return (*(uint32_t *)(mplayer + LMP_MP_OFF_BROKEN_SLOTS_MASK) & (1u << (uint32_t)slot_id)) == 0 ? 1 : 0; /* Allow broken slots whose backup has not been used yet; isCrewAvailableInSession enforces backup eligibility. */
return (*(uint32_t *)(mplayer + LMP_MP_OFF_USED_SPARES_MASK) & (1u << (uint32_t)slot_id)) == 0 ? 1 : 0;
} }
int install_local_mp_player_hooks(void) int install_local_mp_player_hooks(void)
@@ -0,0 +1,2 @@
lastRequestData = null
lastSpawnUnitName = ""
@@ -13,6 +13,7 @@
"written_for": "2.61.0.37", "written_for": "2.61.0.37",
"file": "scripts/respawn/respawn.nut", "file": "scripts/respawn/respawn.nut",
"edits": [ "edits": [
{ "find": "find-backup-field.nut", "replace": "replace-backup-field.nut" },
{ {
"find": "find.nut", "find": "find.nut",
"replace": "replace.nut" "replace": "replace.nut"
@@ -0,0 +1,3 @@
lastRequestData = null
wtoLastBackup = null
lastSpawnUnitName = ""
@@ -3,8 +3,9 @@
let source = request == null ? null : this.getWtoBackupSourceForRequest(request) let source = request == null ? null : this.getWtoBackupSourceForRequest(request)
if (source != null) { if (source != null) {
let backup = this.getWtoBackupToken(request, source) let backup = this.getWtoBackupToken(request, source)
if (backup == null || request?.wtoBackup != backup) { if (backup == null || this.wtoLastBackup != backup) {
this.lastRequestData = null this.lastRequestData = null
this.wtoLastBackup = null
this.isApplyPressed = false this.isApplyPressed = false
this.checkReady() this.checkReady()
this.doSelectAircraft() this.doSelectAircraft()
@@ -1,3 +1,3 @@
if (this.lastRequestData?.wtoBackup == null) if (this.wtoLastBackup == null)
markUsedItemCount(itemType.UNIVERSAL_SPARE, this.lastRequestData.spareUid) markUsedItemCount(itemType.UNIVERSAL_SPARE, this.lastRequestData.spareUid)
this.universalSpareUidForRespawn = "" this.universalSpareUidForRespawn = ""
@@ -1,8 +1,10 @@
function sendWtoAircraftAndWeaponRequest(requestData, isSilent = false) { function sendWtoAircraftAndWeaponRequest(requestData, isSilent = false) {
let wtoNdb = require("nestdb") let wtoNdb = require("nestdb")
if (!wtoNdb.ndbExists(["wto", "activeBattle"])) if (!wtoNdb.ndbExists(["wto", "activeBattle"])) {
this.wtoLastBackup = null
return requestAircraftAndWeaponWithSpare(requestData, requestData.idInCountry, return requestAircraftAndWeaponWithSpare(requestData, requestData.idInCountry,
requestData.respBaseId, requestData.spareUid) requestData.respBaseId, requestData.spareUid)
}
let wtoBattle = wtoNdb.ndbTryRead(["wto", "activeBattle"]) let wtoBattle = wtoNdb.ndbTryRead(["wto", "activeBattle"])
let wtoGuiRespawn = require("guiRespawn") let wtoGuiRespawn = require("guiRespawn")
let wtoChardRes = require("chardResearch") let wtoChardRes = require("chardResearch")
@@ -86,20 +88,27 @@
wtoBlk.enabledMods = ";".join(wtoEnabledMods) wtoBlk.enabledMods = ";".join(wtoEnabledMods)
wtoBlk.disabledMods = ";".join(wtoDisabledMods) wtoBlk.disabledMods = ";".join(wtoDisabledMods)
wtoBlk.isSilentPreview = isSilent ? "1" : "0" wtoBlk.isSilentPreview = isSilent ? "1" : "0"
// Keep backup metadata off requestData so checkReady()'s u.isEqual(lastRequestData, getSelectedRequestData()) stays true.
local wtoBackupToken = null
if (!isSilent) { if (!isSilent) {
let source = this.getWtoBackupSourceForRequest(requestData) let source = this.getWtoBackupSourceForRequest(requestData)
let backup = this.getWtoBackupToken(requestData, source) let backup = this.getWtoBackupToken(requestData, source)
if (backup == null) if (backup == null)
return -1 return -1
if (backup != "") { if (backup != "") {
requestData.wtoBackup <- backup wtoBackupToken = backup
wtoBlk.wtoBackup = backup wtoBlk.wtoBackup = backup
} }
if (source == "vehicle") if (source == "vehicle") {
this.universalSpareUidForRespawn = ""
requestData.spareUid = "" requestData.spareUid = ""
}
} }
return wtoGuiRespawn.requestAircraftAndWeaponWithSlots(requestData, requestData.idInCountry, let wtoTaskId = wtoGuiRespawn.requestAircraftAndWeaponWithSlots(requestData, requestData.idInCountry,
requestData.respBaseId, requestData.spareUid, wtoBlk, DataBlock()) requestData.respBaseId, requestData.spareUid, wtoBlk, DataBlock())
if (wtoTaskId >= 0)
this.wtoLastBackup = wtoBackupToken
return wtoTaskId
} }
function requestAircraftAndWeapon(requestData) { function requestAircraftAndWeapon(requestData) {
@@ -172,20 +172,23 @@ function wtoOnLocalMpNuclearStrike(_ev) {
function wtoOnLocalMpBackupSpawn(ev) { function wtoOnLocalMpBackupSpawn(ev) {
let { ndbTryRead, ndbWrite } = require("nestdb") let { ndbTryRead, ndbWrite } = require("nestdb")
let battle = ndbTryRead(["wto", "activeBattle"]) let battle = ndbTryRead(["wto", "activeBattle"])
if (battle?.backups == null || (ev?.unit ?? "") == "" || (ev?.backup ?? "") == "") // Native UnitInfo::name prefixes ground/naval units (e.g. "tankModels/cn_type_59"); keep the bare unit id.
let parts = (ev?.unit ?? "").split("/")
let unit = parts.len() > 0 ? parts.top() : ""
if (battle?.backups == null || unit == "" || (ev?.backup ?? "") == "")
return return
let slot = ev?.slot ?? -1 let slot = ev?.slot ?? -1
let spawns = ndbTryRead(["wto", "battleLog", "spares"]) ?? [] let spawns = ndbTryRead(["wto", "battleLog", "spares"]) ?? []
if (slot < 0 || spawns.findvalue(@(spawn) spawn.slot == slot) != null) if (slot < 0 || spawns.findvalue(@(spawn) spawn.slot == slot) != null)
return return
wtoAppendBattleLog("spares", { slot, unit = ev.unit, backup = ev.backup }) wtoAppendBattleLog("spares", { slot, unit, backup = ev.backup })
let { DAILY_FREE_SPARE_UID } = require("%scripts/respawn/respawnDailyFreeSpare.nut") let { DAILY_FREE_SPARE_UID } = require("%scripts/respawn/respawnDailyFreeSpare.nut")
if (ev.backup == DAILY_FREE_SPARE_UID) { if (ev.backup == DAILY_FREE_SPARE_UID) {
let path = ["wto", "battleLog", "dailyBackupsUsed"] let path = ["wto", "battleLog", "dailyBackupsUsed"]
ndbWrite(path, (ndbTryRead(path) ?? 0) + 1) ndbWrite(path, (ndbTryRead(path) ?? 0) + 1)
let { matchingApiFunc } = require("%scripts/matching/api.nut") let { matchingApiFunc } = require("%scripts/matching/api.nut")
matchingApiFunc("wto_battle.daily_spare", @(_response) null, { sessionId = battle.sessionId, slot, unit = ev.unit }) matchingApiFunc("wto_battle.daily_spare", @(_response) null, { sessionId = battle.sessionId, slot, unit })
} }
else if (ev.backup != "vehicle") { else if (ev.backup != "vehicle") {
let { markUsedItemCount } = require("%scripts/items/usedItemsInBattle.nut") let { markUsedItemCount } = require("%scripts/items/usedItemsInBattle.nut")