Files
WT-Offline/gui-patches/battle/record-local-battle-awards/replace.nut
T
N0VAN0M4LY 91563f05cb Take the LocalMP battle outcome when the battle is left
The MissionResult HUD event did not reach the scripts before the battle
was left (a nuclear win in game still settled as not won): the HUD can
hold the result back (2.61.0.37 0x183edc0, delay at Hud +0xb8). The
eventbus event onQuitToDebriefing, however, is raised both on the fade
to the debriefing after the battle ends and when leaving from the menu,
while the mission still exists; the stock debriefing gathers its result
on it too.

Record the end time, the closing life and the outcome there, from the
mission status (success / fail; a running battle records no outcome),
and drop the MissionResult subscription.

Checked with check-gui; not yet verified in game.
2026-11-28 20:33:54 +08:00

195 lines
8.0 KiB
Plaintext

// WTOffline: record the local player's lives and combat events in a LocalMP battle and show the
// reward banner. A LocalMP host has no battle server to price events: the native bridge raises the
// HUD event "wtoLocalMpExpEvent" with the facts, the banner amount comes from the parameters the
// local service sent at launch (nestdb wto.activeBattle.awards, battle-economy.ts BannerParams), and
// the record (nestdb wto.battleLog) is settled by the local service after the battle.
// Each fact is also sent to the local service as it happens (matching request wto_battle.fact);
// the battle awards (streaks) it completed come back and are shown with the stock award banner,
// as the battle server's award messages are online.
function wtoShowLiveStreaks(resp) {
let streaks = resp?.streaks ?? []
if (streaks.len() == 0)
return
let { add_streak_message, getLocForStreak } = require("%scripts/streaks.nut")
let { SNT_MY_STREAK_HEADER } = require("%globalScripts/battleMetaConsts.nut")
let { HUD_MSG_STREAK } = require("hudMessages")
let { g_hud_event_manager } = require("%scripts/hud/hudEventManager.nut")
let localId = require("mission").get_local_mplayer()?.id ?? -1
foreach (streak in streaks) {
let stageParam = streak?.stageParam ?? 0
add_streak_message({
header = getLocForStreak(SNT_MY_STREAK_HEADER, streak.id, stageParam)
wp = streak?.wp ?? 0
exp = streak?.exp ?? 0
id = streak.id
})
g_hud_event_manager.onHudEvent("HudMessage", { type = HUD_MSG_STREAK, unlockId = streak.id, stage = stageParam, playerId = localId })
}
}
function wtoSendLiveFact(kind, fact) {
let battle = require("nestdb").ndbTryRead(["wto", "activeBattle"])
if (battle == null)
return
let { matchingApiFunc } = require("%scripts/matching/api.nut")
matchingApiFunc("wto_battle.fact", wtoShowLiveStreaks, fact.__merge({
sessionId = battle.sessionId
kind
localPlayerId = require("mission").get_local_mplayer()?.id ?? -1
localTeam = require("mission").get_mp_local_team()
}))
}
function wtoAppendBattleLog(key, entry) {
let { ndbTryRead, ndbWrite } = require("nestdb")
let list = (ndbTryRead(["wto", "battleLog", key]) ?? []).map(@(v) v)
list.append(entry)
ndbWrite(["wto", "battleLog", key], list)
}
function wtoBannerReward(awards, unit, ev) {
let code = ev?.messageCode ?? -1
let isCapture = code == EXP_EVENT_CAPTURE_ZONE
let expClass = ev?.expClass ?? ""
let group = isCapture ? "own" : (awards?.groups?[expClass] ?? "ground")
let baseReward = awards?.vehicles?[unit]?[$"{code}:{group}"]
if (baseReward == null)
return null
let ai = (isCapture || (ev?.victimIsPlayer ?? 0) != 0) ? 1.0 : awards.aiKillK
let slClass = isCapture ? 1.0 : (awards?.slClass?[expClass] ?? 1.0)
let rpClass = isCapture ? 1.0 : (awards?.rpClass?[expClass] ?? 1.0)
if ((ev?.friendly ?? 0) != 0)
return { wp = -(baseReward[0] * slClass * ai * awards.friendlyKillPenaltyMul + 0.5).tointeger(), exp = 0 }
let count = code == EXP_EVENT_UNIT_DAMAGE ? (ev?.damage ?? 0) : 1
return {
wp = (baseReward[0] * slClass * ai * count + 0.5).tointeger()
exp = (baseReward[1] * rpClass * ai * count + 0.5).tointeger()
}
}
function wtoOnLocalMpExpEvent(ev) {
let { ndbTryRead } = require("nestdb")
let battle = ndbTryRead(["wto", "activeBattle"])
if (battle == null)
return
let wtoMission = require("mission")
let unit = ndbTryRead(["wto", "battleLog", "openLife"])?.unit ?? require("unit").get_player_unit_name()
let entry = {
time = wtoMission.get_mission_time()
unit
code = ev?.messageCode ?? -1
expClass = ev?.expClass ?? ""
victim = ev?.victim ?? ""
victimIsPlayer = (ev?.victimIsPlayer ?? 0) != 0
friendly = (ev?.friendly ?? 0) != 0
modifier = ev?.messageModifier ?? ""
damage = ev?.damage ?? 0
}
wtoAppendBattleLog("events", entry)
wtoSendLiveFact("event", entry)
let reward = wtoBannerReward(battle?.awards, unit, ev)
if (reward == null)
return
let { g_hud_event_manager } = require("%scripts/hud/hudEventManager.nut")
g_hud_event_manager.onHudEvent("InBattleReward", {
messageCode = ev?.messageCode ?? -1
warpoints = reward.wp
experience = reward.exp
counter = 1
expClass = ev?.expClass ?? ""
messageModifier = ev?.messageModifier ?? ""
})
}
function wtoCloseBattleLife(died) {
let { ndbTryRead, ndbDelete } = require("nestdb")
let life = ndbTryRead(["wto", "battleLog", "openLife"])
if (life == null)
return
ndbDelete(["wto", "battleLog", "openLife"])
let closed = life.__merge({ end = require("mission").get_mission_time(), died })
wtoAppendBattleLog("lives", closed)
wtoSendLiveFact("life", closed)
}
function wtoOnLocalPlayerAlive(_ev) {
let { ndbExists, ndbWrite } = require("nestdb")
if (!ndbExists(["wto", "activeBattle"]))
return
wtoCloseBattleLife(false)
let unit = require("unit").get_player_unit_name()
if ((unit ?? "") != "")
ndbWrite(["wto", "battleLog", "openLife"], { unit, start = require("mission").get_mission_time() })
}
function wtoOnLocalPlayerDead(_ev) {
if (require("nestdb").ndbExists(["wto", "activeBattle"]))
wtoCloseBattleLife(true)
}
// Every kill of the HUD kill log, for the awards that depend on who killed whom (first kill,
// avenging). A message without player ids is about the local player (scripts/hud/hudBattleLog.nut).
function wtoOnHudMessage(msg) {
let { HUD_MSG_MULTIPLAYER_DMG } = require("hudMessages")
if (msg?.type != HUD_MSG_MULTIPLAYER_DMG || !(msg?.isKill ?? true))
return
if (!require("nestdb").ndbExists(["wto", "activeBattle"]))
return
let wtoMission = require("mission")
let localId = wtoMission.get_local_mplayer()?.id ?? -1
let localTeam = wtoMission.get_mp_local_team()
let killer = msg?.playerId ?? localId
let victim = msg?.victimPlayerId ?? localId
let kill = {
time = wtoMission.get_mission_time()
killer
killerTeam = msg?.team ?? (killer == localId ? localTeam : 0)
victim
victimTeam = msg?.victimTeam ?? (victim == localId ? localTeam : 0)
}
wtoAppendBattleLog("feed", kill)
wtoSendLiveFact("kill", kill)
}
// The local player's nuclear bomb exploded (raised by the native bridge, local_mp_nuke.c).
function wtoOnLocalMpNuclearStrike(_ev) {
if (!require("nestdb").ndbExists(["wto", "activeBattle"]))
return
let strike = { time = require("mission").get_mission_time() }
wtoAppendBattleLog("strikes", strike)
wtoSendLiveFact("strike", strike)
}
function wtoSubscribeBattleAwards() {
let { g_hud_event_manager } = require("%scripts/hud/hudEventManager.nut")
g_hud_event_manager.subscribe("wtoLocalMpExpEvent", wtoOnLocalMpExpEvent)
g_hud_event_manager.subscribe("LocalPlayerAlive", wtoOnLocalPlayerAlive)
g_hud_event_manager.subscribe("LocalPlayerDead", wtoOnLocalPlayerDead)
g_hud_event_manager.subscribe("HudMessage", wtoOnHudMessage)
g_hud_event_manager.subscribe("wtoLocalMpNuclearStrike", wtoOnLocalMpNuclearStrike)
}
// The local player leaves the battle, after its end (the fade to the debriefing) or from the menu
// before it. The mission still exists here and is unloaded before the debriefing opens, so this is
// where the settlement report gets the end time and the outcome, as the stock debriefing gathers
// its result on this event (debriefingFull.nut). Leaving a running battle records no outcome.
function wtoOnQuitToDebriefing(_p) {
let { ndbExists, ndbWrite } = require("nestdb")
if (!ndbExists(["wto", "activeBattle"]) || ndbExists(["wto", "battleLog", "sessionEnd"]))
return
wtoCloseBattleLife(false)
ndbWrite(["wto", "battleLog", "sessionEnd"], require("mission").get_mission_time())
let guiMis = require("guiMission")
let status = guiMis.get_mission_status()
if (status == guiMis.MISSION_STATUS_SUCCESS || status == guiMis.MISSION_STATUS_FAIL)
ndbWrite(["wto", "battleLog", "outcome"], status == guiMis.MISSION_STATUS_SUCCESS ? "win" : "lose")
}
wtoSubscribeBattleAwards()
require("%sqStdLibs/helpers/subscriptions.nut").add_event_listener("HudEventManagerReset", @(_p) wtoSubscribeBattleAwards())
require("eventbus").eventbus_subscribe("onQuitToDebriefing", wtoOnQuitToDebriefing)
return {
g_hud_reward_message
}