Files
WT-Offline/game-ai/ground/wto_ai_runtime.das
T
N0VAN0M4LY 885e3bbb5d 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.
2026-12-03 14:24:32 +08:00

255 lines
9.9 KiB
Plaintext

// Thin ECS orchestrator for WTOffline's modular ground AI system.
// Frame pipeline: world snapshot -> crowd density -> macro objectives -> tactics & perception ->
// corridor & bidirectional local nav -> chassis drive actuator -> deterministic fire control.
options indenting = 2
options no_global_variables = false
options no_global_heap = true
options stack = 65536
require ecs
require app
require math
require DagorMath
require dm
require WTDamageModel
require Unit
require IGroundModel
require HeavyVehicleModel
require vehicle
require pathfinder
require %appGame.wt_events
require %game.events
require %game.unit.unit_events
require %appGame.wtoai.wto_ai_config_common
require %appGame.wtoai.wto_ai_types_common
require %appGame.wtoai.wto_ai_specs_common
require %appGame.wtoai.wto_ai_world_common
require %appGame.wtoai.wto_ai_crowd_common
require %appGame.wtoai.wto_ai_macro_common
require %appGame.wtoai.wto_ai_tactics_common
require %appGame.wtoai.wto_ai_nav_common
require %appGame.wtoai.wto_ai_drive_common
require %appGame.wtoai.wto_ai_fire_common
var private @safe_when_uninitialized g_world : WtoWorldSnap
var private @safe_when_uninitialized g_crowd : WtoCrowdSummary
var private @safe_when_uninitialized g_bots : WtoBotSlot[64]
var private g_last_world_frame : float = -1.0
var private g_next_macro_at : float = 0.0
var private g_next_diag_at : float = 0.0
def private wto_any_layer_active() : bool
return WTO_AI_ENABLED && (WTO_AI_LAYER_MACRO || WTO_AI_LAYER_TACTICS || WTO_AI_LAYER_NAVIGATION || WTO_AI_LAYER_FIRE_CONTROL)
[never_inline]
def private wto_reset_runtime()
wto_world_reset(g_world)
g_crowd = WtoCrowdSummary()
g_last_world_frame = -1.0
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]
def private wto_find_slot(eid : EntityId) : int
let cap = min(WTO_MAX_BOTS, WTO_AI_MAX_TRACKED_BOTS)
for i in range(cap)
if g_bots[i].used && g_bots[i].eid == eid
return i
return -1
[never_inline]
def private wto_acquire_slot(eid : EntityId; army : int; now : float) : int
let cap = min(WTO_MAX_BOTS, WTO_AI_MAX_TRACKED_BOTS)
var freeIdx = -1
for i in range(cap)
if g_bots[i].used && g_bots[i].eid == eid
g_bots[i].army = army
g_bots[i].lastSeenAt = now
return i
if !g_bots[i].used && freeIdx < 0
freeIdx = i
if freeIdx >= 0
g_bots[freeIdx] = WtoBotSlot()
g_bots[freeIdx].used = true
g_bots[freeIdx].eid = eid
g_bots[freeIdx].army = army
g_bots[freeIdx].lastSeenAt = now
g_bots[freeIdx].nextTacticsAt = now + float(freeIdx % 8) * (WTO_AI_TACTICS_TICK_SEC / 8.0)
g_bots[freeIdx].nextNavAt = now + float(freeIdx % 10) * (WTO_AI_NAV_TICK_SEC / 10.0)
g_bots[freeIdx].route.nextReplanAt = now + float(freeIdx % 12) * 0.2
return freeIdx
[never_inline]
def private wto_phys_slot_available() : bool
if WTO_AI_NAV_MAX_PHYS_BOTS <= 0
return false
var count = 0
for i in range(WTO_MAX_BOTS)
if g_bots[i].used && g_bots[i].drive.routeBOwned && !g_bots[i].drive.routeAFallback
count++
if count >= WTO_AI_NAV_MAX_PHYS_BOTS
return false
return true
[never_inline]
def private wto_log_bot_and_cap_samples()
var sampled = 0
for i in range(WTO_MAX_BOTS)
if !g_bots[i].used || sampled >= 2
continue
assume b = g_bots[i]
print("WTO_AI|BOT_SAMPLE|eid={uint(b.eid)}|role={b.spec.role}|stab={b.spec.hasVertStab}|v0={b.spec.muzzleSpeedMps}|pos={b.tactics.posture}|allyD={b.crowd.nearestAllyDistM}|clust={b.crowd.clusterAllyCount}|ridge={b.tactics.onExposedRidge}|los={b.perceive.hasClearLos}|fc={b.fire.gunReady}/{b.fire.reloading}/{b.fire.lastAligned}|err={b.fire.lastAimErrDeg}|omB={b.fire.barrelOmegaDegSec}|mode={b.driveCmd.mode}|v={b.drive.lastVFwd}|clr={b.nav.frontClearM}/{b.nav.rearClearM}|wp={b.route.corridorIdx}/{b.route.corridorLen}|proj={b.route.lastStartOk}/{b.route.lastGoalOk}\n")
sampled++
for c in range(g_world.capCount)
assume z = g_world.caps[c]
print("WTO_AI|CAP|idx={c}|owner={z.ownerArmy}|rad={z.radius}|prog={z.progress}\n")
[never_inline]
def private wto_log_telemetry(now : float)
var active = 0
var routeB = 0
var revNow = 0
var engNow = 0
var dispNow = 0
var ridgeNow = 0
var revEvents = 0
var ridgeWithdraws = 0
var detours = 0
var shots = 0
var errSum = 0.0
var maxErr = 0.0
var roles : int[5]
for r in range(5)
roles[r] = 0
for i in range(WTO_MAX_BOTS)
if !g_bots[i].used
continue
assume b = g_bots[i]
active++
roles[clamp(b.spec.role, 0, 4)]++
routeB += b.drive.routeBOwned ? 1 : 0
revNow += b.driveCmd.mode == WTO_DRIVE_REVERSE ? 1 : 0
engNow += b.tactics.posture == WTO_POSTURE_ENGAGE_HALT ? 1 : 0
dispNow += b.tactics.posture == WTO_POSTURE_DISPERSE ? 1 : 0
ridgeNow += b.tactics.onExposedRidge ? 1 : 0
revEvents += b.nav.reverseCount
ridgeWithdraws += b.nav.ridgeWithdrawEvents
detours += b.route.detourCount
shots += b.fire.shotCount
errSum += b.fire.shotErrSumDeg
maxErr = max(maxErr, b.fire.maxShotErrDeg)
let avgErr = shots > 0 ? (errSum / float(shots)) : 0.0
let jams = wto_world_active_jams(g_world, now)
let nm1 = pathfinder_is_loaded_ex(1)
print("WTO_AI|STAT|t={now}|bots={active}|roles={roles[0]}/{roles[1]}/{roles[2]}/{roles[3]}/{roles[4]}|crowd={g_crowd.crowdedBotCount}|maxClust={g_crowd.maxClusterSize}|minAllyD={g_crowd.minPairDistM}|dispNow={dispNow}|dispEv={g_crowd.disperseEvents}|ridgeNow={ridgeNow}|ridgeRevEv={ridgeWithdraws}|eng={engNow}|revNow={revNow}|revEv={revEvents}|jams={jams}|detours={detours}|shots={shots}|avgAimErr={avgErr}|maxAimErr={maxErr}|routeB={routeB}|caps={g_world.capCount}|nm1={nm1}\n")
wto_log_bot_and_cap_samples()
[es(tag=server, on_event=(EventMissionUnload, EventEntityManagerBeforeClear, EventEcsMissionLoaded))]
def wto_ai_mission_reset_es(evt : Event)
wto_reset_runtime()
[es(tag=server, on_event=(EventOnUnitDead, EventUnitBecameDead))]
def wto_ai_unit_dead_es(evt : Event; eid : EntityId; unit__ref : UnitRef)
let u = unit__ref.unit
if u != null && as_gm(u) != null && (int(u.army) == 1 || int(u.army) == 2)
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)]
def wto_ai_unit_damage_es(evt : EventUnitDamage; eid : EntityId)
if !wto_any_layer_active() || !WTO_AI_LAYER_TACTICS
return
let idx = wto_find_slot(eid)
let offEid = evt.offender.eid
if idx < 0 || !offEid || offEid == eid
return
let offUnitIdx = wto_world_find_unit(g_world, offEid)
if offUnitIdx >= 0 && g_world.units[offUnitIdx].army == g_bots[idx].army
return
g_bots[idx].perceive.lastDamagedAt = float(get_app().usefulTotalTime)
g_bots[idx].perceive.lastOffenderEid = offEid
[es(tag=server, before=wto_ai_pipeline_es)]
def wto_ai_world_tick_es(act : UpdateStageInfoAct)
if !wto_any_layer_active() || !is_app_in_flight()
return
if abs(act.curTime - g_last_world_frame) <= 1.0e-4
return
g_last_world_frame = act.curTime
let now = float(get_app().usefulTotalTime)
if g_world.valid && now < g_world.time - 1.0
wto_reset_runtime()
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
g_next_macro_at = now + WTO_AI_MACRO_TICK_SEC
wto_macro_update_all(g_world, now, g_bots)
if WTO_AI_DIAG_TELEMETRY && now >= g_next_diag_at
g_next_diag_at = now + WTO_AI_DIAG_INTERVAL_SEC
wto_log_telemetry(now)
[es(tag=server, no_order)]
def wto_ai_pipeline_es(act : UpdateStageInfoAct; eid : EntityId; var unit__ref : UnitRef&;
unit__className : string = "")
if !wto_any_layer_active() || !g_world.valid
return
var unit = unit__ref.unit
if unit == null || !unit.isAlive || unit.isDelayed || unit.isHumanPlayer
return
let army = int(unit.army)
let gm = as_gm(unit)
if (army != 1 && army != 2) || gm == null || gm.moveSys == null || as_hv(unit) == null
return
let now = float(get_app().usefulTotalTime)
let idx = wto_acquire_slot(eid, army, now)
if idx < 0
return
assume slot = g_bots[idx]
if !slot.prepared
slot.prepared = true
wto_specs_load(eid, unit__className, unit, gm, slot.spec)
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)
wto_fire_update(g_world, unit, now, act.dt, slot)
[es(tag=server, after=wto_ai_pipeline_es)]
def wto_ai_phys_tick_es(act : UpdateStageInfoAct; eid : EntityId; var unit__ref : UnitRef&;
unit__missionName : string; var vehicle_net_phys : VehiclePhysActor&)
if !wto_any_layer_active() || !WTO_AI_LAYER_NAVIGATION || WTO_AI_NAV_DRIVE_MODE != "vehicle_phys"
return
var unit = unit__ref.unit
if unit == null || !unit.isAlive || unit.isDelayed || unit.isHumanPlayer || vehicle_net_phys.physEdit == null
return
let idx = wto_find_slot(eid)
if idx < 0 || g_bots[idx].drive.routeAFallback
return
if !g_bots[idx].drive.routeBOwned && !wto_phys_slot_available()
return
let now = float(get_app().usefulTotalTime)
wto_drive_step_phys(unit, unit__missionName, as_gm(unit), vehicle_net_phys, act.dt, now, g_bots[idx])