Files
WT-Offline/native/wto-net/src/local_mp_awards.c
T
N0VAN0M4LY d40623cd47 fix(battle): correct LocalMP capture and hit feedback
Credit completed zone captures to the units counted by the mission engine and send the local capture fact through the existing settlement flow. Filter hit markers and shell-change hints using the actual attacker and active view.

TypeScript checks and the MSVC build passed. Verified exe, Quirrel, and native patches on 2.61.0.37, 2.61.0.58, 2.61.0.60, and 2.61.0.64. Rebuilt the .58 and .64 copies and verified local service health on .58. In-game verification is pending.
2026-12-05 10:24:14 +08:00

158 lines
6.2 KiB
C

/*
* Reports the local player's combat events and confirmed airfield departures 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 server, show the banner and keep
* the record that the local server 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, and the queue's offset in GameSaveData, is captured from a call site
* inside addScoreMessage (patches/native/local_mp_awards.jsonc).
*/
#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 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;
}
static const char *bare_unit_name(const char *unit_name)
{
const char *slash = strrchr(unit_name, '/');
return (slash != NULL && slash[1] != '\0') ? slash + 1 : unit_name;
}
static void report_award_event(const LocalMpAwardEvent *ev, const char *source_unit)
{
__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 : "");
if (source_unit != NULL)
fn_blk_set_str(blk, "unit", bare_unit_name(source_unit));
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 + LMP_GSD_OFF_HUD_QUEUE, LMP_AWARD_EVENT_NAME, blk);
fn_blk_dtor(blk);
}
void local_mp_report_award_event(const LocalMpAwardEvent *ev)
{
report_award_event(ev, NULL);
}
void local_mp_report_capture(const char *unit_name)
{
const LocalMpAwardEvent award = { LMP_EXP_EVENT_CAPTURE_ZONE, NULL, 0, 0, NULL, 0 };
report_award_event(&award, unit_name);
}
/* The fact behind the award killStreak_bomber_nuclear_success, which online the battle server
* judges itself; the event carries no fields. */
void local_mp_report_nuclear_strike(void)
{
__declspec(align(16)) uint8_t blk[LMP_BLK_SIZE];
uint8_t *gsd = g_game_save_data ? *g_game_save_data : NULL;
if (gsd == NULL || fn_hud_notify_script == NULL)
return;
memset(blk, 0, sizeof(blk));
fn_blk_ctor(blk);
fn_hud_notify_script(gsd + LMP_GSD_OFF_HUD_QUEUE, LMP_NUCLEAR_STRIKE_EVENT_NAME, blk);
fn_blk_dtor(blk);
}
void local_mp_report_airfield_spawn(const char *unit_name, int32_t base_id)
{
__declspec(align(16)) uint8_t blk[LMP_BLK_SIZE];
uint8_t *gsd = g_game_save_data ? *g_game_save_data : NULL;
if (gsd == NULL || fn_hud_notify_script == NULL || unit_name == NULL || base_id < 0)
return;
memset(blk, 0, sizeof(blk));
fn_blk_ctor(blk);
fn_blk_set_str(blk, "unitName", unit_name);
blk_set_int(blk, "baseId", base_id);
fn_hud_notify_script(gsd + LMP_GSD_OFF_HUD_QUEUE, "wtoLocalMpAirfieldSpawn", blk);
fn_blk_dtor(blk);
}
void local_mp_report_backup_spawn(const char *unit_name, int32_t slot_id, const char *backup)
{
__declspec(align(16)) uint8_t blk[LMP_BLK_SIZE];
uint8_t *gsd = g_game_save_data ? *g_game_save_data : NULL;
if (gsd == NULL || fn_hud_notify_script == NULL || unit_name == NULL)
return;
memset(blk, 0, sizeof(blk));
fn_blk_ctor(blk);
fn_blk_set_str(blk, "unit", bare_unit_name(unit_name));
fn_blk_set_str(blk, "backup", backup);
blk_set_int(blk, "slot", slot_id);
fn_hud_notify_script(gsd + LMP_GSD_OFF_HUD_QUEUE, "wtoLocalMpBackupSpawn", blk);
fn_blk_dtor(blk);
}
int install_local_mp_award_hooks(void)
{
g_game_save_data = (uint8_t **)g_lmp.game_save_data;
fn_blk_ctor = (BlkCtorFn)g_lmp.blk_ctor;
fn_blk_set_str = (BlkSetStrFn)g_lmp.blk_set_str;
fn_blk_to_owned = (BlkToOwnedFn)g_lmp.blk_to_owned;
fn_blk_set_int = (BlkSetIntFn)g_lmp.blk_set_int;
fn_hud_notify_script = (HudNotifyScriptFn)g_lmp.hud_notify_script;
fn_blk_dtor = (BlkDtorFn)g_lmp.blk_dtor;
return 1;
}