feat(ModularBot): displacement-vector gun, 11 guns total
- Displacement gun: average velocity over sliding window, captures drift - Complements instantaneous-velocity linear gun - Battle-tested vs Crazy, WaveSurfer, PatternMover
This commit is contained in:
@@ -20,12 +20,13 @@ import guns/anti_surfer
|
|||||||
import guns/wall_bounce
|
import guns/wall_bounce
|
||||||
import guns/accel_predictor
|
import guns/accel_predictor
|
||||||
import guns/stop_shot
|
import guns/stop_shot
|
||||||
|
import guns/displacement
|
||||||
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", "Accel", "StopShot"]
|
const GunNames = ["HeadOn", "Linear", "Tsetlin", "Circular", "GuessFactor", "Pattern", "AntiSurf", "WallBounce", "Accel", "StopShot", "Displace"]
|
||||||
|
|
||||||
type
|
type
|
||||||
ModularBot = ref object of Bot
|
ModularBot = ref object of Bot
|
||||||
@@ -44,6 +45,7 @@ type
|
|||||||
wallBounce: WallBounceGun
|
wallBounce: WallBounceGun
|
||||||
accelGun: AccelGun
|
accelGun: AccelGun
|
||||||
stopShot: StopShotGun
|
stopShot: StopShotGun
|
||||||
|
displace: DisplacementGun
|
||||||
mover: PhantomMeteorModule
|
mover: PhantomMeteorModule
|
||||||
virtualHits: int
|
virtualHits: int
|
||||||
virtualMiss: int
|
virtualMiss: int
|
||||||
@@ -84,6 +86,7 @@ method onScannedBot*(bot: ModularBot, e: ScannedBotEvent) =
|
|||||||
var wbPreds: array[len(PowerBins), GunPrediction]
|
var wbPreds: array[len(PowerBins), GunPrediction]
|
||||||
var acPreds: array[len(PowerBins), GunPrediction]
|
var acPreds: array[len(PowerBins), GunPrediction]
|
||||||
var ssPreds: array[len(PowerBins), GunPrediction]
|
var ssPreds: array[len(PowerBins), GunPrediction]
|
||||||
|
var dsPreds: 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]))
|
||||||
@@ -95,6 +98,7 @@ method onScannedBot*(bot: ModularBot, e: ScannedBotEvent) =
|
|||||||
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]))
|
acPreds[i] = bot.accelGun.predict(bot.lastState, bulletSpeed(PowerBins[i]))
|
||||||
ssPreds[i] = bot.stopShot.predict(bot.lastState, bulletSpeed(PowerBins[i]))
|
ssPreds[i] = bot.stopShot.predict(bot.lastState, bulletSpeed(PowerBins[i]))
|
||||||
|
dsPreds[i] = bot.displace.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)
|
||||||
@@ -108,6 +112,7 @@ method onScannedBot*(bot: ModularBot, e: ScannedBotEvent) =
|
|||||||
bot.tracker.spawnBullets(7, wbPreds, bot.lastState)
|
bot.tracker.spawnBullets(7, wbPreds, bot.lastState)
|
||||||
bot.tracker.spawnBullets(8, acPreds, bot.lastState)
|
bot.tracker.spawnBullets(8, acPreds, bot.lastState)
|
||||||
bot.tracker.spawnBullets(9, ssPreds, bot.lastState)
|
bot.tracker.spawnBullets(9, ssPreds, bot.lastState)
|
||||||
|
bot.tracker.spawnBullets(10, dsPreds, bot.lastState)
|
||||||
|
|
||||||
# Resolve bullets that have travelled far enough
|
# Resolve bullets that have travelled far enough
|
||||||
let st = bot.lastState
|
let st = bot.lastState
|
||||||
@@ -128,6 +133,7 @@ method onScannedBot*(bot: ModularBot, e: ScannedBotEvent) =
|
|||||||
of 7: bot.wallBounce.onResult(fe)
|
of 7: bot.wallBounce.onResult(fe)
|
||||||
of 8: bot.accelGun.onResult(fe)
|
of 8: bot.accelGun.onResult(fe)
|
||||||
of 9: bot.stopShot.onResult(fe)
|
of 9: bot.stopShot.onResult(fe)
|
||||||
|
of 10: bot.displace.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:
|
||||||
@@ -154,6 +160,7 @@ method onScannedBot*(bot: ModularBot, e: ScannedBotEvent) =
|
|||||||
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
|
of 8: setTurretColor("#FF00FF"); setBulletColor("#FF66FF") # Accel: magenta
|
||||||
of 9: setTurretColor("#990000"); setBulletColor("#CC3333") # StopShot: dark red
|
of 9: setTurretColor("#990000"); setBulletColor("#CC3333") # StopShot: dark red
|
||||||
|
of 10: setTurretColor("#006600"); setBulletColor("#009900") # Displace: dark green
|
||||||
else: discard
|
else: discard
|
||||||
|
|
||||||
let pred = case selectedGun
|
let pred = case selectedGun
|
||||||
@@ -166,6 +173,7 @@ method onScannedBot*(bot: ModularBot, e: ScannedBotEvent) =
|
|||||||
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))
|
of 8: bot.accelGun.predict(bot.lastState, bulletSpeed(power))
|
||||||
of 9: bot.stopShot.predict(bot.lastState, bulletSpeed(power))
|
of 9: bot.stopShot.predict(bot.lastState, bulletSpeed(power))
|
||||||
|
of 10: bot.displace.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)
|
||||||
|
|
||||||
@@ -225,7 +233,7 @@ method run*(bot: ModularBot) =
|
|||||||
|
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
var bot = ModularBot(
|
var bot = ModularBot(
|
||||||
tracker: vb.initTracker(10), # 0: HeadOn, 1: Linear, 2: Tsetlin, 3: Circular, 4: GuessFactor, 5: Pattern, 6: AntiSurf, 7: WallBounce, 8: Accel, 9: StopShot
|
tracker: vb.initTracker(11), # 0: HeadOn, 1: Linear, 2: Tsetlin, 3: Circular, 4: GuessFactor, 5: Pattern, 6: AntiSurf, 7: WallBounce, 8: Accel, 9: StopShot, 10: Displace
|
||||||
headOn: HeadOnGun(),
|
headOn: HeadOnGun(),
|
||||||
linear: LinearGun(),
|
linear: LinearGun(),
|
||||||
circular: CircularGun(),
|
circular: CircularGun(),
|
||||||
@@ -236,6 +244,7 @@ when isMainModule:
|
|||||||
wallBounce: initWallBounceGun(),
|
wallBounce: initWallBounceGun(),
|
||||||
accelGun: initAccelGun(),
|
accelGun: initAccelGun(),
|
||||||
stopShot: initStopShotGun(),
|
stopShot: initStopShotGun(),
|
||||||
|
displace: initDisplacementGun(),
|
||||||
radar: RadarLockModule(),
|
radar: RadarLockModule(),
|
||||||
mover: initPhantomMeteor(),
|
mover: initPhantomMeteor(),
|
||||||
currentGun: -1,
|
currentGun: -1,
|
||||||
|
|||||||
@@ -0,0 +1,65 @@
|
|||||||
|
## Displacement-vector gun: predict using average velocity over a sliding window.
|
||||||
|
## Captures drift/tendency that instantaneous velocity misses
|
||||||
|
## (e.g. oscillating enemy slowly drifting east has v=0 at midpoints but
|
||||||
|
## displacement still pointing east).
|
||||||
|
|
||||||
|
import std/math
|
||||||
|
import gun_harness/gun_interface
|
||||||
|
|
||||||
|
const WindowSize = 15 # ticks; N+1 frames stored
|
||||||
|
|
||||||
|
type
|
||||||
|
DisplacementGun* = object
|
||||||
|
posX: array[WindowSize + 1, float]
|
||||||
|
posY: array[WindowSize + 1, float]
|
||||||
|
count: int # frames collected so far
|
||||||
|
head: int # ring-buffer head
|
||||||
|
lastTick: int # for per-tick cache
|
||||||
|
cacheSpeed: float
|
||||||
|
cachePred: GunPrediction
|
||||||
|
|
||||||
|
proc predict*(g: var DisplacementGun, state: WorldState, bulletSpeed: float): GunPrediction =
|
||||||
|
# Per-tick cache: same tick + same speed => same prediction
|
||||||
|
if state.tick == g.lastTick and bulletSpeed == g.cacheSpeed:
|
||||||
|
return g.cachePred
|
||||||
|
|
||||||
|
# Push current position into ring buffer
|
||||||
|
g.head = (g.head + 1) mod (WindowSize + 1)
|
||||||
|
g.posX[g.head] = state.enemyX
|
||||||
|
g.posY[g.head] = state.enemyY
|
||||||
|
if g.count < WindowSize + 1:
|
||||||
|
inc g.count
|
||||||
|
|
||||||
|
g.lastTick = state.tick
|
||||||
|
g.cacheSpeed = bulletSpeed
|
||||||
|
|
||||||
|
# Need at least N+1 frames; fall back to head-on if not enough
|
||||||
|
if g.count < WindowSize + 1:
|
||||||
|
g.cachePred = GunPrediction(x: state.enemyX, y: state.enemyY)
|
||||||
|
return g.cachePred
|
||||||
|
|
||||||
|
# Oldest frame is (head + 1) mod (WindowSize + 1)
|
||||||
|
let oldest = (g.head + 1) mod (WindowSize + 1)
|
||||||
|
let dx = (state.enemyX - g.posX[oldest]) / WindowSize.float
|
||||||
|
let dy = (state.enemyY - g.posY[oldest]) / WindowSize.float
|
||||||
|
|
||||||
|
# Iterate time estimate 5 times
|
||||||
|
let dist0 = hypot(state.enemyX - state.selfX, state.enemyY - state.selfY)
|
||||||
|
var ticks = dist0 / bulletSpeed
|
||||||
|
var px = state.enemyX
|
||||||
|
var py = state.enemyY
|
||||||
|
for _ in 0..4:
|
||||||
|
px = state.enemyX + dx * ticks
|
||||||
|
py = state.enemyY + dy * ticks
|
||||||
|
ticks = hypot(px - state.selfX, py - state.selfY) / bulletSpeed
|
||||||
|
|
||||||
|
px = clamp(px, 0.0, state.arenaWidth)
|
||||||
|
py = clamp(py, 0.0, state.arenaHeight)
|
||||||
|
g.cachePred = GunPrediction(x: px, y: py)
|
||||||
|
g.cachePred
|
||||||
|
|
||||||
|
proc onResult*(g: var DisplacementGun, e: FeedbackEvent) =
|
||||||
|
discard # analytical gun — no learning
|
||||||
|
|
||||||
|
proc initDisplacementGun*(): DisplacementGun =
|
||||||
|
DisplacementGun(count: 0, head: 0, lastTick: -1)
|
||||||
Reference in New Issue
Block a user