Files
WT-Offline/native/wto-net/src/local_mp_awards.c
T
N0VAN0M4LY 7c27d09f78 fix(battle): calculate LocalMP rewards in the local service
The LocalMP host has no battle server, and the previous bridge filled the
gap by computing money in the client: the native ledger wrote invented
amounts into SessionStatsNet, the spawn patch derived reward rates from
warpoints.blk, and the settlement patch summed and rescaled the result.
Rewards came out as zero or several orders of magnitude too high.

Now the client reports facts and the local service prices them.

Native (wto_ws.dll):
- local_mp_ledger.c is removed together with its SessionStatsNet
  offsets and four signatures (43 -> 39).
- local_mp_awards.c raises the HUD script event wtoLocalMpExpEvent for
  every combat event of the local player (event code, victim expClass
  and name, player or AI victim, friendly, naval damage). The DataBlock
  helpers and the HUD event queue are resolved from call sites inside
  GameSaveData::addScoreMessage, which already has a signature.

GUI patches:
- record-local-battle-awards records events and lives in nestdb and
  raises the stock InBattleReward banner from launch-time parameters.
- report-local-battle-settlement sends only facts and rebuilds the
  debriefing from the settled table; failures show the stock error box.
- use-settled-debriefing-stats makes gatherDebriefingResult read that
  table instead of the empty stat_get_exp().
- pass-spawn-modifications-blk no longer computes economy factors.

Local service:
- battle-economy dataset reads every reward factor from wpcost.blk,
  warpoints.blk and rank.blk; tuning/battle-economy.jsonc holds the
  mission score per event with its source (official values where
  published, otherwise marked).
- battle-economy.ts is the single pricing function, shared by the
  banner parameters and the settlement; battle-rewards.ts adds battle
  time, timed award, win/defeat bonus, activity, premium account,
  talisman, skill bonus and free research points; the debriefing table
  has the stat_get_exp() layout and travels in EULT_SESSION_RESULT.
- Crews earn skill points from the battle's research points.
- A battle left before it ends counts as a defeat.

Verified: npm run typecheck, check-gui (35 patches), check-patches,
check-offsets, native build. Not yet run in game.
2026-11-28 04:42:27 +08:00

121 lines
5.4 KiB
C

