Files
WT-Offline/native/wto-net/src/local_mp_ground_ai.c
T
N0VAN0M4LY b3f9ff589c fix(ground-ai): short-circuit driveToPos instead of skipping setBehaviorTree
Skipping Unit::setBehaviorTree left unit->behTree null on LocalMP bot tanks, which prevented VehiclePhysActor from applying producedCT to appliedCT (100% dead_echo in sinai_Conq1). Hook driveToPos::update (.37 0x861370, .58 0x863a90) instead and return ER_SUCCESS (1) when the ground AI script is ready, keeping the BehTree initialized while completely suppressing stock tankBot driving. Verified MSVC build (0 warnings) and .37/.58 native module resolution (0 failures).
2026-12-04 01:54:14 +08:00

197 lines
6.9 KiB
C

/*
* Global stock-tankBot replacement gate for LocalMP battles.
*
* When WTOffline's ground AI daScript compiles and loads cleanly with custom movement enabled,
* its [init] hook announces "wtoGroundAiScriptReady". Once announced, stock "tankBot" behavior
* trees are never attached to LocalMP bot tanks and their MoveSystem stays paused for the
* lifetime of the vehicle (no mid-battle fallback or tug-of-war with stock AI).
*
* If the script fails to compile, is missing, or the player chooses "stock" mode, the ready
* signal never arrives and stock tankBot runs untouched.
*/
#include "hook_config.h"
#include "local_mp_internal.h"
#include "local_mp_offsets.h"
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#define SCRIPT_READY_ACTION "wtoGroundAiScriptReady"
#define DRIVE_CONTROL_ACTION "wtoGroundAiDriveControl"
typedef void (__fastcall *MissionActionFn)(const char *name, const void *blk);
typedef uint8_t *(__fastcall *AsGroundModelFn)(uint8_t *unit);
typedef uint8_t (__fastcall *DriveToPosUpdateFn)(void *node, void *agent, float dt);
typedef struct GroundAiDriveControl {
uint8_t *unit;
uint8_t *move_system;
uint32_t eid;
} GroundAiDriveControl;
static MissionActionFn orig_mission_action;
static DriveToPosUpdateFn orig_drive_to_pos_update;
static AsGroundModelFn as_ground_model;
static volatile LONG g_script_owns_movement;
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 = unit ? as_ground_model(unit) : NULL;
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 clear_slot(GroundAiDriveControl *control, const char *reason)
{
log_line("local_mp: ground AI cleared drive slot eid=%#lx (%s)", (unsigned long)control->eid, reason);
memset(control, 0, sizeof(*control));
}
static void clear_by_eid(uint32_t eid, const char *reason)
{
size_t i;
for (i = 0; i < sizeof(controls) / sizeof(controls[0]); i++) {
if (controls[i].unit == NULL)
continue;
if (eid == 0 || controls[i].eid == eid)
clear_slot(&controls[i], reason);
}
}
static void acquire_drive_control(uint32_t eid)
{
uint8_t *unit;
uint8_t *move_system;
uint8_t *owner;
GroundAiDriveControl *control;
if (eid == 0 || !local_mp_is_host() || !InterlockedCompareExchange(&g_script_owns_movement, 0, 0))
return;
unit = (uint8_t *)g_lmp_get_unit_by_eid(eid);
move_system = get_move_system(unit);
owner = move_system ? get_bot_owner(unit, eid) : NULL;
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))
clear_slot(control, "vehicle changed");
move_system[g_lmp.ground_ai_move_inactive] = 1;
if (control->unit == NULL) {
control->unit = unit;
control->move_system = move_system;
control->eid = eid;
log_line("local_mp: ground AI owns drive eid=%#lx", (unsigned long)eid);
}
}
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 uint8_t __fastcall hook_drive_to_pos_update(void *node, void *agent, float dt)
{
/* Keep the BehTree object initialized on the unit so vehicle physics steps normally,
* but short-circuit stock driveToPos to ER_SUCCESS (1) whenever our AI script is active. */
if (local_mp_is_host() && InterlockedCompareExchange(&g_script_owns_movement, 0, 0))
return 1;
return orig_drive_to_pos_update(node, agent, dt);
}
static void __fastcall hook_mission_action(const char *name, const void *blk)
{
uint32_t eid = 0, hold_ms = 0, own = 0;
if (name != NULL && strcmp(name, SCRIPT_READY_ACTION) == 0) {
if (blk != NULL && read_uint(blk, "ownMovement", &own)) {
LONG prev = InterlockedExchange(&g_script_owns_movement, own ? 1 : 0);
if (prev != (own ? 1 : 0)) {
log_line("local_mp: ground AI script ready (ownMovement=%u; stock tankBot %s)",
(unsigned)(own ? 1 : 0), own ? "disabled" : "kept");
}
}
return;
}
if (name == NULL || strcmp(name, DRIVE_CONTROL_ACTION) != 0) {
orig_mission_action(name, blk);
return;
}
if (blk == NULL || !read_uint(blk, "eid", &eid) || !read_uint(blk, "holdMs", &hold_ms))
return;
AcquireSRWLockExclusive(&control_lock);
if (hold_ms == 0)
clear_by_eid(eid, eid == 0 ? "mission reset" : "unit ended");
else
acquire_drive_control(eid);
ReleaseSRWLockExclusive(&control_lock);
}
void local_mp_ground_ai_update(float dt)
{
size_t i;
int hosting;
(void)dt;
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) {
clear_slot(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) {
clear_slot(control, "vehicle changed");
continue;
}
/* Keep stock MoveSystem permanently paused while this bot tank is alive. */
control->move_system[g_lmp.ground_ai_move_inactive] = 1;
}
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 },
{ "drive_to_pos_update", g_lmp.ground_ai_drive_to_pos_update, (void *)&hook_drive_to_pos_update, (void **)&orig_drive_to_pos_update },
};
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]));
}