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.
309 lines
13 KiB
C
309 lines
13 KiB
C
/*
|
|
* 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"
|
|
|
|
typedef uint8_t (__fastcall *BarrelDamageFn)(void *damage, const uint8_t *impact);
|
|
typedef void (__fastcall *FireUpdateFn)(
|
|
const uint8_t *weapon, void *arg2, void *arg3, void *arg4, void *arg5, void *arg6,
|
|
float arg7, uint8_t arg8, float arg9, void *arg10, uint8_t arg11, float arg12);
|
|
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 void (__fastcall *LocalMpiDispatchFn)(void *proxy, const uint8_t *msg, uint32_t recipient_pid);
|
|
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 };
|
|
|
|
/* Diagnostics are bounded so a long battle cannot flood the run log. */
|
|
enum { SUPPRESSED_LOG_LIMIT = 12 };
|
|
|
|
typedef struct DamageNotice {
|
|
const uint8_t *context;
|
|
size_t unit_offset;
|
|
uintptr_t unit_tag;
|
|
int32_t id;
|
|
} 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 void *active_hit_victim;
|
|
|
|
static BarrelDamageFn original_barrel_damage;
|
|
static FireUpdateFn original_fire_update;
|
|
static DeliverHudFn original_deliver_hud;
|
|
static DeliverDamageHudFn original_damage_deliver_hud;
|
|
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 LocalMpiDispatchFn original_local_mpi_dispatch;
|
|
|
|
static int suppressed_avionics_logged;
|
|
static int suppressed_optic_logged;
|
|
static int suppressed_parts_logged;
|
|
static int suppressed_enemy_mpi_logged;
|
|
static int damage_hooks_queued;
|
|
|
|
static const uint8_t *local_mplayer(void)
|
|
{
|
|
return g_local_mp_mplayer != NULL ? *g_local_mp_mplayer : 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 = 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)
|
|
{
|
|
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);
|
|
}
|
|
|
|
static const uint8_t *owning_mplayer(const uint8_t *unit)
|
|
{
|
|
return unit != NULL ? *(const uint8_t *const *)(unit + LMP_UNIT_OFF_MPLAYER) : 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. */
|
|
int local_mp_is_view_hit(const uint8_t *attacker, const uint8_t *victim)
|
|
{
|
|
const uint8_t *mplayer = local_mplayer();
|
|
if (attacker == NULL || attacker == victim || mplayer == NULL)
|
|
return 0;
|
|
|
|
const uint8_t *owner = owning_mplayer(attacker);
|
|
if (owner != NULL && owner == owning_mplayer(victim))
|
|
return 0;
|
|
return owner == mplayer || attacker == controlled_unit(mplayer)
|
|
|| attacker == watched_unit(mplayer);
|
|
}
|
|
|
|
int local_mp_has_enemy_hit_feedback(void)
|
|
{
|
|
return active_hit_victim != NULL;
|
|
}
|
|
|
|
int local_mp_damage_notifications_ready(void)
|
|
{
|
|
return damage_hooks_queued;
|
|
}
|
|
|
|
static uint8_t __fastcall hook_barrel_damage(void *damage, const uint8_t *impact)
|
|
{
|
|
/* The stock handler resolves the reference before it emits the notice and tags it in bit 0. */
|
|
const DamageNotice notice = { impact, g_lmp.barrel_unit_ref, 1, (int32_t)g_lmp.barrel_notice_id };
|
|
const DamageNotice *previous = active_notice;
|
|
uint8_t result;
|
|
|
|
if (local_mp_is_host())
|
|
active_notice = ¬ice;
|
|
result = original_barrel_damage(damage, impact);
|
|
active_notice = previous;
|
|
return result;
|
|
}
|
|
|
|
static void __fastcall hook_fire_update(
|
|
const uint8_t *weapon, void *arg2, void *arg3, void *arg4, void *arg5, void *arg6,
|
|
float arg7, uint8_t arg8, float arg9, void *arg10, uint8_t arg11, float arg12)
|
|
{
|
|
const DamageNotice notice = { weapon, g_lmp.breech_unit, 0, (int32_t)g_lmp.breech_notice_id };
|
|
const DamageNotice *previous = active_notice;
|
|
|
|
if (local_mp_is_host())
|
|
active_notice = ¬ice;
|
|
original_fire_update(weapon, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12);
|
|
active_notice = previous;
|
|
}
|
|
|
|
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)
|
|
{
|
|
const DamageNotice *notice = active_notice;
|
|
|
|
if (notice != NULL && type == HUD_MSG_EVENT && id == notice->id && local_mp_is_host()
|
|
&& !is_controlled_damage(notice))
|
|
return;
|
|
original_deliver_hud(type, id, arg3, arg4, arg5, arg6, arg7, arg8);
|
|
}
|
|
|
|
/*
|
|
* 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)
|
|
{
|
|
const uint8_t *unit = active_avionics_unit;
|
|
|
|
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 void *previous = active_hit_victim;
|
|
uint64_t result;
|
|
|
|
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);
|
|
const uint8_t *victim = unit;
|
|
if (local_mp_is_view_hit(attacker, victim) &&
|
|
(victim[LMP_UNIT_OFF_ARMY] == 0 || victim[LMP_UNIT_OFF_ARMY] != attacker[LMP_UNIT_OFF_ARMY]))
|
|
active_hit_victim = unit;
|
|
}
|
|
result = original_unit_on_event(unit, event, arg3, arg4);
|
|
active_hit_victim = previous;
|
|
return result;
|
|
}
|
|
|
|
static int should_drop_hit_parts_feedback(const uint8_t *victim)
|
|
{
|
|
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 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(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(victim))
|
|
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(victim))
|
|
return;
|
|
original_naval_hit_state(hud, victim, info);
|
|
}
|
|
|
|
/*
|
|
* AcesApp's local MPI proxy ignores recipient_pid and delivers every local message to
|
|
* AcesApp::onEvent, leaking AI-vs-AI aircraft part damage (0xf104 / 0xf118) into the local
|
|
* player's bottom-right kill feed.
|
|
*/
|
|
static void __fastcall hook_local_mpi_dispatch(void *proxy, const uint8_t *msg, uint32_t recipient_pid)
|
|
{
|
|
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;
|
|
const uint8_t *recipient_mp = local_mp_find_mplayer(-1, pid);
|
|
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",
|
|
(unsigned)msg_id, (unsigned)pid);
|
|
}
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
original_local_mpi_dispatch(proxy, msg, recipient_pid);
|
|
}
|
|
|
|
int install_local_mp_damage_notification_hooks(void)
|
|
{
|
|
const LocalMpHookSpec hooks[] = {
|
|
{ "barrel_notice_deliver", g_lmp.barrel_notice_deliver, (void *)&hook_deliver_hud, (void **)&original_deliver_hud },
|
|
{ "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 },
|
|
{ "local_mpi_dispatch", g_lmp.local_mpi_dispatch, (void *)&hook_local_mpi_dispatch, (void **)&original_local_mpi_dispatch },
|
|
};
|
|
|
|
if (g_lmp.barrel_notice_deliver != g_lmp.breech_notice_deliver) {
|
|
log_line("local_mp: local_mp_damage_notifications skipped: barrel and breech deliver addresses differ");
|
|
return 0;
|
|
}
|
|
damage_hooks_queued = local_mp_queue_hooks("local_mp_damage_notifications", hooks, sizeof(hooks) / sizeof(hooks[0]));
|
|
return damage_hooks_queued;
|
|
}
|