## j177 aim capture — the two records that make the aim error ATTRIBUTABLE. ## ## j176 could not answer "why is the aim 11.9 deg off at 450+ px" because the ## corpus has neither the bot's own belief (staleness was an INVERSE problem, ## unidentifiable) nor the gun id (a good gun's contribution was ## indistinguishable from a bad one's). Both are cheap to log and impossible ## to recover later. This module builds the two JSON records; ModularBot.nim ## calls it and the lines go into the EXISTING world-state capture ## (`TR_RECORD_WORLDSTATE` file), so the offline tooling sees one stream. ## ## It is deliberately PURE (no bot API, no env reads): the bot passes plain ## floats, the offline guard test passes the recorded fixture, and both go ## through the SAME row builders — so a field that the test proves present is ## a field the live bot writes. ## ## Key convention: `e*` = the enemy, `s*` = us, `b*` = the enemy's PREVIOUS ## belief in the tracker (before this scan's update), `lst` = the tick that ## state came from. `age = tick - blst` is the scan PARITY, recorded rather ## than inferred. import std/[json, os, math] const ScanRecordKey* = "aim_scan" ## wrapper key, sibling of "meta"/"end" FireRecordKey* = "aim_fire" type AimScan* = object ## One `onScannedBot` event, as the bot saw it BEFORE the update. tick*: int eid*: int ex*, ey*: float ## raw scanned values eh*, es*: float ## scanned heading (= direction) and speed ee*: float sx*, sy*: float ## our state at the scan (the FIRE SITE) sh*, ss*: float gun*: int ## bot.currentGun — the gun that fired, not the rack slot bx*, by*: float ## previous belief, BEFORE this scan's update bh*, bs*: float blst*: int ## previous lastSeenTick; -1 = never scanned before rlock*: bool ## radar lock engaged (false = the melee radar) rdir*: float ## 36 deg scan window centre (radar heading) lbear*: float ## bearing the lock is chasing (believed target) boff*: float ## scanned bearing - rdir: where in the window it landed AimFire* = object ## One firing decision, as the model computed it. tick*: int eid*: int gun*: int ## bot.currentGun = the gun that fired power*: float aim*: float ## the raw angle handed to setFire/turret turret*: float ## getGunDirection() at the command terr*: float ## signed turret error (aim - turret) heat*: float ## getGunHeat() BEFORE firing ax*, ay*: float ## the intercept the gun predicted tof*: float ## implied time of flight, ticks ex*, ey*: float ## the WorldState the predictor CONSUMED eh*, es*: float ee*: float sx*, sy*: float lst*: int ## which tick that enemy state came from (scan parity) proc bearing*(x, y, fx, fy: float): float = arctan2(y - fy, x - fx).radToDeg proc scanRow*(s: AimScan): JsonNode = ## The `aim_scan` line. Every field is unconditional: a capture that ## silently omits a field is worse than no capture. result = newJObject() result[ScanRecordKey] = newJObject() let b = result[ScanRecordKey] b["tick"] = %s.tick b["eid"] = %s.eid b["ex"] = %s.ex b["ey"] = %s.ey b["eh"] = %s.eh b["es"] = %s.es b["ee"] = %s.ee b["sx"] = %s.sx b["sy"] = %s.sy b["sh"] = %s.sh b["ss"] = %s.ss b["gun"] = %s.gun b["bx"] = %s.bx b["by"] = %s.by b["bh"] = %s.bh b["bs"] = %s.bs b["blst"] = %s.blst b["age"] = %(if s.blst >= 0: s.tick - s.blst else: -1) b["rlock"] = %s.rlock b["rdir"] = %s.rdir b["lbear"] = %s.lbear b["boff"] = %s.boff proc fireRow*(f: AimFire): JsonNode = ## The `aim_fire` line. result = newJObject() result[FireRecordKey] = newJObject() let b = result[FireRecordKey] b["tick"] = %f.tick b["eid"] = %f.eid b["gun"] = %f.gun b["power"] = %f.power b["aim"] = %f.aim b["turret"] = %f.turret b["terr"] = %f.terr b["heat"] = %f.heat b["ax"] = %f.ax b["ay"] = %f.ay b["tof"] = %f.tof b["ex"] = %f.ex b["ey"] = %f.ey b["eh"] = %f.eh b["es"] = %f.es b["ee"] = %f.ee b["sx"] = %f.sx b["sy"] = %f.sy b["lst"] = %f.lst proc appendLine*(path: string, row: JsonNode) = ## Append one JSONL line, fully guarded: a full disk or a bad path must ## never take the bot down (same contract as the existing recorders). try: let f = open(path, fmAppend) f.writeLine($row) f.close() except CatchableError: discard