Files
WT-Offline/game-ai/ground/wto_ai_commander_common.das
T
N0VAN0M4LY 7e5eb99696 feat(ground-ai): add task order, mode-aware commander v1, and world fixes
Replace the per-bot greedy cap scorer (wto_ai_macro_common.das) with a
whole-team commander and explicit task order contract, and fix the world,
spec, and navigation bugs found during the codebase audit:

- Classify ground battle modes (bttl, conq, dom) in multiplayer-missions
  v5 from mislist.blk categories (with suffix fallback for event maps) and
  emit wtoGroundMode in customRules/customBots.
- Add the commander section to tuning/ground-ai.jsonc with range and sum
  validation, compiled into WTO_AI_CMD_* constants.
- Split zone/mode/spawn refresh into wto_ai_world_zones_common.das and fix
  four world snapshot bugs: stop double-recording deaths in wreck scans
  and deduplicate with a ring cursor, refresh units before spawn fallback
  while retrying real respawn points, fall back battleCenter to spawns
  when capCount is 0, and relax cap radius clamping to 10..180 m.
- Fix limited-traverse yaw overwrite in wto_ai_specs_common.das and
  compute pivot turn angle against wishDir in wto_ai_nav_common.das.
- Introduce WtoTaskOrder (wto_ai_order_common.das) and whole-team
  commander v1 (wto_ai_commander_common.das +
  wto_ai_commander_anchors_common.das): mode-aware role quotas (Bttl,
  Conq, Dom), deterministic distance + vehicle-suitability matching,
  20-second role hysteresis, separate total-assigned vs cap-insider
  counters, frontline-relative SPAA anchors, EventZoneCaptured immediate
  rebalance, and per-team WTO_AI|CMD telemetry.

Verified root and launcher type checks, multiplayer-missions v5 dataset
extraction (1140 missions with Bttl/Conq/Dom classification), isolated
2.61.0.58 build (18 ground AI modules packed), and clean in-engine
daScript compilation/load (wto_ai_runtime.das loaded in 155 ms, 0 compile
errors). Live battle tuning and acceptance deferred until the full
pipeline is wired.
2026-12-03 17:20:17 +08:00

275 lines
12 KiB
Plaintext

