feat(ModularBot): anti-surfer gun + wave-surf movement module

- Anti-surfer gun: inverse GF targeting for wave-surfing enemies
- Wave-surf movement: danger histogram dodge (not wired yet, needs movement selector)
- 7 guns total, battle-tested

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-09-20 01:28:29 +02:00
parent 1cb35e9e0e
commit ebe5b1b7f1
8 changed files with 362 additions and 3 deletions
+110
View File
@@ -0,0 +1,110 @@
## 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
proc initAntiSurferGun*(): AntiSurferGun =
result.cachedTick = -1
# 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)