# RandomMover — random speed/turn with persistence, head-on firing. # Purpose: test guns against unpredictable movement. import std/[math, os, random] import robocode_tankroyale_botapi const botJsonPath = currentSourcePath().parentDir / "RandomMover.json" type RandomMover = ref object of Bot currentSpeed: float currentTurn: float ticksUntilChange: int proc newDirection(bot: RandomMover) = bot.currentSpeed = rand(-8.0..8.0) # The server caps body turn at calcMaxTurnRate(speed) = 10 - 0.75*|speed| # (~4 deg/tick at full speed); keep the random intent inside that limit so it # is not silently saturated by the server. let maxTurn = calcMaxTurnRate(bot.currentSpeed) bot.currentTurn = rand(-maxTurn..maxTurn) bot.ticksUntilChange = rand(10..20) method onRoundStarted*(bot: RandomMover, e: RoundStartedEvent) = randomize() setAdjustGunForBodyTurn(true) setAdjustRadarForBodyTurn(true) setAdjustRadarForGunTurn(true) newDirection(bot) method onScannedBot*(bot: RandomMover, e: ScannedBotEvent) = 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) < 3.0 and getGunHeat() <= 0.0: discard setFire(1.0) method run*(bot: RandomMover) = while isRunning(): dec bot.ticksUntilChange if bot.ticksUntilChange <= 0: newDirection(bot) setTargetSpeed(bot.currentSpeed) setTurnRate(bot.currentTurn) setRadarTurnRate(45.0) go() when isMainModule: var bot = RandomMover() start(bot, botJsonPath)