#!/usr/bin/env python3 """Extract aim-error labels from raw Tank Royale recordings (ND-JSON, gz). LABEL (documented choice) ------------------------- At the tick a bullet is fired by shooter S with gun bearing `g` (bullet .direction, degrees, = the true gun bearing at fire time) and power p (bullet speed v = p*10 units/tick, verified against consecutive bulletStates): 1. target T's TRUE position (Tx,Ty) and velocity (vx,vy) at the fire tick, both taken from the observer stream (ground truth a bot never gets). 2. d0 = |T - S|. Iterating once: t_f = d0 / v, then T' = T + (vx,vy)*t_f, re-derive d = |T' - S| and t_f = d / v (one fixed-point iteration, sufficient because movement is < tick length relative to d for d > ~100 units). 3. bearingIdeal = atan2(T'.y - S.y, T'.x - S.x) (radians) 4. label = wrap(bearingIdeal - radians(g)) into [-pi, pi] Positive label = shooter aimed too low/clockwise relative to the ideal intercept bearing, i.e. the correction that a perfect predictor would add. The samples are written to samples.ndjson, one JSON object per line. """ import glob, gzip, json, math, os, sys TAU = 2 * math.pi def wrap(a): while a > math.pi: a -= TAU while a < -math.pi: a += TAU return a def extract(path, tag, out): ticks = [] with gzip.open(path, "rt") as fh: for line in fh: line = line.strip() if not line: continue m = json.loads(line) if m.get("type") == "TickEventForObserver": ticks.append(m) pos = {} # (round, botId) -> (x, y) at previous tick lastScan = {} # (round, scannerId) -> last turnNumber it scanned the enemy names = {} n = 0 for i, m in enumerate(ticks): rnd = m["roundNumber"] for ev in m.get("events", []): if ev["type"] == "ScannedBotEvent": lastScan[(rnd, ev["scannedByBotId"])] = ev["turnNumber"] bs = {b["id"]: b for b in m["botStates"]} for bid, b in bs.items(): names[bid] = b["name"] fired = [e for e in m.get("events", []) if e["type"] == "BulletFiredEvent"] for e in fired: bu = e["bullet"] owner = bu["ownerId"] sb = bs.get(owner) if sb is None: continue tgt = [b for b in m["botStates"] if b["id"] != owner and b["energy"] > 0] if not tgt: continue tb = tgt[0] S = (sb["x"], sb["y"]) T = (tb["x"], tb["y"]) p0 = pos.get((rnd, tb["id"])) Tp = T if p0: vel = (T[0] - p0[0], T[1] - p0[1]) else: vel = (0.0, 0.0) v = bu["power"] * 10.0 d = math.dist(S, T) tf = d / v Tp = (T[0] + vel[0] * tf, T[1] + vel[1] * tf) d = math.dist(S, Tp) tf = d / v ideal = math.atan2(Tp[1] - S[1], Tp[0] - S[0]) g = math.radians(bu["direction"]) err = wrap(ideal - g) relb = wrap(ideal - math.radians(sb["direction"])) out.write(json.dumps({ "group": f"{tag}:{rnd}", "round": rnd, "tick": m["turnNumber"], "shooter": sb["name"], "target": tb["name"], "label": err, "dist": d, "relBearing": relb, "tgtSpeed": math.hypot(*vel), "tgtHeading": math.radians(tb["direction"]), "ourHeading": math.radians(sb["direction"]), "bulletSpeed": v, "power": bu["power"], "gunHeat": sb.get("gunHeat", 0.0), "sinceScan": float(m["turnNumber"] - lastScan.get((rnd, owner), 0)), }) + "\n") n += 1 for bid, b in bs.items(): pos[(rnd, bid)] = (b["x"], b["y"]) return n def main(): here = os.path.dirname(os.path.abspath(__file__)) repo = os.path.abspath(os.path.join(here, "..", "..")) dest = os.path.join(here, "samples.ndjson") total = 0 with open(dest, "w") as out: for tag, sub in (("devcontrol", "devcontrol"), ("oscillator", "oscillator")): for f in sorted(glob.glob(os.path.join(repo, "data", "drussgt_raw", sub, "*.battle.gz"))): k = extract(f, tag, out) print(f"{os.path.basename(f)}: {k} shots") total += k print("TOTAL", total, "->", dest) if __name__ == "__main__": main()