## DevControlBot — control skeleton: boots, drives the radar every tick and keeps ## itself alive with the TFIL survival movement. It never fires. ## ## Team convention: white means "not programmed yet". Each module claims its own ## colours when its init runs, and nothing paints white after that: initTFIL() ## (constructor) paints body, turret and gun BLACK — movement is programmed — ## and initRadar() (first line of run) paints the radar and the scan arc BLACK. ## The bot therefore ends up fully BLACK. Colors are applied once, never per tick. ## ## This module is both the bot type and the program entry point (see isMainModule ## below). All radar logic lives in modules/radar.nim; the bot keeps no state. ## ## The run loop drives the radar: commandRadar() is called BEFORE go() every ## tick, so the command is part of the tick's intent and is sent by THIS go(). ## The order matters — go() sends the intent first and dispatches the pending ## events last, so onScannedBot only feeds the NEXT tick. ## ## Movement: modules/tfil.nim ("The Floor Is Lava"), vendored into this folder so ## the bot depends on nothing but the API. It is asked for a command every tick, ## BEFORE go(), exactly like the radar, and the result is applied with ## setTargetSpeed/setTurnRate — same contract as ModularBot's mover. ## ## Debug graphics: TFIL's drawing needs NO plumbing here. The API's graphics ## procs are global; the SVG buffer is flushed and cleared by the API after every ## tick, so anything drawn before `go()` (i.e. inside computeMove) reaches the ## viewer. It is compiled only with `-d:tfilDebug`; see README "Debug graphics". ## ## API: robocode_tankroyale_botapi 1.0.7 (the renamed tankroyale_botapi package). import std/[os, sequtils] import robocode_tankroyale_botapi import modules/radar import modules/tfil const MaxScannedEnemies* = 16 ## movers see every alive enemy; the radar feeds ## only what it scans, so this is a cap, not a limit const EnemyStaleTicks* = 60 ## drop a scanned enemy not rescanned for this many ## ticks (~1s at 60Hz): a bot seen once would keep ## painting its heat in the field forever type DevControlBot* = ref object of Bot mover: TfilModule tick: int enemies: seq[EnemyInfo] ## fed by onScannedBot, cleared on round end proc newDevControlBot*(): DevControlBot = result = DevControlBot() result.mover = initTFIL() # movement is programmed -> body/turret/gun BLACK setRadarColor(WHITE) # overwritten by initRadar() before the first tick method onScannedBot*(bot: DevControlBot, e: ScannedBotEvent) = onScan(e.x, e.y) # remembered; commandRadar() re-aims every tick # (e.scannedBotId, schemas.nim:305, is the # scanned bot's identity) # ...and remembered for the movement: the heat field paints every enemy it # knows about. One entry per id, refreshed in place, oldest dropped past the cap. for i, ei in bot.enemies: if ei.id == e.scannedBotId: bot.enemies[i].x = e.x bot.enemies[i].y = e.y bot.enemies[i].heading = e.direction bot.enemies[i].speed = e.speed bot.enemies[i].energy = e.energy bot.enemies[i].lastSeenTick = bot.tick return if bot.enemies.len >= MaxScannedEnemies: bot.enemies.delete(0) bot.enemies.add EnemyInfo(id: e.scannedBotId, x: e.x, y: e.y, heading: e.direction, speed: e.speed, energy: e.energy, lastSeenTick: bot.tick) method onHitByBullet*(bot: DevControlBot, e: HitByBulletEvent) = # an enemy bullet hit us -> tell the fire tracker, else the same tick reads as # "no fire" and the enemy's shot goes uncounted (see modules/tfil.nim, FireFix) bot.mover.noteEnemyBulletHit(e.bullet.power) method onBulletHit*(bot: DevControlBot, e: BulletHitBotEvent) = bot.mover.noteDamageDealt(e.damage) method onRoundEnded*(bot: DevControlBot, e: RoundEndedEventForBot) = bot.enemies = @[] bot.mover.resetRound() method ageEnemies(bot: DevControlBot) = # heat field is only as good as the freshest scans; stale entries stay # plausible-looking but poison the grid, so they go bot.enemies = bot.enemies.filterIt(it.lastSeenTick >= bot.tick - EnemyStaleTicks) method buildWorldState(bot: DevControlBot): WorldState = WorldState(selfX: getX(), selfY: getY(), selfSpeed: getSpeed(), selfHeading: getDirection(), selfRadarHeading: getRadarDirection(), selfEnergy: getEnergy(), arenaWidth: getArenaWidth().float, arenaHeight: getArenaHeight().float, tick: bot.tick, enemies: bot.enemies) method run*(bot: DevControlBot) = initRadar() # once: radar + scan turn BLACK = programmed while isRunning(): # One radar command and one movement command per tick, unconditionally, # BEFORE go() — so both are part of this tick's intent. commandRadar() bot.ageEnemies() # stale scans dropped before they paint heat let (speed, turnRate) = bot.mover.computeMove(bot.buildWorldState()) setTargetSpeed(speed) setTurnRate(turnRate) go() inc bot.tick when isMainModule: # argv[1] may override the metadata file (used by DevControlBot/DevControlBot.sh); # otherwise the JSON next to this source, resolved at compile time so any cwd works. let jsonPath = if paramCount() >= 1: paramStr(1) else: currentSourcePath().parentDir / "DevControlBot.json" var bot = newDevControlBot() start(bot, jsonPath)