import { STATIC_CHUNK_RADIUS } from "/shared/protocol/constants.js"; import { normalizeDimensionId } from "/shared/world/dimensions.js"; import { MINIMAP_CHUNK_PIXELS, MINIMAP_EMPTY_COLOR, MINIMAP_SPAN_CHUNKS, minimapChunkAtPixel, minimapRadius } from "/shared/render/chunkMinimap.js"; const VIEWPORT_STROKE = "#d5dbe6"; const CURRENT_STROKE = "#e23d3d"; /** * Editor minimap. Chunk images are cached in memory after the server sends them * and redrawn locally. The durable cache is the server's per-dimension RGBA files. */ export function initEditorMinimap ({ canvas, caption, getCenter, onChunkClick }) { const context = canvas.getContext("2d", { alpha: false }); const radius = minimapRadius(MINIMAP_SPAN_CHUNKS); const size = MINIMAP_SPAN_CHUNKS * MINIMAP_CHUNK_PIXELS; canvas.width = size; canvas.height = size; /** @type {Map} */ const images = new Map(); function cacheKey (dimensionId, chunkX, chunkY) { return `${normalizeDimensionId(dimensionId)}:${chunkX | 0},${chunkY | 0}`; } function captionFor (chunkX, chunkY, hovering) { if (!caption) { return; } const where = `Chunk ${chunkX}, ${chunkY}`; caption.textContent = hovering ? where : `${where} ยท click to centre`; } function draw () { const center = getCenter(); const dimensionId = normalizeDimensionId(center.dimensionId); const image = context.createImageData(size, size); const empty = MINIMAP_EMPTY_COLOR; for (let i = 0; i < image.data.length; i += 4) { image.data[i] = empty.r; image.data[i + 1] = empty.g; image.data[i + 2] = empty.b; image.data[i + 3] = 255; } for (let dy = -radius; dy <= radius; dy++) { for (let dx = -radius; dx <= radius; dx++) { const chunkX = (center.chunkX | 0) + dx; const chunkY = (center.chunkY | 0) + dy; const pixels = images.get(cacheKey(dimensionId, chunkX, chunkY)); if (!pixels || pixels.length !== MINIMAP_CHUNK_PIXELS * MINIMAP_CHUNK_PIXELS * 4) { continue; } blit(image.data, size, pixels, (dx + radius) * MINIMAP_CHUNK_PIXELS, (dy + radius) * MINIMAP_CHUNK_PIXELS); } } context.putImageData(image, 0, 0); strokeViewport(); strokeCurrentChunk(); captionFor(center.chunkX | 0, center.chunkY | 0, false); } function strokeViewport () { const view = Math.max(0, STATIC_CHUNK_RADIUS | 0); const origin = (radius - view) * MINIMAP_CHUNK_PIXELS; const extent = (view * 2 + 1) * MINIMAP_CHUNK_PIXELS; context.strokeStyle = VIEWPORT_STROKE; context.lineWidth = 1; context.strokeRect(origin + 0.5, origin + 0.5, extent - 1, extent - 1); } function strokeCurrentChunk () { const origin = radius * MINIMAP_CHUNK_PIXELS; context.strokeStyle = CURRENT_STROKE; context.lineWidth = 2; context.strokeRect(origin + 1, origin + 1, MINIMAP_CHUNK_PIXELS - 2, MINIMAP_CHUNK_PIXELS - 2); } function eventPixel (event) { const rect = canvas.getBoundingClientRect(); if (rect.width <= 0 || rect.height <= 0) { return null; } return { x: (event.clientX - rect.left) * (canvas.width / rect.width), y: (event.clientY - rect.top) * (canvas.height / rect.height) }; } function chunkAtEvent (event) { const pixel = eventPixel(event); if (!pixel) { return null; } const center = getCenter(); return minimapChunkAtPixel(pixel.x, pixel.y, center.chunkX, center.chunkY); } canvas.addEventListener("click", (event) => { const hit = chunkAtEvent(event); if (!hit) { return; } onChunkClick(hit.chunkX, hit.chunkY); }); canvas.addEventListener("mousemove", (event) => { const hit = chunkAtEvent(event); if (!hit) { return; } captionFor(hit.chunkX, hit.chunkY, true); }); canvas.addEventListener("mouseleave", () => { const center = getCenter(); captionFor(center.chunkX | 0, center.chunkY | 0, false); }); draw(); return { setCenter () { draw(); }, apply (message) { const center = getCenter(); const dimensionId = normalizeDimensionId(message?.dimensionId); if (dimensionId !== normalizeDimensionId(center.dimensionId)) { return; } if ((message.chunkX | 0) !== (center.chunkX | 0) || (message.chunkY | 0) !== (center.chunkY | 0)) { return; } const present = new Set(); for (const [key, encoded] of Object.entries(message.chunks ?? {})) { const pixels = rgbaFromBase64(encoded); if (!pixels) { continue; } const [x, y] = key.split(","); const chunkX = Number(x); const chunkY = Number(y); if (!Number.isInteger(chunkX) || !Number.isInteger(chunkY)) { continue; } images.set(cacheKey(dimensionId, chunkX, chunkY), pixels); present.add(`${chunkX},${chunkY}`); } for (let dy = -radius; dy <= radius; dy++) { for (let dx = -radius; dx <= radius; dx++) { const chunkX = (message.chunkX | 0) + dx; const chunkY = (message.chunkY | 0) + dy; const key = `${chunkX},${chunkY}`; if (!present.has(key)) { images.delete(cacheKey(dimensionId, chunkX, chunkY)); } } } draw(); } }; } function blit (dest, destWidth, src, destX, destY) { const span = MINIMAP_CHUNK_PIXELS; for (let row = 0; row < span; row++) { const srcStart = row * span * 4; const destStart = ((destY + row) * destWidth + destX) * 4; dest.set(src.subarray(srcStart, srcStart + span * 4), destStart); } } function rgbaFromBase64 (encoded) { if (typeof encoded !== "string" || !encoded) { return null; } let binary; try { binary = atob(encoded); } catch { return null; } const expected = MINIMAP_CHUNK_PIXELS * MINIMAP_CHUNK_PIXELS * 4; if (binary.length !== expected) { return null; } const out = new Uint8ClampedArray(expected); for (let i = 0; i < expected; i++) { out[i] = binary.charCodeAt(i); } return out; }