diff --git a/game-ai/ground/wto_ai_drive_common.das b/game-ai/ground/wto_ai_drive_common.das index f1c506c..7775b41 100644 --- a/game-ai/ground/wto_ai_drive_common.das +++ b/game-ai/ground/wto_ai_drive_common.das @@ -26,24 +26,19 @@ def public wto_drive_announce_script_ready() : bool let own = (WTO_AI_ENABLED && WTO_AI_LAYER_NAVIGATION && WTO_AI_NAV_DRIVE_MODE == "vehicle_phys") ? "1" : "0" var pve = false using() <| $(var blk : DataBlock) - add(blk, "ownMovement", own) + add(blk, "customDrive", own) call_mission_action("wtoGroundAiScriptReady", blk) pve = datablock_getInt(blk, "pveMode", 0) != 0 return pve [never_inline] -def public wto_drive_request_control(eid : EntityId; holdMs : int) +def private wto_drive_request_control(eid : EntityId) : bool + var ownsDrive = false using() <| $(var blk : DataBlock) add(blk, "eid", "{uint(eid)}") - add(blk, "holdMs", "{holdMs}") - call_mission_action("wtoGroundAiDriveControl", blk) - -[never_inline] -def public wto_drive_release_all() - using() <| $(var blk : DataBlock) - add(blk, "eid", "0") - add(blk, "holdMs", "0") call_mission_action("wtoGroundAiDriveControl", blk) + ownsDrive = datablock_getInt(blk, "ownsDrive", 0) != 0 + return ownsDrive [never_inline] def private wto_set_phys_axes(var actor : VehiclePhysActor&; thr, str, brk, handBrk : float) @@ -179,15 +174,13 @@ def private wto_ensure_phys_owner(gm : IGroundModel const?; var actor : VehicleP assume st = slot.drive if gm == null || gm.moveSys == null return false - if !st.routeBOwned || !gm.moveSys.inactive - wto_drive_request_control(slot.eid, 1000) - if !gm.moveSys.inactive + if !st.routeBOwned + if !wto_drive_request_control(slot.eid) return false - if !st.routeBOwned - slot.spec.isTracked = actor.phys.trackWidth > 0.01 - slot.spec.canPivot = slot.spec.canPivot && slot.spec.isTracked && actor.phys.haveSeparateBrakes - st.routeBOwned = true - return st.routeBOwned + slot.spec.isTracked = actor.phys.trackWidth > 0.01 + slot.spec.canPivot = slot.spec.canPivot && slot.spec.isTracked && actor.phys.haveSeparateBrakes + st.routeBOwned = true + return true // Keep the physical brake active even while a pivot or unstick has requested throttle. def private wto_drive_traffic_brake(vFwd : float; nav : WtoNavState const&; var thr, brk : float&) diff --git a/game-ai/ground/wto_ai_slots_common.das b/game-ai/ground/wto_ai_slots_common.das index fe81acc..59f3e45 100644 --- a/game-ai/ground/wto_ai_slots_common.das +++ b/game-ai/ground/wto_ai_slots_common.das @@ -11,7 +11,6 @@ require %appGame.wtoai.wto_ai_config_common require %appGame.wtoai.wto_ai_combat_types_common require %appGame.wtoai.wto_ai_types_common require %appGame.wtoai.wto_ai_world_common -require %appGame.wtoai.wto_ai_drive_common [never_inline] def public wto_slots_find(bots : WtoBotSlot[64]; eid : EntityId) : int @@ -26,8 +25,6 @@ def public wto_slots_find(bots : WtoBotSlot[64]; eid : EntityId) : int def public wto_slots_forget(var bots : WtoBotSlot[64]&; eid : EntityId) : bool let idx = wto_slots_find(bots, eid) if idx >= 0 - if bots[idx].drive.routeBOwned - wto_drive_request_control(eid, 0) bots[idx] = WtoBotSlot() for i in range(WTO_MAX_BOTS) if bots[i].used && bots[i].tactics.engageTargetEid == eid @@ -61,14 +58,11 @@ def public wto_slots_acquire(var bots : WtoBotSlot[64]&; eid : EntityId; army : created = true return freeIdx -// Releases vehicle control of every bot and clears all slots. +// Clears the mission's bot slots. [never_inline] def public wto_slots_reset_all(var bots : WtoBotSlot[64]&) for i in range(WTO_MAX_BOTS) - if bots[i].drive.routeBOwned - wto_drive_request_control(bots[i].eid, 0) bots[i] = WtoBotSlot() - wto_drive_release_all() // Slots not refreshed by the pipeline for a while belong to units that are gone. [never_inline] diff --git a/native/wto-net/src/local_mp_ground_ai.c b/native/wto-net/src/local_mp_ground_ai.c index 176bd6d..c371c67 100644 --- a/native/wto-net/src/local_mp_ground_ai.c +++ b/native/wto-net/src/local_mp_ground_ai.c @@ -1,13 +1,7 @@ /* - * 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. + * Give the ground AI script exclusive access to driving inputs for LocalMP slot bots. + * Keep the stock behavior tree attached for physics and bypass its driveToPos node. + * The engine uses MoveSystem.inactive for vehicle state, including engine sound. */ #include "hook_config.h" #include "local_mp_internal.h" @@ -27,80 +21,42 @@ typedef uint8_t (__fastcall *DriveToPosUpdateFn)(void *node, void *agent, float typedef void (__fastcall *GroundAiBlkSetIntFn)(void *blk, const char *name, const int32_t *value); typedef void (__fastcall *GroundAiBlkToOwnedFn)(void *blk); -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) +static int custom_drive_active(void) { - uint8_t *ground_model = unit ? as_ground_model(unit) : NULL; - return ground_model ? *(uint8_t **)(ground_model + g_lmp.ground_ai_move_system) : NULL; + return local_mp_is_host() && InterlockedCompareExchange(&g_script_owns_movement, 0, 0); } -static uint8_t *get_bot_owner(uint8_t *unit, uint32_t eid) +static int is_owned_slot_bot(const 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; + const uint8_t *owner = *(const uint8_t *const *)(unit + LMP_UNIT_OFF_MPLAYER); + return owner != NULL && !local_mp_is_local_mplayer(owner) && + owner == local_mp_find_mplayer(-1, owner[LMP_MP_OFF_PID]) && + *(const int64_t *)(owner + LMP_MP_OFF_USER_ID) < -1 && + *(const uint32_t *)(owner + LMP_MP_OFF_UNIT_EID) == eid; } -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) +static int32_t grant_drive_control(uint32_t eid) { uint8_t *unit; - uint8_t *move_system; - uint8_t *owner; - GroundAiDriveControl *control; + const uint8_t *ground_model; - if (eid == 0 || !local_mp_is_host() || !InterlockedCompareExchange(&g_script_owns_movement, 0, 0)) - return; + if (eid == 0 || !custom_drive_active()) + return 0; 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"); + ground_model = unit ? as_ground_model(unit) : NULL; + if (ground_model == NULL || + *(const void *const *)(ground_model + g_lmp.ground_ai_move_system) == NULL || + !is_owned_slot_bot(unit, eid)) + return 0; - 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); - } + log_line("local_mp: ground AI owns drive eid=%#lx", (unsigned long)eid); + return 1; } static int read_uint(const void *blk, const char *key, uint32_t *out) @@ -144,20 +100,25 @@ static int is_slot_bot_agent(const void *agent) *(const int64_t *)(owner + LMP_MP_OFF_USER_ID) < -1; } +static void write_int_reply(void *blk, const char *name, int32_t value) +{ + if (g_lmp.blk_set_int == NULL || g_lmp.blk_to_owned == NULL) + return; + if (*(const int32_t *)((const uint8_t *)blk + LMP_BLK_OFF_OWNED_STATE) != -1) + ((GroundAiBlkToOwnedFn)g_lmp.blk_to_owned)(blk); + ((GroundAiBlkSetIntFn)g_lmp.blk_set_int)(blk, name, &value); +} + static void write_pve_mode_flag(void *blk) { int32_t pve = 0; - if (blk == NULL || g_lmp.blk_set_int == NULL || g_lmp.blk_to_owned == NULL) - return; if (g_local_mp_mission_settings != NULL && g_lmp_datablock_get_str != NULL) { const char *flag = g_lmp_datablock_get_str( g_local_mp_mission_settings + LMP_MIS_OFF_CUSTOM_BOTS, "wtoPveMode", "0"); pve = (flag != NULL && strcmp(flag, "1") == 0) ? 1 : 0; } - if (*(int32_t *)((uint8_t *)blk + LMP_BLK_OFF_OWNED_STATE) != -1) - ((GroundAiBlkToOwnedFn)g_lmp.blk_to_owned)(blk); - ((GroundAiBlkSetIntFn)g_lmp.blk_set_int)(blk, "pveMode", &pve); + write_int_reply(blk, "pveMode", pve); } static uint8_t __fastcall hook_drive_to_pos_update(void *node, void *agent, float dt) @@ -165,25 +126,26 @@ static uint8_t __fastcall hook_drive_to_pos_update(void *node, void *agent, floa /* Keep the BehTree object initialized on the unit so vehicle physics steps normally, * short-circuiting stock driveToPos only for MPlayer slot bots. Non-MPlayer mission AI * units (such as Ground Assault enemy tank waves) keep running stock mission movement. */ - if (local_mp_is_host() && InterlockedCompareExchange(&g_script_owns_movement, 0, 0) && - is_slot_bot_agent(agent)) + if (custom_drive_active() && is_slot_bot_agent(agent)) 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; + uint32_t eid = 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)) { + if (blk != NULL) { + read_uint(blk, "customDrive", &own); + LONG active = own != 0; + LONG prev = InterlockedExchange(&g_script_owns_movement, active); + if (prev != active) { log_line("local_mp: ground AI script ready (ownMovement=%u; stock tankBot %s)", (unsigned)(own ? 1 : 0), own ? "disabled" : "kept"); } + write_pve_mode_flag((void *)blk); } - write_pve_mode_flag((void *)blk); return; } if (name != NULL && strcmp(name, NAV_ACTION) == 0) { @@ -195,47 +157,14 @@ static void __fastcall hook_mission_action(const char *name, const void *blk) 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); + if (blk != NULL && read_uint(blk, "eid", &eid)) + write_int_reply((void *)blk, "ownsDrive", grant_drive_control(eid)); } -void local_mp_ground_ai_update(float dt) +void local_mp_ground_ai_update(void) { - size_t i; - int hosting; - (void)dt; - - if (orig_mission_action == NULL) - return; - hosting = local_mp_is_host(); - if (!hosting) + if (orig_mission_action != NULL && !local_mp_is_host()) local_mp_nav_session_ended(); - 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) diff --git a/native/wto-net/src/local_mp_internal.h b/native/wto-net/src/local_mp_internal.h index 28f8460..c13887c 100644 --- a/native/wto-net/src/local_mp_internal.h +++ b/native/wto-net/src/local_mp_internal.h @@ -120,7 +120,7 @@ int install_local_mp_zone_hooks(void); void local_mp_zones_reset(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); +void local_mp_ground_ai_update(void); int install_local_mp_air_ai_sync_hooks(void); /* "wtoNav" requests of the ground AI script and the battle's navigation pack (local_mp_nav.c). */ diff --git a/native/wto-net/src/local_mp_players.c b/native/wto-net/src/local_mp_players.c index cd2d26c..88373d6 100644 --- a/native/wto-net/src/local_mp_players.c +++ b/native/wto-net/src/local_mp_players.c @@ -187,7 +187,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); + local_mp_ground_ai_update(); if (!local_mp_is_host()) { g_accept_pending = 0; diff --git a/native/wto-net/src/local_mp_values.c b/native/wto-net/src/local_mp_values.c index 7072bdf..2a1047f 100644 --- a/native/wto-net/src/local_mp_values.c +++ b/native/wto-net/src/local_mp_values.c @@ -222,7 +222,6 @@ static const LmpValueSpec SPECS[] = { O(GROUND_AI, "ground_ai_drive_to_pos_update.unit_count", ground_ai_unit_count), O(GROUND_AI, "ground_ai_drive_to_pos_update.unit_array", ground_ai_unit_array), 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), A(AIR_AI_SYNC, "air_ai_add_unsorted_plane", air_ai_add_unsorted_plane), }; diff --git a/native/wto-net/src/local_mp_values.h b/native/wto-net/src/local_mp_values.h index c90c740..2239a52 100644 --- a/native/wto-net/src/local_mp_values.h +++ b/native/wto-net/src/local_mp_values.h @@ -107,7 +107,7 @@ typedef struct LmpValues { /* local_mp_ground_ai */ uint8_t *ground_ai_mission_action, *ground_ai_as_ground_model, *ground_ai_drive_to_pos_update; uint8_t *ground_ai_unit_mgr; - size_t ground_ai_move_system, ground_ai_move_inactive; + size_t ground_ai_move_system; size_t ground_ai_agent_unit_idx, ground_ai_unit_count, ground_ai_unit_array; /* local_mp_air_ai_sync */ uint8_t *air_ai_add_unsorted_plane; diff --git a/patches/native/local_mp_ground_ai.jsonc b/patches/native/local_mp_ground_ai.jsonc index a2a2b1d..48ea302 100644 --- a/patches/native/local_mp_ground_ai.jsonc +++ b/patches/native/local_mp_ground_ai.jsonc @@ -1,5 +1,5 @@ -// Temporary movement ownership for custom ground AI on a LocalMP host. -// The local server cannot restore the MoveSystem flag if a game script stops running. +// Drive input ownership for custom ground AI on a LocalMP host. +// The original behavior node and vehicle physics run in the game process. // Without this module, stock tank movement keeps ownership of the vehicle. // The same mission action carries the script's "wtoNav" navigation queries, answered through the // DataBlock setters of local_mp_awards. @@ -51,7 +51,7 @@ } }] }, - // The MoveSystem daScript annotation registers the bool field named "inactive". + // This capture also supports older hook libraries that read the MoveSystem flag. // 2.61.0.37: 0xa1c374. 2.61.0.58: 0xa1f024. "ground_ai_move_annotation": { "variants": [{