fix(guns): speed-sensitive caches, dead stop-shot branch, exact TM trace pairing

Four guns cached a whole prediction per tick while predict() is called once
per power bin, so every bin after the first (and the real fired shot, which
shares lastState) reused the power-1.0 lead. Fixed by caching only the
speed-INDEPENDENT derived state and recomputing the lead per requested speed:
- stop_shot: also fixes prevSpeed being written before it was read, which
  made abs(speed) < abs(prev) permanently false and the entire
  stop-prediction branch unreachable (it was just Linear).
- displacement: the cache key included bulletSpeed, so the guard missed on
  all four bins and the 15-tick window advanced ~4x/tick, making the
  inferred velocity ~4x too small.
- averaged_lead: tick cache removed outright. pattern_matcher: split into
  speed-independent match+path and per-call lead.

FeedbackEvent gains fireTick/powerBin (additive; only virtual_bullets
constructs one) so guns can pair feedback to the exact shot instead of
guessing by coordinates. tsetlin uses it: traces are now keyed exactly by
(fireTick, powerBin) with a 1024-slot ring, and the 10-frame window shifts
at most once per tick (it was shifting ~4-5x/tick, so isWarmedUp tripped
after ~2 ticks).

KNOWN INCOMPLETE: tsetlin still does not diverge from Linear in battle. The
two named bugs are fixed (a 600-tick sim shows trainedShots=2141,
traceMisses=0, and a fixed-input probe converges to a 9.6px correction), but
the TM's clause feedback itself is broken: ~131 of 1740 literals end up
included per clause, so its conjunction never fires. Sweeping TM_S,
TM_N_CLAUSES and a two-branch Type-I update did not change the correction
from 0. Needs a real TM fix or removal, not another bug fix.

First-ever guard tests for the gun selector: common_libs/tests/
test_gun_harness.nim (14 checks, headless, no Java). There were none before,
which is how six broken guns survived a full analysis cycle. Against the
previous HEAD, 5 of these checks FAIL - that is the regression guard.
This commit is contained in:
2026-09-20 22:47:26 +02:00
parent 0cc682152d
commit e53690036b
9 changed files with 480 additions and 171 deletions
+5 -8
View File
@@ -10,17 +10,16 @@ type AveragedLeadGun* = object
linear: LinearGun
circular: CircularGun
wallBounce: WallBounceGun
cachedTick: int
cachedPred: GunPrediction # per-tick cache; ponytail: single cache, extend if multi-power needed
debugGraphics*: bool
proc initAveragedLeadGun*(): AveragedLeadGun =
AveragedLeadGun(wallBounce: initWallBounceGun(), debugGraphics: false)
proc predict*(g: var AveragedLeadGun, state: WorldState, bulletSpeed: float): GunPrediction =
if state.tick == g.cachedTick:
return g.cachedPred
# No tick cache: every sub-gun's lead depends on bulletSpeed (dist/bulletSpeed),
# so caching one result per tick and reusing it for all four power bins would
# silently collapse every bin onto the first. linear/wallBounce are stateless
# and cheap; circular only caches its speed-independent omega internally.
let lp = g.linear.predict(state, bulletSpeed)
let cp = g.circular.predict(state, bulletSpeed)
let wp = g.wallBounce.predict(state, bulletSpeed)
@@ -30,9 +29,7 @@ proc predict*(g: var AveragedLeadGun, state: WorldState, bulletSpeed: float): Gu
px = clamp(px, BotRadius, state.arenaWidth - BotRadius)
py = clamp(py, BotRadius, state.arenaHeight - BotRadius)
g.cachedTick = state.tick
g.cachedPred = GunPrediction(x: px, y: py)
g.cachedPred
GunPrediction(x: px, y: py)
proc onResult*(g: var AveragedLeadGun, e: FeedbackEvent) =
discard # analytical average — no learning