feat(ModularBot): 6 guns, pattern matcher, melee modules, adversarial bots
- New guns: guess-factor (GF histogram), pattern-matcher (movement tape replay) - New modules: minimum-risk melee movement, spinning melee radar - New test bots: PatternMover, RandomMover, WaveSurfer - Fixed: FeedbackEvent now carries actualX/actualY for proper GF learning - Fixed: TM gun warmup gating + directional residuals - Fixed: circular gun integrated formula + multi-bin omega cache - Fixed: oscillator wall-bounce lockout - Fixed: phantom meteor perpendicular body orientation - 6/6 battle wins across all enemy types
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
## Guess-factor gun: statistical targeting via GF histogram.
|
||||
## Bins: 31, ranging GF -1 (max CW escape) to +1 (max CCW escape).
|
||||
## Learns from virtual bullet outcomes; caches wave state per-tick.
|
||||
|
||||
import std/math
|
||||
import gun_harness/gun_interface
|
||||
|
||||
const
|
||||
GFBins = 31
|
||||
GFPrior = 0.1
|
||||
|
||||
type
|
||||
Wave = object
|
||||
fireX, fireY: float
|
||||
fireBearing: float # atan2(enemyY-selfY, enemyX-selfX) at fire tick (rad)
|
||||
mea: float # max escape angle (rad)
|
||||
|
||||
GFGun* = object
|
||||
bins: array[GFBins, float]
|
||||
waves: seq[Wave] # pending unresolved waves
|
||||
# per-tick cache: store wave only once across multiple power-bin calls
|
||||
cachedTick: int
|
||||
cachedWaveStored: bool
|
||||
|
||||
proc initGFGun*(): GFGun =
|
||||
result.cachedTick = -1
|
||||
for i in 0..<GFBins:
|
||||
result.bins[i] = GFPrior
|
||||
|
||||
proc gfToIndex(gf: float): int {.inline.} =
|
||||
clamp(int(round((gf + 1.0) * 0.5 * float(GFBins - 1))), 0, GFBins - 1)
|
||||
|
||||
proc indexToGF(idx: int): float {.inline.} =
|
||||
float(idx) / float(GFBins - 1) * 2.0 - 1.0
|
||||
|
||||
proc peakBin(g: GFGun): int =
|
||||
var best = 0
|
||||
for i in 1..<GFBins:
|
||||
if g.bins[i] > g.bins[best]:
|
||||
best = i
|
||||
best
|
||||
|
||||
proc predict*(g: var GFGun, state: WorldState, bulletSpeed: float): GunPrediction =
|
||||
if bulletSpeed <= 0.0:
|
||||
return GunPrediction(x: state.enemyX, y: state.enemyY)
|
||||
|
||||
let dx = state.enemyX - state.selfX
|
||||
let dy = state.enemyY - state.selfY
|
||||
let dist = sqrt(dx*dx + dy*dy)
|
||||
let bearing = arctan2(dy, dx)
|
||||
let mea = arcsin(clamp(8.0 / bulletSpeed, -1.0, 1.0))
|
||||
|
||||
# Store one wave per tick regardless of how many power bins call us
|
||||
if state.tick != g.cachedTick:
|
||||
g.cachedTick = state.tick
|
||||
g.cachedWaveStored = false
|
||||
|
||||
if not g.cachedWaveStored:
|
||||
g.waves.add Wave(
|
||||
fireX: state.selfX,
|
||||
fireY: state.selfY,
|
||||
fireBearing: bearing,
|
||||
mea: mea,
|
||||
)
|
||||
g.cachedWaveStored = true
|
||||
|
||||
let gfAngle = bearing + indexToGF(g.peakBin()) * mea
|
||||
# Aim from self at gfAngle, at current dist (angular targeting)
|
||||
let px = state.selfX + cos(gfAngle) * dist
|
||||
let py = state.selfY + sin(gfAngle) * dist
|
||||
|
||||
GunPrediction(
|
||||
x: clamp(px, BotRadius, state.arenaWidth - BotRadius),
|
||||
y: clamp(py, BotRadius, state.arenaHeight - BotRadius),
|
||||
)
|
||||
|
||||
proc onResult*(g: var GFGun, e: FeedbackEvent) =
|
||||
## Called when a virtual bullet resolves. Match the wave by predicted point,
|
||||
## compute actual GF, and increment the histogram with a smoothing kernel.
|
||||
## We don't have the original wave tick here, so we use the prediction coords
|
||||
## to identify and remove the matching wave.
|
||||
## ponytail: O(n) scan over waves; waves list stays tiny (< a dozen at a time)
|
||||
if g.waves.len == 0:
|
||||
return
|
||||
|
||||
# Pop the oldest wave (FIFO matches bullet resolution order)
|
||||
let w = g.waves[0]
|
||||
g.waves.delete(0)
|
||||
|
||||
# Compute actual bearing from fire position to where the enemy actually was
|
||||
let actualDx = e.actualX - w.fireX
|
||||
let actualDy = e.actualY - w.fireY
|
||||
let actualBearing = arctan2(actualDy, actualDx)
|
||||
var bearingDelta = actualBearing - w.fireBearing
|
||||
# Normalize to [-PI, PI]
|
||||
while bearingDelta > PI: bearingDelta -= 2.0*PI
|
||||
while bearingDelta < -PI: bearingDelta += 2.0*PI
|
||||
|
||||
let gf = if w.mea > 1e-10: clamp(bearingDelta / w.mea, -1.0, 1.0) else: 0.0
|
||||
let centerIdx = gfToIndex(gf)
|
||||
|
||||
# Triangular smoothing kernel over adjacent bins
|
||||
for i in 0..<GFBins:
|
||||
let dist = abs(i - centerIdx)
|
||||
g.bins[i] += 1.0 / float(1 + dist)
|
||||
Reference in New Issue
Block a user