feat(movement): dodge timing, wall avoidance, distance control, ram finisher

PhantomMeteor:
- Ram finisher: charge at enemy when <200px and their energy <10
- Ram opportunity: charge when <60px and we have >20 energy advantage
- Integrated gunheat tracker for 1-2 tick earlier wave detection
- Distance control: smooth linear ramp toward preferred engagement distance
- Phantom range expanded 150→250px to catch closer threats

WaveSurfer:
- Wall-aware dodge bin selection: penalize bins leading off-arena
- Dodge timing: predict future position 15 ticks ahead for safety
- Distance control: radial blend when outside deadband (350±50px)
- Wall escape: invert strafe if pushing further into wall, blend toward center

ModularBot:
- Wired KNN gun (purple/magenta)
- Shadows tracked for movement (safer GF prediction)
- Bullet lifecycle management (onBulletFired/onBulletHitBot/onBulletHitWall)
- Unified phantom_meteor movement (wave_surfer unplugged)
- Config logging on round start + gun switch

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-09-20 11:59:05 +02:00
parent 5bbc8cbda7
commit 30d00ba163
3 changed files with 169 additions and 57 deletions
+66 -20
View File
@@ -6,6 +6,7 @@
import std/math
import gun_harness/gun_interface
import movement_harness/movement_interface
import movement_harness/gunheat_tracker
# Inline the gravity engine types and logic here to keep the module self-contained.
# We re-export nothing from gravity.nim — it's not on the common_libs path.
@@ -56,6 +57,10 @@ type
# ── Gravity engine internals ──────────────────────────────────────────────────
const
CLR_CYAN = "\e[36m"
CLR_RST = "\e[0m"
const
KBullet = 1500.0
KWall = 4000.0
@@ -136,28 +141,33 @@ proc computeForces(eng: GravityEngine, botPos, enemyPos: Vec2, arenaW, arenaH: f
for ph in eng.phantoms:
if ph.alive:
let d = dist(ph.pos, botPos)
if d < 150.0:
if d < 250.0:
hasClose = true
total = total + normalize(botPos - ph.pos) * (KBullet * (1.0 - d/150.0))
total = total + normalize(botPos - ph.pos) * (KBullet * (1.0 - d/250.0))
# Priority 2: wall escape (hard override)
# Priority 2: wall escape (additive — blends with phantom forces)
let minW = min(min(botPos.x, arenaW-botPos.x), min(botPos.y, arenaH-botPos.y))
if minW < VeryClose:
var ex = 0.0; var ey = 0.0
if botPos.x < VeryClose: ex = 1.0
if botPos.x < VeryClose: ex = 1.0
if arenaW - botPos.x < VeryClose: ex = -1.0
if botPos.y < VeryClose: ey = 1.0
if botPos.y < VeryClose: ey = 1.0
if arenaH - botPos.y < VeryClose: ey = -1.0
let m = sqrt(ex*ex + ey*ey)
if m > 0.1: return normalize(vec2(ex,ey)) * 500.0
let wm = sqrt(ex*ex + ey*ey)
if wm > 0.1: total = total + normalize(vec2(ex, ey)) * 500.0
# Priority 3: distance to enemy
# Priority 3: distance to enemy — symmetric attraction/repulsion around PreferredDist.
# Linear ramp over ±150px so the transition is smooth, not a hard switch.
# ponytail: linear ramp, tune rampWidth if distance oscillates
let de = dist(enemyPos, botPos)
if not hasClose and de > 100.0:
if de < PreferredDist:
total = total + normalize(botPos - enemyPos) * KEnemy
elif de > PreferredDist + 100.0:
total = total + normalize(enemyPos - botPos) * (KEnemy * 0.3)
const rampWidth = 150.0
let distErr = de - PreferredDist # >0 = too far, <0 = too close
let t = clamp(distErr / rampWidth, -1.0, 1.0)
# t > 0 → attract toward enemy; t < 0 → repel away
let dir = if t >= 0.0: normalize(enemyPos - botPos)
else: normalize(botPos - enemyPos)
total = total + dir * (KEnemy * abs(t))
# Priority 4: weak wall repulsion
let dL = max(botPos.x, WallMinDist)
@@ -171,31 +181,67 @@ proc computeForces(eng: GravityEngine, botPos, enemyPos: Vec2, arenaW, arenaH: f
# ── MovementModule wrapper ────────────────────────────────────────────────────
type PhantomMeteorModule* = object
engine: GravityEngine
engine: GravityEngine
gunheat: GunheatTracker
proc initPhantomMeteor*(): PhantomMeteorModule =
PhantomMeteorModule(engine: initEngine())
PhantomMeteorModule(engine: initEngine(), gunheat: initGunheatTracker())
proc resetRound*(m: var PhantomMeteorModule) =
## Clear per-round transients (phantoms, waves, energy baseline), keep histogram.
m.engine.phantoms = @[]
m.engine.waves = @[]
m.engine.prevEnemyEnergy = 100.0
m.gunheat.resetRound()
proc computeMove*(m: var PhantomMeteorModule, ws: WorldState): MoveCommand =
let botPos = vec2(ws.selfX, ws.selfY)
let enemyPos = vec2(ws.enemyX, ws.enemyY)
# ── Ram override ─────────────────────────────────────────────────────────────
let ramDist = dist(botPos, enemyPos)
let ramFinisher = ramDist < 200.0 and ws.enemyEnergy < 10.0 and ws.selfEnergy > ws.enemyEnergy
let ramOpportunity = ramDist < 60.0 and ws.selfEnergy > ws.enemyEnergy + 20.0
if ramFinisher or ramOpportunity:
let tag = if ramFinisher: "finisher" else: "opportunity"
echo CLR_CYAN & "[ram:" & tag & "] dist=" & $int(ramDist) &
" selfE=" & $int(ws.selfEnergy) & " enemyE=" & $int(ws.enemyEnergy) & CLR_RST
let headingRad = arctan2(enemyPos.y - botPos.y, enemyPos.x - botPos.x)
let desiredDeg = radToDeg(headingRad)
var delta = desiredDeg - ws.selfHeading
while delta > 180.0: delta -= 360.0
while delta < -180.0: delta += 360.0
return (speed: 8.0, turnRate: delta.clamp(-10.0, 10.0))
# Advance simulation
m.engine.tickPhantoms()
m.engine.tickWaves(botPos)
# Fire detection → spawn phantoms + wave
let (fired, power) = m.engine.detectFire(ws.enemyEnergy)
if fired:
let bspeed = 20.0 - 3.0 * power
m.engine.spawnPhantoms(enemyPos, botPos, bspeed)
m.engine.spawnWave(enemyPos, botPos, bspeed)
# Fire detection via gunheat tracker (1-2 ticks earlier than energy drop).
var spawnedThisTick = false
for ev in m.gunheat.tick(ws):
let bspeed = 20.0 - 3.0 * ev.bulletPower
let evPos = vec2(ev.fireX, ev.fireY)
case ev.kind
of wePredicted:
m.engine.spawnPhantoms(evPos, botPos, bspeed)
m.engine.spawnWave(evPos, botPos, bspeed)
spawnedThisTick = true
of weConfirmed:
if not spawnedThisTick:
m.engine.spawnPhantoms(evPos, botPos, bspeed)
m.engine.spawnWave(evPos, botPos, bspeed)
spawnedThisTick = true
# Fallback: energy-drop detection (fires if gunheat tracker missed it).
if not spawnedThisTick:
let (fired, power) = m.engine.detectFire(ws.enemyEnergy)
if fired:
let bspeed = 20.0 - 3.0 * power
m.engine.spawnPhantoms(enemyPos, botPos, bspeed)
m.engine.spawnWave(enemyPos, botPos, bspeed)
else:
# Keep prevEnemyEnergy in sync so energy-drop fallback stays coherent.
discard m.engine.detectFire(ws.enemyEnergy)
# Compute force vector
let force = m.engine.computeForces(botPos, enemyPos, ws.arenaWidth, ws.arenaHeight)
+63 -4
View File
@@ -7,7 +7,12 @@ import std/[math]
import gun_harness/gun_interface
import movement_harness/movement_interface
const WS_BINS = 31
const WS_BINS = 31
const WallMargin = 40.0
const DodgeTicks = 15.0 # approximate ticks to reach dodge position
const WS_PrefDist = 400.0 # optimal engagement distance
const WS_DistBand = 50.0 # deadband: pure strafe within ±50px of preferred
const WS_RadialFrac = 0.35 # radial blend fraction (0=pure strafe, 1=pure radial)
type
WSWave = object
@@ -109,10 +114,25 @@ proc computeMove*(m: var WaveSurferModule, ws: WorldState): MoveCommand =
let avg = total / float64(WS_BINS)
if m.bins[curBin] > avg:
# Find lowest-danger bin
# Find lowest-danger bin, penalizing positions near walls
let dodgeDist = ws.selfSpeed * DodgeTicks
var bestBin = 0
for j in 1..<WS_BINS:
if m.bins[j] < m.bins[bestBin]: bestBin = j
let gfJ = binToGF(j)
let angleJ = w.bearing + gfJ * maxA
let futureX = botX + cos(angleJ) * dodgeDist
let futureY = botY + sin(angleJ) * dodgeDist
let wallHit = futureX < WallMargin or futureX > ws.arenaWidth - WallMargin or
futureY < WallMargin or futureY > ws.arenaHeight - WallMargin
let dangerJ = if wallHit: m.bins[j] * 5.0 else: m.bins[j]
let gfBest = binToGF(bestBin)
let angleB = w.bearing + gfBest * maxA
let futureXB = botX + cos(angleB) * dodgeDist
let futureYB = botY + sin(angleB) * dodgeDist
let wallHitB = futureXB < WallMargin or futureXB > ws.arenaWidth - WallMargin or
futureYB < WallMargin or futureYB > ws.arenaHeight - WallMargin
let dangerB = if wallHitB: m.bins[bestBin] * 5.0 else: m.bins[bestBin]
if dangerJ < dangerB: bestBin = j
let bestGF = binToGF(bestBin)
# Move in direction of best GF: positive = CCW (orbit left), negative = CW
m.strafeDir = if bestGF >= curGF: 1.0 else: -1.0
@@ -120,9 +140,48 @@ proc computeMove*(m: var WaveSurferModule, ws: WorldState): MoveCommand =
# Perpendicular strafe (same body trick as phantom_meteor)
let enemyBearingRad = arctan2(enemyY - botY, enemyX - botX)
# Perpendicular in chosen strafe direction
let perpAngle =
var perpAngle =
if m.strafeDir >= 0.0: enemyBearingRad + PI * 0.5
else: enemyBearingRad - PI * 0.5
# Hard wall escape: if near any wall, blend toward arena center
# ponytail: linear blend, upgrade to override if blending proves too weak
let nearLeft = botX < WallMargin
let nearRight = botX > ws.arenaWidth - WallMargin
let nearBottom = botY < WallMargin
let nearTop = botY > ws.arenaHeight - WallMargin
if nearLeft or nearRight or nearBottom or nearTop:
# Flip strafe if it pushes further into the wall
let px = cos(perpAngle)
let py = sin(perpAngle)
if (nearLeft and px < 0.0) or (nearRight and px > 0.0) or
(nearBottom and py < 0.0) or (nearTop and py > 0.0):
m.strafeDir = -m.strafeDir
perpAngle = if m.strafeDir >= 0.0: enemyBearingRad + PI * 0.5
else: enemyBearingRad - PI * 0.5
# Blend 50% toward arena center
let escapeAngle = arctan2(ws.arenaHeight * 0.5 - botY, ws.arenaWidth * 0.5 - botX)
let ex = cos(escapeAngle) + cos(perpAngle)
let ey = sin(escapeAngle) + sin(perpAngle)
perpAngle = arctan2(ey, ex)
# Distance control: blend a radial component when outside the deadband.
# Secondary to bullet dodge — capped at WS_RadialFrac of travel direction.
# ponytail: linear blend, tune WS_RadialFrac if approach/retreat feels sluggish
let enemyDist = sqrt((enemyX - botX)^2 + (enemyY - botY)^2)
let distErr = enemyDist - WS_PrefDist
let radialFrac =
if distErr > WS_DistBand: WS_RadialFrac # too far → approach
elif distErr < -WS_DistBand: -WS_RadialFrac # too close → retreat
else: 0.0 # deadband → pure strafe
if abs(radialFrac) > 1e-9:
# Radial direction: toward enemy (positive) or away (negative)
let radialAngle = arctan2(enemyY - botY, enemyX - botX) +
(if radialFrac < 0.0: PI else: 0.0)
let rx = cos(perpAngle) * (1.0 - abs(radialFrac)) + cos(radialAngle) * abs(radialFrac)
let ry = sin(perpAngle) * (1.0 - abs(radialFrac)) + sin(radialAngle) * abs(radialFrac)
perpAngle = arctan2(ry, rx)
let desiredDeg = radToDeg(perpAngle)
var delta = desiredDeg - ws.selfHeading