// Whole-team commander (v1): mode-aware role quotas (Bttl / Conq / Dom), deterministic
// distance + vehicle-suitability matching, 20-second role hysteresis, separate total-assigned
// vs insider counters, and per-team assignment telemetry.
options indenting = 2
options no_global_heap = true
module wto_ai_commander_common shared
require ecs
require math
require %appGame.wtoai.wto_ai_config_common
require %appGame.wtoai.wto_ai_math_common
require %appGame.wtoai.wto_ai_order_common
require %appGame.wtoai.wto_ai_types_common
require %appGame.wtoai.wto_ai_world_common
require %appGame.wtoai.wto_ai_commander_anchors_common
[never_inline]
def private wto_cmd_push_demands(capIdx, role, urgency, count : int;
var dCap : int[64]&; var dRole : int[64]&; var dUrg : int[64]&;
var dCount : int&)
for _k in range(count)
if dCount >= WTO_MAX_BOTS
return
dCap[dCount] = capIdx
dRole[dCount] = role
dUrg[dCount] = urgency
dCount++
[never_inline]
def private wto_cmd_plan_bttl(world : WtoWorldSnap const&; army, n : int;
var dCap : int[64]&; var dRole : int[64]&; var dUrg : int[64]&;
var dCount : int&) : int
let spawnPt = world.teamSpawnValid[army] ? world.teamSpawnCenter[army] : world.battleCenter
var enemyCap = -1
var friendlyCap = -1
var bestEnemyD = 1.0e12
var bestFriendD = 1.0e12
for c in range(world.capCount)
let dSq = wto_xz_dist_sq(spawnPt, world.caps[c].center)
if world.caps[c].ownerArmy != army && dSq < bestEnemyD
bestEnemyD = dSq
enemyCap = c
elif world.caps[c].ownerArmy == army && dSq < bestFriendD
bestFriendD = dSq
friendlyCap = c
if enemyCap < 0
enemyCap = friendlyCap >= 0 ? (world.capCount > 1 ? (1 - friendlyCap) : friendlyCap) : 0
if friendlyCap < 0
friendlyCap = enemyCap
let nAssault = clamp((n * WTO_AI_CMD_BTTL_ASSAULT_PCT + 50) / 100, 1, n)
let rem1 = max(0, n - nAssault)
let nFlank = min(rem1, (n * WTO_AI_CMD_BTTL_FLANK_PCT + 50) / 100)
let rem2 = max(0, rem1 - nFlank)
let nOverwatch = min(rem2, (n * WTO_AI_CMD_BTTL_OVERWATCH_PCT + 50) / 100)
let nDefend = max(0, rem2 - nOverwatch)
let defUrg = world.caps[friendlyCap].contested ? WTO_URGENCY_HIGH : WTO_URGENCY_LOW
wto_cmd_push_demands(enemyCap, WTO_CMD_ASSAULT, WTO_URGENCY_HIGH, nAssault, dCap, dRole, dUrg, dCount)
wto_cmd_push_demands(enemyCap, WTO_CMD_FLANK, WTO_URGENCY_HIGH, nFlank, dCap, dRole, dUrg, dCount)
wto_cmd_push_demands(enemyCap, WTO_CMD_OVERWATCH, WTO_URGENCY_MED, nOverwatch, dCap, dRole, dUrg, dCount)
wto_cmd_push_demands(friendlyCap, WTO_CMD_DEFEND, defUrg, nDefend, dCap, dRole, dUrg, dCount)
return enemyCap
[never_inline]
def private wto_cmd_plan_conq(world : WtoWorldSnap const&; army, n : int;
var dCap : int[64]&; var dRole : int[64]&; var dUrg : int[64]&;
var dCount : int&) : int
if world.caps[0].ownerArmy != army
let nAssault = clamp((n * WTO_AI_CMD_CONQ_ASSAULT_PCT + 50) / 100, 1, n)
let rem1 = max(0, n - nAssault)
let nFlank = min(rem1, (n * WTO_AI_CMD_CONQ_FLANK_PCT + 50) / 100)
let nOverwatch = max(0, rem1 - nFlank)
wto_cmd_push_demands(0, WTO_CMD_ASSAULT, WTO_URGENCY_HIGH, nAssault, dCap, dRole, dUrg, dCount)
wto_cmd_push_demands(0, WTO_CMD_FLANK, WTO_URGENCY_HIGH, nFlank, dCap, dRole, dUrg, dCount)
wto_cmd_push_demands(0, WTO_CMD_OVERWATCH, WTO_URGENCY_MED, nOverwatch, dCap, dRole, dUrg, dCount)
else
let nDefend = min(n, (n * WTO_AI_CMD_CONQ_HOLD_DEFEND_PCT + 50) / 100)
let rem1 = max(0, n - nDefend)
let nOverwatch = min(rem1, (n * WTO_AI_CMD_CONQ_HOLD_OVERWATCH_PCT + 50) / 100)
let nFlank = max(0, rem1 - nOverwatch)
let defUrg = world.caps[0].contested ? WTO_URGENCY_HIGH : WTO_URGENCY_LOW
wto_cmd_push_demands(0, WTO_CMD_DEFEND, defUrg, nDefend, dCap, dRole, dUrg, dCount)
wto_cmd_push_demands(0, WTO_CMD_OVERWATCH, WTO_URGENCY_MED, nOverwatch, dCap, dRole, dUrg, dCount)
wto_cmd_push_demands(0, WTO_CMD_FLANK, WTO_URGENCY_MED, nFlank, dCap, dRole, dUrg, dCount)
return 0
[never_inline]
def private wto_cmd_plan_dom(world : WtoWorldSnap const&; army, n : int;
var dCap : int[64]&; var dRole : int[64]&; var dUrg : int[64]&;
var dCount : int&) : int
let spawnPt = world.teamSpawnValid[army] ? world.teamSpawnCenter[army] : world.battleCenter
var prio : int[2]
prio[0] = -1
prio[1] = -1
var prioCnt = 0
// Pick center-most non-friendly cap first, then nearest non-friendly cap to spawn.
var bestMidScore = 1.0e12
for c in range(world.capCount)
if world.caps[c].ownerArmy != army
let sc = wto_xz_dist_sq(world.caps[c].center, world.battleCenter)
if sc < bestMidScore
bestMidScore = sc
prio[0] = c
if prio[0] >= 0
prioCnt = 1
var bestNear = 1.0e12
for c in range(world.capCount)
if c != prio[0] && world.caps[c].ownerArmy != army
let sc = wto_xz_dist_sq(world.caps[c].center, spawnPt)
if sc < bestNear
bestNear = sc
prio[1] = c
if prio[1] >= 0
prioCnt = 2
var remaining = n
for c in range(world.capCount)
if world.caps[c].ownerArmy == army && remaining > (prioCnt > 0 ? 1 : 0)
let keep = min(WTO_AI_CMD_DOM_MAX_DEFENDERS, remaining - (prioCnt > 0 ? 1 : 0))
let urg = world.caps[c].contested ? WTO_URGENCY_HIGH : WTO_URGENCY_LOW
wto_cmd_push_demands(c, WTO_CMD_DEFEND, urg, keep, dCap, dRole, dUrg, dCount)
remaining -= keep
if prioCnt == 0
let perCap = max(1, remaining / max(1, world.capCount))
for c in range(world.capCount)
let take = c == world.capCount - 1 ? remaining : min(remaining, perCap)
let urg = world.caps[c].contested ? WTO_URGENCY_HIGH : WTO_URGENCY_LOW
wto_cmd_push_demands(c, WTO_CMD_OVERWATCH, urg, take, dCap, dRole, dUrg, dCount)
remaining -= take
return 0
let firstShare = prioCnt == 1 ? remaining : (remaining + 1) / 2
for p in range(prioCnt)
let share = p == 0 ? firstShare : (remaining - firstShare)
if share <= 0
continue
let nAssault = clamp((share * WTO_AI_CMD_DOM_ASSAULT_PCT + 50) / 100, 1, share)
let nOverwatch = max(0, share - nAssault)
wto_cmd_push_demands(prio[p], WTO_CMD_ASSAULT, WTO_URGENCY_HIGH, nAssault, dCap, dRole, dUrg, dCount)
wto_cmd_push_demands(prio[p], WTO_CMD_OVERWATCH, WTO_URGENCY_MED, nOverwatch, dCap, dRole, dUrg, dCount)
return prio[0]
[never_inline]
def private wto_cmd_apply_order(world : WtoWorldSnap const&; army : int; now : float;
capIdx, cmdRole, urgency : int;
var capInsiders : int[8]&; var capAssigned : int[8]&;
var slot : WtoBotSlot&)
assume ord = slot.order
let sameTask = ord.valid && ord.role == cmdRole && ord.capIndex == capIdx
let insideCircle = cmdRole == WTO_CMD_ASSAULT && capInsiders[capIdx] < WTO_AI_MACRO_CAP_MAX_INSIDERS
if insideCircle
capInsiders[capIdx]++
capAssigned[capIdx]++
ord.role = cmdRole
ord.capIndex = capIdx
ord.urgency = urgency
ord.maxStopRangeM = wto_order_max_stop_range(cmdRole, urgency, WTO_AI_FC_MAX_RANGE_M)
ord.overwatchOutside = !insideCircle
ord.cautious = wto_cmd_death_zone_kills(world, army, world.caps[capIdx].center, now) >= WTO_AI_MACRO_DEATH_ZONE_MIN_KILLS
ord.goalPos = wto_cmd_pick_role_anchor(world, army, slot.eid, capIdx, cmdRole, insideCircle)
if !sameTask
ord.assignedAt = now
ord.expiresAt = now + max(10.0, WTO_AI_CMD_ROLE_HOLD_SEC * 2.0)
ord.valid = true
[never_inline]
def private wto_cmd_match_and_log(world : WtoWorldSnap const&; army : int; now : float; forceRebalance : bool;
candCount : int; cands : int[64]; candPos : float3[64];
dCount : int; dCap : int[64]; dRole : int[64]; dUrg : int[64];
spaaCount : int; var bots : WtoBotSlot[64]&)
var botUsed : bool[64]
var demUsed : bool[64]
for i in range(WTO_MAX_BOTS)
botUsed[i] = false
demUsed[i] = false
var capInsiders : int[8]
var capAssigned : int[8]
for c in range(WTO_MAX_CAPS)
capInsiders[c] = 0
capAssigned[c] = 0
// Pass 1: honor 20s role hysteresis unless cap ownership/mode forced a rebalance.
if !forceRebalance
for ci in range(candCount)
let bIdx = cands[ci]
assume ord = bots[bIdx].order
if !ord.valid || (now - ord.assignedAt) >= WTO_AI_CMD_ROLE_HOLD_SEC || now >= ord.expiresAt
continue
for di in range(dCount)
if !demUsed[di] && dCap[di] == ord.capIndex && dRole[di] == ord.role
demUsed[di] = true
botUsed[ci] = true
wto_cmd_apply_order(world, army, now, dCap[di], dRole[di], dUrg[di], capInsiders, capAssigned, bots[bIdx])
break
// Pass 2: deterministic best suitability + distance match for remaining slots.
for di in range(dCount)
if demUsed[di]
continue
var bestCi = -1
var bestScore = 1.0e12
var bestEid = 0xffffffffu
let tgtPos = world.caps[dCap[di]].center
for ci in range(candCount)
if botUsed[ci]
continue
let bIdx = cands[ci]
let dist = sqrt(wto_xz_dist_sq(candPos[ci], tgtPos))
let sc = dist + wto_cmd_vehicle_penalty(bots[bIdx].spec.role, dRole[di])
let eidU = uint(bots[bIdx].eid)
if sc < bestScore - 0.1 || (abs(sc - bestScore) <= 0.1 && eidU < bestEid)
bestScore = sc
bestEid = eidU
bestCi = ci
if bestCi >= 0
demUsed[di] = true
botUsed[bestCi] = true
wto_cmd_apply_order(world, army, now, dCap[di], dRole[di], dUrg[di], capInsiders, capAssigned, bots[cands[bestCi]])
wto_cmd_log_summary(world, army, now, candCount, cands, candPos, capAssigned, capInsiders, spaaCount, bots)
[never_inline]
def private wto_cmd_assign_army(world : WtoWorldSnap const&; army : int; now : float;
forceRebalance : bool; var bots : WtoBotSlot[64]&)
var cands : int[64]
var candPos : float3[64]
var spaaIdx : int[64]
var spaaPos : float3[64]
var candCount = 0
var spaaCount = 0
for i in range(WTO_MAX_BOTS)
if !bots[i].used || bots[i].army != army
continue
let uIdx = wto_world_find_unit(world, bots[i].eid)
if uIdx < 0
continue
if bots[i].spec.role == WTO_ROLE_SPAA
spaaIdx[spaaCount] = i
spaaPos[spaaCount] = world.units[uIdx].pos
spaaCount++
else
cands[candCount] = i
candPos[candCount] = world.units[uIdx].pos
candCount++
if world.capCount <= 0
for ci in range(candCount)
assume ord = bots[cands[ci]].order
ord.role = WTO_CMD_ASSAULT
ord.capIndex = -1
ord.goalPos = wto_cmd_clamp_away_enemy_spawn(world, army, world.battleCenter)
ord.urgency = WTO_URGENCY_MED
ord.maxStopRangeM = 300.0
ord.assignedAt = now
ord.expiresAt = now + 30.0
ord.valid = true
for si in range(spaaCount)
wto_cmd_assign_spaa(world, now, spaaPos[si], world.battleCenter, bots[spaaIdx[si]])
return
var dCap : int[64]
var dRole : int[64]
var dUrg : int[64]
var dCount = 0
var primaryCap = 0
if candCount > 0
if world.battleMode == WTO_MODE_BTTL
primaryCap = wto_cmd_plan_bttl(world, army, candCount, dCap, dRole, dUrg, dCount)
elif world.battleMode == WTO_MODE_CONQ || world.capCount == 1
primaryCap = wto_cmd_plan_conq(world, army, candCount, dCap, dRole, dUrg, dCount)
else
primaryCap = wto_cmd_plan_dom(world, army, candCount, dCap, dRole, dUrg, dCount)
wto_cmd_match_and_log(world, army, now, forceRebalance, candCount, cands, candPos, dCount, dCap, dRole, dUrg, spaaCount, bots)
let frontPt = world.caps[clamp(primaryCap, 0, world.capCount - 1)].center
for si in range(spaaCount)
wto_cmd_assign_spaa(world, now, spaaPos[si], frontPt, bots[spaaIdx[si]])
[never_inline]
def public wto_commander_update_all(world : WtoWorldSnap const&; now : float;
forceRebalance : bool; var bots : WtoBotSlot[64]&)
wto_cmd_assign_army(world, 1, now, forceRebalance, bots)
wto_cmd_assign_army(world, 2, now, forceRebalance, bots)