feat(ModularBot): stop-shot gun, 10 guns total
- Stop-shot gun: predicts enemy deceleration stop point - Catches bots at direction reversal pauses - Battle-tested vs WaveSurfer, Crazy, RandomMover Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
## ModularBot — plugin gun architecture tracer bullet.
|
||||
## Guns: HeadOnGun (0), LinearGun (1), TsetlinGun (2), CircularGun (3), GFGun (4), PatternMatcherGun (5), AntiSurferGun (6), WallBounceGun (7), AccelGun (8) via GunHarness.
|
||||
## Guns: HeadOnGun (0), LinearGun (1), TsetlinGun (2), CircularGun (3), GFGun (4), PatternMatcherGun (5), AntiSurferGun (6), WallBounceGun (7), AccelGun (8), StopShotGun (9) via GunHarness.
|
||||
## Radar: RadarLockModule via radar harness.
|
||||
## Movement: OscillatorModule (perpendicular strafing).
|
||||
|
||||
@@ -19,12 +19,13 @@ import guns/pattern_matcher
|
||||
import guns/anti_surfer
|
||||
import guns/wall_bounce
|
||||
import guns/accel_predictor
|
||||
import guns/stop_shot
|
||||
import movements/phantom_meteor
|
||||
|
||||
const botJsonPath = currentSourcePath().parentDir / "ModularBot.json"
|
||||
const DebugVBullets = false
|
||||
const DebugCircular = false
|
||||
const GunNames = ["HeadOn", "Linear", "Tsetlin", "Circular", "GuessFactor", "Pattern", "AntiSurf", "WallBounce", "Accel"]
|
||||
const GunNames = ["HeadOn", "Linear", "Tsetlin", "Circular", "GuessFactor", "Pattern", "AntiSurf", "WallBounce", "Accel", "StopShot"]
|
||||
|
||||
type
|
||||
ModularBot = ref object of Bot
|
||||
@@ -42,6 +43,7 @@ type
|
||||
antiSurfer: AntiSurferGun
|
||||
wallBounce: WallBounceGun
|
||||
accelGun: AccelGun
|
||||
stopShot: StopShotGun
|
||||
mover: PhantomMeteorModule
|
||||
virtualHits: int
|
||||
virtualMiss: int
|
||||
@@ -81,6 +83,7 @@ method onScannedBot*(bot: ModularBot, e: ScannedBotEvent) =
|
||||
var asPreds: array[len(PowerBins), GunPrediction]
|
||||
var wbPreds: array[len(PowerBins), GunPrediction]
|
||||
var acPreds: array[len(PowerBins), GunPrediction]
|
||||
var ssPreds: 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]))
|
||||
@@ -91,6 +94,7 @@ method onScannedBot*(bot: ModularBot, e: ScannedBotEvent) =
|
||||
asPreds[i] = bot.antiSurfer.predict(bot.lastState, bulletSpeed(PowerBins[i]))
|
||||
wbPreds[i] = bot.wallBounce.predict(bot.lastState, bulletSpeed(PowerBins[i]))
|
||||
acPreds[i] = bot.accelGun.predict(bot.lastState, bulletSpeed(PowerBins[i]))
|
||||
ssPreds[i] = bot.stopShot.predict(bot.lastState, bulletSpeed(PowerBins[i]))
|
||||
|
||||
bot.tracker.spawnBullets(0, headsUp, bot.lastState)
|
||||
bot.tracker.spawnBullets(1, linPreds, bot.lastState)
|
||||
@@ -103,6 +107,7 @@ method onScannedBot*(bot: ModularBot, e: ScannedBotEvent) =
|
||||
bot.tracker.spawnBullets(6, asPreds, bot.lastState)
|
||||
bot.tracker.spawnBullets(7, wbPreds, bot.lastState)
|
||||
bot.tracker.spawnBullets(8, acPreds, bot.lastState)
|
||||
bot.tracker.spawnBullets(9, ssPreds, bot.lastState)
|
||||
|
||||
# Resolve bullets that have travelled far enough
|
||||
let st = bot.lastState
|
||||
@@ -122,6 +127,7 @@ method onScannedBot*(bot: ModularBot, e: ScannedBotEvent) =
|
||||
of 6: bot.antiSurfer.onResult(fe)
|
||||
of 7: bot.wallBounce.onResult(fe)
|
||||
of 8: bot.accelGun.onResult(fe)
|
||||
of 9: bot.stopShot.onResult(fe)
|
||||
else: discard
|
||||
if fe.hit: inc bot.virtualHits else: inc bot.virtualMiss
|
||||
when DebugVBullets:
|
||||
@@ -147,6 +153,7 @@ method onScannedBot*(bot: ModularBot, e: ScannedBotEvent) =
|
||||
of 6: setTurretColor("#00CCFF"); setBulletColor("#66DDFF") # AntiSurf: cyan
|
||||
of 7: setTurretColor("#CCCCCC"); setBulletColor("#EEEEEE") # WallBounce: silver/white
|
||||
of 8: setTurretColor("#FF00FF"); setBulletColor("#FF66FF") # Accel: magenta
|
||||
of 9: setTurretColor("#990000"); setBulletColor("#CC3333") # StopShot: dark red
|
||||
else: discard
|
||||
|
||||
let pred = case selectedGun
|
||||
@@ -158,6 +165,7 @@ method onScannedBot*(bot: ModularBot, e: ScannedBotEvent) =
|
||||
of 6: bot.antiSurfer.predict(bot.lastState, bulletSpeed(power))
|
||||
of 7: bot.wallBounce.predict(bot.lastState, bulletSpeed(power))
|
||||
of 8: bot.accelGun.predict(bot.lastState, bulletSpeed(power))
|
||||
of 9: bot.stopShot.predict(bot.lastState, bulletSpeed(power))
|
||||
else: bot.headOn.predict(bot.lastState, bulletSpeed(power))
|
||||
let target = aimAngle(getX(), getY(), pred.x, pred.y)
|
||||
|
||||
@@ -217,7 +225,7 @@ method run*(bot: ModularBot) =
|
||||
|
||||
when isMainModule:
|
||||
var bot = ModularBot(
|
||||
tracker: vb.initTracker(9), # 0: HeadOn, 1: Linear, 2: Tsetlin, 3: Circular, 4: GuessFactor, 5: Pattern, 6: AntiSurf, 7: WallBounce, 8: Accel
|
||||
tracker: vb.initTracker(10), # 0: HeadOn, 1: Linear, 2: Tsetlin, 3: Circular, 4: GuessFactor, 5: Pattern, 6: AntiSurf, 7: WallBounce, 8: Accel, 9: StopShot
|
||||
headOn: HeadOnGun(),
|
||||
linear: LinearGun(),
|
||||
circular: CircularGun(),
|
||||
@@ -227,6 +235,7 @@ when isMainModule:
|
||||
antiSurfer: initAntiSurferGun(),
|
||||
wallBounce: initWallBounceGun(),
|
||||
accelGun: initAccelGun(),
|
||||
stopShot: initStopShotGun(),
|
||||
radar: RadarLockModule(),
|
||||
mover: initPhantomMeteor(),
|
||||
currentGun: -1,
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
## Stop-shot gun: predicts the point where a decelerating enemy will stop.
|
||||
## Many bots pause briefly when reversing direction — catches them at the stop.
|
||||
## Falls back to linear prediction when enemy is not decelerating.
|
||||
## Coordinate system: 0° = East, CCW positive (Tank Royale standard).
|
||||
|
||||
import std/math
|
||||
import gun_harness/gun_interface
|
||||
|
||||
# TR deceleration constants (px/tick²)
|
||||
const BrakeDecel = 2.0 ## same-direction stop
|
||||
const CoastDecel = 1.0 ## cross-zero coasting stop
|
||||
|
||||
type StopShotGun* = object
|
||||
prevSpeed: float
|
||||
prevHeading: float
|
||||
prevTick: int
|
||||
frames: int
|
||||
cachedTick: int
|
||||
cachedPredX: float
|
||||
cachedPredY: float
|
||||
|
||||
proc initStopShotGun*(): StopShotGun = StopShotGun()
|
||||
|
||||
proc predict*(g: var StopShotGun, state: WorldState, bulletSpeed: float): GunPrediction =
|
||||
if bulletSpeed <= 0.0:
|
||||
return GunPrediction(x: state.enemyX, y: state.enemyY)
|
||||
|
||||
# Per-tick cache: all power bins share one prediction
|
||||
if state.tick == g.cachedTick:
|
||||
return GunPrediction(x: g.cachedPredX, y: g.cachedPredY)
|
||||
|
||||
defer:
|
||||
g.cachedTick = state.tick
|
||||
g.cachedPredX = result.x
|
||||
g.cachedPredY = result.y
|
||||
|
||||
# Update history
|
||||
let isNew = state.tick > g.prevTick
|
||||
if isNew:
|
||||
g.prevSpeed = state.enemySpeed
|
||||
g.prevHeading = state.enemyHeading
|
||||
g.prevTick = state.tick
|
||||
inc g.frames
|
||||
|
||||
# Need 2+ frames to detect deceleration
|
||||
if g.frames < 2:
|
||||
result = GunPrediction(x: state.enemyX, y: state.enemyY)
|
||||
return
|
||||
|
||||
let speed = state.enemySpeed
|
||||
let prev = g.prevSpeed
|
||||
|
||||
# Detect deceleration: |speed| is shrinking
|
||||
let decelerating = abs(speed) < abs(prev) and abs(speed) > 0.01
|
||||
|
||||
if not decelerating:
|
||||
# Linear fallback
|
||||
let headRad = degToRad(state.enemyHeading)
|
||||
let dist = hypot(state.enemyX - state.selfX, state.enemyY - state.selfY)
|
||||
let t = dist / bulletSpeed
|
||||
var px = state.enemyX + cos(headRad) * speed * t
|
||||
var py = state.enemyY + sin(headRad) * speed * t
|
||||
result = GunPrediction(
|
||||
x: clamp(px, 0.0, state.arenaWidth),
|
||||
y: clamp(py, 0.0, state.arenaHeight)
|
||||
)
|
||||
return
|
||||
|
||||
# Pick decel rate: braking (speed toward zero on same sign) = 2, else 1
|
||||
# ponytail: simplified; TR has exact rules per direction but this is close enough
|
||||
let decel = if speed * prev > 0.0: BrakeDecel else: CoastDecel
|
||||
|
||||
# Simulate stop position
|
||||
let headRad = degToRad(state.enemyHeading)
|
||||
var v = speed
|
||||
var sx = state.enemyX
|
||||
var sy = state.enemyY
|
||||
while abs(v) > 0.001:
|
||||
sx += v * cos(headRad)
|
||||
sy += v * sin(headRad)
|
||||
let step = if v > 0.0: -decel else: decel
|
||||
v += step
|
||||
if (v > 0.0) != (speed > 0.0): v = 0.0 # crossed zero
|
||||
|
||||
# Bullet travel time to current pos, check against ticks to stop
|
||||
let dist = hypot(state.enemyX - state.selfX, state.enemyY - state.selfY)
|
||||
let bulletTicks = dist / bulletSpeed
|
||||
let stopTicks = abs(speed) / decel
|
||||
|
||||
# If bullet arrives well after stop, aim at stop; otherwise linear blend
|
||||
let px = if bulletTicks >= stopTicks: sx else: state.enemyX + cos(headRad) * speed * bulletTicks
|
||||
let py = if bulletTicks >= stopTicks: sy else: state.enemyY + sin(headRad) * speed * bulletTicks
|
||||
|
||||
result = GunPrediction(
|
||||
x: clamp(px, BotRadius, state.arenaWidth - BotRadius),
|
||||
y: clamp(py, BotRadius, state.arenaHeight - BotRadius)
|
||||
)
|
||||
|
||||
proc onResult*(g: var StopShotGun, e: FeedbackEvent) =
|
||||
discard # analytical gun — no learning
|
||||
Reference in New Issue
Block a user