import { CHUNK_SIZE } from "../protocol/constants.js"; import { GroundType, normalizeGround, normalizeBlock, groundAverageColor, blockAverageColor } from "../definitions/tiles.js"; /** Odd span so one chunk sits in the centre of the editor minimap. */ export const MINIMAP_SPAN_CHUNKS = 25; /** * One minimap pixel per tile. Chunks are {@link CHUNK_SIZE} tiles on a side * (16 today), so each cached chunk image is that many pixels square. */ export const MINIMAP_CHUNK_PIXELS = CHUNK_SIZE; /** Unauthored / missing chunks. Matches the editor sidebar (`#1a1e26`). */ export const MINIMAP_EMPTY_COLOR = Object.freeze({ r: 0x1a, g: 0x1e, b: 0x26 }); /** Saved tile whose sprite has no computed colour. */ export const MINIMAP_MISSING_COLOR = Object.freeze({ r: 0x3a, g: 0x42, b: 0x50 }); export function minimapRadius (span = MINIMAP_SPAN_CHUNKS) { return (span - 1) >> 1; } /** * Chunk under a minimap pixel. Coordinates are in the bitmap's pixel space * (top-left of the whole minimap is 0, 0), not CSS pixels. * @returns {{ chunkX: number, chunkY: number } | null} */ export function minimapChunkAtPixel ( pixelX, pixelY, centerChunkX, centerChunkY, span = MINIMAP_SPAN_CHUNKS, chunkPixels = MINIMAP_CHUNK_PIXELS ) { if (!Number.isFinite(pixelX) || !Number.isFinite(pixelY) || chunkPixels <= 0) { return null; } const col = Math.floor(pixelX / chunkPixels); const row = Math.floor(pixelY / chunkPixels); if (col < 0 || row < 0 || col >= span || row >= span) { return null; } const radius = minimapRadius(span); return { chunkX: (centerChunkX | 0) + col - radius, chunkY: (centerChunkY | 0) + row - radius }; } /** * 16×16 (or chunk-sized) RGBA image for one saved chunk. * A block colour wins where a block is present; otherwise the ground colour. * Uses the authored state when that state has its own colour, else the type default. * @param {{ grounds?: unknown[], blocks?: unknown[] } | null | undefined} chunkData * @param {number} [fallbackGroundTypeId] * @returns {Uint8ClampedArray} */ export function renderChunkMinimapRgba (chunkData, fallbackGroundTypeId = GroundType.grass) { const tileCount = MINIMAP_CHUNK_PIXELS * MINIMAP_CHUNK_PIXELS; const rgba = new Uint8ClampedArray(tileCount * 4); const grounds = chunkData?.grounds ?? []; const blocks = chunkData?.blocks ?? []; for (let index = 0; index < tileCount; index++) { const block = normalizeBlock(blocks[index]); const ground = normalizeGround(grounds[index], fallbackGroundTypeId); const color = (block && blockAverageColor(block.typeId, block.state)) || groundAverageColor(ground.typeId, ground.state) || MINIMAP_MISSING_COLOR; const offset = index * 4; rgba[offset] = color.r; rgba[offset + 1] = color.g; rgba[offset + 2] = color.b; rgba[offset + 3] = 255; } return rgba; }