Files
WT-Offline/apps/ai-lab/src/Timeline.tsx
T
N0VAN0M4LY 5786fee63b feat(ground-ai): add a replay trace and the AI lab viewer
Battle behaviour could only be judged by watching one bot at a time. The
AI now logs a replay trace and a local viewer plays it back on the map.

Game scripts:
- wto_ai_trace_common.das writes every diagnostics.traceIntervalSec the
  capture zones (WTO_AI|TRACE_CAP), every ground unit including players
  (TRACE_U) and each bot's decision state (TRACE_B: order, behavior,
  targets, what the turret looks at and why, barrel heading, fire denial,
  hit probability), plus part damage with its offender (HIT) and deaths
  (DEATH). It uses no engine module so a simulator can load it as is.
- The turret state records the reason and world direction of each look.
- New tuning diagnostics.trace and diagnostics.traceIntervalSec through
  server reader, patcher constants, launcher schema, fields and labels.

packages/ground-sim: readers for the baked .wtonav pack (mesh polygons,
cover points, tactical grid) and .pois.json, the trace parser and the
names of the logged codes.

apps/ai-lab: Vite viewer with a local API that lists nav packs and lab
logs and decrypts a chosen .clog with wtotools. It draws the nav mesh,
vehicles, barrels and wished turret directions, goals, target links,
shots, hits and deaths over a timeline, with per-bot strips for behavior,
fire denial and turret reason. Paths live in an untracked lab.local.json.
The lab has its own tsconfig and stays out of the root typecheck.

Root and launcher typechecks pass; the lab typechecks and replayed two
lab battles.
2026-12-06 13:53:52 +08:00

87 lines
3.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* Playback controls and, for the selected bot, three strips over the whole battle: its behavior,
* the reason it did not fire, and why its turret pointed where it did. Click a strip to seek.
*/
import type { TraceBattle } from '@wto/ground-sim';
import { useEffect, useRef } from 'react';
import { BEHAVIOR_COLORS, DENY_COLORS, LOOK_COLORS } from './labels.ts';
interface Props {
readonly battle: TraceBattle;
readonly t: number;
readonly playing: boolean;
readonly speed: number;
readonly selected: string | undefined;
readonly onSeek: (t: number) => void;
readonly onPlaying: (playing: boolean) => void;
readonly onSpeed: (speed: number) => void;
}
const SPEEDS = [0.5, 1, 2, 4, 8, 16];
const STRIP_H = 10;
function drawStrips(canvas: HTMLCanvasElement, battle: TraceBattle, eid: string | undefined, t: number): void {
const ctx = canvas.getContext('2d');
if (ctx === null) return;
const w = canvas.width;
const span = Math.max(1, battle.endT - battle.startT);
ctx.fillStyle = '#16181a';
ctx.fillRect(0, 0, w, canvas.height);
if (eid !== undefined) {
const frames = battle.frames;
for (let i = 0; i < frames.length; i++) {
const f = frames[i];
const bot = f?.bots.get(eid);
if (f === undefined || bot === undefined) continue;
const x0 = ((f.t - battle.startT) / span) * w;
const x1 = (((frames[i + 1]?.t ?? f.t + 0.5) - battle.startT) / span) * w;
const bw = Math.max(1, x1 - x0);
ctx.fillStyle = BEHAVIOR_COLORS[bot.behavior] ?? '#444';
ctx.fillRect(x0, 0, bw, STRIP_H);
ctx.fillStyle = DENY_COLORS[bot.deny] ?? '#444';
ctx.fillRect(x0, STRIP_H + 2, bw, STRIP_H);
ctx.fillStyle = LOOK_COLORS[bot.lookReason] ?? '#444';
ctx.fillRect(x0, 2 * (STRIP_H + 2), bw, STRIP_H);
}
}
const cx = ((t - battle.startT) / span) * w;
ctx.fillStyle = '#fff';
ctx.fillRect(cx - 1, 0, 2, canvas.height);
}
export function Timeline({ battle, t, playing, speed, selected, onSeek, onPlaying, onSpeed }: Props): React.JSX.Element {
const stripRef = useRef<HTMLCanvasElement>(null);
useEffect(() => {
const c = stripRef.current;
if (c === null) return;
c.width = Math.max(1, Math.floor(c.getBoundingClientRect().width));
drawStrips(c, battle, selected, t);
}, [battle, selected, t]);
return (
<div className="timeline">
<div className="controls">
<button type="button" onClick={() => onPlaying(!playing)}>{playing ? '暂停' : '播放'}</button>
<select value={speed} onChange={(e) => onSpeed(Number(e.target.value))}>
{SPEEDS.map((s) => <option key={s} value={s}>{s}×</option>)}
</select>
<span className="time">{t.toFixed(1)} s / {battle.endT.toFixed(0)} s</span>
<input
type="range" min={battle.startT} max={battle.endT} step={0.1} value={t}
onChange={(e) => onSeek(Number(e.target.value))}
/>
</div>
<div className="strip-labels"><span>行为</span><span>拒绝开火</span><span>炮塔依据</span></div>
<canvas
ref={stripRef}
className="strips"
height={3 * (STRIP_H + 2)}
onClick={(e) => {
const w = e.currentTarget.width;
onSeek(battle.startT + (e.nativeEvent.offsetX / w) * (battle.endT - battle.startT));
}}
/>
</div>
);
}