Files
SirRoboGarage/libs/radar_lock/radar_lock.nim
2026-08-20 23:33:40 +02:00

39 lines
1.3 KiB
Nim

## Standalone radar lock module for Robocode Tank Royale (1v1).
## No bot-specific imports — takes plain floats, returns radarTurnRate.
##
## Tank Royale radar uses standard math convention: 0° = east, CCW positive.
## Angles are in degrees.
import std/math
const
MaxRadarTurn* = 45.0
DefaultOvershootDeg* = 5.0
var overShootDeg* = DefaultOvershootDeg
proc init*() =
## Reset module to defaults.
## In your bot's constructor set:
## adjustRadarForBodyTurn = true
## adjustRadarForGunTurn = true
overShootDeg = DefaultOvershootDeg
proc normalizeRelative(angle: float64): float64 {.inline.} =
result = angle mod 360.0
if result >= 180.0: result -= 360.0
elif result < -180.0: result += 360.0
proc doRadar*(currentRadarHeading, enemyBearing: float64): float64 =
## Returns radarTurnRate (degrees/tick, positive = clockwise).
##
## currentRadarHeading: current radar direction in degrees (0=east, CCW+).
## enemyBearing: absolute bearing to enemy in same coordinate system.
##
## Handles angle wrapping, clamps to [-45, +45], adds overshoot sweep.
var turn = normalizeRelative(enemyBearing - currentRadarHeading)
# Add overshoot in the same direction as the turn to maintain lock
if turn < 0.0: turn -= overShootDeg
else: turn += overShootDeg
result = turn.clamp(-MaxRadarTurn, MaxRadarTurn)