# OscillatorBot — predictable zigzag sparring partner for GA gun testing. # Reverses direction + turn every PERIOD ticks. Head-on targeting only. import std/[math, os] import tankroyale_botapi const botJsonPath = currentSourcePath().parentDir / "OscillatorBot.json" const SPEED = 7.0 # forward/backward speed PERIOD = 25 # ticks between direction reversals type OscillatorBot = ref object of Bot tickCount: int moveSign: float # +1 forward, -1 backward turnSign: float # +1 right, -1 left method onRoundStarted*(bot: OscillatorBot, e: RoundStartedEvent) = setAdjustGunForBodyTurn(true) setAdjustRadarForBodyTurn(true) setAdjustRadarForGunTurn(true) bot.tickCount = 0 bot.moveSign = 1.0 bot.turnSign = 1.0 method onScannedBot*(bot: OscillatorBot, e: ScannedBotEvent) = # Head-on targeting: aim gun directly at enemy, fire medium power let bearing = directionTo(getX(), getY(), e.x, e.y) let gunDelta = normalizeRelativeAngle(bearing - getGunDirection()) setGunTurnRate(gunDelta.clamp(-MAX_GUN_TURN_RATE, MAX_GUN_TURN_RATE)) if abs(gunDelta) < 10.0 and getGunHeat() <= 0.0: discard setFire(2.0) method run*(bot: OscillatorBot) = while isRunning(): inc bot.tickCount if bot.tickCount mod PERIOD == 0: bot.moveSign *= -1.0 bot.turnSign *= -1.0 setTargetSpeed(bot.moveSign * SPEED) setTurnRate(bot.turnSign * 4.0) setRadarTurnRate(45.0) # spin radar to keep scanning go() when isMainModule: var bot = OscillatorBot(moveSign: 1.0, turnSign: 1.0) start(bot, botJsonPath)