Files
WT-Offline/game-ai/ground/wto_ai_runtime.das
T
N0VAN0M4LY 80113bdd2e feat(ground-ai): add cover reload, stalemate reposition, and damage repair
Complete the single-vehicle tactical loop for reload survival, stalemate
breaking, and vehicle damage repair:

- Add cover, reload, hull-angle, stalemate, reposition, and repair-cover
  parameters to tuning/ground-ai.jsonc, validated in ground-ai-tuning.ts
  and compiled into WTO_AI_SM_* constants.
- Add wto_ai_cover_common.das: continuously record the last hard-concealed
  hull position before peeking, and evaluate 9 cover candidates around
  the tank (with enemy-clearance scoring when searching for a repair
  cover).
- Add wto_ai_tactics_actions_common.das and wire wto_ai_tactics_common.das:
  - Damage retreat & repair (plan 6.1): when gun is inoperable while
    mobile, retreat to an enemy-free cover and send
    EventRepairSystemRequestRepair(repair=true) once safe; when engine or
    tracks immobilize the chassis, halt in place, request repair, and keep
    gun self-defense active; stay halted while
    repair_system_remaining_repair_time > 0.
  - 3-way reload action: pull back into remembered/nearby cover (up to 6 s
    budget, uninterrupted by flashes/new orders) -> zigzag advance at
    +/-18 deg when pushing without cover -> angle hull front +/-15 deg
    when side-exposed >30 deg with pivot room.
  - Stalemate reposition: confirm outgoing effective damage via
    EventUnitDamage (uint(hpFixed) < uint(prevHpFixed)) and enemy death;
    trigger lateral reposition after 2 no-effect shots, 2 no-shot halts,
    or 12 s cumulative halt time, unlocking short-halt only after 20 m
    progress or reaching the new position.
- Split periodic telemetry out of wto_ai_runtime.das into
  wto_ai_diag_common.das to keep every module under 300 lines.

Verified root and launcher type checks, isolated 2.61.0.58 build (24
ground AI modules packed), and clean in-engine daScript compilation/load
(wto_ai_runtime.das loaded in 228 ms, 0 compile errors). Live battle
acceptance deferred until the full pipeline is wired.
2026-12-03 18:14:43 +08:00

258 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_order_common
require %appGame.wtoai.wto_ai_combat_types_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_commander_common
require %appGame.wtoai.wto_ai_perceive_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
require %appGame.wtoai.wto_ai_diag_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_cmd_at : float = 0.0
var private g_cmd_dirty : bool = true
var private g_last_zone_ver : int = -1
var private g_fire_budget : int = 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_cmd_at = 0.0
g_cmd_dirty = true
g_last_zone_ver = -1
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()
wto_drive_release_all()
[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
g_cmd_dirty = true
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
[es(tag=server, on_event=(EventMissionUnload, EventEntityManagerBeforeClear, EventEcsMissionLoaded))]
def wto_ai_mission_reset_es(evt : Event)
wto_reset_runtime()
[es(tag=server)]
def wto_ai_zone_captured_es(evt : EventZoneCaptured)
if !wto_any_layer_active()
return
g_world.zoneVersion++
g_cmd_dirty = true
print("WTO_AI|ZONE_CAP|area={evt.areaId}|from={int(evt.wasArmy)}|to={int(evt.army)}\n")
[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, eid, 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()
g_cmd_dirty = true
for i in range(WTO_MAX_BOTS)
if g_bots[i].used && g_bots[i].tactics.engageTargetEid == eid
g_bots[i].tactics.engageShotsNoEffect = 0
g_bots[i].tactics.engageNoShotHalts = 0
g_bots[i].tactics.engageTotalHaltSec = 0.0
[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()
g_cmd_dirty = true
for i in range(WTO_MAX_BOTS)
if g_bots[i].used && g_bots[i].tactics.engageTargetEid == eid
g_bots[i].tactics.engageShotsNoEffect = 0
g_bots[i].tactics.engageNoShotHalts = 0
g_bots[i].tactics.engageTotalHaltSec = 0.0
[es(tag=server)]
def wto_ai_unit_damage_es(evt : EventUnitDamage; eid : EntityId; unit__ref : UnitRef)
if !wto_any_layer_active() || !WTO_AI_LAYER_TACTICS
return
let offEid = evt.offender.eid
let u = unit__ref.unit
if u == null || !offEid || offEid == eid
return
let now = float(get_app().usefulTotalTime)
let vicArmy = int(u.army)
// Outgoing effective damage confirmation for our firing bot (resets stalemate counters).
let offBotIdx = wto_find_slot(offEid)
if offBotIdx >= 0 && g_bots[offBotIdx].army != vicArmy && uint(evt.hpFixed) < uint(evt.prevHpFixed)
assume tac = g_bots[offBotIdx].tactics
if tac.engageTargetEid == eid || g_bots[offBotIdx].perceive.targetEid == eid
tac.engageShotsNoEffect = 0
tac.engageNoShotHalts = 0
tac.lastEffectAt = now
// Incoming hit direction cue for our damaged bot (never leaks hidden offender coords).
let idx = wto_find_slot(eid)
if idx < 0
return
let offUnitIdx = wto_world_find_unit(g_world, offEid)
if offUnitIdx >= 0 && g_world.units[offUnitIdx].army == g_bots[idx].army
return
let imp = evt.pos - u.pos
g_bots[idx].perceive.lastHitYawRad = (imp.x * imp.x + imp.z * imp.z) > 0.04 ? atan2(imp.z, imp.x) : atan2(u.unitTm[0].z, u.unitTm[0].x)
g_bots[idx].perceive.lastDamagedAt = now
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
g_fire_budget = WTO_AI_FC_MAX_CHECKS_PER_TICK
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)
let zoneChanged = g_world.zoneVersion != g_last_zone_ver
if zoneChanged
g_last_zone_ver = g_world.zoneVersion
g_cmd_dirty = true
if WTO_AI_LAYER_MACRO && (now >= g_next_cmd_at || g_cmd_dirty)
g_next_cmd_at = now + WTO_AI_CMD_REBALANCE_SEC
g_cmd_dirty = false
wto_commander_update_all(g_world, now, zoneChanged, g_bots)
if WTO_AI_DIAG_TELEMETRY && now >= g_next_diag_at
g_next_diag_at = now + WTO_AI_DIAG_INTERVAL_SEC
wto_diag_log_telemetry(g_world, now, g_bots, g_crowd)
[es(tag=server, no_order)]
def wto_ai_pipeline_es(act : UpdateStageInfoAct; eid : EntityId; var unit__ref : UnitRef&;
unit__className : string = "";
repair_system_remaining_repair_time : float = -1.0)
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
if WTO_AI_LAYER_TACTICS || WTO_AI_LAYER_FIRE_CONTROL
wto_perceive_update(g_world, now, WTO_AI_TACTICS_TICK_SEC, unit.pos, unit.unitTm[0], slot)
wto_tactics_update(g_world, now, WTO_AI_TACTICS_TICK_SEC, unit.pos, unit.unitTm[0], unit.isBurn, repair_system_remaining_repair_time, 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, g_fire_budget)
[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])