Files
WT-Offline/game-ai/ground/wto_ai_runtime.das
T
N0VAN0M4LY 1590a2fea9 feat(ground-ai): layered perception, threat table and turret channel
First step of the single-bot decision layer. Not yet checked in game:
the decision layer is coupled, so it is tested once its hull and fire
parts are in.

Perception is layered: the gun sight covers perception.gunFovHalfDeg
either side of the barrel; within nearAwareM the crew notices enemies
around the hull except a rear blind arc, which a periodic look-around
opens; within midAwareM the commander covers the hull's front half.
Near and mid layers take longer to see an enemy clearly. Each track
remembers when and where its enemy was last in sight.

Each bot keeps a threat table of the enemies it has discovered, rated
by distance, whether seen clearly, whether aiming at it or recently
hitting it, and whether it is in their sight. Penetration is left out
until the engine damage test is usable.

The turret no longer rests on the hull's front: without a clearly seen
target it looks at the most dangerous discovered threat, then the last
exit, the remembered target, cues, and otherwise sweeps across the
route. A threat clearly more dangerous than the engaged target takes
the turret first.

Bot slot bookkeeping moves out of the runtime entry into
wto_ai_slots_common.das, and battle statistics count an enemy as known
when it is in the threat table.

Tuning: perception gains nine settings and loses frontConeHalfDeg and
peripheralCloseM, tactics loses visionFovDeg and peripheralRangeM, and
the new threat and turret sections are wired through the server
reader, generated constants and launcher fields in both languages.
2026-12-06 00:03:55 +08:00

221 lines
8.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_nav_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_lanes_build_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_route_common
require %appGame.wtoai.wto_ai_nav_common
require %appGame.wtoai.wto_ai_navmesh_common
require %appGame.wtoai.wto_ai_drive_common
require %appGame.wtoai.wto_ai_fire_common
require %appGame.wtoai.wto_ai_diag_common
require %appGame.wtoai.wto_ai_stats_common
require %appGame.wtoai.wto_ai_slots_common
require %appGame.wtoai.wto_ai_threat_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
var private @safe_when_uninitialized g_navmesh : WtoNavmeshBattle
var private @safe_when_uninitialized g_stats : WtoBattleStats
// 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_stats_battle_end(float(get_app().usefulTotalTime), g_stats)
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
wto_navmesh_battle_reset(g_navmesh)
wto_slots_reset_all(g_bots)
[es(tag=server, on_event=(EventMissionUnload, EventEntityManagerBeforeClear, EventEcsMissionLoaded))]
def wto_ai_mission_reset_es(evt : Event)
wto_reset_runtime()
wto_drive_announce_script_ready()
// Battle loading: read the navigation pack in the background before the first world tick.
[es(tag=server, on_event=EventEcsMissionLoaded)]
def wto_ai_mission_loaded_es(evt : Event)
if wto_any_layer_active() && WTO_AI_LAYER_NAVIGATION
wto_navmesh_prefetch()
[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)
let now = float(get_app().usefulTotalTime)
wto_world_record_death(g_world, eid, int(u.army), u.pos, now)
wto_route_report_wreck(g_navmesh.state, now, u.pos, g_bots)
if wto_slots_forget(g_bots, eid)
g_cmd_dirty = true
[es(tag=server, on_disappear, on_event=EventUnitRespawn)]
def wto_ai_unit_cleanup_es(evt : Event; eid : EntityId)
if wto_slots_forget(g_bots, eid)
g_cmd_dirty = true
[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 hpDropped = uint(evt.hpFixed) < uint(evt.prevHpFixed)
wto_slots_on_damage(g_world, g_bots, eid, offEid, u, evt.pos, hpDropped, float(get_app().usefulTotalTime))
[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()
wto_slots_drop_stale(g_bots, now)
if !g_world.valid
wto_drive_announce_script_ready()
print("WTO_AI|RUNTIME_READY\n")
wto_world_refresh(now, g_world)
if WTO_AI_LAYER_NAVIGATION
wto_navmesh_battle_tick(g_world, now, g_navmesh)
g_lanes.navState = g_navmesh.state
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_tick_rallies(g_world, now, g_lanes, 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)
wto_navmesh_log_stats(g_navmesh)
wto_stats_tick(now, g_stats)
[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)
var created = false
let idx = wto_slots_acquire(g_bots, eid, army, now, created)
g_cmd_dirty = g_cmd_dirty || created
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_threat_update(g_world, now, unit.pos, 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)
// A failed pack with navEngine.allowFallback off leaves every bot standing for the battle.
if now >= slot.nextNavAt && g_navmesh.state != WTO_NAVMESH_FAILED
slot.nextNavAt = now + WTO_AI_NAV_TICK_SEC
wto_nav_update(g_world, g_navmesh.state, idx, now, unit.pos, unit.unitTm[0], slot)
wto_stats_bot_frame(g_world, now, act.dt, unit.pos, unit.unitTm[0], slot, g_stats)
[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" || g_navmesh.state == WTO_NAVMESH_FAILED
return
var unit = unit__ref.unit
if unit == null || !unit.isAlive || unit.isDelayed || unit.isHumanPlayer || vehicle_net_phys.physEdit == null
return
let idx = wto_slots_find(g_bots, eid)
if idx < 0
return
if !g_bots[idx].drive.routeBOwned && !wto_slots_phys_available(g_bots)
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])