feat(ModularBot): acceleration predictor gun, 9 guns total
- Accel gun: tracks velocity+heading changes, extrapolates with acceleration - Handles speed-up, slow-down, and combined accel+turn - Battle-tested vs Crazy, RandomMover, Walls
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
## ModularBot — plugin gun architecture tracer bullet.
|
## ModularBot — plugin gun architecture tracer bullet.
|
||||||
## Guns: HeadOnGun (0), LinearGun (1), TsetlinGun (2), CircularGun (3), GFGun (4), PatternMatcherGun (5), AntiSurferGun (6) via GunHarness.
|
## Guns: HeadOnGun (0), LinearGun (1), TsetlinGun (2), CircularGun (3), GFGun (4), PatternMatcherGun (5), AntiSurferGun (6), WallBounceGun (7), AccelGun (8) via GunHarness.
|
||||||
## Radar: RadarLockModule via radar harness.
|
## Radar: RadarLockModule via radar harness.
|
||||||
## Movement: OscillatorModule (perpendicular strafing).
|
## Movement: OscillatorModule (perpendicular strafing).
|
||||||
|
|
||||||
@@ -18,12 +18,13 @@ import guns/guess_factor
|
|||||||
import guns/pattern_matcher
|
import guns/pattern_matcher
|
||||||
import guns/anti_surfer
|
import guns/anti_surfer
|
||||||
import guns/wall_bounce
|
import guns/wall_bounce
|
||||||
|
import guns/accel_predictor
|
||||||
import movements/phantom_meteor
|
import movements/phantom_meteor
|
||||||
|
|
||||||
const botJsonPath = currentSourcePath().parentDir / "ModularBot.json"
|
const botJsonPath = currentSourcePath().parentDir / "ModularBot.json"
|
||||||
const DebugVBullets = false
|
const DebugVBullets = false
|
||||||
const DebugCircular = false
|
const DebugCircular = false
|
||||||
const GunNames = ["HeadOn", "Linear", "Tsetlin", "Circular", "GuessFactor", "Pattern", "AntiSurf", "WallBounce"]
|
const GunNames = ["HeadOn", "Linear", "Tsetlin", "Circular", "GuessFactor", "Pattern", "AntiSurf", "WallBounce", "Accel"]
|
||||||
|
|
||||||
type
|
type
|
||||||
ModularBot = ref object of Bot
|
ModularBot = ref object of Bot
|
||||||
@@ -40,6 +41,7 @@ type
|
|||||||
patternMatcher: PatternMatcherGun
|
patternMatcher: PatternMatcherGun
|
||||||
antiSurfer: AntiSurferGun
|
antiSurfer: AntiSurferGun
|
||||||
wallBounce: WallBounceGun
|
wallBounce: WallBounceGun
|
||||||
|
accelGun: AccelGun
|
||||||
mover: PhantomMeteorModule
|
mover: PhantomMeteorModule
|
||||||
virtualHits: int
|
virtualHits: int
|
||||||
virtualMiss: int
|
virtualMiss: int
|
||||||
@@ -78,6 +80,7 @@ method onScannedBot*(bot: ModularBot, e: ScannedBotEvent) =
|
|||||||
var pmPreds: array[len(PowerBins), GunPrediction]
|
var pmPreds: array[len(PowerBins), GunPrediction]
|
||||||
var asPreds: array[len(PowerBins), GunPrediction]
|
var asPreds: array[len(PowerBins), GunPrediction]
|
||||||
var wbPreds: array[len(PowerBins), GunPrediction]
|
var wbPreds: array[len(PowerBins), GunPrediction]
|
||||||
|
var acPreds: array[len(PowerBins), GunPrediction]
|
||||||
for i in 0..<len(PowerBins):
|
for i in 0..<len(PowerBins):
|
||||||
headsUp[i] = bot.headOn.predict(bot.lastState, bulletSpeed(PowerBins[i]))
|
headsUp[i] = bot.headOn.predict(bot.lastState, bulletSpeed(PowerBins[i]))
|
||||||
linPreds[i] = bot.linear.predict(bot.lastState, bulletSpeed(PowerBins[i]))
|
linPreds[i] = bot.linear.predict(bot.lastState, bulletSpeed(PowerBins[i]))
|
||||||
@@ -87,6 +90,7 @@ method onScannedBot*(bot: ModularBot, e: ScannedBotEvent) =
|
|||||||
pmPreds[i] = bot.patternMatcher.predict(bot.lastState, bulletSpeed(PowerBins[i]))
|
pmPreds[i] = bot.patternMatcher.predict(bot.lastState, bulletSpeed(PowerBins[i]))
|
||||||
asPreds[i] = bot.antiSurfer.predict(bot.lastState, bulletSpeed(PowerBins[i]))
|
asPreds[i] = bot.antiSurfer.predict(bot.lastState, bulletSpeed(PowerBins[i]))
|
||||||
wbPreds[i] = bot.wallBounce.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]))
|
||||||
|
|
||||||
bot.tracker.spawnBullets(0, headsUp, bot.lastState)
|
bot.tracker.spawnBullets(0, headsUp, bot.lastState)
|
||||||
bot.tracker.spawnBullets(1, linPreds, bot.lastState)
|
bot.tracker.spawnBullets(1, linPreds, bot.lastState)
|
||||||
@@ -98,6 +102,7 @@ method onScannedBot*(bot: ModularBot, e: ScannedBotEvent) =
|
|||||||
bot.tracker.spawnBullets(5, pmPreds, bot.lastState)
|
bot.tracker.spawnBullets(5, pmPreds, bot.lastState)
|
||||||
bot.tracker.spawnBullets(6, asPreds, bot.lastState)
|
bot.tracker.spawnBullets(6, asPreds, bot.lastState)
|
||||||
bot.tracker.spawnBullets(7, wbPreds, bot.lastState)
|
bot.tracker.spawnBullets(7, wbPreds, bot.lastState)
|
||||||
|
bot.tracker.spawnBullets(8, acPreds, bot.lastState)
|
||||||
|
|
||||||
# Resolve bullets that have travelled far enough
|
# Resolve bullets that have travelled far enough
|
||||||
let st = bot.lastState
|
let st = bot.lastState
|
||||||
@@ -116,6 +121,7 @@ method onScannedBot*(bot: ModularBot, e: ScannedBotEvent) =
|
|||||||
of 5: bot.patternMatcher.onResult(fe)
|
of 5: bot.patternMatcher.onResult(fe)
|
||||||
of 6: bot.antiSurfer.onResult(fe)
|
of 6: bot.antiSurfer.onResult(fe)
|
||||||
of 7: bot.wallBounce.onResult(fe)
|
of 7: bot.wallBounce.onResult(fe)
|
||||||
|
of 8: bot.accelGun.onResult(fe)
|
||||||
else: discard
|
else: discard
|
||||||
if fe.hit: inc bot.virtualHits else: inc bot.virtualMiss
|
if fe.hit: inc bot.virtualHits else: inc bot.virtualMiss
|
||||||
when DebugVBullets:
|
when DebugVBullets:
|
||||||
@@ -140,6 +146,7 @@ method onScannedBot*(bot: ModularBot, e: ScannedBotEvent) =
|
|||||||
of 5: setTurretColor("#FF6600"); setBulletColor("#FF9944") # Pattern: orange
|
of 5: setTurretColor("#FF6600"); setBulletColor("#FF9944") # Pattern: orange
|
||||||
of 6: setTurretColor("#00CCFF"); setBulletColor("#66DDFF") # AntiSurf: cyan
|
of 6: setTurretColor("#00CCFF"); setBulletColor("#66DDFF") # AntiSurf: cyan
|
||||||
of 7: setTurretColor("#CCCCCC"); setBulletColor("#EEEEEE") # WallBounce: silver/white
|
of 7: setTurretColor("#CCCCCC"); setBulletColor("#EEEEEE") # WallBounce: silver/white
|
||||||
|
of 8: setTurretColor("#FF00FF"); setBulletColor("#FF66FF") # Accel: magenta
|
||||||
else: discard
|
else: discard
|
||||||
|
|
||||||
let pred = case selectedGun
|
let pred = case selectedGun
|
||||||
@@ -150,6 +157,7 @@ method onScannedBot*(bot: ModularBot, e: ScannedBotEvent) =
|
|||||||
of 5: bot.patternMatcher.predict(bot.lastState, bulletSpeed(power))
|
of 5: bot.patternMatcher.predict(bot.lastState, bulletSpeed(power))
|
||||||
of 6: bot.antiSurfer.predict(bot.lastState, bulletSpeed(power))
|
of 6: bot.antiSurfer.predict(bot.lastState, bulletSpeed(power))
|
||||||
of 7: bot.wallBounce.predict(bot.lastState, bulletSpeed(power))
|
of 7: bot.wallBounce.predict(bot.lastState, bulletSpeed(power))
|
||||||
|
of 8: bot.accelGun.predict(bot.lastState, bulletSpeed(power))
|
||||||
else: bot.headOn.predict(bot.lastState, bulletSpeed(power))
|
else: bot.headOn.predict(bot.lastState, bulletSpeed(power))
|
||||||
let target = aimAngle(getX(), getY(), pred.x, pred.y)
|
let target = aimAngle(getX(), getY(), pred.x, pred.y)
|
||||||
|
|
||||||
@@ -209,7 +217,7 @@ method run*(bot: ModularBot) =
|
|||||||
|
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
var bot = ModularBot(
|
var bot = ModularBot(
|
||||||
tracker: vb.initTracker(8), # 0: HeadOn, 1: Linear, 2: Tsetlin, 3: Circular, 4: GuessFactor, 5: Pattern, 6: AntiSurf, 7: WallBounce
|
tracker: vb.initTracker(9), # 0: HeadOn, 1: Linear, 2: Tsetlin, 3: Circular, 4: GuessFactor, 5: Pattern, 6: AntiSurf, 7: WallBounce, 8: Accel
|
||||||
headOn: HeadOnGun(),
|
headOn: HeadOnGun(),
|
||||||
linear: LinearGun(),
|
linear: LinearGun(),
|
||||||
circular: CircularGun(),
|
circular: CircularGun(),
|
||||||
@@ -218,6 +226,7 @@ when isMainModule:
|
|||||||
patternMatcher: PatternMatcherGun(),
|
patternMatcher: PatternMatcherGun(),
|
||||||
antiSurfer: initAntiSurferGun(),
|
antiSurfer: initAntiSurferGun(),
|
||||||
wallBounce: initWallBounceGun(),
|
wallBounce: initWallBounceGun(),
|
||||||
|
accelGun: initAccelGun(),
|
||||||
radar: RadarLockModule(),
|
radar: RadarLockModule(),
|
||||||
mover: initPhantomMeteor(),
|
mover: initPhantomMeteor(),
|
||||||
currentGun: -1,
|
currentGun: -1,
|
||||||
|
|||||||
@@ -0,0 +1,34 @@
|
|||||||
|
## Test AccelGun: 3 battles vs Crazy, RandomMover, Walls — 10 rounds each.
|
||||||
|
## Run with: nim c -r --path:../../common_libs tests/test_accel_gun.nim
|
||||||
|
|
||||||
|
import std/[os, strformat]
|
||||||
|
import test_framework/test_framework
|
||||||
|
|
||||||
|
const
|
||||||
|
modularBotDir = currentSourcePath().parentDir.parentDir
|
||||||
|
samplesDir = "/home/davide/Downloads/robocode-tankroyale/sample-bots-nim-linux-1.0.7"
|
||||||
|
crazyDir = samplesDir / "Crazy"
|
||||||
|
wallsDir = samplesDir / "Walls"
|
||||||
|
randomMoverDir = "/home/davide/Projects/SirRoboGarage/RandomMover_garage"
|
||||||
|
|
||||||
|
if not fileExists("/home/davide/Projects/tank-royale/server/build/libs/robocode-tankroyale-server-0.35.5-all.jar") or
|
||||||
|
not fileExists("/home/davide/Projects/tank-royale/runner/examples/lib/robocode-tankroyale-runner.jar"):
|
||||||
|
echo "Skipping: Tank Royale JARs not found"
|
||||||
|
quit(0)
|
||||||
|
|
||||||
|
proc runAndReport(label, enemyName, enemyDir: string) =
|
||||||
|
echo fmt"\n=== {label} ==="
|
||||||
|
let r = runBattle(@[modularBotDir, enemyDir], rounds = 10, timeout = 180000)
|
||||||
|
var mbScore = 0
|
||||||
|
var enScore = 0
|
||||||
|
for b in r.results:
|
||||||
|
if b.name == "ModularBot": mbScore = b.totalScore
|
||||||
|
if b.name == enemyName: enScore = b.totalScore
|
||||||
|
let winner = if mbScore > enScore: "ModularBot" else: enemyName
|
||||||
|
echo fmt" ModularBot: {mbScore} {enemyName}: {enScore} → {winner} wins"
|
||||||
|
echo fmt" Rounds: {r.rounds.len}"
|
||||||
|
|
||||||
|
runAndReport("Battle 1/3: vs Crazy", "Crazy", crazyDir)
|
||||||
|
runAndReport("Battle 2/3: vs RandomMover", "RandomMover", randomMoverDir)
|
||||||
|
runAndReport("Battle 3/3: vs Walls", "Walls", wallsDir)
|
||||||
|
echo "\nAccelGun gauntlet complete."
|
||||||
@@ -0,0 +1,92 @@
|
|||||||
|
## Acceleration predictor gun: tracks velocity and heading changes across frames,
|
||||||
|
## extrapolates with accel+turn for bots that speed up/slow down.
|
||||||
|
## Needs 3+ frames for stable accel; falls back to linear (2 frames) or head-on (1 frame).
|
||||||
|
## Coordinate system: 0° = East, CCW positive (Tank Royale standard).
|
||||||
|
|
||||||
|
import std/math
|
||||||
|
import gun_harness/gun_interface
|
||||||
|
|
||||||
|
const MaxSpeed = 8.0 ## TR bot max speed (px/tick)
|
||||||
|
|
||||||
|
type AccelGun* = object
|
||||||
|
prevSpeed: float
|
||||||
|
prevHeading: float
|
||||||
|
prevPrevSpeed: float
|
||||||
|
prevTick: int
|
||||||
|
frames: int ## how many observations we have
|
||||||
|
cachedAccel: float ## accel (px/tick²) cached per tick
|
||||||
|
cachedTurnRate: float ## turn rate (deg/tick) cached per tick
|
||||||
|
cachedTick: int
|
||||||
|
|
||||||
|
proc initAccelGun*(): AccelGun =
|
||||||
|
AccelGun()
|
||||||
|
|
||||||
|
proc predict*(g: var AccelGun, state: WorldState, bulletSpeed: float): GunPrediction =
|
||||||
|
if bulletSpeed <= 0.0:
|
||||||
|
return GunPrediction(x: state.enemyX, y: state.enemyY)
|
||||||
|
|
||||||
|
# Update observation window on new tick
|
||||||
|
if state.tick > g.cachedTick:
|
||||||
|
if g.frames >= 1:
|
||||||
|
# Compute turn rate (normalised to [-180, 180])
|
||||||
|
var tr = state.enemyHeading - g.prevHeading
|
||||||
|
if tr > 180.0: tr -= 360.0
|
||||||
|
elif tr < -180.0: tr += 360.0
|
||||||
|
let tickDelta = max(1, state.tick - g.prevTick).float
|
||||||
|
g.cachedTurnRate = tr / tickDelta
|
||||||
|
|
||||||
|
if g.frames >= 2:
|
||||||
|
# Compute acceleration
|
||||||
|
g.cachedAccel = (state.enemySpeed - g.prevSpeed) / tickDelta
|
||||||
|
else:
|
||||||
|
g.cachedAccel = 0.0
|
||||||
|
else:
|
||||||
|
g.cachedTurnRate = 0.0
|
||||||
|
g.cachedAccel = 0.0
|
||||||
|
|
||||||
|
# Advance window
|
||||||
|
g.prevPrevSpeed = g.prevSpeed
|
||||||
|
g.prevSpeed = state.enemySpeed
|
||||||
|
g.prevHeading = state.enemyHeading
|
||||||
|
g.prevTick = state.tick
|
||||||
|
g.cachedTick = state.tick
|
||||||
|
inc g.frames
|
||||||
|
|
||||||
|
# fallback: not enough history
|
||||||
|
if g.frames < 2:
|
||||||
|
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)
|
||||||
|
var t = dist / bulletSpeed # initial ticks estimate
|
||||||
|
|
||||||
|
let accel = g.cachedAccel
|
||||||
|
let turnRate = g.cachedTurnRate # deg/tick
|
||||||
|
|
||||||
|
# Iterative convergence (same pattern as circular gun)
|
||||||
|
var ex = state.enemyX
|
||||||
|
var ey = state.enemyY
|
||||||
|
for _ in 0..4:
|
||||||
|
var vx = state.enemySpeed
|
||||||
|
var hd = state.enemyHeading # degrees
|
||||||
|
var px = state.enemyX
|
||||||
|
var py = state.enemyY
|
||||||
|
let iters = int(t + 0.5)
|
||||||
|
for _ in 0..<iters:
|
||||||
|
vx = clamp(vx + accel, -MaxSpeed, MaxSpeed)
|
||||||
|
hd += turnRate
|
||||||
|
let hr = degToRad(hd)
|
||||||
|
px += vx * cos(hr)
|
||||||
|
py += vx * sin(hr)
|
||||||
|
# Clamp to arena
|
||||||
|
ex = clamp(px, BotRadius, state.arenaWidth - BotRadius)
|
||||||
|
ey = clamp(py, BotRadius, state.arenaHeight - BotRadius)
|
||||||
|
let ndx = ex - state.selfX
|
||||||
|
let ndy = ey - state.selfY
|
||||||
|
t = sqrt(ndx*ndx + ndy*ndy) / bulletSpeed
|
||||||
|
|
||||||
|
GunPrediction(x: ex, y: ey)
|
||||||
|
|
||||||
|
proc onResult*(g: var AccelGun, e: FeedbackEvent) =
|
||||||
|
discard # analytical gun — no learning
|
||||||
Reference in New Issue
Block a user