diff --git a/native/wto-net/src/local_mp_damage_notifications.c b/native/wto-net/src/local_mp_damage_notifications.c index 1f3dab6..cab1823 100644 --- a/native/wto-net/src/local_mp_damage_notifications.c +++ b/native/wto-net/src/local_mp_damage_notifications.c @@ -38,7 +38,7 @@ typedef struct DamageNotice { /* 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 __declspec(thread) const void *active_hit_victim; static BarrelDamageFn original_barrel_damage; static FireUpdateFn original_fire_update; @@ -57,25 +57,22 @@ static int suppressed_optic_logged; static int suppressed_parts_logged; static int suppressed_enemy_mpi_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) +static const uint8_t *local_mplayer(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; + return g_local_mp_mplayer != NULL ? *g_local_mp_mplayer : NULL; +} - if (unit != NULL) - return unit; - return g_local_mp_hero != NULL ? *g_local_mp_hero : NULL; +static const uint8_t *controlled_unit(const uint8_t *mplayer) +{ + return mplayer != NULL ? ((ControlledUnitFn)g_lmp.mplayer_controlled_unit)(mplayer) : 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; + const uint8_t *controlled = controlled_unit(local_mplayer()); + if (controlled == NULL && g_local_mp_hero != NULL) + controlled = *g_local_mp_hero; + return unit != NULL && unit == controlled; } static int is_controlled_damage(const DamageNotice *notice) @@ -88,25 +85,29 @@ static int is_controlled_damage(const DamageNotice *notice) 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) +static const uint8_t *owning_mplayer(const uint8_t *unit) { - const uint8_t *owner_mp; - const uint8_t *watched; + return unit != NULL ? *(const uint8_t *const *)(unit + LMP_UNIT_OFF_MPLAYER) : NULL; +} - if (offender == NULL) +static const uint8_t *watched_unit(const uint8_t *mplayer) +{ + if (mplayer == NULL || mplayer[LMP_MP_OFF_GUI_STATE] == LMP_GUI_STATE_PILOT_CONTROL + || !*g_lmp.respawn_spectator_active) + return NULL; + return *(const uint8_t *const *)g_lmp.watched_hero; +} + +/* The owner check also covers projectiles fired before switching to a support vehicle. */ +static int is_local_enemy_hit(const uint8_t *attacker, const uint8_t *victim) +{ + const uint8_t *mplayer = local_mplayer(); + if (attacker == NULL || attacker == victim || mplayer == 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; + if (victim[LMP_UNIT_OFF_ARMY] != 0 && victim[LMP_UNIT_OFF_ARMY] == attacker[LMP_UNIT_OFF_ARMY]) + return 0; + return owning_mplayer(attacker) == mplayer || attacker == controlled_unit(mplayer) + || attacker == watched_unit(mplayer); } static uint8_t __fastcall hook_barrel_damage(void *damage, const uint8_t *impact) @@ -194,66 +195,49 @@ static uint64_t __fastcall hook_exit_optic_view(void *camera, int32_t arg2, uint 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; + const void *previous = active_hit_victim; uint64_t result; - if (event != NULL && local_mp_is_host()) - active_hit_event = event; + active_hit_victim = NULL; + if (unit != NULL && event != NULL && local_mp_is_host() + && *(const uint16_t *)(event + g_lmp.mpi_msg_id_off) == LMP_MPI_MSG_HIT_FEEDBACK) { + const uint8_t *attacker = *(const uint8_t *const *)(event + g_lmp.hit_event_offender); + if (is_local_enemy_hit(attacker, unit)) + active_hit_victim = unit; + } result = original_unit_on_event(unit, event, arg3, arg4); - active_hit_event = previous; + active_hit_victim = previous; return result; } -static int should_drop_hit_parts_feedback(void) +static int should_drop_hit_parts_feedback(const uint8_t *victim) { - 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)) + if (!local_mp_is_host() || (victim != NULL && victim == active_hit_victim)) 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"); + log_line("local_mp: dropped enemy parts feedback without a matching local enemy hit"); } return 1; } static void __fastcall hook_enqueue_enemy_parts(void *hud, const uint8_t *victim, const void *info) { - if (should_drop_hit_parts_feedback()) + if (should_drop_hit_parts_feedback(victim)) 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()) + if (should_drop_hit_parts_feedback(victim)) return; original_hit_cam_important(hud, victim, info); } -static int is_local_or_watched_mplayer(const uint8_t *mp) -{ - const uint8_t *watched; - const uint8_t *watched_mp; - - if (mp == NULL) - return 0; - if (local_mp_is_local_mplayer(mp)) - return 1; - watched = g_lmp.watched_hero != NULL ? *(const uint8_t *const *)g_lmp.watched_hero : NULL; - if (watched == NULL) - return 0; - watched_mp = *(const uint8_t *const *)(watched + LMP_UNIT_OFF_MPLAYER); - return watched_mp != NULL && mp == watched_mp; -} - static void __fastcall hook_naval_hit_state(void *hud, const uint8_t *victim, const void *info) { - if (should_drop_hit_parts_feedback()) + if (should_drop_hit_parts_feedback(victim)) return; original_naval_hit_state(hud, victim, info); } @@ -268,9 +252,10 @@ static void __fastcall hook_local_mpi_dispatch(void *proxy, const uint8_t *msg, if (msg != NULL && local_mp_is_host()) { uint16_t msg_id = *(const uint16_t *)(msg + g_lmp.mpi_msg_id_off); if (msg_id == (uint16_t)g_lmp.enemy_part_dmg_mpi_id || msg_id == (uint16_t)g_lmp.enemy_part_hit_mpi_id) { - uint8_t pid = (uint8_t)(recipient_pid & 0xffu); + uint8_t pid = (uint8_t)recipient_pid; const uint8_t *recipient_mp = local_mp_find_mplayer(-1, pid); - if (!is_local_or_watched_mplayer(recipient_mp)) { + const uint8_t *mplayer = local_mplayer(); + if (recipient_mp == NULL || (recipient_mp != mplayer && recipient_mp != owning_mplayer(watched_unit(mplayer)))) { if (suppressed_enemy_mpi_logged < SUPPRESSED_LOG_LIMIT) { ++suppressed_enemy_mpi_logged; log_line("local_mp: dropped enemy part damage MPI 0x%04x for non-local pid %u", diff --git a/native/wto-net/src/local_mp_events.c b/native/wto-net/src/local_mp_events.c index dc793ec..d48d898 100644 --- a/native/wto-net/src/local_mp_events.c +++ b/native/wto-net/src/local_mp_events.c @@ -1,6 +1,6 @@ /* * LocalMP non-fatal combat event dispatchers: deduplicated assists across air/ground/naval, - * zone captures, hits/criticals, severe damage, and naval damage awards. + * zone captures, hit results, severe damage, and naval damage awards. * * `IGroundModel::onBecameDead` calls both `IGroundModel::awardAssists` (`0x465540`) and * `Unit::awardAssists` (`0xe5b200` -> `dispatchAssist` `0x35beb0`); deduplicating `(victim, mp)` @@ -28,14 +28,15 @@ typedef struct 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_repeat_flush, uint8_t is_crit, uint8_t extra); +typedef void (__fastcall *DispatchHitFn)( + int64_t uid, uint8_t *victim, uint8_t *offender, float dmg, uint8_t repeat_flush, uint8_t effective_hit, 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 DispatchHitFn orig_dispatch_hit; static DispatchSevereDamageFn orig_dispatch_severe_damage; static DispatchNavalDamageFn orig_dispatch_naval_damage; @@ -153,14 +154,12 @@ static void __fastcall hook_dispatch_capture_zone(int64_t uid, uint8_t *capturer } /* - * The 5th parameter is `r8b & (fabsf(offender->lastHitAwardTime) >= FLT_EPSILON)` from - * Unit::flushOffenderHitAward (2.61.0.58 0xffcf70), not a kill flag. Ground, naval and helicopter - * hits flush via 0xffcd20 with r8b=0, whereas fixed-wing aircraft skip 0xffcd20 (0x13d7cd8) and - * only flush via 0xffdf10 with r8b=1; gating on it dropped every hit/crit after the first flush - * on an aircraft. The native single-player reward path (0xffd345) ignores it as well. + * Unit::flushOffenderHitAward passes a repeat marker and an effective-hit flag. Its stock + * single-player path (.58 0xffd369) selects HIT / INEFFECTIVE_HIT from that same flag. The + * repeat marker is forwarded for the native listeners, including fixed-wing periodic flushes. */ -static void __fastcall hook_dispatch_hit_crit( - int64_t uid, uint8_t *victim, uint8_t *offender, float dmg, uint8_t is_repeat_flush, uint8_t is_crit, uint8_t extra +static void __fastcall hook_dispatch_hit( + int64_t uid, uint8_t *victim, uint8_t *offender, float dmg, uint8_t repeat_flush, uint8_t effective_hit, uint8_t extra ) { if (local_mp_is_host() && victim != NULL) { @@ -168,21 +167,20 @@ static void __fastcall hook_dispatch_hit_crit( int same_team = offender != NULL && victim_army != 0 && *(int32_t *)(offender + LMP_OFFENDER_OFF_ARMY) == (int32_t)victim_army; uint8_t *mp = !same_team ? local_mp_find_mplayer(uid, offender ? offender[LMP_OFFENDER_OFF_PID] : 0xffu) : NULL; - 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); + const uint8_t *victim_mp = *(const uint8_t *const *)(victim + LMP_UNIT_OFF_MPLAYER); + if (mp != NULL && mp != victim_mp) { + if (effective_hit) { + local_mp_add_mplayer_score(mp, local_mp_event_score("hit", victim, victim_mp != NULL), LMP_EXP_EVENT_HIT); + 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 - }; + uint32_t event = effective_hit ? LMP_EXP_EVENT_HIT : LMP_EXP_EVENT_INEFFECTIVE_HIT; + LocalMpAwardEvent award = { event, victim, victim_mp != NULL, 0, NULL, 0 }; local_mp_report_award_event(&award); } } } - orig_dispatch_hit_crit(uid, victim, offender, dmg, is_repeat_flush, is_crit, extra); + orig_dispatch_hit(uid, victim, offender, dmg, repeat_flush, effective_hit, extra); } static void __fastcall hook_dispatch_severe_damage(int64_t uid, uint8_t *victim, uint8_t *offender) @@ -240,7 +238,7 @@ int install_local_mp_event_hooks(void) { "iground_award_assists", g_lmp.iground_award_assists, (void *)&hook_iground_award_assists, (void **)&orig_iground_award_assists }, { "dispatch_assist", g_lmp.dispatch_assist, (void *)&hook_dispatch_assist, (void **)&orig_dispatch_assist }, { "dispatch_capture_zone", g_lmp.dispatch_capture_zone, (void *)&hook_dispatch_capture_zone, (void **)&orig_dispatch_capture_zone }, - { "dispatch_hit_crit", g_lmp.dispatch_hit_crit, (void *)&hook_dispatch_hit_crit, (void **)&orig_dispatch_hit_crit }, + { "dispatch_hit", g_lmp.dispatch_hit, (void *)&hook_dispatch_hit, (void **)&orig_dispatch_hit }, { "dispatch_severe_damage", g_lmp.dispatch_severe_damage, (void *)&hook_dispatch_severe_damage, (void **)&orig_dispatch_severe_damage }, { "dispatch_naval_damage", g_lmp.dispatch_naval_damage, (void *)&hook_dispatch_naval_damage, (void **)&orig_dispatch_naval_damage }, }; diff --git a/native/wto-net/src/local_mp_offsets.h b/native/wto-net/src/local_mp_offsets.h index 005c460..c2d97b4 100644 --- a/native/wto-net/src/local_mp_offsets.h +++ b/native/wto-net/src/local_mp_offsets.h @@ -23,8 +23,8 @@ #define LMP_EXP_EVENT_KILL 5u #define LMP_EXP_EVENT_KILL_GROUND 6u #define LMP_EXP_EVENT_ASSIST 9u -#define LMP_EXP_EVENT_CRITICAL_HIT 10u #define LMP_EXP_EVENT_HIT 13u +#define LMP_EXP_EVENT_INEFFECTIVE_HIT 20u #define LMP_EXP_EVENT_UNIT_DAMAGE 24u #define LMP_EXP_EVENT_SEVERE_DAMAGE 35u #define LMP_EXP_EVENT_KILL_HUMAN 39u @@ -181,6 +181,8 @@ #define LMP_MPI_MSG_OFF_OBJ 0x08u #define LMP_MPI_MSG_OFF_ID 0x20u #define LMP_MPI_MSG_UNIT_RESPAWN 0xf030u +/* Unit::onEvent switch case for the decoded projectile feedback sent to the HUD. */ +#define LMP_MPI_MSG_HIT_FEEDBACK 0xf0c2u #define LMP_NAMEMAP_OFF_ENTRIES 0x00u #define LMP_NAMEMAP_OFF_COUNT 0x08u #define LMP_NAMEMAP_OFF_TAIL_PAGE 0x10u diff --git a/native/wto-net/src/local_mp_values.c b/native/wto-net/src/local_mp_values.c index c065def..1bb0fd7 100644 --- a/native/wto-net/src/local_mp_values.c +++ b/native/wto-net/src/local_mp_values.c @@ -131,7 +131,7 @@ static const LmpValueSpec SPECS[] = { O(KILLS, "stat_ship_severe.field", mp_ship_severe_dmg), A(EVENTS, "dispatch_assist", dispatch_assist), A(EVENTS, "caller_capture_zone.call", dispatch_capture_zone), - A(EVENTS, "caller_hit_crit.call", dispatch_hit_crit), A(EVENTS, "caller_severe_damage.call", dispatch_severe_damage), + A(EVENTS, "caller_hit_crit.call", dispatch_hit), A(EVENTS, "caller_severe_damage.call", dispatch_severe_damage), A(EVENTS, "caller_naval_damage.call", dispatch_naval_damage), O(EVENTS, "offender_assist.assist_sent", offender_assist_sent), A(NUKE, "shell_launch", shell_launch), A(NUKE, "shell_explode", shell_explode), A(NUKE, "cta_logic_action", cta_logic_action), @@ -155,6 +155,7 @@ static const LmpValueSpec SPECS[] = { A(DAMAGE_NOTICES, "avionics_exit_optic.call", avionics_exit_optic), O(DAMAGE_NOTICES, "hit_event_offender.offender", hit_event_offender), A(DAMAGE_NOTICES, "hit_watched_hero.watched_hero", watched_hero), + A(DAMAGE_NOTICES, "respawn_spectator.active", respawn_spectator_active), 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), diff --git a/native/wto-net/src/local_mp_values.h b/native/wto-net/src/local_mp_values.h index c620cd4..a97bebf 100644 --- a/native/wto-net/src/local_mp_values.h +++ b/native/wto-net/src/local_mp_values.h @@ -61,7 +61,7 @@ typedef struct LmpValues { size_t mp_award_damage, mp_air_severe_dmg, mp_ship_severe_dmg; /* local_mp_events */ - uint8_t *dispatch_assist, *dispatch_capture_zone, *dispatch_hit_crit, *dispatch_severe_damage, *dispatch_naval_damage; + uint8_t *dispatch_assist, *dispatch_capture_zone, *dispatch_hit, *dispatch_severe_damage, *dispatch_naval_damage; size_t offender_assist_sent; /* local_mp_nuke */ @@ -77,7 +77,8 @@ 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, *avionics_damage, *avionics_exit_optic; - uint8_t *watched_hero, *enqueue_enemy_parts, *hit_cam_important, *naval_hit_state, *local_mpi_dispatch; + uint8_t *watched_hero, *respawn_spectator_active; + uint8_t *enqueue_enemy_parts, *hit_cam_important, *naval_hit_state, *local_mpi_dispatch; size_t barrel_unit_ref, breech_unit, barrel_notice_id, breech_notice_id, avionics_unit, hit_event_offender; size_t mpi_msg_id_off, enemy_part_dmg_mpi_id, enemy_part_hit_mpi_id; diff --git a/patches/gui/battle/record-local-battle-awards/replace.nut b/patches/gui/battle/record-local-battle-awards/replace.nut index 884a29e..6b8e942 100644 --- a/patches/gui/battle/record-local-battle-awards/replace.nut +++ b/patches/gui/battle/record-local-battle-awards/replace.nut @@ -49,6 +49,8 @@ function wtoAppendBattleLog(key, entry) { function wtoBannerReward(awards, unit, ev) { let code = ev?.messageCode ?? -1 + if (code == EXP_EVENT_INEFFECTIVE_HIT) + return { wp = 0, exp = 0 } let isCapture = code == EXP_EVENT_CAPTURE_ZONE let expClass = ev?.expClass ?? "" let group = isCapture ? "own" : (awards?.groups?[expClass] ?? "ground") diff --git a/patches/native/local_mp_damage_notifications.jsonc b/patches/native/local_mp_damage_notifications.jsonc index 191324a..d21b715 100644 --- a/patches/native/local_mp_damage_notifications.jsonc +++ b/patches/native/local_mp_damage_notifications.jsonc @@ -8,7 +8,9 @@ "K-Player damage notices" ], "requires": [ - "local_mp_core" + "local_mp_core", + "local_mp_players", + "local_mp_kills" ], "sites": { // Damage handler prologue reads the impact's tagged unit reference. @@ -174,6 +176,22 @@ } ] }, + // guiSpectator.onSpectatorMode implementation writes the active respawn spectator byte. + // 2.61.0.37: 0x1a46070 -> 0x7bb9e16; 2.61.0.58: 0x1a4e830 -> 0x7bcb2a6. + "respawn_spectator": { + "variants": [ + { + "written_for": "2.61.0.37", + "locate": { + "method": "signature", + "pattern": "89 ca 84 c9 74 ?? 48 8b 05 ?? ?? ?? ?? c6 80 ?? ?? ?? ?? 01 88 15 ?? ?? ?? ?? 48 8b 0d ?? ?? ?? ?? 48 85 c9 74 ??", + "captures": { + "active": { "offset": 22, "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; diff --git a/patches/native/local_mp_events.jsonc b/patches/native/local_mp_events.jsonc index ad104da..62a4a42 100644 --- a/patches/native/local_mp_events.jsonc +++ b/patches/native/local_mp_events.jsonc @@ -1,5 +1,5 @@ -// Combat events on the LocalMP host (native/wto-net/src/local_mp_events.c): assists, captures, hits, -// critical and severe damage, counted and reported as award facts. +// Combat events on the LocalMP host (native/wto-net/src/local_mp_events.c): assists, captures, +// effective and ineffective hits, and severe damage, reported as award facts. // Without it: assists, captures and damage events score nothing in LocalMP battles. { "module": "local_mp_events", @@ -45,7 +45,7 @@ } ] }, - // A caller of the hit / critical hit dispatch. + // Flushes effective / ineffective hits. The site key also identifies this capture in version records. // 2.61.0.37: 0xff85ea "caller_hit_crit": { "variants": [