BlueGit

timfa questward

Files

questward / shared / render / chunkMinimap.js

chunkMinimap.js

feat: add sprite average colours and an editor minimap
timfa project_85689633_bot_760517ee9267c86ff1d6c3a0237abf7d · pushed by timfa · 6 weeks ago
8a24eb51 History
chunkMinimap.js 2.8 KiB Download Raw
1import { CHUNK_SIZE } from "../protocol/constants.js";
2import
3{
4 GroundType,
5 normalizeGround,
6 normalizeBlock,
7 groundAverageColor,
8 blockAverageColor
9} from "../definitions/tiles.js";
10
11/** Odd span so one chunk sits in the centre of the editor minimap. */
12export const MINIMAP_SPAN_CHUNKS = 25;
13
14/**
15 * One minimap pixel per tile. Chunks are {@link CHUNK_SIZE} tiles on a side
16 * (16 today), so each cached chunk image is that many pixels square.
17 */
18export const MINIMAP_CHUNK_PIXELS = CHUNK_SIZE;
19
20/** Unauthored / missing chunks. Matches the editor sidebar (`#1a1e26`). */
21export const MINIMAP_EMPTY_COLOR = Object.freeze({ r: 0x1a, g: 0x1e, b: 0x26 });
22
23/** Saved tile whose sprite has no computed colour. */
24export const MINIMAP_MISSING_COLOR = Object.freeze({ r: 0x3a, g: 0x42, b: 0x50 });
25
26export function minimapRadius (span = MINIMAP_SPAN_CHUNKS)
27{
28 return (span - 1) >> 1;
29}
30
31/**
32 * Chunk under a minimap pixel. Coordinates are in the bitmap's pixel space
33 * (top-left of the whole minimap is 0, 0), not CSS pixels.
34 * @returns {{ chunkX: number, chunkY: number } | null}
35 */
36export function minimapChunkAtPixel (
37 pixelX,
38 pixelY,
39 centerChunkX,
40 centerChunkY,
41 span = MINIMAP_SPAN_CHUNKS,
42 chunkPixels = MINIMAP_CHUNK_PIXELS
43)
44{
45 if (!Number.isFinite(pixelX) || !Number.isFinite(pixelY) || chunkPixels <= 0)
46 { return null; }
47 const col = Math.floor(pixelX / chunkPixels);
48 const row = Math.floor(pixelY / chunkPixels);
49 if (col < 0 || row < 0 || col >= span || row >= span)
50 { return null; }
51 const radius = minimapRadius(span);
52 return {
53 chunkX: (centerChunkX | 0) + col - radius,
54 chunkY: (centerChunkY | 0) + row - radius
55 };
56}
57
58/**
59 * 16×16 (or chunk-sized) RGBA image for one saved chunk.
60 * A block colour wins where a block is present; otherwise the ground colour.
61 * Uses the authored state when that state has its own colour, else the type default.
62 * @param {{ grounds?: unknown[], blocks?: unknown[] } | null | undefined} chunkData
63 * @param {number} [fallbackGroundTypeId]
64 * @returns {Uint8ClampedArray}
65 */
66export function renderChunkMinimapRgba (chunkData, fallbackGroundTypeId = GroundType.grass)
67{
68 const tileCount = MINIMAP_CHUNK_PIXELS * MINIMAP_CHUNK_PIXELS;
69 const rgba = new Uint8ClampedArray(tileCount * 4);
70 const grounds = chunkData?.grounds ?? [];
71 const blocks = chunkData?.blocks ?? [];
72 for (let index = 0; index < tileCount; index++)
73 {
74 const block = normalizeBlock(blocks[index]);
75 const ground = normalizeGround(grounds[index], fallbackGroundTypeId);
76 const color = (block && blockAverageColor(block.typeId, block.state))
77 || groundAverageColor(ground.typeId, ground.state)
78 || MINIMAP_MISSING_COLOR;
79 const offset = index * 4;
80 rgba[offset] = color.r;
81 rgba[offset + 1] = color.g;
82 rgba[offset + 2] = color.b;
83 rgba[offset + 3] = 255;
84 }
85 return rgba;
86}