gun belief: bound dead reckoning, reject untrustworthy velocity samples

Cap dead-reckoning lead at MaxDeadReckonTicks (freeze at the last scan past
it) and return (0,0) from targetVelocity when the two scans it averages span
more than MaxVelGap ticks. computeGun now uses gunAimErrorDeg.
This commit is contained in:
2026-10-06 19:54:25 +02:00
parent eb7f750a3f
commit 2fd2388f32
2 changed files with 26 additions and 10 deletions
@@ -40,6 +40,13 @@ const EnemyStaleTicks* = 60 ## drop a scanned enemy not rescanned for this m
## ticks (~1s at 60Hz): a bot seen once would keep
## painting its heat in the field forever
const MaxDeadReckonTicks* = 4 ## How far a constant-velocity guess may be
## extrapolated past the last scan. The radar
## lock re-scans almost every tick, so this only
## bites when the target is genuinely lost: past
## it, freeze at the last seen position rather
## than let old vx/vy run the aim away.
# A/B switch, compile time: `-d:baselineGun` builds the geometric-only gun, the
# default build is the ELM gun. Both go through the same predictAim() call, so
# the two binaries differ by exactly the learned correction and nothing else —
@@ -144,9 +151,11 @@ method buildBelief(bot: DevControlBot, power: float): GunBelief =
result.winTick[k] = winTick[k]
result.vx = vx
result.vy = vy
# dead-reckon: last seen position advanced by the estimated velocity
result.ex = lastScan.x + vx * age
result.ey = lastScan.y + vy * age
# dead-reckon: last seen position advanced by the estimated velocity, but no
# farther than a constant-velocity guess stays credible (see the const).
let lead = min(age, float(MaxDeadReckonTicks))
result.ex = lastScan.x + vx * lead
result.ey = lastScan.y + vy * lead
result.power = power
result.bulletSpeed = 20.0 - 3.0 * power
result.gunHeat = getGunHeat()
@@ -184,11 +193,11 @@ proc computeGun(bot: DevControlBot) =
echo "[gun] t=", bot.tick, " self=(", getX(), ",", getY(), ") lastScan=(", lastScan.x, ",", lastScan.y,
") age=", scanAge(scanTick), ") aimDeg=", radToDeg(aim.aim),
" corr=", aim.correction, " gunDeg=", getGunDirection()
let err = normalizeRelativeAngle(radToDeg(aim.aim) - getGunDirection())
# `err` is DEGREES: normalizeRelativeAngle returns degrees and the API speaks
# degrees. Comparing it against degToRad(FireSlopDeg) meant "within 0.052 deg",
# not 3 deg - the gun withheld fire long after it was on target. Compare in the
# same unit.
let err = gunAimErrorDeg(aim.aim, getGunDirection())
# `err` is DEGREES: gunAimErrorDeg is the single rad->deg conversion point and
# its name carries the unit, so `<= FireSlopDeg` can only compare degrees with
# degrees. (The old code compared a degree error against degToRad(FireSlopDeg)
# i.e. 0.052 deg of slop, and withheld fire far past the target.)
if abs(err) <= FireSlopDeg:
# Park the gun: the pending correction from the previous tick is applied
# AFTER firing (TurnProcessor fires before executeBotIntents), so it would