From 73bae4785f901311c3e2eaa8f1c7beaddd96c91f Mon Sep 17 00:00:00 2001 From: N0VAN0M4LY <224130499+N0VAN0M4LY@users.noreply.github.com> Date: Thu, 3 Dec 2026 16:47:44 +0800 Subject: [PATCH] fix(ground-ai): harden drive ownership, teardown and failure switch Address two issues found in the first isolated 2.61.0.58 live runs and add a test switch to disable silent fallback to stock movement: - Add navigation.returnToStockOnFailure in tuning/ground-ai.jsonc (default true), compiled into WTO_AI_NAV_RETURN_TO_STOCK and written to wto_ws.cfg as ground_ai_return_to_stock=0 when disabled. When false, failed or timed-out bots keep stock movement paused, brake in place and log the failure once instead of silently handing control back to stock tankBot. - Allow holdMs=0 release actions even after local_mp_is_host() flips false during mission unload, add eid=0 bulk release on wto_reset_runtime(), and match slots by eid rather than live owner lookup so match exit cleans up the native control table immediately. - Stop releasing drive ownership when a vehicle is immobilized by damage or repair; brake in place and pause the echo watchdog instead, avoiding the 1-second release/re-acquire flapping observed during combat. Log exact release reasons and back off for 1 second if MoveSystem.inactive is cleared externally. Verified MSVC build of wto_ws.dll (0 warnings in project C sources), root and launcher type checks, 14/14 check-patches on 2.61.0.58, and a temporary 2.61.0.58 game copy build confirming ground_ai_return_to_stock=0 in wto_ws.cfg and 15 packed daScript modules. Live battle re-check pending. --- apps/server/src/tuning/ground-ai-tuning.ts | 1 + apps/server/tuning/ground-ai.jsonc | 3 + game-ai/ground/wto_ai_drive_common.das | 65 +++++++++++++++---- game-ai/ground/wto_ai_runtime.das | 1 + game-ai/ground/wto_ai_types_common.das | 1 + native/wto-net/src/hook_config.c | 10 +++ native/wto-net/src/hook_config.h | 2 + native/wto-net/src/local_mp_ground_ai.c | 49 ++++++++++---- packages/patcher/src/build-game-copy.ts | 3 + .../patcher/src/ground-ai/ground-ai-config.ts | 2 + packages/patcher/src/network/network-hook.ts | 6 +- 11 files changed, 116 insertions(+), 27 deletions(-) diff --git a/apps/server/src/tuning/ground-ai-tuning.ts b/apps/server/src/tuning/ground-ai-tuning.ts index 20ed0de..8417061 100644 --- a/apps/server/src/tuning/ground-ai-tuning.ts +++ b/apps/server/src/tuning/ground-ai-tuning.ts @@ -126,6 +126,7 @@ function readNavigationSection(nav: TuningReader): GroundAiTuning['navigation'] steerSign, deadEchoSec: nav.number('deadEchoSec', { min: 0.3, max: 10 }), controlTimeoutSec: nav.number('controlTimeoutSec', { min: 0.3, max: 10 }), + returnToStockOnFailure: nav.bool('returnToStockOnFailure'), cornerBrakeAccel: nav.number('cornerBrakeAccel', { min: 0.5, max: 8 }), pivotAngleDeg: nav.number('pivotAngleDeg', { min: 25, max: 150 }), stuckWindowSec: nav.number('stuckWindowSec', { min: 1, max: 15 }), diff --git a/apps/server/tuning/ground-ai.jsonc b/apps/server/tuning/ground-ai.jsonc index 1de0cfa..11e0bbe 100644 --- a/apps/server/tuning/ground-ai.jsonc +++ b/apps/server/tuning/ground-ai.jsonc @@ -65,6 +65,9 @@ "steerSign": 1.0, "deadEchoSec": 1.2, "controlTimeoutSec": 1.0, + // When false, failed or timed-out bots brake in place and log the cause instead of + // handing movement back to stock tankBot (intended for testing). + "returnToStockOnFailure": true, "cornerBrakeAccel": 1.75, "pivotAngleDeg": 75.0, "stuckWindowSec": 3.6, diff --git a/game-ai/ground/wto_ai_drive_common.das b/game-ai/ground/wto_ai_drive_common.das index 4ec0fd8..4013122 100644 --- a/game-ai/ground/wto_ai_drive_common.das +++ b/game-ai/ground/wto_ai_drive_common.das @@ -25,6 +25,13 @@ def public wto_drive_request_control(eid : EntityId; holdMs : int) 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) + [never_inline] def private wto_set_phys_axes(var actor : VehiclePhysActor&; thr, str, brk, handBrk : float) actor.phys.producedCT |> ground_control_state_setAutomaticTransmission(true) @@ -49,8 +56,7 @@ def private wto_check_echo_watchdog(unitMissionName : string; var actor : Vehicl if !throttleEchoed || !steeringEchoed st.deadEchoSec += clamp(dt, 0.0, 0.15) if st.deadEchoSec >= WTO_AI_NAV_DEAD_ECHO_SEC - st.routeAFallback = true - print("WTO_AI|PHYS_FALLBACK|actor={unitMissionName}|eid={uint(slot.eid)}\n") + print("WTO_AI|PHYS_FALLBACK|actor={unitMissionName}|eid={uint(slot.eid)}|autoReturn={WTO_AI_NAV_RETURN_TO_STOCK}\n") return false else st.deadEchoSec = 0.0 @@ -127,10 +133,11 @@ def private wto_compute_cruise_axes(cmd : WtoDriveCmd const&; spec : WtoVehicleS [never_inline] def private wto_release_phys_owner(gm : IGroundModel const?; var actor : VehiclePhysActor&; - var slot : WtoBotSlot&) + var slot : WtoBotSlot&; reason : string) assume st = slot.drive if !st.routeBOwned return + print("WTO_AI|DRIVE_RELEASE|eid={uint(slot.eid)}|reason={reason}\n") if gm != null && gm.moveSys != null && gm.moveSys.inactive wto_set_phys_axes(actor, 0.0, 0.0, 0.0, 0.0) wto_drive_request_control(slot.eid, 0) @@ -139,22 +146,39 @@ def private wto_release_phys_owner(gm : IGroundModel const?; var actor : Vehicle st.prevStr = 0.0 st.deadEchoSec = 0.0 +[never_inline] +def private wto_drive_is_immobilized(actor : VehiclePhysActor const&) : bool + assume dmg = actor.phys.currentState.damageState + return (!dmg.allowMoveControl || dmg.engineReadyness <= 0.0 || + dmg.driverReadyness <= 0.0 || (dmg.trackLBroken && dmg.trackRBroken)) + +[never_inline] +def private wto_drive_handle_failure(gm : IGroundModel const?; var actor : VehiclePhysActor&; + var slot : WtoBotSlot&; reason : string; permanent : bool) + assume st = slot.drive + if WTO_AI_NAV_RETURN_TO_STOCK + if permanent + st.routeAFallback = true + wto_release_phys_owner(gm, actor, slot, reason) + return + if !st.failLogged + st.failLogged = true + print("WTO_AI|DRIVE_FAIL|eid={uint(slot.eid)}|reason={reason}|autoReturn=false\n") + st.prevThr = 0.0 + st.prevStr = 0.0 + st.deadEchoSec = 0.0 + wto_set_phys_axes(actor, 0.0, 0.0, 1.0, 1.0) + [never_inline] def private wto_prepare_phys_owner(gm : IGroundModel const?; var actor : VehiclePhysActor&; now : float; var slot : WtoBotSlot&) : bool assume st = slot.drive if gm == null || gm.moveSys == null - wto_release_phys_owner(gm, actor, slot) - return false - assume dmg = actor.phys.currentState.damageState - if (!slot.driveCmd.valid || (!slot.macro.valid && !WTO_AI_LAYER_TACTICS) || - now > slot.nextNavAt + WTO_AI_NAV_TICK_SEC || - !dmg.allowMoveControl || dmg.engineReadyness <= 0.0 || dmg.driverReadyness <= 0.0 || - (dmg.trackLBroken && dmg.trackRBroken)) - wto_release_phys_owner(gm, actor, slot) + wto_release_phys_owner(gm, actor, slot, "missing_movesys") return false if st.routeBOwned && !gm.moveSys.inactive - wto_release_phys_owner(gm, actor, slot) + wto_release_phys_owner(gm, actor, slot, "external_inactive_reset") + st.nextControlRequestAt = now + 1.0 return false if now >= st.nextControlRequestAt if !st.routeBOwned && gm.moveSys.inactive @@ -178,9 +202,22 @@ def public wto_drive_step_phys(var unit : Unit?; unitMissionName : string; return if !wto_prepare_phys_owner(gm, actor, now, slot) return - if !wto_check_echo_watchdog(unitMissionName, actor, dt, slot) - wto_release_phys_owner(gm, actor, slot) + if !slot.driveCmd.valid || (!slot.macro.valid && !WTO_AI_LAYER_TACTICS) + wto_drive_handle_failure(gm, actor, slot, "invalid_drive_cmd", false) return + if now > slot.nextNavAt + WTO_AI_NAV_TICK_SEC + wto_drive_handle_failure(gm, actor, slot, "stale_nav_tick", false) + return + if wto_drive_is_immobilized(actor) + slot.drive.prevThr = 0.0 + slot.drive.prevStr = 0.0 + slot.drive.deadEchoSec = 0.0 + wto_set_phys_axes(actor, 0.0, 0.0, 1.0, 1.0) + return + if !wto_check_echo_watchdog(unitMissionName, actor, dt, slot) + wto_drive_handle_failure(gm, actor, slot, "dead_echo", true) + return + slot.drive.failLogged = false vehicle_actor_set_allow_move_control(actor, true) assume st = slot.drive assume cmd = slot.driveCmd diff --git a/game-ai/ground/wto_ai_runtime.das b/game-ai/ground/wto_ai_runtime.das index c276754..b25b4d9 100644 --- a/game-ai/ground/wto_ai_runtime.das +++ b/game-ai/ground/wto_ai_runtime.das @@ -52,6 +52,7 @@ def private wto_reset_runtime() if g_bots[i].drive.routeBOwned wto_drive_request_control(g_bots[i].eid, 0) g_bots[i] = WtoBotSlot() + wto_drive_release_all() [never_inline] def private wto_find_slot(eid : EntityId) : int diff --git a/game-ai/ground/wto_ai_types_common.das b/game-ai/ground/wto_ai_types_common.das index 2c8cd53..342ee48 100644 --- a/game-ai/ground/wto_ai_types_common.das +++ b/game-ai/ground/wto_ai_types_common.das @@ -242,6 +242,7 @@ struct public WtoDriveCmd struct public WtoDriveState routeBOwned : bool = false routeAFallback : bool = false + failLogged : bool = false nextControlRequestAt : float = -1.0 deadEchoSec : float = 0.0 prevYaw : float = 0.0 diff --git a/native/wto-net/src/hook_config.c b/native/wto-net/src/hook_config.c index a049a16..db97098 100644 --- a/native/wto-net/src/hook_config.c +++ b/native/wto-net/src/hook_config.c @@ -5,6 +5,8 @@ * port= without a valid port the hooks still answer every host name * locally but refuse every connection that would leave the machine * log=0|1 optional, defaults to 1; when 0, wto_ws.log is not opened + * ground_ai_return_to_stock=0|1 optional, defaults to 1; when 0, timed-out ground AI control + * stays paused instead of returning to stock tankBot (testing) * exe.time_date_stamp=0x... the executable the native values were resolved for * exe.size_of_image=0x... * native.= what the LocalMP modules need from the executable: RVAs of @@ -35,6 +37,7 @@ static wchar_t g_folder[MAX_PATH]; static CRITICAL_SECTION g_log_lock; static FILE *g_log; static int g_log_enabled = 1; +static int g_ground_ai_return_to_stock = 1; static NativeValue g_native[NATIVE_VALUES_MAX]; static size_t g_native_count; static int64_t g_exe_time_date_stamp = -1; @@ -102,6 +105,8 @@ static void read_settings(void) read_port(line + 5); else if (strncmp(line, "log=", 4) == 0) g_log_enabled = atoi(line + 4) != 0; + else if (strncmp(line, "ground_ai_return_to_stock=", 26) == 0) + g_ground_ai_return_to_stock = atoi(line + 26) != 0; else if (strncmp(line, "exe.time_date_stamp=", 20) == 0) parse_value(line + 20, &g_exe_time_date_stamp); else if (strncmp(line, "exe.size_of_image=", 18) == 0) @@ -138,6 +143,11 @@ int config_native_matches_executable(void) (int64_t)nt->OptionalHeader.SizeOfImage == g_exe_size_of_image; } +int config_ground_ai_return_to_stock(void) +{ + return g_ground_ai_return_to_stock; +} + void config_load(HMODULE self) { wchar_t path[MAX_PATH]; diff --git a/native/wto-net/src/hook_config.h b/native/wto-net/src/hook_config.h index 5e232f8..ec208d2 100644 --- a/native/wto-net/src/hook_config.h +++ b/native/wto-net/src/hook_config.h @@ -13,3 +13,5 @@ void describe_address(const struct sockaddr *name, int length, char *text, size_ int config_native_value(const char *name, int64_t *value); /* Whether the settings were resolved for the executable this process runs (PE time stamp and size). */ int config_native_matches_executable(void); +/* Whether timed-out ground AI control returns to stock tankBot (wto_ws.cfg "ground_ai_return_to_stock="); 1 by default. */ +int config_ground_ai_return_to_stock(void); diff --git a/native/wto-net/src/local_mp_ground_ai.c b/native/wto-net/src/local_mp_ground_ai.c index 1ed4c66..64df48e 100644 --- a/native/wto-net/src/local_mp_ground_ai.c +++ b/native/wto-net/src/local_mp_ground_ai.c @@ -21,6 +21,7 @@ typedef struct GroundAiDriveControl { uint8_t *move_system; uint32_t eid; float remaining_seconds; + int stalled_logged; } GroundAiDriveControl; static MissionActionFn orig_mission_action; @@ -58,25 +59,40 @@ static void give_back_drive(GroundAiDriveControl *control, const char *reason) memset(control, 0, sizeof(*control)); } +static void release_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) + give_back_drive(&controls[i], reason); + } +} + 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; + uint8_t *unit; + uint8_t *move_system; + uint8_t *owner; GroundAiDriveControl *control; uint8_t *inactive; + if (hold_ms == 0) { + release_by_eid(eid, eid == 0 ? "mission reset" : "script released"); + return; + } + if (eid == 0 || !local_mp_is_host()) + 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)) 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) { @@ -89,6 +105,7 @@ static void update_drive_control(uint32_t eid, uint32_t hold_ms) log_line("local_mp: ground AI acquired drive eid=%#lx", (unsigned long)eid); } control->remaining_seconds = (float)hold_ms / 1000.0f; + control->stalled_logged = 0; } static int read_uint(const void *blk, const char *key, uint32_t *out) @@ -115,8 +132,9 @@ static void __fastcall hook_mission_action(const char *name, const void *blk) 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)) + if (blk == NULL || !read_uint(blk, "eid", &eid) || !read_uint(blk, "holdMs", &hold_ms)) + return; + if (hold_ms > 0 && (eid == 0 || !local_mp_is_host())) return; AcquireSRWLockExclusive(&control_lock); update_drive_control(eid, hold_ms); @@ -149,8 +167,15 @@ void local_mp_ground_ai_update(float dt) } if (dt > 0.0f) control->remaining_seconds -= dt; - if (control->remaining_seconds <= 0.0f) - give_back_drive(control, "script stopped renewing control"); + if (control->remaining_seconds <= 0.0f) { + if (config_ground_ai_return_to_stock()) { + give_back_drive(control, "script stopped renewing control"); + } else if (!control->stalled_logged) { + control->stalled_logged = 1; + log_line("local_mp: ground AI control stalled eid=%#lx (auto-return disabled; stock movement kept paused)", + (unsigned long)control->eid); + } + } } ReleaseSRWLockExclusive(&control_lock); } diff --git a/packages/patcher/src/build-game-copy.ts b/packages/patcher/src/build-game-copy.ts index 2666592..5ede41d 100644 --- a/packages/patcher/src/build-game-copy.ts +++ b/packages/patcher/src/build-game-copy.ts @@ -140,6 +140,9 @@ export function buildGameCopy(inputs: GameCopyInputs): GameCopyBuild { const executableFolder = dirname(GAME_FILES.executable); const hookSettings = networkHookSettings(endpoint, stockImage, native.values, { loggingEnabled: inputs.hookLogging ?? true, + ...(customGroundDrive && inputs.groundAi?.config.navigation.returnToStockOnFailure === false + ? { groundAiReturnToStock: false } + : {}), }); const files: GameCopyFile[] = [ { path: GAME_FILES.charPack, content: core.charPack }, diff --git a/packages/patcher/src/ground-ai/ground-ai-config.ts b/packages/patcher/src/ground-ai/ground-ai-config.ts index 59f20b9..a6c8689 100644 --- a/packages/patcher/src/ground-ai/ground-ai-config.ts +++ b/packages/patcher/src/ground-ai/ground-ai-config.ts @@ -56,6 +56,7 @@ export interface GroundAiConfig { readonly steerSign: number; readonly deadEchoSec: number; readonly controlTimeoutSec: number; + readonly returnToStockOnFailure: boolean; readonly cornerBrakeAccel: number; readonly pivotAngleDeg: number; readonly stuckWindowSec: number; @@ -165,6 +166,7 @@ function collectConfigEntries(c: GroundAiConfig): readonly DasEntry[] { ['WTO_AI_NAV_STEER_SIGN', 'float', c.navigation.steerSign], ['WTO_AI_NAV_DEAD_ECHO_SEC', 'float', c.navigation.deadEchoSec], ['WTO_AI_NAV_CONTROL_TIMEOUT_SEC', 'float', c.navigation.controlTimeoutSec], + ['WTO_AI_NAV_RETURN_TO_STOCK', 'bool', c.navigation.returnToStockOnFailure], ['WTO_AI_NAV_CORNER_BRAKE_ACCEL', 'float', c.navigation.cornerBrakeAccel], ['WTO_AI_NAV_PIVOT_ANGLE_DEG', 'float', c.navigation.pivotAngleDeg], ['WTO_AI_NAV_STUCK_WINDOW_SEC', 'float', c.navigation.stuckWindowSec], diff --git a/packages/patcher/src/network/network-hook.ts b/packages/patcher/src/network/network-hook.ts index 583dbbf..cb2c3f7 100644 --- a/packages/patcher/src/network/network-hook.ts +++ b/packages/patcher/src/network/network-hook.ts @@ -17,11 +17,15 @@ export function networkHookSettings( endpoint: LocalEndpoint, stock: PeImage, nativeValues: ReadonlyMap, - options?: { readonly loggingEnabled?: boolean }, + options?: { + readonly loggingEnabled?: boolean; + readonly groundAiReturnToStock?: boolean; + }, ): { name: string; content: Uint8Array } { const lines = [ `port=${endpoint.port}`, ...(options?.loggingEnabled === false ? ['log=0'] : []), + ...(options?.groundAiReturnToStock === false ? ['ground_ai_return_to_stock=0'] : []), ...nativeSettingsLines(stock, nativeValues), ]; return { name: SETTINGS, content: new TextEncoder().encode(`${lines.join('\n')}\n`) };