fix(battle): release the support unit after control returns

Clearing missionSupportUnitRef as soon as the support unit died kept the
player from getting the vehicle back: the unit update (2.61.0.37
0xfd1450, 0xfd2f01 / 0xfd25a7) recognises the player's dead support
unit by that reference (unit +0x88 == MPlayer +0xb58) and only then calls
setControlledUnit(mp, 0) (0x598a70) to return control. In game the
camera stayed on the wreck until the player respawned.

Remember the player and the dead unit's entity id at the death, and
clear the reference after setControlledUnit has run for that player
(new hook, LMP_SIG_SET_CONTROLLED_UNIT, 2.61.0.37 hit 0x598a70), as
long as it still names that unit.

Checked with the native build and check-offsets; not yet verified in
game.
This commit is contained in:
N0VAN0M4LY committed 2026-11-29 02:53:34 +08:00
1 parent c1a9fe8d26
commit 9c813ccf7d
4 files changed
+91 -10

No files matched your search

+2 -1
View File
@@ -284,7 +284,8 @@ void install_local_mp_hooks(void)
!install_local_mp_kill_hooks(text, text_size, p_ground_dead) ||
!install_local_mp_event_hooks(text, text_size) ||
!install_local_mp_score_reader(text, text_size) ||
!install_local_mp_nuke_hooks(text, text_size)) {
!install_local_mp_nuke_hooks(text, text_size) ||
!install_local_mp_support_hooks(text, text_size)) {
log_line("local_mp: one or more LocalMP subsystem hooks failed to initialize");
return;
}
+3 -1
View File
@@ -66,7 +66,8 @@ void local_mp_players_queue_spawn(int32_t slot_id, int32_t spare_type, int is_si
void local_mp_report_award_event(const LocalMpAwardEvent *ev);
void local_mp_report_nuclear_strike(void);
/* A destroyed unit that is some player's mission support unit frees the reference (local_mp_support.c). */
/* A destroyed unit that is some player's mission support unit: its reference is freed once the
* player has been given back control (local_mp_support.c). */
void local_mp_support_unit_dead(const uint8_t *victim);
const char *local_mp_unit_exp_class(const uint8_t *unit);
@@ -92,3 +93,4 @@ int install_local_mp_kill_hooks(const uint8_t *text, size_t text_size, uint8_t *
int install_local_mp_event_hooks(const uint8_t *text, size_t text_size);
int install_local_mp_score_reader(const uint8_t *text, size_t text_size);
int install_local_mp_nuke_hooks(const uint8_t *text, size_t text_size);
int install_local_mp_support_hooks(const uint8_t *text, size_t text_size);
+10
View File
@@ -295,4 +295,14 @@
#define LMP_SIG_SHELL_LAUNCH "41 57 41 56 41 54 56 57 55 53 48 81 ec 30 02 00 00 44 0f 29 bc 24 20 02 00 00"
#define LMP_SIG_SHELL_EXPLODE "41 57 41 56 41 55 41 54 56 57 55 53 48 81 ec a8 02 00 00 44 0f 29 bc 24 90 02 00 00 44 0f 29 b4 24 80 02 00 00 44 0f 29 ac 24 70 02 00 00 44 0f 29 a4 24 60 02 00 00 44 0f 29 9c 24 50 02 00 00 44 0f 29 94 24 40 02 00 00 44 0f 29 8c 24 30 02 00 00 44 0f 29 84 24 20 02 00 00 0f 29 bc 24 10 02 00 00 0f 29 b4 24 00 02 00 00 41 89 d3"
#define LMP_SIG_CTA_LOGIC_ACTION "41 57 41 56 41 55 41 54 56 57 55 53 48 81 ec a8 02 00 00 44 0f 29 bc 24 90 02 00 00 44 0f 29 b4 24 80 02 00 00 44 0f 29 ac 24 70 02 00 00 44 0f 29 a4 24 60 02 00 00 44 0f 29 9c 24 50 02 00 00 44 0f 29 94 24 40 02 00 00 44 0f 29 8c 24 30 02 00 00 44 0f 29 84 24 20 02 00 00 0f 29 bc 24 10 02 00 00 0f 29 b4 24 00 02 00 00 48 89 cf"
/*
* Mission support unit reference (local_mp_support.c), 2.61.0.37:
* setControlledUnit 0x598a70 (MPlayer *mp, uint32 eid), log "setControlledUnit() called with
* eid:%08x"; the unit update gives a player back the vehicle with eid 0 when the player's
* support unit is gone (0xfd1450, 0xfd25c3).
*
* @anchor "setControlledUnit() called with eid:%08x"
*/
#define LMP_SIG_SET_CONTROLLED_UNIT "56 57 53 48 83 ec 40 89 d7 48 89 ce 89 d0 48 89 44 24 28 c7 44 24 20 03 00 00 00"
#define LMP_SIG_IS_SLOT_READY_AT_HOST "41 56 56 57 55 53 48 81 ec a0 00 00 00 0f 29 b4 24 90 00 00 00 44 89 c3 48 89 d6 48 83 3d"
+76 -8
View File
@@ -5,21 +5,57 @@
* the player has none: MPlayer per-frame update 0x59d270 at 0x59d9c8..0x59da47 (2.61.0.37) checks
* missionSupportUnitRef (MPlayer +0xb50 cached pointer, +0xb58 entity id) == 0, creates the unit
* with async_create_support_unit (0xcb55d0) and stores its entity id there. The action bar button
* launches the unit the reference names; the host then hands it control (setControlledUnit, via
* 0xfd7160) and respawns it (0xe4fdf0, which picks an airfield only for a unit with an owner).
* launches the unit the reference names; the host then hands it control (setControlledUnit
* 0x598a70, via 0xfd7160) and respawns it (0xe4fdf0, which picks an airfield only for a unit with
* an owner). When the support unit is gone, the unit update (0xfd1450 at 0xfd2f01 / 0xfd25a7)
* recognises it by that reference (unit +0x88 == MPlayer +0xb58) and gives the player back the
* vehicle (setControlledUnit with eid 0).
*
* The client never clears the reference while a battle runs (only MPlayer::initMPlayer /
* clearBeforeLobby reset it, 0x595a30 / 0x599680), so after the first support unit was destroyed
* the next launch reused the dead unit: control was refused ("changed controlled unit to already
* dead unit"), the unit respawned in the air where it died and nobody flew it. Online the battle
* server presumably drops the reference (not observed). Here the host does it when the unit dies,
* so the stock update creates a new support unit the next time the threshold is reached.
* server presumably drops the reference (not observed). Here the host does it once the player has
* been given back control after the unit died (clearing it at the death itself kept the player
* from getting the vehicle back), so the stock update creates a new support unit the next time
* the threshold is reached.
*/
#include "hooks.h"
#include "hook_config.h"
#include "local_mp_internal.h"
#include "local_mp_offsets.h"
#include "MinHook.h"
typedef void (__fastcall *SetControlledUnitFn)(uint8_t *mp, uint32_t eid);
static SetControlledUnitFn orig_set_controlled_unit;
/* Players whose support unit died, with that unit's entity id; cleared after control moves on. */
#define LMP_SUPPORT_PENDING_MAX 32
static struct {
uint8_t *mp;
uint32_t eid;
} g_pending[LMP_SUPPORT_PENDING_MAX];
static void remember_dead_support_unit(uint8_t *mp, uint32_t eid)
{
size_t i, free_slot = LMP_SUPPORT_PENDING_MAX;
for (i = 0; i < LMP_SUPPORT_PENDING_MAX; i++) {
if (g_pending[i].mp == mp) {
g_pending[i].eid = eid;
return;
}
if (g_pending[i].mp == NULL && free_slot == LMP_SUPPORT_PENDING_MAX)
free_slot = i;
}
if (free_slot < LMP_SUPPORT_PENDING_MAX) {
g_pending[free_slot].mp = mp;
g_pending[free_slot].eid = eid;
}
}
void local_mp_support_unit_dead(const uint8_t *victim)
{
uint8_t **table = g_local_mp_player_table ? *g_local_mp_player_table : NULL;
@@ -37,9 +73,41 @@ void local_mp_support_unit_dead(const uint8_t *victim)
eid = *(uint32_t *)(mp + LMP_MP_OFF_SUPPORT_UNIT_EID);
if (eid == 0 || (const uint8_t *)g_lmp_get_unit_by_eid(eid) != victim)
continue;
log_line("local_mp: support unit eid %08x of pid %u destroyed, reference cleared",
(unsigned)eid, (unsigned)mp[LMP_MP_OFF_PID]);
*(uint64_t *)(mp + LMP_MP_OFF_SUPPORT_UNIT_PTR) = 0;
*(uint32_t *)(mp + LMP_MP_OFF_SUPPORT_UNIT_EID) = 0;
log_line("local_mp: support unit eid %08x of pid %u destroyed", (unsigned)eid, (unsigned)mp[LMP_MP_OFF_PID]);
remember_dead_support_unit(mp, eid);
}
}
static void __fastcall hook_set_controlled_unit(uint8_t *mp, uint32_t eid)
{
size_t i;
orig_set_controlled_unit(mp, eid);
if (mp == NULL || !local_mp_is_host())
return;
for (i = 0; i < LMP_SUPPORT_PENDING_MAX; i++) {
if (g_pending[i].mp != mp)
continue;
if (*(uint32_t *)(mp + LMP_MP_OFF_SUPPORT_UNIT_EID) == g_pending[i].eid) {
log_line("local_mp: support unit reference of pid %u cleared", (unsigned)mp[LMP_MP_OFF_PID]);
*(uint64_t *)(mp + LMP_MP_OFF_SUPPORT_UNIT_PTR) = 0;
*(uint32_t *)(mp + LMP_MP_OFF_SUPPORT_UNIT_EID) = 0;
}
g_pending[i].mp = NULL;
g_pending[i].eid = 0;
}
}
int install_local_mp_support_hooks(const uint8_t *text, size_t text_size)
{
uint8_t *p_set = local_mp_scan_unique(text, text_size, "setControlledUnit", LMP_SIG_SET_CONTROLLED_UNIT);
if (!p_set)
return 0;
if (MH_CreateHook(p_set, (void *)&hook_set_controlled_unit, (void **)&orig_set_controlled_unit) != MH_OK ||
MH_EnableHook(p_set) != MH_OK) {
log_line("local_mp: failed to install the support unit hook");
return 0;
}
return 1;
}