Files
SirRoboGarage/common_libs/gun_harness/gun_interface.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

60 lines
1.8 KiB
Nim

## Gun harness — shared types and Gun concept.
## Coordinate system: 0° = East, CCW positive (Tank Royale standard).
## Bullet speed formula: 20 - 3 * power.
import std/math
const BotRadius* = 18.0 ## hit detection radius in px
type
EnemyInfo* = object
id*: int
x*, y*: float
heading*, speed*, energy*: float
WorldState* = object
## All raw data given to every gun every tick.
# Enemy (current TARGET)
enemyX*, enemyY*: float
enemySpeed*, enemyHeading*: float
# Self
selfX*, selfY*: float
selfSpeed*, selfHeading*: float
selfRadarHeading*: float
selfEnergy*: float
enemyEnergy*: float
# Arena
arenaWidth*, arenaHeight*: float
# Meta
tick*: int
# All alive enemies (for movement modules)
enemies*: seq[EnemyInfo]
GunPrediction* = object
## Absolute (x, y) where the gun predicts the enemy will be.
x*, y*: float
FeedbackEvent* = object
## Outcome of a resolved virtual bullet.
prediction*: GunPrediction
actualX*, actualY*: float ## actual enemy position at resolution time
bulletPower*: float
fireTick*: int ## tick the virtual bullet was spawned (predict time)
powerBin*: int ## index into PowerBins the bullet belongs to
missDistance*: float ## px; < BotRadius = hit
hit*: bool
proc bulletSpeed*(power: float): float {.inline.} =
20.0 - 3.0 * power
## Gun concept — any type T implementing these two procs is a valid gun.
template isGun*(T: typedesc): bool =
compiles(
block:
var g: T
let ws = WorldState()
let p: GunPrediction = g.predict(ws, 0.0)
let fe = FeedbackEvent()
g.onResult(fe)
)