DevControlBot: fix gun plumbing — detach from body turn, non-blocking fire, degree slop

The gun turn math is written for setAdjustGunForBodyTurn(true) (absolute bearing
-> normalizeRelativeAngle(bearing - getGunDirection())), but the flag was never
set: the gun inherited the body's turn. Set the three adjust flags.

Two firing bugs found while measuring the geometric baseline against a stationary
SittingDuck:
- computeGun called the BLOCKING fire(), which calls go() itself, and the run
  loop called go() again => every shot double-stepped the server. Use setFire.
- the fire slop compared a degrees error against degToRad(FireSlopDeg) = 0.052
  deg. Compare in degrees; 1 deg (3 deg fired mid-slew, up to 3 deg off).

Baseline vs stationary: 110/112 hits, aim error sd 0.04 deg.
This commit is contained in:
2026-10-06 08:28:15 +02:00
parent dacb650692
commit a024a2e32b
@@ -46,10 +46,15 @@ const EnemyStaleTicks* = 60 ## drop a scanned enemy not rescanned for this m
# which is what makes the measured difference attributable.
const UseElm* = not defined(baselineGun)
const FireSlopDeg = 3.0'f64 ## fire inside this much gun error. The gun turns
## at most MAX_GUN_TURN_RATE, so a tighter slop
## just means waiting a tick for no gain: at any
## real range 3 deg is well under the hit radius.
const FireSlopDeg = 1.0'f64 ## fire inside this much gun error, in
## DEGREES (the API's unit).
##
## Was 3.0 but compared against radians
## (`degToRad(FireSlopDeg)` = 0.052 deg): fixing
## the unit made the gun fire mid-slew, up to
## 3 deg off, and cost hits. 1 deg is tight
## enough that the shot is on target while
## still firing the tick after a correction.
type DevControlBot* = ref object of Bot
mover: TfilModule
@@ -161,9 +166,21 @@ proc computeGun(bot: DevControlBot) =
") age=", scanAge(scanTick), ") aimDeg=", radToDeg(aim.aim),
" corr=", aim.correction, " gunDeg=", getGunDirection()
let err = normalizeRelativeAngle(radToDeg(aim.aim) - getGunDirection())
if abs(err) <= degToRad(FireSlopDeg):
# `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.
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
# only over-rotate the gun on the NEXT turn. Zeroing it keeps the gun on the
# aim point while it holds fire.
setGunTurnRate(0.0)
if getGunHeat() == 0.0:
fire(FirePower)
# Non-blocking. `fire()` is the BLOCKING variant: it calls go() itself, so
# the outer loop's go() ran a second, empty turn - the bot double-stepped
# the server on every shot, desyncing tick/scan bookkeeping from the game.
discard setFire(FirePower)
inc bot.shotsFired
when defined(gunLog):
# One line per shot: what the gun THOUGHT. The truth (the aim error it
@@ -178,11 +195,13 @@ proc computeGun(bot: DevControlBot) =
$(radToDeg(baseAim)) & "," & $(radToDeg(aim.aim)) & "," &
$(getGunDirection()) & "," &
buildFeatures(belief).mapIt($it).join(" ") & "\n")
setGunTurnRate(0.0)
else:
setGunTurnRate(err)
method run*(bot: DevControlBot) =
setAdjustGunForBodyTurn(true) # turret ignores body rotation
setAdjustRadarForBodyTurn(true) # radar ignores body rotation
setAdjustRadarForGunTurn(true) # radar ignores turret rotation too
initRadar() # once: radar + scan turn BLACK = programmed
while isRunning():
# One radar command and one movement command per tick, unconditionally,