diff --git a/native/wto-net/src/local_mp_damage_notifications.c b/native/wto-net/src/local_mp_damage_notifications.c index d7bd965..03c9386 100644 --- a/native/wto-net/src/local_mp_damage_notifications.c +++ b/native/wto-net/src/local_mp_damage_notifications.c @@ -1,11 +1,11 @@ /* - * 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. + * Bind self damage, avionics failure and hit-parts feedback to the local player on a LocalMP host. + * All native damage, avionics failure and firing effects remain unchanged for every unit. */ #include "local_mp_internal.h" +#include "local_mp_offsets.h" #include "local_mp_values.h" #include "hook_config.h" -#include "MinHook.h" typedef uint8_t (__fastcall *BarrelDamageFn)(void *damage, const uint8_t *impact); typedef void (__fastcall *FireUpdateFn)( @@ -15,18 +15,15 @@ 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 void (__fastcall *AvionicsDamageFn)(const uint8_t *component, uint32_t kind); +typedef uint64_t (__fastcall *ExitOpticViewFn)(void *camera, int32_t arg2, uint8_t arg3); +typedef uint64_t (__fastcall *UnitOnEventFn)(void *unit, const uint8_t *event, void *arg3, void *arg4); +typedef void (__fastcall *HitPartsFeedbackFn)(void *hud, const uint8_t *victim, const void *info); typedef const uint8_t *(__fastcall *ControlledUnitFn)(const uint8_t *mplayer); /* 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 }; @@ -37,13 +34,77 @@ typedef struct DamageNotice { int32_t id; } DamageNotice; -/* Both producers deliver synchronously. Keep nested events and simulation threads independent. */ +/* All hooked producers deliver synchronously; thread-local slots keep nested calls isolated. */ static __declspec(thread) const DamageNotice *active_notice; +static __declspec(thread) const uint8_t *active_avionics_unit; +static __declspec(thread) const uint8_t *active_hit_event; + 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 AvionicsDamageFn original_avionics_damage; +static ExitOpticViewFn original_exit_optic_view; +static UnitOnEventFn original_unit_on_event; +static HitPartsFeedbackFn original_enqueue_enemy_parts; +static HitPartsFeedbackFn original_hit_cam_important; +static HitPartsFeedbackFn original_naval_hit_state; + +static int suppressed_avionics_logged; +static int suppressed_optic_logged; +static int suppressed_parts_logged; + +/* The unit the local player's MPlayer controls, falling back to g_hero when MPlayer is absent. */ +static const uint8_t *local_controlled_unit(void) +{ + const uint8_t *mplayer = g_local_mp_mplayer ? *g_local_mp_mplayer : NULL; + const uint8_t *unit = mplayer != NULL ? (const uint8_t *)((ControlledUnitFn)g_lmp.mplayer_controlled_unit)(mplayer) : NULL; + + if (unit != NULL) + return unit; + return g_local_mp_hero != NULL ? *g_local_mp_hero : NULL; +} + +static int is_local_controlled_unit(const uint8_t *unit) +{ + const uint8_t *controlled; + + if (unit == NULL) + return 0; + controlled = local_controlled_unit(); + return controlled != NULL && unit == controlled; +} + +static int is_controlled_damage(const DamageNotice *notice) +{ + uintptr_t unit; + + if (notice->context == NULL) + return 0; + unit = *(const uintptr_t *)(notice->context + notice->unit_offset) & ~notice->unit_tag; + return is_local_controlled_unit((const uint8_t *)unit); +} + +/* + * Enemy parts-list and hit-camera special-event feedback belong to the local player's own attacks + * (including in-flight munitions fired before switching to a drone or support plane) or to the + * watched unit while spectating. + */ +static int is_local_or_watched_attacker(const uint8_t *offender) +{ + const uint8_t *owner_mp; + const uint8_t *watched; + + if (offender == NULL) + return 0; + if (is_local_controlled_unit(offender)) + return 1; + owner_mp = *(const uint8_t *const *)(offender + LMP_UNIT_OFF_MPLAYER); + if (local_mp_is_local_mplayer(owner_mp)) + return 1; + watched = g_lmp.watched_hero != NULL ? *(const uint8_t *const *)g_lmp.watched_hero : NULL; + return watched != NULL && offender == watched; +} static uint8_t __fastcall hook_barrel_damage(void *damage, const uint8_t *impact) { @@ -72,24 +133,6 @@ static void __fastcall hook_fire_update( active_notice = previous; } -/* 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 (notice->context == NULL) - return 0; - unit = *(const uintptr_t *)(notice->context + notice->unit_offset) & ~notice->unit_tag; - return unit != 0 && (const uint8_t *)unit == local_controlled_unit(); -} - static void __fastcall hook_deliver_hud( int32_t type, int32_t id, int32_t arg3, int32_t arg4, int32_t arg5, float arg6, float arg7, uint8_t arg8) @@ -103,30 +146,99 @@ static void __fastcall hook_deliver_hud( } /* - * 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. + * Track the unit whose avionics/electronics component is failing. The stock dispatcher checks the + * unit's hero flag only for night-vision failure and emits HUD_MSG_DAMAGE (and optic-view exits) + * unconditionally for all other cases (guidance, sensors, radar, RWR, TWS, optics, targeting pod, + * laser designator, indicators). */ +static void __fastcall hook_avionics_damage(const uint8_t *component, uint32_t kind) +{ + const uint8_t *previous = active_avionics_unit; + + if (component != NULL && local_mp_is_host()) + active_avionics_unit = *(const uint8_t *const *)(component + g_lmp.avionics_unit); + original_avionics_damage(component, kind); + active_avionics_unit = previous; +} + 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(); + const uint8_t *unit = active_avionics_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; + if (unit != NULL && type == HUD_MSG_DAMAGE && local_mp_is_host() && !is_local_controlled_unit(unit)) { + if (suppressed_avionics_logged < SUPPRESSED_LOG_LIMIT) { + ++suppressed_avionics_logged; + log_line("local_mp: dropped avionics damage notice %d of a non-local unit", (int)text_id); } + return; } original_damage_deliver_hud(type, text_id, id); } +static uint64_t __fastcall hook_exit_optic_view(void *camera, int32_t arg2, uint8_t arg3) +{ + const uint8_t *unit = active_avionics_unit; + + if (unit != NULL && local_mp_is_host() && !is_local_controlled_unit(unit)) { + if (suppressed_optic_logged < 4) { + ++suppressed_optic_logged; + log_line("local_mp: prevented optic view exit triggered by a non-local unit"); + } + return 0; + } + return original_exit_optic_view(camera, arg2, arg3); +} + +static uint64_t __fastcall hook_unit_on_event(void *unit, const uint8_t *event, void *arg3, void *arg4) +{ + const uint8_t *previous = active_hit_event; + uint64_t result; + + if (event != NULL && local_mp_is_host()) + active_hit_event = event; + result = original_unit_on_event(unit, event, arg3, arg4); + active_hit_event = previous; + return result; +} + +static int should_drop_hit_parts_feedback(void) +{ + const uint8_t *event = active_hit_event; + const uint8_t *offender; + + if (event == NULL || !local_mp_is_host()) + return 0; + offender = *(const uint8_t *const *)(event + g_lmp.hit_event_offender); + if (is_local_or_watched_attacker(offender)) + return 0; + if (suppressed_parts_logged < SUPPRESSED_LOG_LIMIT) { + ++suppressed_parts_logged; + log_line("local_mp: dropped enemy parts feedback from a non-local attacker"); + } + return 1; +} + +static void __fastcall hook_enqueue_enemy_parts(void *hud, const uint8_t *victim, const void *info) +{ + if (should_drop_hit_parts_feedback()) + return; + original_enqueue_enemy_parts(hud, victim, info); +} + +static void __fastcall hook_hit_cam_important(void *hud, const uint8_t *victim, const void *info) +{ + if (should_drop_hit_parts_feedback()) + return; + original_hit_cam_important(hud, victim, info); +} + +static void __fastcall hook_naval_hit_state(void *hud, const uint8_t *victim, const void *info) +{ + if (should_drop_hit_parts_feedback()) + return; + original_naval_hit_state(hud, victim, info); +} + int install_local_mp_damage_notification_hooks(void) { const LocalMpHookSpec hooks[] = { @@ -134,6 +246,12 @@ int install_local_mp_damage_notification_hooks(void) { "barrel_damage", g_lmp.barrel_damage, (void *)&hook_barrel_damage, (void **)&original_barrel_damage }, { "breech_fire_update", g_lmp.breech_fire_update, (void *)&hook_fire_update, (void **)&original_fire_update }, { "hud_damage_deliver", g_lmp.hud_damage_deliver, (void *)&hook_deliver_damage_hud, (void **)&original_damage_deliver_hud }, + { "avionics_damage", g_lmp.avionics_damage, (void *)&hook_avionics_damage, (void **)&original_avionics_damage }, + { "avionics_exit_optic", g_lmp.avionics_exit_optic, (void *)&hook_exit_optic_view, (void **)&original_exit_optic_view }, + { "unit_on_event", g_lmp.unit_on_event, (void *)&hook_unit_on_event, (void **)&original_unit_on_event }, + { "enqueue_enemy_parts", g_lmp.enqueue_enemy_parts, (void *)&hook_enqueue_enemy_parts, (void **)&original_enqueue_enemy_parts }, + { "hit_cam_important", g_lmp.hit_cam_important, (void *)&hook_hit_cam_important, (void **)&original_hit_cam_important }, + { "naval_hit_state", g_lmp.naval_hit_state, (void *)&hook_naval_hit_state, (void **)&original_naval_hit_state }, }; if (g_lmp.barrel_notice_deliver != g_lmp.breech_notice_deliver) { diff --git a/native/wto-net/src/local_mp_values.c b/native/wto-net/src/local_mp_values.c index 17433ff..357edc6 100644 --- a/native/wto-net/src/local_mp_values.c +++ b/native/wto-net/src/local_mp_values.c @@ -151,7 +151,13 @@ static const LmpValueSpec SPECS[] = { 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), + A(DAMAGE_NOTICES, "avionics_damage", avionics_damage), O(DAMAGE_NOTICES, "avionics_damage.unit", avionics_unit), + A(DAMAGE_NOTICES, "avionics_exit_optic.call", avionics_exit_optic), + A(DAMAGE_NOTICES, "unit_on_event", unit_on_event), O(DAMAGE_NOTICES, "hit_event_offender.offender", hit_event_offender), + A(DAMAGE_NOTICES, "hit_watched_hero.watched_hero", watched_hero), + A(DAMAGE_NOTICES, "hit_parts_feedback.enqueue_enemy_parts", enqueue_enemy_parts), + A(DAMAGE_NOTICES, "hit_parts_feedback.hit_cam_important", hit_cam_important), + A(DAMAGE_NOTICES, "hit_parts_feedback.naval_hit_state", naval_hit_state), }; 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 976c24b..ca4d6ea 100644 --- a/native/wto-net/src/local_mp_values.h +++ b/native/wto-net/src/local_mp_values.h @@ -77,8 +77,9 @@ 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; + uint8_t *hud_damage_deliver, *avionics_damage, *avionics_exit_optic; + uint8_t *unit_on_event, *watched_hero, *enqueue_enemy_parts, *hit_cam_important, *naval_hit_state; + size_t barrel_unit_ref, breech_unit, barrel_notice_id, breech_notice_id, avionics_unit, hit_event_offender; } LmpValues; extern LmpValues g_lmp; diff --git a/patches/native/local_mp_damage_notifications.jsonc b/patches/native/local_mp_damage_notifications.jsonc index e6a4128..78d6ee9 100644 --- a/patches/native/local_mp_damage_notifications.jsonc +++ b/patches/native/local_mp_damage_notifications.jsonc @@ -1,7 +1,7 @@ -// 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. +// Bind self damage, avionics failure and hit-parts feedback to the player on a LocalMP host. +// The stock damage producers skip hero/attacker checks on dedicated-server paths because a +// dedicated server has no HUD; on a LocalMP host both run in the same process. +// The HUD receives only the type, text index and ID, or the victim's damaged parts list. { "module": "local_mp_damage_notifications", "features": [ @@ -91,8 +91,7 @@ ] }, // 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. + // {type, text, id} into the local HUD event queue. // 2.61.0.37: 0x19b1030; 2.61.0.43: 0x19b6ba0. "hud_damage_deliver": { "variants": [ @@ -105,18 +104,105 @@ } ] }, - // 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": { + // Unit avionics/electronics failure dispatcher (13-entry switch for guidance, sensors, radar, + // RWR, TWS, optics, targeting pod, laser designator, NVG, indicators). Reads its owning Unit* + // from [comp + unit]; most cases emit HUD_MSG_DAMAGE without checking the unit's hero flag. + // 2.61.0.37: 0xfca8b0; 2.61.0.43: 0xfce8e0. + "avionics_damage": { "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", + "pattern": "41 57 41 56 41 55 41 54 56 57 55 53 48 83 ec 58 0f 29 74 24 40 48 89 ce 83 fa 0c 0f 87 ?? ?? ?? ?? 89 d0 48 8d 0d ?? ?? ?? ?? 48 63 04 81 48 01 c8 ff e0 48 8b 46 ??", "captures": { - "cell": { "offset": 10, "type": "rel32" } + "unit": { "offset": 54, "type": "u8" } + } + } + } + ] + }, + // Camera optic-view exit invoked by avionics_damage cases 2, 7 and 8 without checking hero. + // 2.61.0.37: 0xfcab0b -> 0x15cd9c0; 2.61.0.43: 0xfceb3b -> 0x15d2760. + "avionics_exit_optic": { + "variants": [ + { + "written_for": "2.61.0.37", + "locate": { + "method": "within", + "in": "avionics_damage", + "pattern": "48 81 c1 ?? ?? ?? ?? 89 d3 ba 01 00 00 00 41 b0 01 e8 ?? ?? ?? ??", + "captures": { + "call": { "offset": 18, "type": "rel32" } + } + } + } + ] + }, + // Unit::onEvent: receives hit events for every unit on a LocalMP host and falls through into + // enemy parts-list and hit-camera special-event delivery even when the attacker is an AI unit. + // 2.61.0.37: 0x103fbc0; 2.61.0.43: 0x1043fe0. + "unit_on_event": { + "variants": [ + { + "written_for": "2.61.0.37", + "locate": { + "method": "signature", + "pattern": "41 57 41 56 41 55 41 54 56 57 55 53 48 81 ec c8 04 00 00 44 0f 29 bc 24 b0 04 00 00 44 0f 29 b4 24 a0 04 00 00 44 0f 29 ac 24 90 04 00 00 44 0f 29 a4 24 80 04 00 00 44 0f 29 9c 24 70 04 00 00 44 0f 29 94 24 60 04 00 00 44 0f 29 8c 24 50 04 00 00 44 0f 29 84 24 40 04 00 00 0f 29 bc 24 30 04 00 00 0f 29 b4 24 20 04 00 00 48 89 d7 49 89 cd 0f b7 42 20" + } + } + ] + }, + // Hit event attacker Unit* offset loaded before comparing against g_hero in Unit::onEvent. + // 2.61.0.37: 0x104495d (offender=0xd0); 2.61.0.43: 0x1048d7d (offender=0xd0). + "hit_event_offender": { + "variants": [ + { + "written_for": "2.61.0.37", + "locate": { + "method": "within", + "in": "unit_on_event", + "pattern": "48 8b bf ?? ?? ?? ?? 48 8b 35 ?? ?? ?? ?? 48 85 f6 74 ?? 48 8b 8e ?? ?? ?? ??", + "captures": { + "offender": { "offset": 3, "type": "u32" } + } + } + } + ] + }, + // Watched hero cell used when spectating in Unit::onEvent. + // 2.61.0.37: 0x10449ab -> 0x7bad420; 2.61.0.43: 0x1048dcb -> 0x7bb9780. + "hit_watched_hero": { + "variants": [ + { + "written_for": "2.61.0.37", + "locate": { + "method": "within", + "in": "unit_on_event", + "pattern": "40 84 ed 74 ?? 48 8b 15 ?? ?? ?? ?? 48 8b 0d ?? ?? ?? ?? 48 39 d7 75 ??", + "captures": { + "watched_hero": { "offset": 8, "type": "rel32" } + } + } + } + ] + }, + // Unconditional enemy parts-list, hit-camera important events and naval hit-state calls + // reached after the hit-camera attacker check in Unit::onEvent. + // 2.61.0.37: 0x1045fc3 -> 0x18977c0, 0x1897cc0, 0x1896a40; + // 2.61.0.43: 0x104a3d9 -> 0x189d140, 0x189d640, 0x189c3c0. + "hit_parts_feedback": { + "variants": [ + { + "written_for": "2.61.0.37", + "locate": { + "method": "within", + "in": "unit_on_event", + "pattern": "48 8b 0d ?? ?? ?? ?? 4c 8d b4 24 00 01 00 00 4c 89 ea 4d 89 f0 e8 ?? ?? ?? ?? 48 8b 0d ?? ?? ?? ?? 4c 89 ea 4d 89 f0 e8 ?? ?? ?? ?? 66 41 83 bd 8c 00 00 00 05 75 ?? 48 8b 0d ?? ?? ?? ?? 4c 8d 84 24 00 01 00 00 4c 89 ea e8 ?? ?? ?? ??", + "captures": { + "enqueue_enemy_parts": { "offset": 22, "type": "rel32" }, + "hit_cam_important": { "offset": 40, "type": "rel32" }, + "naval_hit_state": { "offset": 74, "type": "rel32" } } } }