2cc2a3bd87
- 30-tick cooldown after ghost-stuck/timeout ram exit prevents re-entry loop - enemy_tracker.update() skips dead bots to prevent same-tick scan resurrection - TFIL graphics cleared when ramming is active movement - [config] logs: white base with green-highlighted changes only - [ram:enter] logs trigger reason and key values on false→true transition - [death] and [target-invalid] logs retained for diagnostics
99 lines
2.9 KiB
Nim
99 lines
2.9 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
|
|
debugGraphics*: bool
|
|
|
|
proc initDecayGFGun*(): DecayGFGun =
|
|
result.cachedTick = -1
|
|
result.debugGraphics = false
|
|
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)
|