/*
* Reports the local player's combat events to the GUI scripts.
*
* Online, the battle server prices every combat event and sends it to the client
* (packet 0xb03d -> GameSaveData::addScoreMessage), which raises the HUD script event
* "InBattleReward". A LocalMP host has no battle server, so the kill and event hooks call
* local_mp_report_award_event() instead. It raises the HUD script event
* LMP_AWARD_EVENT_NAME carrying facts only (event code, victim class and name); the
* scripts price it with the parameters sent by the local service, show the banner and keep
* the record that the local service settles after the battle. Nothing here computes money.
*
* The event is queued with the same call addScoreMessage uses, but without its
* "hudShowAwardMessages" option gate, so the record does not depend on a HUD setting.
* Every engine helper below is resolved from a call site inside addScoreMessage
* (LMP_SIG_HUD_ADD_SCORE_MSG); see local_mp_offsets.h for the 2.61.0.37 addresses.
*/
#include "hooks.h"
#include "hook_config.h"
#include "local_mp_internal.h"
#include "local_mp_offsets.h"
#include <string.h>
typedef void (__fastcall *BlkCtorFn)(void *blk);
typedef void (__fastcall *BlkDtorFn)(void *blk);
typedef void (__fastcall *BlkToOwnedFn)(void *blk);
typedef void (__fastcall *BlkSetStrFn)(void *blk, const char *name, const char *value);
typedef void (__fastcall *BlkSetIntFn)(void *blk, const char *name, const int32_t *value);
typedef void (__fastcall *HudNotifyScriptFn)(void *queue, const char *event_name, const void *blk);
static uint8_t **g_game_save_data;
static uint32_t g_hud_queue_off;
static BlkCtorFn fn_blk_ctor;
static BlkDtorFn fn_blk_dtor;
static BlkToOwnedFn fn_blk_to_owned;
static BlkSetStrFn fn_blk_set_str;
static BlkSetIntFn fn_blk_set_int;
static HudNotifyScriptFn fn_hud_notify_script;
/* Mirrors addScoreMessage: integer setters expect an owned DataBlock, string setters convert it. */
static void blk_set_int(uint8_t *blk, const char *name, int32_t value)
{
if (*(int32_t *)(blk + LMP_BLK_OFF_OWNED_STATE) != -1)
fn_blk_to_owned(blk);
fn_blk_set_int(blk, name, &value);
}
const char *local_mp_unit_exp_class(const uint8_t *unit)
{
const uint8_t *uinfo;
const char *exp_class;
if (unit == NULL)
return NULL;
uinfo = *(const uint8_t *const *)(unit + LMP_UNIT_OFF_INFO);
if (uinfo == NULL)
return NULL;
exp_class = *(const char *const *)(uinfo + LMP_UNIT_INFO_OFF_EXP_CLASS);
return (exp_class != NULL && exp_class[0] != '\0') ? exp_class : NULL;
}
void local_mp_report_award_event(const LocalMpAwardEvent *ev)
{
__declspec(align(16)) uint8_t blk[LMP_BLK_SIZE];
uint8_t *gsd = g_game_save_data ? *g_game_save_data : NULL;
const char *victim_name = ev ? local_mp_unit_name(ev->victim) : NULL;
const char *victim_class = ev ? local_mp_unit_exp_class(ev->victim) : NULL;
if (ev == NULL || gsd == NULL || fn_hud_notify_script == NULL)
return;
memset(blk, 0, sizeof(blk));
fn_blk_ctor(blk);
fn_blk_set_str(blk, "expClass", victim_class ? victim_class : "");
fn_blk_set_str(blk, "victim", victim_name ? victim_name : "");
fn_blk_set_str(blk, "messageModifier", ev->modifier ? ev->modifier : "");
blk_set_int(blk, "messageCode", (int32_t)ev->exp_event);
blk_set_int(blk, "victimIsPlayer", ev->victim_is_player ? 1 : 0);
blk_set_int(blk, "friendly", ev->friendly ? 1 : 0);
blk_set_int(blk, "damage", ev->damage);
fn_hud_notify_script(gsd + g_hud_queue_off, LMP_AWARD_EVENT_NAME, blk);
fn_blk_dtor(blk);
}
static void *call_target(uint8_t *score_msg, size_t call_off)
{
return local_mp_resolve_rip(score_msg + call_off, 1, 5);
}
int install_local_mp_award_hooks(const uint8_t *text, size_t text_size, uint8_t *p_ground_dead)
{
uint8_t *p_score_msg = local_mp_scan_unique(text, text_size, "GameSaveData::addScoreMessage", LMP_SIG_HUD_ADD_SCORE_MSG);
static const size_t call_offs[] = {
LMP_SCORE_MSG_CALL_BLK_CTOR, LMP_SCORE_MSG_CALL_BLK_SET_STR, LMP_SCORE_MSG_CALL_BLK_TO_OWNED,
LMP_SCORE_MSG_CALL_BLK_SET_INT, LMP_SCORE_MSG_CALL_HUD_NOTIFY, LMP_SCORE_MSG_CALL_BLK_DTOR,
};
size_t i;
if (!p_ground_dead || !p_score_msg)
return 0;
if (p_ground_dead[0xa19] != 0x48 || p_ground_dead[0xa1a] != 0x8b || p_ground_dead[0xa1b] != 0x0d)
return 0;
for (i = 0; i < sizeof(call_offs) / sizeof(call_offs[0]); i++) {
if (p_score_msg[call_offs[i]] != 0xe8)
return 0;
}
if (p_score_msg[LMP_SCORE_MSG_ADD_HUD_QUEUE] != 0x48 ||
p_score_msg[LMP_SCORE_MSG_ADD_HUD_QUEUE + 1] != 0x81 ||
p_score_msg[LMP_SCORE_MSG_ADD_HUD_QUEUE + 2] != 0xc1)
return 0;
g_game_save_data = (uint8_t **)local_mp_resolve_rip(p_ground_dead + 0xa19, 3, 7);
memcpy(&g_hud_queue_off, p_score_msg + LMP_SCORE_MSG_ADD_HUD_QUEUE + 3, sizeof(g_hud_queue_off));
fn_blk_ctor = (BlkCtorFn)call_target(p_score_msg, LMP_SCORE_MSG_CALL_BLK_CTOR);
fn_blk_set_str = (BlkSetStrFn)call_target(p_score_msg, LMP_SCORE_MSG_CALL_BLK_SET_STR);
fn_blk_to_owned = (BlkToOwnedFn)call_target(p_score_msg, LMP_SCORE_MSG_CALL_BLK_TO_OWNED);
fn_blk_set_int = (BlkSetIntFn)call_target(p_score_msg, LMP_SCORE_MSG_CALL_BLK_SET_INT);
fn_hud_notify_script = (HudNotifyScriptFn)call_target(p_score_msg, LMP_SCORE_MSG_CALL_HUD_NOTIFY);
fn_blk_dtor = (BlkDtorFn)call_target(p_score_msg, LMP_SCORE_MSG_CALL_BLK_DTOR);
return 1;
}