Files
SirRoboGarage/common_libs/guns/decay_gf.nim
T
SirStone c94ba1f2fe feat(ModularBot): decay-GF gun (recency-weighted), 13 guns total
- Decay-GF: exponential decay on GF histogram (0.998/tick, ~350-tick half-life)
- Adapts faster to mid-battle strategy changes than standard GF
- Battle-tested vs WaveSurfer, Crazy, RandomMover
2026-09-20 01:52:22 +02:00

97 lines
2.8 KiB
Nim

## Recency-weighted GF gun: exponential decay on histogram bins.
## decay=0.998/tick gives ~350-tick half-life — adapts to mid-battle strategy shifts.
## Everything else identical to guess_factor.nim.
import std/math
import gun_harness/gun_interface
const
GFBins = 31
GFPrior = 0.1
DecayRate = 0.998 # ponytail: single global decay, tune if adaptation too slow/fast
type
DWave = object
fireX, fireY: float
fireBearing: float
DecayGFGun* = object
bins: array[GFBins, float]
waves: seq[DWave]
cachedTick: int
cachedWaveStored: bool
proc initDecayGFGun*(): DecayGFGun =
result.cachedTick = -1
let center = (GFBins - 1) div 2
for i in 0..<GFBins:
let d = abs(i - center)
result.bins[i] = GFPrior + 0.5 / float(1 + d)
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: DecayGFGun): int =
var best = 0
for i in 1..<GFBins:
if g.bins[i] > g.bins[best]:
best = i
best
proc predict*(g: var DecayGFGun, 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))
if state.tick != g.cachedTick:
# Decay all bins once per tick
for i in 0..<GFBins:
g.bins[i] *= DecayRate
g.cachedTick = state.tick
g.cachedWaveStored = false
if not g.cachedWaveStored:
g.waves.add DWave(fireX: state.selfX, fireY: state.selfY, fireBearing: bearing)
g.cachedWaveStored = true
let peak = g.peakBin()
let peakGF = indexToGF(peak)
let gfAngle = bearing + peakGF * mea
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 DecayGFGun, e: FeedbackEvent) =
if g.waves.len == 0: return
let w = g.waves[0]
g.waves.delete(0)
let speed = bulletSpeed(e.bulletPower)
let mea = arcsin(clamp(8.0 / speed, -1.0, 1.0))
let actualDx = e.actualX - w.fireX
let actualDy = e.actualY - w.fireY
let actualBearing = arctan2(actualDy, actualDx)
var bearingDelta = actualBearing - w.fireBearing
while bearingDelta > PI: bearingDelta -= 2.0*PI
while bearingDelta < -PI: bearingDelta += 2.0*PI
let gf = if mea > 1e-10: clamp(bearingDelta / mea, -1.0, 1.0) else: 0.0
let centerIdx = gfToIndex(gf)
for i in 0..<GFBins:
let d = abs(i - centerIdx)
g.bins[i] += 1.0 / float(1 + d)