Files
SirStone e53690036b 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.
2026-09-20 22:47:26 +02:00

36 lines
1.4 KiB
Nim

## Averaged-lead gun: mean prediction from linear, circular, and wall-bounce guns.
## Smooths noise when enemy movement is between analytical models.
import gun_harness/gun_interface
import guns/linear
import guns/circular
import guns/wall_bounce
type AveragedLeadGun* = object
linear: LinearGun
circular: CircularGun
wallBounce: WallBounceGun
debugGraphics*: bool
proc initAveragedLeadGun*(): AveragedLeadGun =
AveragedLeadGun(wallBounce: initWallBounceGun(), debugGraphics: false)
proc predict*(g: var AveragedLeadGun, state: WorldState, bulletSpeed: float): GunPrediction =
# 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)
var px = (lp.x + cp.x + wp.x) / 3.0
var py = (lp.y + cp.y + wp.y) / 3.0
px = clamp(px, BotRadius, state.arenaWidth - BotRadius)
py = clamp(py, BotRadius, state.arenaHeight - BotRadius)
GunPrediction(x: px, y: py)
proc onResult*(g: var AveragedLeadGun, e: FeedbackEvent) =
discard # analytical average — no learning