DevControlBot: reset online model on GameStarted, feed delayed labels

This commit is contained in:
2026-10-06 19:37:38 +02:00
parent a62391d400
commit 2e6735a8bd
@@ -46,6 +46,10 @@ 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)
# Online-start variant: default = neutral (zero output weights).
# `-d:elmPrior` = weak pooled prior (pretrained beta scaled by 0.1).
const OnlinePriorScale* = (when defined(elmPrior): 0.1 else: 0.0)
const FireSlopDeg = 1.0'f64 ## fire inside this much gun error, in
## DEGREES (the API's unit).
##
@@ -68,7 +72,7 @@ type DevControlBot* = ref object of Bot
proc newDevControlBot*(): DevControlBot =
result = DevControlBot()
result.mover = initTFIL() # movement is programmed -> body/turret/gun BLACK
result.gun = initElmGun() # the gun loads its exported weights here, once
result.gun = initElmGun(priorScale = OnlinePriorScale) # hidden layer loaded, online
setRadarColor(WHITE) # overwritten by initRadar() before the first tick
method onScannedBot*(bot: DevControlBot, e: ScannedBotEvent) =
@@ -77,9 +81,9 @@ method onScannedBot*(bot: DevControlBot, e: ScannedBotEvent) =
# scanned bot's identity)
# ...and the full history the gun needs: this tick, the position, and where WE
# were when we saw it (own displacement since the scan is a model feature).
onScanFull(bot.tick, e.x, e.y, getX(), getY())
# ...and remembered for the movement: the heat field paints every enemy it
# knows about. One entry per id, refreshed in place, oldest dropped past the cap.
onScanFull(bot.tick, e.x, e.y, getX(), getY()) # history the gun needs
if bot.gun.online:
bot.gun.noteEnemyAt(bot.tick, e.x, e.y) # delayed-label trace
for i, ei in bot.enemies:
if ei.id == e.scannedBotId:
bot.enemies[i].x = e.x
@@ -103,6 +107,13 @@ method onHitByBullet*(bot: DevControlBot, e: HitByBulletEvent) =
method onBulletHit*(bot: DevControlBot, e: BulletHitBotEvent) =
bot.mover.noteDamageDealt(e.damage)
method onGameStarted*(bot: DevControlBot, e: GameStartedEventForBot) =
## A new game = a new opponent. This is the ONLY reset point for the online
## model: within a battle the same opponent fights round after round with the
## same movement code, so RoundStartedEvent deliberately does NOT touch it.
## (The API fires onGameStarted once per battle and onRoundStarted per round.)
bot.gun.resetOnline()
method onRoundEnded*(bot: DevControlBot, e: RoundEndedEventForBot) =
inc bot.roundNo
bot.enemies = @[]
@@ -159,7 +170,10 @@ method buildWorldState(bot: DevControlBot): WorldState =
## fire only when the gun is both COOLED (getGunHeat() == 0) and ON the aim, and
## do it before go() so the shot is part of this tick's intent.
proc computeGun(bot: DevControlBot) =
if bot.gun.online:
bot.gun.drainPending(bot.tick) # label any shot whose flight time elapsed
let belief = bot.buildBelief(FirePower)
let feat = buildFeatures(belief)
let aim = bot.gun.predictAim(belief, FirePower, UseElm)
let baseAim = bot.gun.heuristicAim(belief, FirePower)
if aim.aim.isNaN:
@@ -187,19 +201,27 @@ proc computeGun(bot: DevControlBot) =
# the server on every shot, desyncing tick/scan bookkeeping from the game.
discard setFire(FirePower)
inc bot.shotsFired
if bot.gun.online:
# Enqueue a delayed label: the true impact position is only knowable
# once tof ticks of flight have passed (keyed by dueTick).
let d = hypot(belief.ex - belief.selfX, belief.ey - belief.selfY)
let tofTicks = max(1, int(round(d / belief.bulletSpeed)))
bot.gun.recordShot(feat, baseAim, belief.selfX, belief.selfY,
bot.tick + tofTicks)
when defined(gunLog):
# One line per shot: what the gun THOUGHT. The truth (the aim error it
# actually made) is computed offline from the observer recording, which
# is the only place ground truth exists. Format:
# round,tick,predCorrection,baseAimDeg,finalAimDeg,gunDeg
# One line per shot. Columns:
# round,tick,predCorrection,baseAimDeg,finalAimDeg,gunDeg,
# nOnline,gateOpen,modelErr,zeroErr,<32 features>
let path = getEnv("ELM_GUN_LOG", "")
if path.len > 0:
let f = open(path, fmAppend)
defer: f.close()
f.write($bot.roundNo & "," & $bot.tick & "," & $aim.correction & "," &
$(radToDeg(baseAim)) & "," & $(radToDeg(aim.aim)) & "," &
$(getGunDirection()) & "," &
buildFeatures(belief).mapIt($it).join(" ") & "\n")
$(getGunDirection()) & "," & $bot.gun.nOnline & "," &
(if bot.gun.gateOpen: "1" else: "0") & "," &
$bot.gun.modelErr & "," & $bot.gun.zeroErr & "," &
feat.mapIt($it).join(" ") & "\n")
else:
setGunTurnRate(err)