Files
SirRoboGarage/common_libs/guns/displacement.nim
T
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

71 lines
2.7 KiB
Nim

## Displacement-vector gun: predict using average velocity over a sliding window.
## Captures drift/tendency that instantaneous velocity misses
## (e.g. oscillating enemy slowly drifting east has v=0 at midpoints but
## displacement still pointing east).
import std/math
import gun_harness/gun_interface
const WindowSize = 15 # ticks; N+1 frames stored
type
DisplacementGun* = object
posX: array[WindowSize + 1, float]
posY: array[WindowSize + 1, float]
count: int # frames collected so far
head: int # ring-buffer head
# Per-tick derived state. The ring must advance exactly ONCE per tick and the
# average per-tick velocity is speed-independent, so both are computed once
# per tick and shared by all four power bins. The iterative bullet lead is
# recomputed from (dx, dy) on every call.
derivedTick: int
dx, dy: float
ready: bool
debugGraphics*: bool
proc predict*(g: var DisplacementGun, state: WorldState, bulletSpeed: float): GunPrediction =
# Sample the enemy position exactly once per tick (the harness calls predict()
# 4-5x/tick, once per power bin). Keying the old cache on bulletSpeed too made
# every bin miss, so the nominal 15-tick window was actually advanced ~4x/tick.
if state.tick != g.derivedTick:
g.derivedTick = state.tick
# Push current position into ring buffer
g.head = (g.head + 1) mod (WindowSize + 1)
g.posX[g.head] = state.enemyX
g.posY[g.head] = state.enemyY
if g.count < WindowSize + 1:
inc g.count
# Need at least N+1 frames; fall back to head-on if not enough
if g.count < WindowSize + 1:
g.ready = false
else:
g.ready = true
# Oldest frame is (head + 1) mod (WindowSize + 1)
let oldest = (g.head + 1) mod (WindowSize + 1)
g.dx = (state.enemyX - g.posX[oldest]) / WindowSize.float
g.dy = (state.enemyY - g.posY[oldest]) / WindowSize.float
if not g.ready:
return GunPrediction(x: state.enemyX, y: state.enemyY)
# Iterate time estimate 5 times
let dist0 = hypot(state.enemyX - state.selfX, state.enemyY - state.selfY)
var ticks = dist0 / bulletSpeed
var px = state.enemyX
var py = state.enemyY
for _ in 0..4:
px = state.enemyX + g.dx * ticks
py = state.enemyY + g.dy * ticks
ticks = hypot(px - state.selfX, py - state.selfY) / bulletSpeed
px = clamp(px, 0.0, state.arenaWidth)
py = clamp(py, 0.0, state.arenaHeight)
GunPrediction(x: px, y: py)
proc onResult*(g: var DisplacementGun, e: FeedbackEvent) =
discard # analytical gun — no learning
proc initDisplacementGun*(): DisplacementGun =
DisplacementGun(count: 0, head: 0, derivedTick: -1, debugGraphics: false)