feat(ground-ai): log battle statistics for comparing builds

The decision layer rework is judged against a baseline, so the
runtime now accumulates per vehicle role and logs WTO_AI|BATTLE_STATS
every diagnostics.statsLogIntervalSec and when the battle ends:

- time from acquiring a ground target to the first shot at it, and
  targets let go without a shot;
- time with a target in sight within range and the gun ready, split by
  the last reason a shot was refused (WTO_AI|BATTLE_STATS_FIRE);
- shots by distance band and how many were fired on the move, and the
  autocannon rate;
- every statsSampleSec, the nearest enemy in line of sight within
  statsSampleRangeM: whether the bot knows it, how far the turret is
  off it, and whether a tank or tank destroyer shows it its rear
  (statsRearArcDeg).

The five diagnostics settings go through the server reader, the
generated script constants and the launcher fields in both languages.
This commit is contained in:
N0VAN0M4LY committed 2026-12-05 22:49:31 +08:00
1 parent 77e4806ffc
commit 4136021c8c
10 files changed
+279 -3

No files matched your search

@@ -799,7 +799,12 @@
},
"diagnostics": {
"telemetry": "Enable AI telemetry logs",
"telemetryIntervalSec": "Telemetry logging interval"
"telemetryIntervalSec": "Telemetry logging interval",
"battleStats": "Log battle statistics for comparing builds",
"statsLogIntervalSec": "Battle statistics logging interval",
"statsSampleSec": "Statistics sampling interval per bot",
"statsSampleRangeM": "Range of the nearest-enemy statistics sample",
"statsRearArcDeg": "Rear arc counted as showing the rear"
}
},
"shared": "These settings apply to all saves in this client.",
@@ -799,7 +799,12 @@
},
"diagnostics": {
"telemetry": "输出 AI 诊断遥测日志",
"telemetryIntervalSec": "遥测日志输出间隔"
"telemetryIntervalSec": "遥测日志输出间隔",
"battleStats": "输出用于版本对比的每局统计",
"statsLogIntervalSec": "每局统计的输出间隔",
"statsSampleSec": "每辆车的统计抽查间隔",
"statsSampleRangeM": "统计抽查最近敌人的范围",
"statsRearArcDeg": "算作车尾对敌的后方扇区角度"
}
},
"shared": "这些数值适用于此客户端的全部存档。",
@@ -375,6 +375,11 @@ export const BOT_TUNING_GROUPS: readonly BotTuningGroup[] = [
num('cadence', 'navTickSec', 0.05, 2, 's'),
{ id: 'groundAi.diagnostics.telemetry', section: 'diagnostics', field: 'telemetry', kind: 'bool' },
num('diagnostics', 'telemetryIntervalSec', 0.5, 60, 's'),
{ id: 'groundAi.diagnostics.battleStats', section: 'diagnostics', field: 'battleStats', kind: 'bool' },
num('diagnostics', 'statsLogIntervalSec', 10, 600, 's'),
num('diagnostics', 'statsSampleSec', 0.2, 5, 's'),
num('diagnostics', 'statsSampleRangeM', 50, 1000, 'm'),
num('diagnostics', 'statsRearArcDeg', 30, 180, '°'),
],
},
];
@@ -250,6 +250,11 @@ const GroundAiFireControlSchema = z.object({
const GroundAiDiagnosticsSchema = z.object({
telemetry: z.boolean(),
telemetryIntervalSec: z.number().min(0.5).max(60),
battleStats: z.boolean(),
statsLogIntervalSec: z.number().min(10).max(600),
statsSampleSec: z.number().min(0.2).max(5),
statsSampleRangeM: z.number().min(50).max(1000),
statsRearArcDeg: z.number().min(30).max(180),
});
export const GroundAiTuningSchema = z.object({
@@ -61,6 +61,11 @@ export function readGroundAiTuning(reader: TuningReader): GroundAiTuning {
diagnostics: {
telemetry: diag.bool('telemetry'),
telemetryIntervalSec: diag.number('telemetryIntervalSec', { min: 0.5, max: 60 }),
battleStats: diag.bool('battleStats'),
statsLogIntervalSec: diag.number('statsLogIntervalSec', { min: 10, max: 600 }),
statsSampleSec: diag.number('statsSampleSec', { min: 0.2, max: 5 }),
statsSampleRangeM: diag.number('statsSampleRangeM', { min: 50, max: 1000 }),
statsRearArcDeg: diag.number('statsRearArcDeg', { min: 30, max: 180 }),
},
};
}
+10 -1
View File
@@ -343,6 +343,15 @@
"diagnostics": {
"telemetry": true,
"telemetryIntervalSec": 5.0
"telemetryIntervalSec": 5.0,
// Battle statistics for comparing builds (WTO_AI|BATTLE_STATS): cumulative per vehicle class,
// logged every statsLogIntervalSec and when the battle ends. Every statsSampleSec each bot
// checks the nearest enemy in line of sight within statsSampleRangeM: whether it knows it,
// how far the turret is off it, and whether its rear (statsRearArcDeg wide) faces it.
"battleStats": true,
"statsLogIntervalSec": 60.0,
"statsSampleSec": 1.0,
"statsSampleRangeM": 300.0,
"statsRearArcDeg": 120.0
}
}
+5
View File
@@ -39,6 +39,7 @@ 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
var private @safe_when_uninitialized g_world : WtoWorldSnap
var private @safe_when_uninitialized g_crowd : WtoCrowdSummary
@@ -52,6 +53,7 @@ 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.
@@ -66,6 +68,7 @@ def wto_ai_script_init()
[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()
@@ -240,6 +243,7 @@ def wto_ai_world_tick_es(act : UpdateStageInfoAct)
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&;
@@ -272,6 +276,7 @@ def wto_ai_pipeline_es(act : UpdateStageInfoAct; eid : EntityId; var unit__ref :
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&;
+216
View File
@@ -0,0 +1,216 @@
// Battle statistics for comparing builds (decision layer design, section 12). Cumulative per
// vehicle role and logged as WTO_AI|BATTLE_STATS every statsLogIntervalSec and when the battle
// ends: time from acquiring a target to the first shot at it, time with a target in sight and the
// gun ready split by the last reason a shot was refused, shots by distance band and motion, and a
// sample every statsSampleSec of the nearest enemy in line of sight (known or not, turret error,
// rear facing it).
options indenting = 2
options no_global_heap = true
module wto_ai_stats_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_combat_types_common
require %appGame.wtoai.wto_ai_types_common
require %appGame.wtoai.wto_ai_sight_common
// Arrays of five are indexed by WTO_ROLE_*; bands are within closeRangeM, within midRangeM and
// beyond; denySec is indexed by WTO_DENY_*.
[safe_when_uninitialized]
struct public WtoBattleStats
active : bool = false
nextLogAt : float = 0.0
spotted : int[5]
spotShot : int[5]
spotShotSec : float[5]
spotNoShot : int[5]
windowSec : float[5]
samples : int[5]
unaware : int[5]
turretSamples : int[5]
turretErrSum : float[5]
rearSamples : int[5]
rearExposed : int[5]
shots : int[5]
movingShots : int[5]
bandShots : int[3]
bandMoving : int[3]
denySec : float[13]
acWindowSec : float = 0.0
acShots : int = 0
def private wto_stats_role(slot : WtoBotSlot const&) : int
return clamp(slot.spec.role, 0, 4)
def private wto_stats_role_name(role : int) : string
if role == WTO_ROLE_LIGHT
return "light"
if role == WTO_ROLE_HEAVY
return "heavy"
if role == WTO_ROLE_TD
return "td"
if role == WTO_ROLE_SPAA
return "spaa"
return "medium"
def private wto_stats_ratio(part, whole : int) : float
return whole > 0 ? float(part) / float(whole) : 0.0
def private wto_stats_band(dist : float) : int
if dist <= WTO_AI_FC_CLOSE_RANGE_M
return 0
return dist <= WTO_AI_FC_MID_RANGE_M ? 1 : 2
// A target acquired (seen clearly) and whether it was shot at before the bot let it go.
[never_inline]
def private wto_stats_spot(now : float; groundTgt : bool; var slot : WtoBotSlot&; var bs : WtoBattleStats&)
assume st = slot.stats
let role = wto_stats_role(slot)
if st.spotPending && slot.fire.shotCount > st.spotShots
st.spotPending = false
bs.spotShot[role]++
bs.spotShotSec[role] += now - st.spotAt
let tgt = slot.perceive.targetEid
if groundTgt && tgt != st.spotEid
if st.spotPending
bs.spotNoShot[role]++
st.spotEid = tgt
st.spotAt = now
st.spotShots = slot.fire.shotCount
st.spotPending = true
bs.spotted[role]++
elif !tgt
if st.spotPending
bs.spotNoShot[role]++
st.spotPending = false
st.spotEid = INVALID_ENTITY_ID
[never_inline]
def private wto_stats_shots(myPos : float3; var slot : WtoBotSlot&; var bs : WtoBattleStats&)
assume st = slot.stats
let fired = slot.fire.shotCount - st.lastShots
st.lastShots = slot.fire.shotCount
if fired <= 0
return
let role = wto_stats_role(slot)
let band = wto_stats_band(sqrt(wto_xz_dist_sq(myPos, slot.perceive.knownPos)))
bs.shots[role] += fired
bs.bandShots[band] += fired
if abs(slot.drive.lastVFwd) >= WTO_AI_PF_STATIONARY_MPS
bs.movingShots[role] += fired
bs.bandMoving[band] += fired
if slot.spec.isAutocannonBelt
bs.acShots += fired
// A shooting window: a ground target seen clearly within engagement range and the gun ready.
[never_inline]
def private wto_stats_window(dt : float; myPos : float3; groundTgt : bool; slot : WtoBotSlot const&;
var bs : WtoBattleStats&)
if !groundTgt || !slot.fire.gunReady || slot.fire.reloading
return
if wto_xz_dist_sq(myPos, slot.perceive.knownPos) > WTO_AI_FC_MAX_RANGE_M * WTO_AI_FC_MAX_RANGE_M
return
bs.windowSec[wto_stats_role(slot)] += dt
bs.denySec[clamp(slot.fire.lastDenyReason, 0, 12)] += dt
if slot.spec.isAutocannonBelt
bs.acWindowSec += dt
// The nearest enemy ground vehicle within statsSampleRangeM if it is in line of sight, else -1.
[never_inline]
def private wto_stats_nearest_seen(world : WtoWorldSnap const&; myPos : float3; slot : WtoBotSlot const&) : int
var best = -1
var bestDistSq = WTO_AI_STATS_RANGE_M * WTO_AI_STATS_RANGE_M
for i in range(world.unitCount)
assume u = world.units[i]
if !u.isAlive || !u.isGround || u.army == slot.army
continue
let distSq = wto_xz_dist_sq(myPos, u.pos)
if distSq < bestDistSq
bestDistSq = distSq
best = i
if best < 0
return -1
let eye = myPos + float3(0.0, 2.15, 0.0)
assume enemy = world.units[best]
return wto_sight_visual_clear(world, slot.eid, enemy.eid, eye, enemy.pos + float3(0.0, 1.3, 0.0)) == WTO_BLOCK_NONE ? best : -1
def private wto_stats_knows(slot : WtoBotSlot const&; eid : EntityId) : bool
if slot.perceive.targetEid == eid
return true
for k in range(WTO_MAX_EXPOSURE_TRACKS)
if slot.perceive.expEids[k] == eid && slot.perceive.expScore[k] >= 0.5
return true
return false
[never_inline]
def private wto_stats_sample(world : WtoWorldSnap const&; now : float; myPos, hullFwd : float3;
var slot : WtoBotSlot&; var bs : WtoBattleStats&)
if now < slot.stats.nextSampleAt
return
slot.stats.nextSampleAt = now + WTO_AI_STATS_SAMPLE_SEC
let idx = wto_stats_nearest_seen(world, myPos, slot)
if idx < 0
return
assume u = world.units[idx]
let role = wto_stats_role(slot)
let fwd = wto_xz_normalize(hullFwd, float3(1.0, 0.0, 0.0))
let toEnemy = wto_xz_normalize(u.pos - myPos, fwd)
bs.samples[role]++
if !wto_stats_knows(slot, u.eid)
bs.unaware[role]++
if slot.fire.servoInit
let yaw = atan2(fwd.z, fwd.x) - slot.fire.curYawDeg * WTO_DEG_TO_RAD
let gunDir = float3(cos(yaw), 0.0, sin(yaw))
bs.turretSamples[role]++
bs.turretErrSum[role] += acos(clamp(dot(gunDir, toEnemy), -1.0, 1.0)) * WTO_RAD_TO_DEG
if role == WTO_ROLE_MEDIUM || role == WTO_ROLE_HEAVY || role == WTO_ROLE_TD
bs.rearSamples[role]++
if acos(clamp(dot(-fwd, toEnemy), -1.0, 1.0)) * WTO_RAD_TO_DEG <= WTO_AI_STATS_REAR_ARC_DEG * 0.5
bs.rearExposed[role]++
// Per frame and bot, after fire control.
[never_inline]
def public wto_stats_bot_frame(world : WtoWorldSnap const&; now, dt : float; myPos, hullFwd : float3;
var slot : WtoBotSlot&; var bs : WtoBattleStats&)
if !WTO_AI_STATS_ENABLED
return
bs.active = true
let groundTgt = slot.perceive.hasClearLos && !!slot.perceive.targetEid && !slot.perceive.targetIsAir
wto_stats_spot(now, groundTgt, slot, bs)
wto_stats_shots(myPos, slot, bs)
wto_stats_window(dt, myPos, groundTgt, slot, bs)
wto_stats_sample(world, now, myPos, hullFwd, slot, bs)
[never_inline]
def private wto_stats_log_role(now : float; why : string; role : int; bs : WtoBattleStats const&)
let spotAvg = bs.spotShot[role] > 0 ? bs.spotShotSec[role] / float(bs.spotShot[role]) : 0.0
let turretErr = bs.turretSamples[role] > 0 ? bs.turretErrSum[role] / float(bs.turretSamples[role]) : 0.0
print("WTO_AI|BATTLE_STATS|why={why}|t={now}|class={wto_stats_role_name(role)}|spotted={bs.spotted[role]}|spotToShotSec={spotAvg}|spotShot={bs.spotShot[role]}|spotNoShot={bs.spotNoShot[role]}|windowSec={bs.windowSec[role]}|samples={bs.samples[role]}|unaware={wto_stats_ratio(bs.unaware[role], bs.samples[role])}|turretErrDeg={turretErr}|rear={wto_stats_ratio(bs.rearExposed[role], bs.rearSamples[role])}|shots={bs.shots[role]}|moving={wto_stats_ratio(bs.movingShots[role], bs.shots[role])}\n")
[never_inline]
def private wto_stats_log(now : float; why : string; bs : WtoBattleStats const&)
for role in range(5)
if bs.spotted[role] + bs.samples[role] + bs.shots[role] > 0
wto_stats_log_role(now, why, role, bs)
assume d = bs.denySec
print("WTO_AI|BATTLE_STATS_FIRE|why={why}|t={now}|shotsBand={bs.bandShots[0]}/{bs.bandShots[1]}/{bs.bandShots[2]}|movingBand={bs.bandMoving[0]}/{bs.bandMoving[1]}/{bs.bandMoving[2]}|denySec=ok:{d[0]},noTarget:{d[1]},notReady:{d[2]},speed:{d[3]},longWindow:{d[4]},aimErr:{d[5]},settle:{d[6]},budget:{d[7]},muzzle:{d[8]},arc:{d[9]},friendly:{d[10]},multiGun:{d[11]},traverse:{d[12]}|acWindowSec={bs.acWindowSec}|acShots={bs.acShots}\n")
// Per world tick: the cumulative statistics every statsLogIntervalSec.
[never_inline]
def public wto_stats_tick(now : float; var bs : WtoBattleStats&)
if !WTO_AI_STATS_ENABLED || !bs.active
return
if bs.nextLogAt <= 0.0
bs.nextLogAt = now + WTO_AI_STATS_LOG_SEC
elif now >= bs.nextLogAt
bs.nextLogAt = now + WTO_AI_STATS_LOG_SEC
wto_stats_log(now, "periodic", bs)
// Battle over (or a new one loading): the final statistics, then a clean slate.
[never_inline]
def public wto_stats_battle_end(now : float; var bs : WtoBattleStats&)
if WTO_AI_STATS_ENABLED && bs.active
wto_stats_log(now, "end", bs)
bs = WtoBattleStats()
+11
View File
@@ -221,6 +221,16 @@ struct public WtoDriveState
prevStr : float = 0.0
lastVFwd : float = 0.0
// Per-bot bookkeeping of the battle statistics (wto_ai_stats_common.das).
[safe_when_uninitialized]
struct public WtoBotStats
spotEid : EntityId = INVALID_ENTITY_ID
spotAt : float = -100.0
spotShots : int = 0
spotPending : bool = false
lastShots : int = 0
nextSampleAt : float = 0.0
[safe_when_uninitialized]
struct public WtoBotSlot
used : bool = false
@@ -240,3 +250,4 @@ struct public WtoBotSlot
driveCmd : WtoDriveCmd = WtoDriveCmd()
drive : WtoDriveState = WtoDriveState()
fire : WtoFireState = WtoFireState()
stats : WtoBotStats = WtoBotStats()
@@ -263,6 +263,11 @@ export interface GroundAiConfig {
readonly diagnostics: {
readonly telemetry: boolean;
readonly telemetryIntervalSec: number;
readonly battleStats: boolean;
readonly statsLogIntervalSec: number;
readonly statsSampleSec: number;
readonly statsSampleRangeM: number;
readonly statsRearArcDeg: number;
};
}
@@ -444,6 +449,11 @@ function collectConfigEntries(c: GroundAiConfig): readonly DasEntry[] {
['WTO_AI_FC_MAX_CHECKS_PER_TICK', 'int', c.fireControl.maxPreFireChecksPerTick],
['WTO_AI_DIAG_TELEMETRY', 'bool', c.diagnostics.telemetry],
['WTO_AI_DIAG_INTERVAL_SEC', 'float', c.diagnostics.telemetryIntervalSec],
['WTO_AI_STATS_ENABLED', 'bool', c.diagnostics.battleStats],
['WTO_AI_STATS_LOG_SEC', 'float', c.diagnostics.statsLogIntervalSec],
['WTO_AI_STATS_SAMPLE_SEC', 'float', c.diagnostics.statsSampleSec],
['WTO_AI_STATS_RANGE_M', 'float', c.diagnostics.statsSampleRangeM],
['WTO_AI_STATS_REAR_ARC_DEG', 'float', c.diagnostics.statsRearArcDeg],
];
}