Files
WT-Offline/native/wto-net/src/local_mp_events.c
T
N0VAN0M4LY fdac56a036 refactor(native)!: load LocalMP values from the build configuration
wto_ws.dll no longer scans aces.exe or carries structure offsets of a game
build. It reads the values the patcher resolved (wto_ws.cfg native.* lines)
into g_lmp through one table that names, per module, which value fills which
field (local_mp_values.c), and installs each module whose values are all
present, in dependency order, instead of giving up on the whole bridge when
one subsystem is missing. When the settings were resolved for another
executable (PE time stamp and image size differ), the bridge stays off.

local_mp_offsets.h keeps the existing names so the module code reads as
before; only Dagor library layouts (dag::Vector, DataBlock), protocol enums,
the reflection buffer size and the two change request members with no reader
in the client remain constants. A few fields are derived where the client has
no instruction of its own for them (local_mp_values.c derive_values). The
signatures moved to patches/native, and the header-based check-offsets
command is gone: record-game-version and the launcher's patch check resolve
the native modules now.

Shared sites moved to local_mp_core (mission customBots, the player's unit
eid) so modules do not depend on each other for them.

Checked: DLL build (no warnings outside MinHook); root and launcher type
checks; check-patches on 2.61.0.37; version records for 2.61.0.37 and
2.61.0.43 (10 native modules each); a lab run of the 2.61.0.37 copy logs
239 native values and "9 of 9 LocalMP modules installed". Battles were not
played in this run.

BREAKING CHANGE: Rebuild game copies to generate the new native values in wto_ws.cfg. Use record-game-version.ts or the launcher patch check in place of wtotools check-offsets.
2026-11-29 12:26:11 +08:00

253 lines
11 KiB
C

