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

+58 -55
View File
@@ -1,6 +1,6 @@
// Chassis drive actuator: translates `WtoDriveCmd` into closed-loop `VehiclePhysActor` axes
// (throttle, steering, brakes, auto-transmission) with space-aware 3-point turns, smooth
// long-distance reverse control, and dead-echo watchdog fallback to `MoveSys`.
// long-distance reverse control, and dead-echo watchdog return to stock movement.
options indenting = 2
options no_global_heap = true
module wto_ai_drive_common shared
@@ -19,33 +19,11 @@ require %appGame.wtoai.wto_ai_math_common
require %appGame.wtoai.wto_ai_types_common
[never_inline]
def public wto_drive_prepare_unit(unitMissionName : string)
if empty(unitMissionName)
return
def public wto_drive_request_control(eid : EntityId; holdMs : int)
using() <| $(var blk : DataBlock)
add(blk, "object", unitMissionName)
add(blk, "avoidObstacles", false)
add(blk, "movementTraceRI", true)
add(blk, "destroyRIByCollision", true)
call_mission_action("unitSetProperties", blk)
[never_inline]
def public wto_drive_apply_movesys(var unit : Unit?; cmd : WtoDriveCmd const&)
if unit == null || !cmd.valid
return
let gm = as_gm(unit)
if gm == null || gm.moveSys == null
return
if cmd.mode == WTO_DRIVE_HALT || cmd.speedMps <= 0.05
set_destination_way_point(unit, "HALT", unit.unitTm, 0.0)
return
var tm = unit.unitTm
tm[3] = cmd.targetPos
let dir = wto_xz_normalize(cmd.desiredDir, float3(1.0, 0.0, 0.0))
tm[0] = cmd.mode == WTO_DRIVE_REVERSE ? -dir : dir
tm[1] = float3(0.0, 1.0, 0.0)
tm[2] = float3(-tm[0].z, 0.0, tm[0].x)
set_destination_way_point(unit, "MOVE_TO_STRAIGHT", tm, cmd.speedMps)
add(blk, "eid", "{uint(eid)}")
add(blk, "holdMs", "{holdMs}")
call_mission_action("wtoGroundAiDriveControl", blk)
[never_inline]
def private wto_set_phys_axes(var actor : VehiclePhysActor&; thr, str, brk, handBrk : float)
@@ -59,20 +37,18 @@ def private wto_set_phys_axes(var actor : VehiclePhysActor&; thr, str, brk, hand
actor.phys.producedCT |> ground_control_state_setAxisValue(EGroundControlAxis.GCA_HAND_BRAKE, clamp(handBrk, 0.0, 1.0))
[never_inline]
def private wto_check_echo_watchdog(unitMissionName : string; var gm : IGroundModel?;
var actor : VehiclePhysActor&; dt : float; var slot : WtoBotSlot&) : bool
def private wto_check_echo_watchdog(unitMissionName : string; var actor : VehiclePhysActor&;
dt : float; var slot : WtoBotSlot&) : bool
assume st = slot.drive
let appThr = ground_control_state_getAxisValue(actor.phys.appliedCT, EGroundControlAxis.GCA_THROTTLE)
let appStr = ground_control_state_getAxisValue(actor.phys.appliedCT, EGroundControlAxis.GCA_STEERING)
let wanted = abs(st.prevThr) >= 0.35 || abs(st.prevStr) >= 0.45
let echoed = abs(appThr) >= 0.05 || abs(appStr) >= 0.05
if wanted && !echoed
let wantsThrottle = abs(st.prevThr) >= 0.35
let wantsSteering = abs(st.prevStr) >= 0.45
let throttleEchoed = !wantsThrottle || abs(appThr) >= 0.05
let steeringEchoed = !wantsSteering || abs(appStr) >= 0.05
if !throttleEchoed || !steeringEchoed
st.deadEchoSec += clamp(dt, 0.0, 0.15)
if st.deadEchoSec >= WTO_AI_NAV_DEAD_ECHO_SEC
wto_set_phys_axes(actor, 0.0, 0.0, 0.0, 0.0)
if gm != null && gm.moveSys != null
gm.moveSys.inactive = false
st.routeBOwned = false
st.routeAFallback = true
print("WTO_AI|PHYS_FALLBACK|actor={unitMissionName}|eid={uint(slot.eid)}\n")
return false
@@ -150,35 +126,62 @@ def private wto_compute_cruise_axes(cmd : WtoDriveCmd const&; spec : WtoVehicleS
outThr = clamp(0.32 + vErr * 0.28, 0.12, 1.0)
[never_inline]
def private wto_prepare_phys_owner(var unit : Unit?; unitMissionName : string;
var gm : IGroundModel?; var actor : VehiclePhysActor&;
dt : float; var slot : WtoBotSlot&) : bool
def private wto_release_phys_owner(gm : IGroundModel const?; var actor : VehiclePhysActor&;
var slot : WtoBotSlot&)
assume st = slot.drive
if !st.routeBOwned
slot.spec.isTracked = actor.phys.trackWidth > 0.01
slot.spec.canPivot = slot.spec.isTracked && actor.phys.haveSeparateBrakes
if gm != null && gm.moveSys != null
set_destination_way_point(unit, "HALT", unit.unitTm, 0.0)
gm.moveSys.inactive = true
st.routeBOwned = true
elif gm != null && gm.moveSys != null
gm.moveSys.inactive = true
assume dmg = actor.phys.currentState.damageState
if !dmg.allowMoveControl || dmg.engineReadyness <= 0.0 || dmg.driverReadyness <= 0.0 || (dmg.trackLBroken && dmg.trackRBroken)
st.deadEchoSec = 0.0
wto_set_phys_axes(actor, 0.0, 0.0, 1.0, 1.0)
return
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)
st.routeBOwned = false
st.prevThr = 0.0
st.prevStr = 0.0
st.deadEchoSec = 0.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
vehicle_actor_set_allow_move_control(actor, true)
return wto_check_echo_watchdog(unitMissionName, gm, actor, dt, slot)
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)
return false
if st.routeBOwned && !gm.moveSys.inactive
wto_release_phys_owner(gm, actor, slot)
return false
if now >= st.nextControlRequestAt
if !st.routeBOwned && gm.moveSys.inactive
return false
wto_drive_request_control(slot.eid, int(WTO_AI_NAV_CONTROL_TIMEOUT_SEC * 1000.0))
if !gm.moveSys.inactive
st.nextControlRequestAt = now + WTO_AI_NAV_CONTROL_TIMEOUT_SEC
return false
st.nextControlRequestAt = now + min(WTO_AI_NAV_TICK_SEC, WTO_AI_NAV_CONTROL_TIMEOUT_SEC * 0.5)
if !st.routeBOwned
slot.spec.isTracked = actor.phys.trackWidth > 0.01
slot.spec.canPivot = slot.spec.isTracked && actor.phys.haveSeparateBrakes
st.routeBOwned = true
return st.routeBOwned
[never_inline]
def public wto_drive_step_phys(var unit : Unit?; unitMissionName : string;
var gm : IGroundModel?; var actor : VehiclePhysActor&;
gm : IGroundModel const?; var actor : VehiclePhysActor&;
dt, now : float; var slot : WtoBotSlot&)
if unit == null || actor.physEdit == null || slot.drive.routeAFallback
return
if !wto_prepare_phys_owner(unit, unitMissionName, gm, actor, dt, slot)
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)
return
vehicle_actor_set_allow_move_control(actor, true)
assume st = slot.drive
assume cmd = slot.driveCmd
let fwd = wto_xz_normalize(unit.unitTm[0], float3(1.0, 0.0, 0.0))
+9 -6
View File
@@ -49,6 +49,8 @@ def private wto_reset_runtime()
g_next_macro_at = 0.0
g_next_diag_at = 0.0
for i in range(WTO_MAX_BOTS)
if g_bots[i].drive.routeBOwned
wto_drive_request_control(g_bots[i].eid, 0)
g_bots[i] = WtoBotSlot()
[never_inline]
@@ -157,12 +159,16 @@ def wto_ai_unit_dead_es(evt : Event; eid : EntityId; unit__ref : UnitRef)
wto_world_record_death(g_world, int(u.army), u.pos, float(get_app().usefulTotalTime))
let idx = wto_find_slot(eid)
if idx >= 0
if g_bots[idx].drive.routeBOwned
wto_drive_request_control(eid, 0)
g_bots[idx] = WtoBotSlot()
[es(tag=server, on_disappear, on_event=EventUnitRespawn)]
def wto_ai_unit_cleanup_es(evt : Event; eid : EntityId)
let idx = wto_find_slot(eid)
if idx >= 0
if g_bots[idx].drive.routeBOwned
wto_drive_request_control(eid, 0)
g_bots[idx] = WtoBotSlot()
[es(tag=server)]
@@ -192,6 +198,8 @@ def wto_ai_world_tick_es(act : UpdateStageInfoAct)
for i in range(WTO_MAX_BOTS)
if g_bots[i].used && (now - g_bots[i].lastSeenAt) > 3.5
g_bots[i] = WtoBotSlot()
if !g_world.valid
print("WTO_AI|RUNTIME_READY\n")
wto_world_refresh(now, g_world)
wto_crowd_update_all(g_world, g_bots, g_crowd)
if WTO_AI_LAYER_MACRO && now >= g_next_macro_at
@@ -203,7 +211,7 @@ def wto_ai_world_tick_es(act : UpdateStageInfoAct)
[es(tag=server, no_order)]
def wto_ai_pipeline_es(act : UpdateStageInfoAct; eid : EntityId; var unit__ref : UnitRef&;
unit__missionName : string; unit__className : string = "")
unit__className : string = "")
if !wto_any_layer_active() || !g_world.valid
return
var unit = unit__ref.unit
@@ -221,17 +229,12 @@ def wto_ai_pipeline_es(act : UpdateStageInfoAct; eid : EntityId; var unit__ref :
if !slot.prepared
slot.prepared = true
wto_specs_load(eid, unit__className, unit, gm, slot.spec)
if WTO_AI_LAYER_NAVIGATION
wto_drive_prepare_unit(unit__missionName)
if now >= slot.nextTacticsAt
slot.nextTacticsAt = now + WTO_AI_TACTICS_TICK_SEC
wto_tactics_update(g_world, now, WTO_AI_TACTICS_TICK_SEC, unit.pos, unit.unitTm[0], unit.isBurn, slot, g_crowd)
if now >= slot.nextNavAt
slot.nextNavAt = now + WTO_AI_NAV_TICK_SEC
wto_nav_update(g_world, now, unit.pos, unit.unitTm[0], slot)
let usingPhys = WTO_AI_NAV_DRIVE_MODE == "vehicle_phys" && slot.drive.routeBOwned && !slot.drive.routeAFallback
if !usingPhys
wto_drive_apply_movesys(unit, slot.driveCmd)
wto_fire_update(g_world, unit, now, act.dt, slot)
[es(tag=server, after=wto_ai_pipeline_es)]
+1
View File
@@ -242,6 +242,7 @@ struct public WtoDriveCmd
struct public WtoDriveState
routeBOwned : bool = false
routeAFallback : bool = false
nextControlRequestAt : float = -1.0
deadEchoSec : float = 0.0
prevYaw : float = 0.0
prevYawAt : float = -100.0