## Tier A tests for the CHIMERA twin gun (chimera/aimer.nim, spec G8 / M7). ## Pure Nim, no Java. Run: from ChimeraBot_garage, `nimble test`. import std/[math, os, strutils] import chimera/dsl import chimera/twinsim import chimera/residual import chimera/aimer var failed = 0 proc check(name: string, cond: bool, detail: string = "") = if cond: echo " ok ", name else: echo " FAIL ", name, " [", detail, "]"; inc failed let kernels = allKernels() proc origin(x = 200.0, y = 300.0, dir = 0.0, sp = 6.0): ReplayTick = var r = ReplayTick(tick: 100, ex: x, ey: y, edir: dir, espeed: sp, eenergy: 80.0, ox: 600.0, oy: 300.0, odir: 180.0, ospeed: 4.0, lastTurn: 0.0, lastSpeed: sp) r.weights = newSeq[float64](kernels.len) for i in 0 ..< kernels.len: r.weights[i] = 1.0 / float64(kernels.len) r.mems = newSeq[KernelMem](kernels.len) for i in 0 ..< kernels.len: r.mems[i] = defaultMemory(kernels[i].defaultParams) r block: echo "aimer 1: flight time = dist / (20 - 3*power)" check("power 3.0", abs(flightTime(110.0, 3.0) - 10.0) < 1e-9, $flightTime(110.0, 3.0)) check("power 0.1", abs(flightTime(197.0, 0.1) - 10.0) < 1e-9, $flightTime(197.0, 0.1)) check("power 1.0", abs(flightTime(17.0, 1.0) - 1.0) < 1e-9, $flightTime(17.0, 1.0)) check("tolerance widens as range shrinks", angularTolerance(100.0) > angularTolerance(500.0)) check("tolerance = 2*atan2(r, dist)", abs(angularTolerance(300.0) - 2.0 * arctan2(BotRadius, 300.0)) < 1e-12) block: echo "aimer 2: bucket indexing" check("h=1 -> bucket 0", bucketIndex(1.0) == 0) check("h=7 -> bucket 0", bucketIndex(7.0) == 0) check("h=8 -> bucket 1", bucketIndex(8.0) == 1) check("h=18 -> bucket 2", bucketIndex(18.0) == 2) check("h=36 -> bucket 3", bucketIndex(36.0) == 3) check("h=200 clamps to last", bucketIndex(200.0) == 3) block: echo "aimer 3: max pairwise spread" let s0 = maxPairwiseSpread([(100.0, 0.0), (100.0, 0.0)], [0.5, 0.5], 0.0, 0.0, 4.0) check("co-located kernels have zero spread", s0 == 0.0, $s0) let s1 = maxPairwiseSpread([(100.0, 100.0), (100.0, -100.0)], [0.9, 0.1], 0.0, 0.0, 4.0) check("opposite bearings give a nonzero spread", s1 > 1.0, $s1) check("top-k weight ranking is used", maxPairwiseSpread([(100.0, 100.0), (100.0, -100.0)], [0.1, 0.9], 0.0, 0.0, 1.0) == 0.0) block: echo "aimer 4: power monotone non-increasing in spread" let base = [0.002, 0.004, 0.006, 0.008, 0.010, 0.012, 0.014] let tol = angularTolerance(300.0) var prev = 99.0 var monotone = true for mult in [0.1, 0.5, 1.0, 2.0, 5.0, 20.0, 100.0]: var spreads: seq[float64] for b in base: spreads.add b * mult let p = selectPowerFromSpreads(spreads, tol) if p > prev + 1e-12: monotone = false prev = p check("chosen power never rises as spread scales up", monotone) check("spread far below tolerance picks the max power", selectPowerFromSpreads([1e-6, 1e-6, 1e-6, 1e-6, 1e-6, 1e-6, 1e-6], tol) == 3.0) check("spread far above tolerance picks the min power", selectPowerFromSpreads([9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0], tol) == 0.1) block: echo "aimer 5: iterated flight time converges (hostile dodger)" let a = initAimer() let rp = ResidualPrediction() let aim = a.computeAim(kernels, origin(), rp, 600.0, 300.0, 800.0, 600.0, 2.0) check("aim is active", aim.active) let hCheck = flightTime(aim.dist, aim.power) check("h == flightTime(dist, power) after convergence", abs(hCheck - aim.horizon) < 0.5, $hCheck & " vs " & $aim.horizon) check("aim point inside arena", aim.x >= 0.0 and aim.x <= 800.0 and aim.y >= 0.0 and aim.y <= 600.0) check("chosen power is legal", aim.power in FirePowers, $aim.power) check("horizon in a plausible bullet-flight band (>= 5 ticks)", aim.horizon >= 5.0, $aim.horizon) let aim2 = a.computeAim(kernels, origin(), rp, 600.0, 300.0, 800.0, 600.0, 2.0) check("aim is deterministic", aim2.horizon == aim.horizon and aim2.power == aim.power and aim2.x == aim.x and aim2.y == aim.y) block: echo "aimer 6: arena clipping" let c = clipToArena(900.0, -30.0, 800.0, 600.0) check("x clipped to arena", c.x == 800.0 and c.y == 0.0) check("clipped flag set", c.clipped) let n = clipToArena(400.0, 300.0, 800.0, 600.0) check("inside point unchanged", not n.clipped and n.x == 400.0 and n.y == 300.0) block: echo "aimer 7: aim-bias corrector is deterministic and correct" proc feed(a: var Aimer) = var aim = TwinAim(active: true, bucket: 0, horizon: 10.0, dist: 100.0, enemySpeed: 1.0, selfX: 0.0, selfY: 0.0, aimBearing: 0.0, mixPt: (100.0, 0.0), resPt: (100.0, 0.0), persPt: (100.0, 0.0)) a.noteShot(aim, 100) a.resolve(110, 100.0, 10.0) # realized bearing = atan2(10,100) var a1 = initAimer() var a2 = initAimer() feed(a1) feed(a2) let expected = arctan2(10.0, 100.0) check("bias offset equals the observed miss", abs(a1.biasOffset(100.0, 1.0) - expected) < 1e-9, $a1.biasOffset(100.0, 1.0)) check("bias is deterministic across identical runs", a1.biasOffset(100.0, 1.0) == a2.biasOffset(100.0, 1.0)) check("bias is zero for an untouched segment", a1.biasOffset(700.0, 7.0) == 0.0) check("bias is clamped", abs(a1.biasOffset(100.0, 1.0)) <= MaxBias + 1e-9) block: echo "aimer 8: no-spray cooldown" check("tiny spread allows the fast cadence", cooldownTicks(0.001) == TinyCooldown) check("wide spread forces the conservative cadence", cooldownTicks(0.1) == NormalCooldown) check("conservative cadence is at least 8 ticks", NormalCooldown >= 8 and TinyCooldown < NormalCooldown) block: echo "aimer 9: horizon-bucketed hedge trains online" var a = initAimer() # Mixture/residual predict far away, persistence predicts the realized spot: # after enough scored shots the bucket must put most mass on persistence. for i in 0 ..< 40: let aim = TwinAim(active: true, bucket: 1, horizon: 12.0, dist: 200.0, enemySpeed: 2.0, selfX: 0.0, selfY: 0.0, aimBearing: 0.0, mixPt: (0.0, 0.0), resPt: (0.0, 0.0), persPt: (200.0, 0.0)) a.noteShot(aim, 0) a.resolve(12, 200.0, 0.0) let w = a.bucketWeights(1) check("persistence gains mass on consistent realized positions", w[2] > w[0] and w[2] > w[1], $w) check("bucket weights stay normalized", abs(w[0] + w[1] + w[2] - 1.0) < 1e-9) echo "" if failed == 0: echo "all aimer tests passed" else: echo failed, " aimer test(s) FAILED"; quit(1)