feat(ground-ai): return bot movement control on failure

Grant custom physics control per LocalMP bot and let the native layer release it when the script stops renewing control. Verified both TypeScript checks, the MSVC build, native resolution and check-patches on .37 and .58, and isolated .58 script loading.
This commit is contained in:
N0VAN0M4LY committed 2026-12-03 14:24:32 +08:00
1 parent bb680c6abd
commit 885e3bbb5d
15 files changed
+325 -63

No files matched your search

+1 -1
View File
@@ -23,7 +23,7 @@ New-Item -ItemType Directory -Force $obj | Out-Null
$sources = @(
"$src\dll_main.c", "$src\hook_config.c", "$src\redirect_rules.c", "$src\resolver_hooks.c", "$src\connection_hooks.c",
"$src\local_mp_core.c", "$src\local_mp_values.c", "$src\local_mp_players.c", "$src\local_mp_loadout.c", "$src\local_mp_actions.c", "$src\local_mp_awards.c", "$src\local_mp_kills.c", "$src\local_mp_events.c", "$src\local_mp_scores.c", "$src\local_mp_nuke.c", "$src\local_mp_support.c",
"$src\local_mp_damage_notifications.c", "$src\local_mp_air_events.c",
"$src\local_mp_damage_notifications.c", "$src\local_mp_air_events.c", "$src\local_mp_ground_ai.c",
"$mh\src\hook.c", "$mh\src\buffer.c", "$mh\src\trampoline.c", "$mh\src\hde\hde64.c"
) | ForEach-Object { '"' + (Resolve-Path $_) + '"' }
+1
View File
@@ -143,6 +143,7 @@ static const LocalMpModule MODULES[] = {
{ "local_mp_support", install_local_mp_support_hooks },
{ "local_mp_damage_notifications", install_local_mp_damage_notification_hooks },
{ "local_mp_air_events", install_local_mp_air_event_hooks },
{ "local_mp_ground_ai", install_local_mp_ground_ai_hooks },
};
static void use_core_values(void)
+166
View File
@@ -0,0 +1,166 @@
/*
* Temporarily pause stock tank movement for a LocalMP bot receiving custom physics inputs.
* Only the native side changes the MoveSystem flag, so it can give control back if the script
* stops renewing it. All other mission actions still go through the original daScript binding.
*/
#include "hook_config.h"
#include "local_mp_internal.h"
#include "local_mp_offsets.h"
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#define DRIVE_CONTROL_ACTION "wtoGroundAiDriveControl"
typedef void (__fastcall *MissionActionFn)(const char *name, const void *blk);
typedef uint8_t *(__fastcall *AsGroundModelFn)(uint8_t *unit);
typedef struct GroundAiDriveControl {
uint8_t *unit;
uint8_t *move_system;
uint32_t eid;
float remaining_seconds;
} GroundAiDriveControl;
static MissionActionFn orig_mission_action;
static AsGroundModelFn as_ground_model;
/* Player IDs are one byte in the engine. */
static GroundAiDriveControl controls[UINT8_MAX + 1];
static SRWLOCK control_lock = SRWLOCK_INIT;
static uint8_t *get_move_system(uint8_t *unit)
{
uint8_t *ground_model = as_ground_model(unit);
return ground_model ? *(uint8_t **)(ground_model + g_lmp.ground_ai_move_system) : NULL;
}
static uint8_t *get_bot_owner(uint8_t *unit, uint32_t eid)
{
uint8_t *owner = *(uint8_t **)(unit + LMP_UNIT_OFF_MPLAYER);
if (owner == NULL || local_mp_is_local_mplayer(owner) ||
owner != local_mp_find_mplayer(-1, owner[LMP_MP_OFF_PID]) ||
*(int64_t *)(owner + LMP_MP_OFF_USER_ID) >= -1 ||
*(uint32_t *)(owner + LMP_MP_OFF_UNIT_EID) != eid)
return NULL;
return owner;
}
static void give_back_drive(GroundAiDriveControl *control, const char *reason)
{
uint8_t *unit = (uint8_t *)g_lmp_get_unit_by_eid(control->eid);
/* The old unit or its movement component may already have been destroyed. */
if (unit == control->unit && get_move_system(unit) == control->move_system)
control->move_system[g_lmp.ground_ai_move_inactive] = 0;
log_line("local_mp: ground AI returned drive eid=%#lx (%s)", (unsigned long)control->eid, reason);
memset(control, 0, sizeof(*control));
}
static void update_drive_control(uint32_t eid, uint32_t hold_ms)
{
uint8_t *unit = (uint8_t *)g_lmp_get_unit_by_eid(eid);
uint8_t *move_system = get_move_system(unit);
uint8_t *owner = move_system ? get_bot_owner(unit, eid) : NULL;
GroundAiDriveControl *control;
uint8_t *inactive;
if (owner == NULL)
return;
control = &controls[owner[LMP_MP_OFF_PID]];
if (control->unit != NULL &&
(control->eid != eid || control->unit != unit || control->move_system != move_system))
give_back_drive(control, "vehicle changed");
if (hold_ms == 0) {
if (control->unit != NULL)
give_back_drive(control, "script released");
return;
}
inactive = move_system + g_lmp.ground_ai_move_inactive;
if (control->unit == NULL) {
if (*inactive)
return; /* Leave mission-disabled vehicles under their existing owner. */
control->unit = unit;
control->move_system = move_system;
control->eid = eid;
*inactive = 1;
log_line("local_mp: ground AI acquired drive eid=%#lx", (unsigned long)eid);
}
control->remaining_seconds = (float)hold_ms / 1000.0f;
}
static int read_uint(const void *blk, const char *key, uint32_t *out)
{
const char *value = g_lmp_datablock_get_str(blk, key, "");
char *end;
unsigned long parsed;
if (value == NULL || *value < '0' || *value > '9')
return 0;
errno = 0;
parsed = strtoul(value, &end, 0);
if (errno != 0 || *end != 0)
return 0;
*out = (uint32_t)parsed;
return 1;
}
static void __fastcall hook_mission_action(const char *name, const void *blk)
{
uint32_t eid, hold_ms;
if (name == NULL || strcmp(name, DRIVE_CONTROL_ACTION) != 0) {
orig_mission_action(name, blk);
return;
}
if (blk == NULL || !local_mp_is_host() ||
!read_uint(blk, "eid", &eid) || eid == 0 || !read_uint(blk, "holdMs", &hold_ms))
return;
AcquireSRWLockExclusive(&control_lock);
update_drive_control(eid, hold_ms);
ReleaseSRWLockExclusive(&control_lock);
}
void local_mp_ground_ai_update(float dt)
{
size_t i;
int hosting;
if (orig_mission_action == NULL)
return;
hosting = local_mp_is_host();
AcquireSRWLockExclusive(&control_lock);
for (i = 0; i < sizeof(controls) / sizeof(controls[0]); i++) {
GroundAiDriveControl *control = &controls[i];
uint8_t *unit, *owner;
if (control->unit == NULL)
continue;
if (!hosting) {
give_back_drive(control, "session ended");
continue;
}
unit = (uint8_t *)g_lmp_get_unit_by_eid(control->eid);
owner = unit == control->unit ? get_bot_owner(unit, control->eid) : NULL;
if (owner == NULL || owner[LMP_MP_OFF_PID] != i || get_move_system(unit) != control->move_system) {
give_back_drive(control, "vehicle changed");
continue;
}
if (dt > 0.0f)
control->remaining_seconds -= dt;
if (control->remaining_seconds <= 0.0f)
give_back_drive(control, "script stopped renewing control");
}
ReleaseSRWLockExclusive(&control_lock);
}
int install_local_mp_ground_ai_hooks(void)
{
LocalMpHookSpec hooks[] = {
{ "das_call_mission_action", g_lmp.ground_ai_mission_action, (void *)&hook_mission_action, (void **)&orig_mission_action },
};
as_ground_model = (AsGroundModelFn)g_lmp.ground_ai_as_ground_model;
return local_mp_queue_hooks("local_mp_ground_ai", hooks, sizeof(hooks) / sizeof(hooks[0]));
}
+2
View File
@@ -106,3 +106,5 @@ int install_local_mp_nuke_hooks(void);
int install_local_mp_support_hooks(void);
int install_local_mp_damage_notification_hooks(void);
int install_local_mp_air_event_hooks(void);
int install_local_mp_ground_ai_hooks(void);
void local_mp_ground_ai_update(float dt);
+1
View File
@@ -166,6 +166,7 @@ static void __fastcall hook_gui_state_update(uint8_t *gui_state_obj, float dt)
uint8_t *mplayer;
orig_gui_state_update(gui_state_obj, dt);
local_mp_ground_ai_update(dt);
if (!local_mp_is_host()) {
g_accept_pending = 0;
+6
View File
@@ -41,6 +41,7 @@ typedef struct LmpValueSpec {
#define SUPPORT "local_mp_support"
#define DAMAGE_NOTICES "local_mp_damage_notifications"
#define AIR_EVENTS "local_mp_air_events"
#define GROUND_AI "local_mp_ground_ai"
static const LmpValueSpec SPECS[] = {
A(CORE, "do_respawn_player", do_respawn_player), O(CORE, "do_respawn_player.app_score_rules", app_score_rules),
@@ -186,6 +187,11 @@ static const LmpValueSpec SPECS[] = {
A(AIR_EVENTS, "air_event_orientation.call", air_event_orientation),
A(AIR_EVENTS, "send_unit_respawn", send_unit_respawn),
A(AIR_EVENTS, "send_unit_respawn_broadcast.mpi_sendto", mpi_sendto),
A(GROUND_AI, "ground_ai_mission_binding.action", ground_ai_mission_action),
A(GROUND_AI, "ground_ai_as_ground_model", ground_ai_as_ground_model),
O(GROUND_AI, "ground_ai_move_properties.move_system", ground_ai_move_system),
O(GROUND_AI, "ground_ai_move_annotation.inactive", ground_ai_move_inactive),
};
static void store(const LmpValueSpec *spec, int64_t value, uint8_t *base)
+3
View File
@@ -87,6 +87,9 @@ typedef struct LmpValues {
uint8_t *air_event_orientation, *send_unit_respawn, *mpi_sendto;
size_t mis_mode_name, gp_ks_rules, gp_ks_rule_count, ks_rule_tag, ks_rule_air_ai_mult, ks_rule_ground_ai_mult;
size_t ks_rule_spawn_pos, ks_rule_spawn_pos_count, ks_spawn_pos_stride, mp_ks_progress, app_terrain;
/* local_mp_ground_ai */
uint8_t *ground_ai_mission_action, *ground_ai_as_ground_model;
size_t ground_ai_move_system, ground_ai_move_inactive;
} LmpValues;
extern LmpValues g_lmp;