/*
* LocalMP non-fatal combat event dispatchers: deduplicated assists across air/ground/naval,
* zone captures, hits/criticals, severe damage, and naval damage awards.
*
* `IGroundModel::onBecameDead` calls both `IGroundModel::awardAssists` (`0x465540`) and
* `Unit::awardAssists` (`0xe5b200` -> `dispatchAssist` `0x35beb0`); deduplicating `(victim, mp)`
* per unit death ensures every assist across air, ground, and naval is credited strictly once.
* Scores come from the mission settings (local_mp_scores.c).
*/
#include "hooks.h"
#include "hook_config.h"
#include "local_mp_internal.h"
#include "local_mp_offsets.h"
#include "MinHook.h"
#include <string.h>
#define MAX_OFFENDER_SNAP 64u
#define MAX_ASSIST_DEDUP 64u
typedef struct AssistDedupRecord {
const uint8_t *victim;
const uint8_t *offender_mp;
} AssistDedupRecord;
typedef void (__fastcall *IGroundAwardAssistsFn)(uint8_t *victim);
typedef void (__fastcall *DispatchAssistFn)(int64_t uid, uint8_t *victim, uint8_t *offender, float share, int32_t extra);
typedef void (__fastcall *DispatchCaptureZoneFn)(int64_t uid, uint8_t *capturer_unit);
typedef void (__fastcall *DispatchHitCritFn)(int64_t uid, uint8_t *victim, uint8_t *offender, float dmg, uint8_t is_kill, uint8_t is_crit, uint8_t extra);
typedef void (__fastcall *DispatchSevereDamageFn)(int64_t uid, uint8_t *victim, uint8_t *offender);
typedef void (__fastcall *DispatchNavalDamageFn)(int64_t uid, uint8_t *victim, uint8_t *offender, float award_dmg);
static IGroundAwardAssistsFn orig_iground_award_assists;
static DispatchAssistFn orig_dispatch_assist;
static DispatchCaptureZoneFn orig_dispatch_capture_zone;
static DispatchHitCritFn orig_dispatch_hit_crit;
static DispatchSevereDamageFn orig_dispatch_severe_damage;
static DispatchNavalDamageFn orig_dispatch_naval_damage;
static AssistDedupRecord g_assist_dedup[MAX_ASSIST_DEDUP];
static size_t g_assist_dedup_cursor = 0;
void local_mp_events_reset(void)
{
memset(g_assist_dedup, 0, sizeof(g_assist_dedup));
g_assist_dedup_cursor = 0;
}
void local_mp_events_begin_death(const uint8_t *victim)
{
size_t i;
if (victim == NULL)
return;
for (i = 0; i < MAX_ASSIST_DEDUP; i++) {
if (g_assist_dedup[i].victim == victim) {
g_assist_dedup[i].victim = NULL;
g_assist_dedup[i].offender_mp = NULL;
}
}
}
static int mark_assist_credited_once(const uint8_t *victim, const uint8_t *mp)
{
size_t i;
for (i = 0; i < MAX_ASSIST_DEDUP; i++) {
if (g_assist_dedup[i].victim == victim && g_assist_dedup[i].offender_mp == mp)
return 0;
}
g_assist_dedup[g_assist_dedup_cursor].victim = victim;
g_assist_dedup[g_assist_dedup_cursor].offender_mp = mp;
g_assist_dedup_cursor = (g_assist_dedup_cursor + 1u) % MAX_ASSIST_DEDUP;
return 1;
}
static void credit_assist_to_mplayer(uint8_t *mp, uint8_t *victim)
{
uint8_t *victim_mp = *(uint8_t **)(victim + LMP_UNIT_OFF_MPLAYER);
float pts;
if (mp == NULL || mp == victim_mp || !mark_assist_credited_once(victim, mp))
return;
pts = local_mp_event_score("assist", victim, victim_mp != NULL);
local_mp_inc_mplayer_stat(mp, LMP_MP_OFF_ASSISTS);
local_mp_add_mplayer_score(mp, pts, LMP_EXP_EVENT_ASSIST);
local_mp_mark_mplayer_dirty(mp);
if (local_mp_is_local_mplayer(mp)) {
LocalMpAwardEvent award = { LMP_EXP_EVENT_ASSIST, victim, victim_mp != NULL, 0, NULL, 0 };
local_mp_report_award_event(&award);
}
}
static void __fastcall hook_iground_award_assists(uint8_t *victim)
{
uint8_t prev_sent[MAX_OFFENDER_SNAP];
uint32_t count = 0;
uint8_t *arr = NULL;
uint32_t i;
if (local_mp_is_host() && victim != NULL) {
arr = *(uint8_t **)(victim + LMP_IGM_OFF_OFFENDERS_PTR);
count = *(uint32_t *)(victim + LMP_IGM_OFF_OFFENDERS_CNT);
if (count > MAX_OFFENDER_SNAP)
count = MAX_OFFENDER_SNAP;
for (i = 0; arr != NULL && i < count; i++)
prev_sent[i] = arr[i * LMP_OFFENDER_STRIDE + LMP_OFFENDER_OFF_ASSIST_SENT];
}
orig_iground_award_assists(victim);
if (local_mp_is_host() && victim != NULL && arr != NULL && count > 0) {
arr = *(uint8_t **)(victim + LMP_IGM_OFF_OFFENDERS_PTR);
for (i = 0; arr != NULL && i < count; i++) {
uint8_t *off = arr + i * LMP_OFFENDER_STRIDE;
if (prev_sent[i] == 0 && off[LMP_OFFENDER_OFF_ASSIST_SENT] != 0) {
uint8_t *mp = local_mp_find_mplayer(
*(int64_t *)(off + LMP_OFFENDER_OFF_UID), off[LMP_OFFENDER_OFF_PID]);
credit_assist_to_mplayer(mp, victim);
}
}
}
}
static void __fastcall hook_dispatch_assist(int64_t uid, uint8_t *victim, uint8_t *offender, float share, int32_t extra)
{
if (local_mp_is_host() && victim != NULL) {
uint8_t *mp = local_mp_find_mplayer(uid, offender ? offender[LMP_OFFENDER_OFF_PID] : 0xffu);
credit_assist_to_mplayer(mp, victim);
}
orig_dispatch_assist(uid, victim, offender, share, extra);
}
static void __fastcall hook_dispatch_capture_zone(int64_t uid, uint8_t *capturer_unit)
{
if (local_mp_is_host()) {
uint8_t *mp = local_mp_find_mplayer(uid, 0xffu);
if (mp == NULL && capturer_unit != NULL)
mp = *(uint8_t **)(capturer_unit + LMP_UNIT_OFF_MPLAYER);
if (mp != NULL) {
const uint8_t *own_unit = capturer_unit != NULL ? capturer_unit
: (g_lmp_get_unit_by_eid ? (const uint8_t *)g_lmp_get_unit_by_eid(*(uint32_t *)(mp + LMP_MP_OFF_UNIT_EID)) : NULL);
local_mp_inc_mplayer_stat(mp, LMP_MP_OFF_CAPTURE_ZONE);
local_mp_add_mplayer_score(mp, local_mp_event_score("capture", own_unit, 1), LMP_EXP_EVENT_CAPTURE_ZONE);
local_mp_mark_mplayer_dirty(mp);
if (local_mp_is_local_mplayer(mp)) {
LocalMpAwardEvent award = { LMP_EXP_EVENT_CAPTURE_ZONE, NULL, 0, 0, NULL, 0 };
local_mp_report_award_event(&award);
}
}
}
orig_dispatch_capture_zone(uid, capturer_unit);
}
static void __fastcall hook_dispatch_hit_crit(
int64_t uid, uint8_t *victim, uint8_t *offender, float dmg, uint8_t is_kill, uint8_t is_crit, uint8_t extra
)
{
if (local_mp_is_host() && !is_kill && victim != NULL) {
uint8_t *mp = local_mp_find_mplayer(uid, offender ? offender[LMP_OFFENDER_OFF_PID] : 0xffu);
if (mp != NULL && mp != *(uint8_t **)(victim + LMP_UNIT_OFF_MPLAYER)) {
float pts = local_mp_event_score(is_crit ? "criticalHit" : "hit", victim,
*(uint8_t **)(victim + LMP_UNIT_OFF_MPLAYER) != NULL);
uint32_t ev = is_crit ? LMP_EXP_EVENT_CRITICAL_HIT : LMP_EXP_EVENT_HIT;
local_mp_add_mplayer_score(mp, pts, ev);
local_mp_mark_mplayer_dirty(mp);
if (local_mp_is_local_mplayer(mp)) {
LocalMpAwardEvent award = {
ev, victim, *(uint8_t **)(victim + LMP_UNIT_OFF_MPLAYER) != NULL, 0, NULL, 0
};
local_mp_report_award_event(&award);
}
}
}
orig_dispatch_hit_crit(uid, victim, offender, dmg, is_kill, is_crit, extra);
}
static void __fastcall hook_dispatch_severe_damage(int64_t uid, uint8_t *victim, uint8_t *offender)
{
if (local_mp_is_host() && victim != NULL && offender != NULL) {
uint8_t victim_army = victim[LMP_UNIT_OFF_ARMY];
if (victim_army == 0 || *(int32_t *)(offender + LMP_OFFENDER_OFF_ARMY) != (int32_t)victim_army) {
uint8_t *mp = local_mp_find_mplayer(uid, offender[LMP_OFFENDER_OFF_PID]);
uint8_t *victim_mp = *(uint8_t **)(victim + LMP_UNIT_OFF_MPLAYER);
if (mp != NULL && mp != victim_mp && victim_mp != NULL) {
uint16_t utype = *(uint16_t *)(victim + LMP_UNIT_OFF_TYPE);
int is_air = (utype == 0u || utype == 0xdu);
if (is_air) local_mp_inc_mplayer_stat(mp, LMP_MP_OFF_AIR_SEVERE_DMG);
else if (utype == 5u) local_mp_inc_mplayer_stat(mp, LMP_MP_OFF_SHIP_SEVERE_DMG);
local_mp_add_mplayer_score(mp, local_mp_event_score("severeDamage", victim, 1), LMP_EXP_EVENT_SEVERE_DAMAGE);
local_mp_mark_mplayer_dirty(mp);
if (local_mp_is_local_mplayer(mp)) {
LocalMpAwardEvent award = { LMP_EXP_EVENT_SEVERE_DAMAGE, victim, 1, 0, NULL, 0 };
local_mp_report_award_event(&award);
}
}
}
}
orig_dispatch_severe_damage(uid, victim, offender);
}
static void __fastcall hook_dispatch_naval_damage(int64_t uid, uint8_t *victim, uint8_t *offender, float award_dmg)
{
if (local_mp_is_host() && victim != NULL && offender != NULL && award_dmg > 0.0f) {
uint8_t victim_army = victim[LMP_UNIT_OFF_ARMY];
if (victim_army == 0 || *(int32_t *)(offender + LMP_OFFENDER_OFF_ARMY) != (int32_t)victim_army) {
uint8_t *mp = local_mp_find_mplayer(uid, offender[LMP_OFFENDER_OFF_PID]);
if (mp != NULL) {
int32_t add_dmg = (int32_t)(award_dmg + 0.5f);
float pts = award_dmg * local_mp_score_param("wtoScore_damagePerPoint");
if (add_dmg > 0) *(int32_t *)(mp + LMP_MP_OFF_AWARD_DAMAGE) += add_dmg;
if (pts >= 1.0f) local_mp_add_mplayer_score(mp, pts, LMP_EXP_EVENT_UNIT_DAMAGE);
local_mp_mark_mplayer_dirty(mp);
if (local_mp_is_local_mplayer(mp) && add_dmg > 0) {
LocalMpAwardEvent award = {
LMP_EXP_EVENT_UNIT_DAMAGE, victim, *(uint8_t **)(victim + LMP_UNIT_OFF_MPLAYER) != NULL,
0, NULL, add_dmg
};
local_mp_report_award_event(&award);
}
}
}
}
orig_dispatch_naval_damage(uid, victim, offender, award_dmg);
}
int install_local_mp_event_hooks(void)
{
uint8_t *p_igm_ass = g_lmp.iground_award_assists;
uint8_t *p_assist = g_lmp.dispatch_assist;
uint8_t *p_cap = g_lmp.dispatch_capture_zone;
uint8_t *p_hit = g_lmp.dispatch_hit_crit;
uint8_t *p_sev = g_lmp.dispatch_severe_damage;
uint8_t *p_nav = g_lmp.dispatch_naval_damage;
if (MH_CreateHook(p_igm_ass, (void *)&hook_iground_award_assists, (void **)&orig_iground_award_assists) != MH_OK ||
MH_EnableHook(p_igm_ass) != MH_OK ||
MH_CreateHook(p_assist, (void *)&hook_dispatch_assist, (void **)&orig_dispatch_assist) != MH_OK ||
MH_EnableHook(p_assist) != MH_OK ||
MH_CreateHook(p_cap, (void *)&hook_dispatch_capture_zone, (void **)&orig_dispatch_capture_zone) != MH_OK ||
MH_EnableHook(p_cap) != MH_OK ||
MH_CreateHook(p_hit, (void *)&hook_dispatch_hit_crit, (void **)&orig_dispatch_hit_crit) != MH_OK ||
MH_EnableHook(p_hit) != MH_OK ||
MH_CreateHook(p_sev, (void *)&hook_dispatch_severe_damage, (void **)&orig_dispatch_severe_damage) != MH_OK ||
MH_EnableHook(p_sev) != MH_OK ||
MH_CreateHook(p_nav, (void *)&hook_dispatch_naval_damage, (void **)&orig_dispatch_naval_damage) != MH_OK ||
MH_EnableHook(p_nav) != MH_OK) {
log_line("local_mp: failed to install combat event hooks");
return 0;
}
return 1;
}