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:
@@ -23,20 +23,22 @@ import guns/stop_shot
|
||||
import guns/displacement
|
||||
import guns/averaged_lead
|
||||
import guns/decay_gf
|
||||
import guns/knn_gun
|
||||
import movements/phantom_meteor
|
||||
import movements/wave_surfer
|
||||
import movement_harness/virtual_bodies as mvb
|
||||
import movement_harness/bullet_shadows
|
||||
|
||||
const botJsonPath = currentSourcePath().parentDir / "ModularBot.json"
|
||||
const DebugVBullets = false
|
||||
const DebugCircular = false
|
||||
const GunNames = ["HeadOn", "Linear", "Tsetlin", "Circular", "GuessFactor", "Pattern", "AntiSurf", "WallBounce", "Accel", "StopShot", "Displace", "AvgLead", "DecayGF"]
|
||||
const GunNames = ["HeadOn", "Linear", "Tsetlin", "Circular", "GuessFactor", "Pattern", "AntiSurf", "WallBounce", "Accel", "StopShot", "Displace", "AvgLead", "DecayGF", "KNN"]
|
||||
|
||||
const
|
||||
CLR_GUN = "\e[33m" # yellow
|
||||
CLR_MOVE = "\e[36m" # cyan
|
||||
CLR_FIRE = "\e[31m" # red
|
||||
CLR_RADAR = "\e[32m" # green
|
||||
CLR_CHANGE = "\e[32m" # green (highlight for changed module)
|
||||
CLR_ROUND = "\e[1;37m" # bold white
|
||||
CLR_RST = "\e[0m" # reset
|
||||
|
||||
@@ -60,15 +62,20 @@ type
|
||||
displace: DisplacementGun
|
||||
avgLead: AveragedLeadGun
|
||||
decayGF: DecayGFGun
|
||||
knnGun: KNNGun
|
||||
mover: PhantomMeteorModule
|
||||
waveMover: WaveSurferModule
|
||||
movementId: int ## 0=phantom, 1=waveSurf
|
||||
moveTracker: VirtualBodyTracker
|
||||
virtualHits: int
|
||||
virtualMiss: int
|
||||
tick: int
|
||||
currentGun: int
|
||||
|
||||
proc printConfig(bot: ModularBot, changed: string = "") =
|
||||
let gunName = GunNames[bot.currentGun]
|
||||
let gunColor = if changed == "gun" or changed == "all": CLR_CHANGE else: CLR_RST
|
||||
let radarColor = if changed == "radar" or changed == "all": CLR_CHANGE else: CLR_RST
|
||||
echo CLR_ROUND & "[config]" & CLR_RST & " " & gunColor & "gun=" & gunName & CLR_RST & " | " & CLR_MOVE & "move=phantom_meteor" & CLR_RST & " | " & radarColor & "radar=radar_lock" & CLR_RST
|
||||
|
||||
proc buildState(bot: ModularBot, ex, ey, espeed, eheading, eenergy: float): WorldState =
|
||||
WorldState(
|
||||
enemyX: ex,
|
||||
@@ -106,6 +113,7 @@ method onScannedBot*(bot: ModularBot, e: ScannedBotEvent) =
|
||||
var dsPreds: array[len(PowerBins), GunPrediction]
|
||||
var alPreds: array[len(PowerBins), GunPrediction]
|
||||
var dgPreds: array[len(PowerBins), GunPrediction]
|
||||
var knnPreds: array[len(PowerBins), GunPrediction]
|
||||
for i in 0..<len(PowerBins):
|
||||
headsUp[i] = bot.headOn.predict(bot.lastState, bulletSpeed(PowerBins[i]))
|
||||
linPreds[i] = bot.linear.predict(bot.lastState, bulletSpeed(PowerBins[i]))
|
||||
@@ -120,6 +128,7 @@ method onScannedBot*(bot: ModularBot, e: ScannedBotEvent) =
|
||||
dsPreds[i] = bot.displace.predict(bot.lastState, bulletSpeed(PowerBins[i]))
|
||||
alPreds[i] = bot.avgLead.predict(bot.lastState, bulletSpeed(PowerBins[i]))
|
||||
dgPreds[i] = bot.decayGF.predict(bot.lastState, bulletSpeed(PowerBins[i]))
|
||||
knnPreds[i] = bot.knnGun.predict(bot.lastState, bulletSpeed(PowerBins[i]))
|
||||
|
||||
bot.tracker.spawnBullets(0, headsUp, bot.lastState)
|
||||
bot.tracker.spawnBullets(1, linPreds, bot.lastState)
|
||||
@@ -136,6 +145,7 @@ method onScannedBot*(bot: ModularBot, e: ScannedBotEvent) =
|
||||
bot.tracker.spawnBullets(10, dsPreds, bot.lastState)
|
||||
bot.tracker.spawnBullets(11, alPreds, bot.lastState)
|
||||
bot.tracker.spawnBullets(12, dgPreds, bot.lastState)
|
||||
bot.tracker.spawnBullets(13, knnPreds, bot.lastState)
|
||||
|
||||
# Resolve bullets that have travelled far enough
|
||||
let st = bot.lastState
|
||||
@@ -159,6 +169,7 @@ method onScannedBot*(bot: ModularBot, e: ScannedBotEvent) =
|
||||
of 10: bot.displace.onResult(fe)
|
||||
of 11: bot.avgLead.onResult(fe)
|
||||
of 12: bot.decayGF.onResult(fe)
|
||||
of 13: bot.knnGun.onResult(fe)
|
||||
else: discard
|
||||
if fe.hit: inc bot.virtualHits else: inc bot.virtualMiss
|
||||
when DebugVBullets:
|
||||
@@ -172,8 +183,8 @@ method onScannedBot*(bot: ModularBot, e: ScannedBotEvent) =
|
||||
|
||||
# Log gun switch + update turret/bullet colors
|
||||
if selectedGun != bot.currentGun:
|
||||
echo CLR_GUN & fmt"[gun] switched: {GunNames[selectedGun]}" & CLR_RST
|
||||
bot.currentGun = selectedGun
|
||||
bot.printConfig("gun")
|
||||
case selectedGun
|
||||
of 0: setTurretColor("#FF3333"); setBulletColor("#FF6666") # HeadOn: warm red
|
||||
of 1: setTurretColor("#3366FF"); setBulletColor("#6699FF") # Linear: electric blue
|
||||
@@ -188,6 +199,7 @@ method onScannedBot*(bot: ModularBot, e: ScannedBotEvent) =
|
||||
of 10: setTurretColor("#006600"); setBulletColor("#009900") # Displace: dark green
|
||||
of 11: setTurretColor("#996633"); setBulletColor("#CC9966") # AvgLead: brown
|
||||
of 12: setTurretColor("#008888"); setBulletColor("#00AAAA") # DecayGF: dark cyan
|
||||
of 13: setTurretColor("#CC00CC"); setBulletColor("#FF44FF") # KNN: purple/magenta
|
||||
else: discard
|
||||
|
||||
let pred = case selectedGun
|
||||
@@ -203,6 +215,7 @@ method onScannedBot*(bot: ModularBot, e: ScannedBotEvent) =
|
||||
of 10: bot.displace.predict(bot.lastState, bulletSpeed(power))
|
||||
of 11: bot.avgLead.predict(bot.lastState, bulletSpeed(power))
|
||||
of 12: bot.decayGF.predict(bot.lastState, bulletSpeed(power))
|
||||
of 13: bot.knnGun.predict(bot.lastState, bulletSpeed(power))
|
||||
else: bot.headOn.predict(bot.lastState, bulletSpeed(power))
|
||||
let target = aimAngle(getX(), getY(), pred.x, pred.y)
|
||||
|
||||
@@ -220,14 +233,24 @@ method onScannedBot*(bot: ModularBot, e: ScannedBotEvent) =
|
||||
setGunTurnRate(normDelta)
|
||||
|
||||
|
||||
method onBulletFired*(bot: ModularBot, e: BulletFiredEvent) =
|
||||
bot.moveTracker.shadows.addBullet(e.bullet.x, e.bullet.y,
|
||||
degToRad(e.bullet.direction), e.bullet.power)
|
||||
|
||||
method onBulletHitBot*(bot: ModularBot, e: BulletHitBotEvent) =
|
||||
bot.moveTracker.shadows.removeBulletNear(e.bullet.x, e.bullet.y)
|
||||
|
||||
method onBulletHitWall*(bot: ModularBot, e: BulletHitWallEvent) =
|
||||
bot.moveTracker.shadows.removeBulletNear(e.bullet.x, e.bullet.y)
|
||||
|
||||
method onBulletHitBullet*(bot: ModularBot, e: BulletHitBulletEvent) =
|
||||
bot.moveTracker.shadows.removeBulletNear(e.bullet.x, e.bullet.y)
|
||||
|
||||
method onHitByBullet*(bot: ModularBot, e: HitByBulletEvent) =
|
||||
bot.moveTracker.registerHit(e.bullet.power, e.bullet.direction, getX(), getY())
|
||||
|
||||
method onRoundEnded*(bot: ModularBot, e: RoundEndedEventForBot) =
|
||||
let newId = bot.moveTracker.bestMovement()
|
||||
if newId != bot.movementId:
|
||||
echo CLR_MOVE & "[move] switching: " & (if newId == 0: "phantom_meteor" else: "wave_surfer") & CLR_RST
|
||||
bot.movementId = newId
|
||||
discard
|
||||
|
||||
method onRoundStarted*(bot: ModularBot, e: RoundStartedEvent) =
|
||||
setAdjustGunForBodyTurn(true)
|
||||
@@ -235,22 +258,15 @@ method onRoundStarted*(bot: ModularBot, e: RoundStartedEvent) =
|
||||
setAdjustRadarForGunTurn(true)
|
||||
setRadarColor("#004444") # RadarLock: dark teal, faint without alpha
|
||||
setScanColor("#0D4D4D") # RadarLock scan arc: very dark teal, faint without alpha
|
||||
setBodyColor("#FF9900") # PhantomMeteor: deep orange/gold
|
||||
bot.radar.init()
|
||||
bot.mover.resetRound()
|
||||
bot.hasContact = false
|
||||
bot.tick = 0
|
||||
bot.currentGun = -1
|
||||
bot.moveTracker.resetRound(getX(), getY(), getDirection(), getSpeed())
|
||||
|
||||
if bot.movementId == 0:
|
||||
bot.mover.resetRound()
|
||||
setBodyColor("#FF9900") # PhantomMeteor: deep orange/gold
|
||||
echo CLR_MOVE & "[move] using phantom_meteor" & CLR_RST
|
||||
else:
|
||||
bot.waveMover.resetRound()
|
||||
setBodyColor("#0099FF") # WaveSurfer: blue
|
||||
echo CLR_MOVE & "[move] using wave_surfer" & CLR_RST
|
||||
|
||||
echo CLR_GUN & fmt"[gun] starting: {GunNames[0]}" & CLR_RST
|
||||
bot.currentGun = 0
|
||||
bot.printConfig("all")
|
||||
# tracker fitness persists across rounds (rolling window carries over)
|
||||
|
||||
method onGameStarted*(bot: ModularBot, e: GameStartedEventForBot) =
|
||||
@@ -267,24 +283,15 @@ method run*(bot: ModularBot) =
|
||||
go()
|
||||
continue
|
||||
|
||||
# Movement harness: compute all, feed tracker, apply active
|
||||
let phantomCmd = bot.mover.computeMove(bot.lastState)
|
||||
let waveSurfCmd = bot.waveMover.computeMove(bot.lastState)
|
||||
bot.moveTracker.tick(bot.lastState, [phantomCmd, waveSurfCmd])
|
||||
let activeId = bot.moveTracker.bestMovement()
|
||||
if activeId != bot.movementId:
|
||||
bot.movementId = activeId
|
||||
echo CLR_MOVE & "[move] switched to: " & (if activeId == 0: "phantom_meteor" else: "wave_surfer") & CLR_RST
|
||||
let (spd, tr) = if bot.movementId == 0: phantomCmd else: waveSurfCmd
|
||||
let (spd, tr) = bot.mover.computeMove(bot.lastState)
|
||||
setTargetSpeed(spd)
|
||||
setTurnRate(tr)
|
||||
|
||||
setRadarTurnRate(bot.radar.computeScan(bot.lastState))
|
||||
go()
|
||||
|
||||
when isMainModule:
|
||||
var bot = ModularBot(
|
||||
tracker: vb.initTracker(13), # 0: HeadOn, 1: Linear, 2: Tsetlin, 3: Circular, 4: GuessFactor, 5: Pattern, 6: AntiSurf, 7: WallBounce, 8: Accel, 9: StopShot, 10: Displace, 11: AvgLead, 12: DecayGF
|
||||
tracker: vb.initTracker(14), # 0: HeadOn, 1: Linear, 2: Tsetlin, 3: Circular, 4: GuessFactor, 5: Pattern, 6: AntiSurf, 7: WallBounce, 8: Accel, 9: StopShot, 10: Displace, 11: AvgLead, 12: DecayGF, 13: KNN
|
||||
headOn: HeadOnGun(),
|
||||
linear: LinearGun(),
|
||||
circular: CircularGun(),
|
||||
@@ -298,10 +305,10 @@ when isMainModule:
|
||||
displace: initDisplacementGun(),
|
||||
avgLead: initAveragedLeadGun(),
|
||||
decayGF: initDecayGFGun(),
|
||||
knnGun: initKNNGun(),
|
||||
radar: RadarLockModule(),
|
||||
mover: initPhantomMeteor(),
|
||||
waveMover: initWaveSurfer(),
|
||||
moveTracker: mvb.initVirtualBodyTracker(2), # 0=phantom, 1=waveSurf
|
||||
moveTracker: mvb.initVirtualBodyTracker(1),
|
||||
currentGun: -1,
|
||||
)
|
||||
start(bot, botJsonPath)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user