import { readFileSync } from 'fs'; import type { Position } from '../../../sdk/types.js'; // Walkability bitmap baked from the server's inflated_weights (walls already // inflated by the agent collision radius, so a walkable tile means the agent // center may stand there). Regenerate with scripts/export-walkable-grid.py. const HEADER_SIZE = 20; export interface WalkableGrid { readonly width: number; readonly height: number; readonly tileSize: number; isWalkable(tx: number, ty: number): boolean; isWalkableCell(cell: number): boolean; /** cell = ty * width + tx; world coords are clamped into the map. */ worldToCell(wx: number, wy: number): number; /** Tile center in world coords. */ cellToWorld(cell: number): Position; /** Nearest walkable cell within maxRadius tiles (Chebyshev rings); -1 if none. */ snapToWalkable(cell: number, maxRadius?: number): number; } class Grid implements WalkableGrid { readonly width: number; readonly height: number; readonly tileSize: number; private readonly bits: Buffer; private readonly rowBytes: number; constructor(buf: Buffer) { if (buf.toString('ascii', 0, 4) !== 'CCWG') throw new Error('walkable grid: bad magic'); const version = buf.readUInt32LE(4); if (version !== 1) throw new Error(`walkable grid: unsupported version ${version}`); this.width = buf.readUInt32LE(8); this.height = buf.readUInt32LE(12); this.tileSize = buf.readUInt32LE(16); this.rowBytes = Math.ceil(this.width / 8); if (buf.length !== HEADER_SIZE + this.rowBytes * this.height) { throw new Error('walkable grid: size mismatch'); } this.bits = buf; } isWalkable(tx: number, ty: number): boolean { if (tx < 0 || ty < 0 || tx >= this.width || ty >= this.height) return false; const byte = this.bits[HEADER_SIZE + ty * this.rowBytes + (tx >> 3)]; return ((byte >> (tx & 7)) & 1) === 1; } isWalkableCell(cell: number): boolean { return this.isWalkable(cell % this.width, Math.floor(cell / this.width)); } worldToCell(wx: number, wy: number): number { const tx = Math.min(this.width - 1, Math.max(0, Math.floor(wx / this.tileSize))); const ty = Math.min(this.height - 1, Math.max(0, Math.floor(wy / this.tileSize))); return ty * this.width + tx; } cellToWorld(cell: number): Position { const tx = cell % this.width; const ty = Math.floor(cell / this.width); return { x: tx * this.tileSize + this.tileSize / 2, y: ty * this.tileSize + this.tileSize / 2 }; } snapToWalkable(cell: number, maxRadius = 8): number { const cx = cell % this.width; const cy = Math.floor(cell / this.width); for (let r = 0; r <= maxRadius; r++) { let best = -1; let bestSq = Infinity; for (let dy = -r; dy <= r; dy++) { for (let dx = -r; dx <= r; dx++) { if (Math.max(Math.abs(dx), Math.abs(dy)) !== r) continue; if (!this.isWalkable(cx + dx, cy + dy)) continue; const sq = dx * dx + dy * dy; if (sq < bestSq) { bestSq = sq; best = (cy + dy) * this.width + (cx + dx); } } } if (best >= 0) return best; } return -1; } } let cached: WalkableGrid | null = null; export function loadWalkableGrid(): WalkableGrid { if (!cached) { cached = new Grid(readFileSync(new URL('./clawclaw-walkable.bin', import.meta.url))); } return cached; } const STOP_NEIGHBORS = [[1, 0], [-1, 0], [0, 1], [0, -1], [1, 1], [1, -1], [-1, 1], [-1, -1]]; /** * A walkable tile center >= 2px away from pos, used to stop in place: the * server ignores moves shorter than 1px without interrupting the current * movement, so stopping means moving somewhere nearby. */ export function nearbyStopPoint(pos: Position): Position { const grid = loadWalkableGrid(); const cell = grid.worldToCell(pos.x, pos.y); const tx = cell % grid.width; const ty = Math.floor(cell / grid.width); for (const [dx, dy] of STOP_NEIGHBORS) { if (grid.isWalkable(tx + dx, ty + dy)) return grid.cellToWorld(cell + dy * grid.width + dx); } const center = grid.cellToWorld(cell); if (Math.hypot(center.x - pos.x, center.y - pos.y) >= 1.5) return center; return { x: pos.x + 2, y: pos.y }; }