BlueGit

timfa questward

Files

questward / client / src / editor / minimap.js

minimap.js

feat: add sprite average colours and an editor minimap
timfa project_85689633_bot_760517ee9267c86ff1d6c3a0237abf7d · pushed by timfa · 6 weeks ago
8a24eb51 History
minimap.js 5.6 KiB Download Raw
1import { STATIC_CHUNK_RADIUS } from "/shared/protocol/constants.js";
2import { normalizeDimensionId } from "/shared/world/dimensions.js";
3import
4{
5 MINIMAP_CHUNK_PIXELS,
6 MINIMAP_EMPTY_COLOR,
7 MINIMAP_SPAN_CHUNKS,
8 minimapChunkAtPixel,
9 minimapRadius
10} from "/shared/render/chunkMinimap.js";
11
12const VIEWPORT_STROKE = "#d5dbe6";
13const CURRENT_STROKE = "#e23d3d";
14
15/**
16 * Editor minimap. Chunk images are cached in memory after the server sends them
17 * and redrawn locally. The durable cache is the server's per-dimension RGBA files.
18 */
19export function initEditorMinimap ({ canvas, caption, getCenter, onChunkClick })
20{
21 const context = canvas.getContext("2d", { alpha: false });
22 const radius = minimapRadius(MINIMAP_SPAN_CHUNKS);
23 const size = MINIMAP_SPAN_CHUNKS * MINIMAP_CHUNK_PIXELS;
24 canvas.width = size;
25 canvas.height = size;
26 /** @type {Map<string, Uint8ClampedArray>} */
27 const images = new Map();
28
29 function cacheKey (dimensionId, chunkX, chunkY)
30 {
31 return `${normalizeDimensionId(dimensionId)}:${chunkX | 0},${chunkY | 0}`;
32 }
33
34 function captionFor (chunkX, chunkY, hovering)
35 {
36 if (!caption)
37 { return; }
38 const where = `Chunk ${chunkX}, ${chunkY}`;
39 caption.textContent = hovering ? where : `${where} · click to centre`;
40 }
41
42 function draw ()
43 {
44 const center = getCenter();
45 const dimensionId = normalizeDimensionId(center.dimensionId);
46 const image = context.createImageData(size, size);
47 const empty = MINIMAP_EMPTY_COLOR;
48 for (let i = 0; i < image.data.length; i += 4)
49 {
50 image.data[i] = empty.r;
51 image.data[i + 1] = empty.g;
52 image.data[i + 2] = empty.b;
53 image.data[i + 3] = 255;
54 }
55 for (let dy = -radius; dy <= radius; dy++)
56 {
57 for (let dx = -radius; dx <= radius; dx++)
58 {
59 const chunkX = (center.chunkX | 0) + dx;
60 const chunkY = (center.chunkY | 0) + dy;
61 const pixels = images.get(cacheKey(dimensionId, chunkX, chunkY));
62 if (!pixels || pixels.length !== MINIMAP_CHUNK_PIXELS * MINIMAP_CHUNK_PIXELS * 4)
63 { continue; }
64 blit(image.data, size, pixels, (dx + radius) * MINIMAP_CHUNK_PIXELS, (dy + radius) * MINIMAP_CHUNK_PIXELS);
65 }
66 }
67 context.putImageData(image, 0, 0);
68 strokeViewport();
69 strokeCurrentChunk();
70 captionFor(center.chunkX | 0, center.chunkY | 0, false);
71 }
72
73 function strokeViewport ()
74 {
75 const view = Math.max(0, STATIC_CHUNK_RADIUS | 0);
76 const origin = (radius - view) * MINIMAP_CHUNK_PIXELS;
77 const extent = (view * 2 + 1) * MINIMAP_CHUNK_PIXELS;
78 context.strokeStyle = VIEWPORT_STROKE;
79 context.lineWidth = 1;
80 context.strokeRect(origin + 0.5, origin + 0.5, extent - 1, extent - 1);
81 }
82
83 function strokeCurrentChunk ()
84 {
85 const origin = radius * MINIMAP_CHUNK_PIXELS;
86 context.strokeStyle = CURRENT_STROKE;
87 context.lineWidth = 2;
88 context.strokeRect(origin + 1, origin + 1, MINIMAP_CHUNK_PIXELS - 2, MINIMAP_CHUNK_PIXELS - 2);
89 }
90
91 function eventPixel (event)
92 {
93 const rect = canvas.getBoundingClientRect();
94 if (rect.width <= 0 || rect.height <= 0)
95 { return null; }
96 return {
97 x: (event.clientX - rect.left) * (canvas.width / rect.width),
98 y: (event.clientY - rect.top) * (canvas.height / rect.height)
99 };
100 }
101
102 function chunkAtEvent (event)
103 {
104 const pixel = eventPixel(event);
105 if (!pixel)
106 { return null; }
107 const center = getCenter();
108 return minimapChunkAtPixel(pixel.x, pixel.y, center.chunkX, center.chunkY);
109 }
110
111 canvas.addEventListener("click", (event) =>
112 {
113 const hit = chunkAtEvent(event);
114 if (!hit)
115 { return; }
116 onChunkClick(hit.chunkX, hit.chunkY);
117 });
118 canvas.addEventListener("mousemove", (event) =>
119 {
120 const hit = chunkAtEvent(event);
121 if (!hit)
122 { return; }
123 captionFor(hit.chunkX, hit.chunkY, true);
124 });
125 canvas.addEventListener("mouseleave", () =>
126 {
127 const center = getCenter();
128 captionFor(center.chunkX | 0, center.chunkY | 0, false);
129 });
130
131 draw();
132
133 return {
134 setCenter ()
135 {
136 draw();
137 },
138 apply (message)
139 {
140 const center = getCenter();
141 const dimensionId = normalizeDimensionId(message?.dimensionId);
142 if (dimensionId !== normalizeDimensionId(center.dimensionId))
143 { return; }
144 if ((message.chunkX | 0) !== (center.chunkX | 0) || (message.chunkY | 0) !== (center.chunkY | 0))
145 { return; }
146 const present = new Set();
147 for (const [key, encoded] of Object.entries(message.chunks ?? {}))
148 {
149 const pixels = rgbaFromBase64(encoded);
150 if (!pixels)
151 { continue; }
152 const [x, y] = key.split(",");
153 const chunkX = Number(x);
154 const chunkY = Number(y);
155 if (!Number.isInteger(chunkX) || !Number.isInteger(chunkY))
156 { continue; }
157 images.set(cacheKey(dimensionId, chunkX, chunkY), pixels);
158 present.add(`${chunkX},${chunkY}`);
159 }
160 for (let dy = -radius; dy <= radius; dy++)
161 {
162 for (let dx = -radius; dx <= radius; dx++)
163 {
164 const chunkX = (message.chunkX | 0) + dx;
165 const chunkY = (message.chunkY | 0) + dy;
166 const key = `${chunkX},${chunkY}`;
167 if (!present.has(key))
168 { images.delete(cacheKey(dimensionId, chunkX, chunkY)); }
169 }
170 }
171 draw();
172 }
173 };
174}
175
176function blit (dest, destWidth, src, destX, destY)
177{
178 const span = MINIMAP_CHUNK_PIXELS;
179 for (let row = 0; row < span; row++)
180 {
181 const srcStart = row * span * 4;
182 const destStart = ((destY + row) * destWidth + destX) * 4;
183 dest.set(src.subarray(srcStart, srcStart + span * 4), destStart);
184 }
185}
186
187function rgbaFromBase64 (encoded)
188{
189 if (typeof encoded !== "string" || !encoded)
190 { return null; }
191 let binary;
192 try
193 { binary = atob(encoded); }
194 catch
195 { return null; }
196 const expected = MINIMAP_CHUNK_PIXELS * MINIMAP_CHUNK_PIXELS * 4;
197 if (binary.length !== expected)
198 { return null; }
199 const out = new Uint8ClampedArray(expected);
200 for (let i = 0; i < expected; i++)
201 { out[i] = binary.charCodeAt(i); }
202 return out;
203}