- Capture unit_mgr, agent_unit_idx, unit_count, and unit_array inside driveToPos::update (patches/native/local_mp_ground_ai.jsonc) so local_mp_ground_ai.c short-circuits stock movement only for MPlayer slot bots (userId < -1) and leaves non-MPlayer mission wave AI (such as Ground Assault enemy tanks) on stock mission control. - Return customBots/wtoPveMode from wtoGroundAiScriptReady so ground AI scripts in Assault PvE take over only Team A friendly defenders (army 1) and synthesize a friendly defense anchor from the mission safe_zone area. Verified: MSVC wto_ws.dll build (0 project warnings), .37 check-patches (14/14), .37/.64 native module resolution (18/18), and temporary game-copy build packing 55 ground AI modules; in-game acceptance pending.
252 lines
11 KiB
Plaintext
252 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_nav_types_common
|
|
require %appGame.wtoai.wto_ai_specs_common
|
|
require %appGame.wtoai.wto_ai_world_common
|
|
require %appGame.wtoai.wto_ai_world_refresh_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_ammo_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
|
|
require %appGame.wtoai.wto_ai_trace_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
|
|
var private g_next_trace_at : float = 0.0
|
|
var private g_trace_due : bool = false
|
|
var private g_pve_mode : bool = false
|
|
|
|
// 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()
|
|
g_pve_mode = 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
|
|
g_next_trace_at = 0.0
|
|
g_trace_due = false
|
|
g_pve_mode = false
|
|
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()
|
|
g_pve_mode = 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_trace_death(now, eid, int(u.army), u.pos)
|
|
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_trace_hit(float(get_app().usefulTotalTime), eid, offEid, evt.pos, evt.damage, evt.damageTypeId, hpDropped)
|
|
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
|
|
g_pve_mode = wto_drive_announce_script_ready()
|
|
g_world.pveMode = g_pve_mode
|
|
print("WTO_AI|RUNTIME_READY|pve={g_pve_mode ? 1 : 0}\n")
|
|
wto_world_refresh(now, wto_trace_is_due(now, g_next_trace_at), 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)
|
|
g_trace_due = wto_trace_tick(g_world, now, g_bots, g_next_trace_at)
|
|
wto_stats_tick(now, g_stats)
|
|
|
|
[es(tag=server, before=wto_ai_pipeline_es)]
|
|
def wto_ai_ammo_sample_es(act : UpdateStageInfoAct; eid : EntityId; unit__ref : UnitRef;
|
|
ammo_stowage__ammoState : uint; ammo_stowage__ammoTypeRemap : IPoint4List;
|
|
ammo_stowage__weaponsIds : IntList; ammo_slots__ammoMaskAndType : UInt32List;
|
|
ammo_slots__numAmmo : IPoint2List)
|
|
if act.dt <= 0.0 || !g_world.valid || !WTO_AI_LAYER_FIRE_CONTROL
|
|
return
|
|
let index = wto_slots_find(g_bots, eid)
|
|
let u = unit__ref.unit
|
|
if index < 0 || u == null || u.weap == null || g_world.time < g_bots[index].nextTacticsAt
|
|
return
|
|
wto_ammo_sample(u, ammo_stowage__ammoState, ammo_stowage__ammoTypeRemap, ammo_stowage__weaponsIds,
|
|
ammo_slots__ammoMaskAndType, ammo_slots__numAmmo, g_bots[index].fire)
|
|
|
|
[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)
|
|
// In Ground Assault PvE, only friendly defenders (army 1) use custom AI; enemy waves keep stock AI.
|
|
if (army != 1 && army != 2) || (g_pve_mode && army != 1) || 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_fire_sample_sight(unit, slot.fire)
|
|
wto_perceive_update(g_world, now, WTO_AI_TACTICS_TICK_SEC, unit.pos, 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)
|
|
if g_trace_due
|
|
wto_trace_bot(now, 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" || 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])
|