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
113 lines
3.5 KiB
Nim
113 lines
3.5 KiB
Nim
## Anti-surfer gun: inverse of GF histogram — aim at the VALLEY bin.
|
|
## Wave surfers dodge to where they predict bullets won't be (the GF peak).
|
|
## They congregate at the valley → aim there to punish their dodge.
|
|
## Bins: 31, same as GFGun. Learns from virtual bullet outcomes.
|
|
|
|
import std/[math, strformat]
|
|
import gun_harness/gun_interface
|
|
|
|
const
|
|
ASBins = 31
|
|
ASPrior = 0.1
|
|
DebugAS* = false
|
|
|
|
type
|
|
ASWave = object
|
|
fireX, fireY: float
|
|
fireBearing: float
|
|
|
|
AntiSurferGun* = object
|
|
bins: array[ASBins, float]
|
|
waves: seq[ASWave]
|
|
cachedTick: int
|
|
cachedWaveStored: bool
|
|
debugGraphics*: bool
|
|
|
|
proc initAntiSurferGun*(): AntiSurferGun =
|
|
result.cachedTick = -1
|
|
result.debugGraphics = false
|
|
# Same triangular head-on prior as GFGun — cold-start aims head-on.
|
|
let center = (ASBins - 1) div 2 # = 15
|
|
for i in 0..<ASBins:
|
|
let d = abs(i - center)
|
|
result.bins[i] = ASPrior + 0.5 / float(1 + d)
|
|
|
|
proc asToIndex(gf: float): int {.inline.} =
|
|
clamp(int(round((gf + 1.0) * 0.5 * float(ASBins - 1))), 0, ASBins - 1)
|
|
|
|
proc indexToAS(idx: int): float {.inline.} =
|
|
float(idx) / float(ASBins - 1) * 2.0 - 1.0
|
|
|
|
proc valleyBin(g: AntiSurferGun): int =
|
|
## Find the bin with MINIMUM value — where the surfer thinks is safe.
|
|
var best = 0
|
|
for i in 1..<ASBins:
|
|
if g.bins[i] < g.bins[best]:
|
|
best = i
|
|
best
|
|
|
|
proc predict*(g: var AntiSurferGun, 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:
|
|
g.cachedTick = state.tick
|
|
g.cachedWaveStored = false
|
|
|
|
if not g.cachedWaveStored:
|
|
g.waves.add ASWave(
|
|
fireX: state.selfX,
|
|
fireY: state.selfY,
|
|
fireBearing: bearing,
|
|
)
|
|
g.cachedWaveStored = true
|
|
|
|
let valley = g.valleyBin()
|
|
let valleyGF = indexToAS(valley)
|
|
let gfAngle = bearing + valleyGF * mea
|
|
let px = state.selfX + cos(gfAngle) * dist
|
|
let py = state.selfY + sin(gfAngle) * dist
|
|
|
|
when DebugAS:
|
|
echo fmt"[as-dbg] predict: valleyGF={valleyGF:.2f} valleyBin={valley} mea={radToDeg(mea):.1f}° aimAngle={radToDeg(gfAngle):.1f}°"
|
|
|
|
GunPrediction(
|
|
x: clamp(px, BotRadius, state.arenaWidth - BotRadius),
|
|
y: clamp(py, BotRadius, state.arenaHeight - BotRadius),
|
|
)
|
|
|
|
proc onResult*(g: var AntiSurferGun, e: FeedbackEvent) =
|
|
## Same learning as GFGun — track where the enemy actually goes.
|
|
## ponytail: O(n) scan over waves; stays tiny (< a dozen at a time)
|
|
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 = asToIndex(gf)
|
|
|
|
when DebugAS:
|
|
echo fmt"[as-dbg] onResult: delta={radToDeg(bearingDelta):.1f}° MEA={radToDeg(mea):.1f}° GF={gf:.2f} bin={centerIdx}"
|
|
|
|
for i in 0..<ASBins:
|
|
let dist = abs(i - centerIdx)
|
|
g.bins[i] += 1.0 / float(1 + dist)
|