Files
WT-Offline/game-ai/ground/wto_ai_runtime.das
T
N0VAN0M4LY 0843fde6ff fix(ground-ai): respect mounted gun traverse limits
Read the selected gun mount limits from the vehicle data and constrain its aiming commands. Send out-of-traverse aim requests to the chassis driver and require alignment before firing.

Verified type checks, executable patch checks and builds on 2.61.0.37 and 2.61.0.64, and guarded daScript initialization on 2.61.0.64. In-battle acceptance remains pending.
2026-12-05 18:36:19 +08:00

291 lines
11 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_lanes_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_lanes : WtoTeamLanes
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_next_rally_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
// Custom ground AI must only run inside a real multiplayer battle. Single-player test
// drives also dispatch tag=server ECS, but their target vehicles must never be taken over.
def private wto_any_layer_active() : bool
if !WTO_AI_ENABLED || !get_app().isMultiplayerGame
return false
return WTO_AI_LAYER_MACRO || WTO_AI_LAYER_TACTICS || WTO_AI_LAYER_NAVIGATION || WTO_AI_LAYER_FIRE_CONTROL
[init]
def wto_ai_script_init()
wto_drive_announce_script_ready()
[never_inline]
def private wto_reset_runtime()
wto_world_reset(g_world)
wto_lanes_reset(g_lanes)
g_crowd = WtoCrowdSummary()
g_last_world_frame = -1.0
g_next_cmd_at = 0.0
g_next_rally_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 - 0.01
g_bots[freeIdx].nextNavAt = now - 0.01
g_bots[freeIdx].route.nextReplanAt = now - 0.01
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()
wto_drive_announce_script_ready()
[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
wto_drive_announce_script_ready()
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
wto_lanes_reset_rallies(g_lanes)
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_next_rally_at = now + 1.0
g_cmd_dirty = false
wto_commander_update_all(g_world, now, zoneChanged, g_lanes, g_bots)
elif WTO_AI_LAYER_MACRO && now >= g_next_rally_at
g_next_rally_at = now + 1.0
wto_lanes_update_rallies(g_world, 1, now, g_lanes, g_bots)
wto_lanes_update_rallies(g_world, 2, now, g_lanes, g_bots)
for i in range(WTO_MAX_BOTS)
assume b = g_bots[i]
if b.used && b.order.valid && b.order.rallyHold && b.order.capIndex >= 0
let sk = wto_lanes_sub_key(b.army, b.order.capIndex, b.order.laneId)
if g_lanes.rallyReleased[sk]
b.order.rallyHold = false
if b.order.role == WTO_CMD_FLANK && b.order.laneId > 0
let ck = wto_lanes_cap_key(b.army, b.order.capIndex)
b.order.routeRelayValid = g_lanes.flankValid[ck * 2 + b.order.laneId - 1]
b.order.routeRelayPos = g_lanes.flankRelay[ck * 2 + b.order.laneId - 1]
else
b.order.routeRelayValid = false
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)
wto_fire_update(g_world, unit, unit__className, now, act.dt, slot, g_fire_budget)
if now >= slot.nextNavAt
slot.nextNavAt = now + WTO_AI_NAV_TICK_SEC
wto_nav_update(g_world, now, unit.pos, unit.unitTm[0], 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
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])