From 938c259260daa7005c75c7e6a78de364d2e6c9a5 Mon Sep 17 00:00:00 2001 From: Davide Cappellini Date: Thu, 17 Sep 2026 23:17:26 +0200 Subject: [PATCH] feat(BNNBot): add wall distances for both bots to binary encoding (100 bits total) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 8 new fields in ScanData (myWallN/S/E/W, enemyWallN/S/E/W), 7 bits each via Gray code, appended after existing 44 bits. TOTAL_BITS 44 → 100. Co-Authored-By: Claude Sonnet 4.6 --- BNNBot_garage/src/BNNBot.nim | 20 ++++++++++++++++---- BNNBot_garage/src/binary_encoding.nim | 25 ++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/BNNBot_garage/src/BNNBot.nim b/BNNBot_garage/src/BNNBot.nim index 413e728..e5c5b71 100644 --- a/BNNBot_garage/src/BNNBot.nim +++ b/BNNBot_garage/src/BNNBot.nim @@ -36,11 +36,23 @@ method onScannedBot*(bot: BNNBot, e: ScannedBotEvent) = bot.hasLastPos = true bot.hasContact = true + let arenaW = getArenaWidth().float + let arenaH = getArenaHeight().float + let enemyX = e.x + let enemyY = e.y let scan = ScanData( - bearing: bot.enemyBearing, - distance: bot.distance, - velocity: bot.velocity, - heading: bot.heading, + bearing: bot.enemyBearing, + distance: bot.distance, + velocity: bot.velocity, + heading: bot.heading, + myWallN: arenaH - by, + myWallS: by, + myWallE: arenaW - bx, + myWallW: bx, + enemyWallN: arenaH - enemyY, + enemyWallS: enemyY, + enemyWallE: arenaW - enemyX, + enemyWallW: enemyX, ) let vec = encode(scan) let hamming = if bot.hasPrev: hammingDistance(bot.prevVec, vec) else: 0 diff --git a/BNNBot_garage/src/binary_encoding.nim b/BNNBot_garage/src/binary_encoding.nim index 2acce7e..708e950 100644 --- a/BNNBot_garage/src/binary_encoding.nim +++ b/BNNBot_garage/src/binary_encoding.nim @@ -1,7 +1,7 @@ import std/math const - TOTAL_BITS* = 44 + TOTAL_BITS* = 100 type BinaryVector* = array[TOTAL_BITS, uint8] @@ -11,6 +11,14 @@ type distance*: float # 0-1414 pixels (will be converted to 0-100%) velocity*: float # -8 to +8 integer steps heading*: float # 0-360 degrees + myWallN*: float # our distance to north wall + myWallS*: float # our distance to south wall + myWallE*: float # our distance to east wall + myWallW*: float # our distance to west wall + enemyWallN*: float # enemy distance to north wall + enemyWallS*: float # enemy distance to south wall + enemyWallE*: float # enemy distance to east wall + enemyWallW*: float # enemy distance to west wall const MAX_DISTANCE = 1414.0 # diagonal of 1000x1000 arena @@ -60,6 +68,21 @@ proc encode*(scan: ScanData): BinaryVector = let hCos = int((cos(degToRad(scan.heading)) + 1.0) / 2.0 * 199.0) let hCosBits = toBits(clamp(hCos, 0, 199), 8) for i in 0..<8: result[offset + i] = hCosBits[i] + offset += 8 + + # 7. My wall distances (7 bits each, 0-100% of arena dimension) + for wall in [scan.myWallN, scan.myWallS, scan.myWallE, scan.myWallW]: + let pct = int(clamp(wall / 1000.0 * 99.0, 0.0, 99.0)) + let bits = toBits(pct, 7) + for i in 0..<7: result[offset + i] = bits[i] + offset += 7 + + # 8. Enemy wall distances (7 bits each) + for wall in [scan.enemyWallN, scan.enemyWallS, scan.enemyWallE, scan.enemyWallW]: + let pct = int(clamp(wall / 1000.0 * 99.0, 0.0, 99.0)) + let bits = toBits(pct, 7) + for i in 0..<7: result[offset + i] = bits[i] + offset += 7 proc formatBinary*(vec: BinaryVector): string = for b in vec: