diff --git a/native/wto-net/src/local_mp_damage_notifications.c b/native/wto-net/src/local_mp_damage_notifications.c index 6c48852..95e0983 100644 --- a/native/wto-net/src/local_mp_damage_notifications.c +++ b/native/wto-net/src/local_mp_damage_notifications.c @@ -1,9 +1,10 @@ /* - * Bind the stock barrel and breech HUD notices to the damaged unit on a LocalMP host. + * Bind the stock damage and control-loss HUD notices to the unit they describe on a LocalMP host. * The native damage and firing handlers retain their normal effects. */ #include "local_mp_internal.h" #include "local_mp_values.h" +#include "hook_config.h" #include "MinHook.h" typedef uint8_t (__fastcall *BarrelDamageFn)(void *damage, const uint8_t *impact); @@ -13,10 +14,21 @@ typedef void (__fastcall *FireUpdateFn)( typedef void (__fastcall *DeliverHudFn)( int32_t type, int32_t id, int32_t arg3, int32_t arg4, int32_t arg5, float arg6, float arg7, uint8_t arg8); +typedef void (__fastcall *DeliverDamageHudFn)(int32_t type, int32_t text_id, int32_t id); typedef const uint8_t *(__fastcall *ControlledUnitFn)(const uint8_t *mplayer); -/* HUD_MSG_EVENT is registered in the stock hudMessages module. */ -enum { HUD_MSG_EVENT = 9 }; +/* HUD message types, registered in the stock hudMessages module. */ +enum { HUD_MSG_DAMAGE = 4, HUD_MSG_EVENT = 9 }; + +/* + * Control-loss hints of aircraft and helicopters: indices 261..283 in the stock HUD text table + * (tail, wing and flight controls, weapon and guided-weapon control, sensors, radar, RWR, TWS, + * optics, targeting pod, countermeasures, laser designator, night vision, indicators, snapped-off + * surfaces). The stock code emits them from the current hero unit's module state. + */ +enum { CONTROL_HINT_FIRST = 261, CONTROL_HINT_LAST = 283 }; +/* Diagnostics are bounded so a long battle cannot flood the run log. */ +enum { SUPPRESSED_LOG_LIMIT = 12 }; typedef struct DamageNotice { const uint8_t *context; @@ -30,6 +42,8 @@ static __declspec(thread) const DamageNotice *active_notice; static BarrelDamageFn original_barrel_damage; static FireUpdateFn original_fire_update; static DeliverHudFn original_deliver_hud; +static DeliverDamageHudFn original_damage_deliver_hud; +static int suppressed_logged; static uint8_t __fastcall hook_barrel_damage(void *damage, const uint8_t *impact) { @@ -58,15 +72,22 @@ static void __fastcall hook_fire_update( active_notice = previous; } -static int is_controlled_damage(const DamageNotice *notice) +/* The unit the local player's MPlayer controls, as the stock lookup resolves it. */ +static const uint8_t *local_controlled_unit(void) { const uint8_t *mplayer = g_local_mp_mplayer ? *g_local_mp_mplayer : NULL; + + return mplayer != NULL ? (const uint8_t *)((ControlledUnitFn)g_lmp.mplayer_controlled_unit)(mplayer) : NULL; +} + +static int is_controlled_damage(const DamageNotice *notice) +{ uintptr_t unit; - if (mplayer == NULL || notice->context == NULL) + if (notice->context == NULL) return 0; unit = *(const uintptr_t *)(notice->context + notice->unit_offset) & ~notice->unit_tag; - return unit != 0 && (const uint8_t *)unit == ((ControlledUnitFn)g_lmp.mplayer_controlled_unit)(mplayer); + return unit != 0 && (const uint8_t *)unit == local_controlled_unit(); } static void __fastcall hook_deliver_hud( @@ -81,12 +102,38 @@ static void __fastcall hook_deliver_hud( original_deliver_hud(type, id, arg3, arg4, arg5, arg6, arg7, arg8); } +/* + * The control-loss hints describe the unit in the stock hero manager's current-hero cell. That cell + * is one process-wide slot, and on a LocalMP host the local HUD can be handed another unit's hints. + * Drop a hint whose described unit is not the one the local player controls; the local player's own + * hints stay untouched. + */ +static void __fastcall hook_deliver_damage_hud(int32_t type, int32_t text_id, int32_t id) +{ + if (type == HUD_MSG_DAMAGE && text_id >= CONTROL_HINT_FIRST && text_id <= CONTROL_HINT_LAST + && local_mp_is_host()) { + const uint8_t *described = g_lmp.hero_unit != NULL ? *(const uint8_t *const *)g_lmp.hero_unit : NULL; + const uint8_t *controlled = local_controlled_unit(); + + if (described != NULL && controlled != NULL && described != controlled) { + if (suppressed_logged < SUPPRESSED_LOG_LIMIT) { + ++suppressed_logged; + log_line("local_mp: dropped control hint %d of a unit the local player does not control", + (int)text_id); + } + return; + } + } + original_damage_deliver_hud(type, text_id, id); +} + int install_local_mp_damage_notification_hooks(void) { const struct { void *target, *detour, **original; } hooks[] = { { g_lmp.barrel_notice_deliver, (void *)&hook_deliver_hud, (void **)&original_deliver_hud }, { g_lmp.barrel_damage, (void *)&hook_barrel_damage, (void **)&original_barrel_damage }, { g_lmp.breech_fire_update, (void *)&hook_fire_update, (void **)&original_fire_update }, + { g_lmp.hud_damage_deliver, (void *)&hook_deliver_damage_hud, (void **)&original_damage_deliver_hud }, }; size_t created = 0; diff --git a/native/wto-net/src/local_mp_values.c b/native/wto-net/src/local_mp_values.c index c2ab271..17433ff 100644 --- a/native/wto-net/src/local_mp_values.c +++ b/native/wto-net/src/local_mp_values.c @@ -150,6 +150,8 @@ static const LmpValueSpec SPECS[] = { A(DAMAGE_NOTICES, "breech_damage_notice.deliver", breech_notice_deliver), O(DAMAGE_NOTICES, "breech_damage_notice.id", breech_notice_id), A(DAMAGE_NOTICES, "mplayer_controlled_unit", mplayer_controlled_unit), + A(DAMAGE_NOTICES, "hud_damage_deliver", hud_damage_deliver), + A(DAMAGE_NOTICES, "hero_unit.cell", hero_unit), }; static void store(const LmpValueSpec *spec, int64_t value, uint8_t *base) diff --git a/native/wto-net/src/local_mp_values.h b/native/wto-net/src/local_mp_values.h index ca4eb1e..976c24b 100644 --- a/native/wto-net/src/local_mp_values.h +++ b/native/wto-net/src/local_mp_values.h @@ -77,6 +77,7 @@ typedef struct LmpValues { /* local_mp_damage_notifications */ uint8_t *barrel_damage, *breech_fire_update, *barrel_notice_deliver, *breech_notice_deliver, *mplayer_controlled_unit; + uint8_t *hud_damage_deliver, *hero_unit; size_t barrel_unit_ref, breech_unit, barrel_notice_id, breech_notice_id; } LmpValues; diff --git a/patches/native/local_mp_damage_notifications.jsonc b/patches/native/local_mp_damage_notifications.jsonc index 3dc09d1..e6a4128 100644 --- a/patches/native/local_mp_damage_notifications.jsonc +++ b/patches/native/local_mp_damage_notifications.jsonc @@ -1,5 +1,7 @@ -// Send barrel explosion and breech misfire notices only for the locally controlled unit. -// The native producers know the affected unit. The HUD receives only the type, text and ID. +// Send damage and control-loss notices only for the unit they describe on a LocalMP host. +// The barrel and breech producers know the affected unit; the control-loss hints describe the +// stock hero manager's current-hero cell, which on a local host can be left on another unit. +// The HUD receives only the type, text index and ID. { "module": "local_mp_damage_notifications", "features": [ @@ -87,6 +89,38 @@ } } ] + }, + // HUD_MSG_DAMAGE delivery: resolves the text index to localized text, then pushes + // {type, text, id} into the local HUD event queue. The control-loss hints (HUD text indices + // 261..283) are pushed here. + // 2.61.0.37: 0x19b1030; 2.61.0.43: 0x19b6ba0. + "hud_damage_deliver": { + "variants": [ + { + "written_for": "2.61.0.37", + "locate": { + "method": "signature", + "pattern": "56 57 53 48 83 ec 50 48 83 3d ?? ?? ?? ?? 00 0f 84 ?? ?? ?? ?? 44 89 c6 89 cf b0 01" + } + } + ] + }, + // HeroManager::on_controlled_hero_changed stores the previous and current controlled unit into + // two adjacent process-wide cells. The hint refresh code reads the current one. + // 2.61.0.37: 0xec6319, cell 0x7bad418; 2.61.0.43: 0xeca349, cell 0x7bb9778. + "hero_unit": { + "variants": [ + { + "written_for": "2.61.0.37", + "locate": { + "method": "signature", + "pattern": "48 89 05 ?? ?? ?? ?? 48 89 3d ?? ?? ?? ?? 48 8b 3d ?? ?? ?? ?? 48 39 fe 0f 94 c0 08 c8", + "captures": { + "cell": { "offset": 10, "type": "rel32" } + } + } + } + ] } } }