Improve minimap, switch to block palette, update faster
This commit is contained in:
1 parent
8c75502966
commit
9be381c2de
6 files changed
+173
-45
No files matched your search
@@ -81,7 +81,7 @@
|
||||
<div class="minimap-controls" aria-label="Minimap controls">
|
||||
<button id="minimap-zoom-out" type="button" aria-label="Zoom out" title="Zoom out">−</button>
|
||||
<button id="minimap-zoom-in" type="button" aria-label="Zoom in" title="Zoom in">+</button>
|
||||
<button id="minimap-north-lock" type="button" aria-label="Lock north to the top" aria-pressed="false" title="Lock north to the top">N ↑</button>
|
||||
<button id="minimap-north-lock" type="button" aria-label="Rotate with the bot" aria-pressed="true" title="Rotate with the bot">N ↑</button>
|
||||
<button id="minimap-size" type="button" aria-label="Enlarge minimap" aria-pressed="false" title="Enlarge minimap">⤢</button>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
@@ -81,7 +81,7 @@
|
||||
<div class="minimap-controls" aria-label="Minimap controls">
|
||||
<button id="minimap-zoom-out" type="button" aria-label="Zoom out" title="Zoom out">−</button>
|
||||
<button id="minimap-zoom-in" type="button" aria-label="Zoom in" title="Zoom in">+</button>
|
||||
<button id="minimap-north-lock" type="button" aria-label="Lock north to the top" aria-pressed="false" title="Lock north to the top">N ↑</button>
|
||||
<button id="minimap-north-lock" type="button" aria-label="Rotate with the bot" aria-pressed="true" title="Rotate with the bot">N ↑</button>
|
||||
<button id="minimap-size" type="button" aria-label="Enlarge minimap" aria-pressed="false" title="Enlarge minimap">⤢</button>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
'use strict';
|
||||
|
||||
const POLL_MS = 1000;
|
||||
const MINIMAP_POLL_MS = 2000;
|
||||
const MINIMAP_POLL_MS = 250;
|
||||
const MINIMAP_MIN_RADIUS = 4;
|
||||
const MINIMAP_MAX_RADIUS = 24;
|
||||
const MINIMAP_MAX_RADIUS = 36;
|
||||
const MINIMAP_ZOOM_STEP = 4;
|
||||
const $ = (id) => document.getElementById(id);
|
||||
let itemNames = {};
|
||||
@@ -15,7 +15,7 @@
|
||||
let menuAnchor = null;
|
||||
let toastTimer = null;
|
||||
let minimapRadius = 12;
|
||||
let minimapNorthLocked = false;
|
||||
let minimapNorthLocked = true;
|
||||
let minimapLarge = false;
|
||||
let minimapRequestPending = false;
|
||||
let lastMinimapData = null;
|
||||
@@ -100,6 +100,27 @@
|
||||
return '#7a806f';
|
||||
}
|
||||
|
||||
function decodeMinimapPalette(data) {
|
||||
if (typeof data?.cells_b64 !== 'string' || !Array.isArray(data.palette)) {
|
||||
return data;
|
||||
}
|
||||
const binary = atob(data.cells_b64);
|
||||
const bits = Number(data.cell_bits) === 16 ? 16 : 8;
|
||||
if (bits === 8) {
|
||||
data.cellIndices = Uint8Array.from(binary, (character) =>
|
||||
character.charCodeAt(0));
|
||||
} else {
|
||||
const indices = new Uint16Array(binary.length / 2);
|
||||
for (let index = 0; index < indices.length; index++) {
|
||||
indices[index] = binary.charCodeAt(index * 2) |
|
||||
(binary.charCodeAt(index * 2 + 1) << 8);
|
||||
}
|
||||
data.cellIndices = indices;
|
||||
}
|
||||
data.paletteColors = data.palette.map(minimapBlockColor);
|
||||
return data;
|
||||
}
|
||||
|
||||
function drawMinimap(data = lastMinimapData) {
|
||||
const canvas = $('minimap-canvas');
|
||||
if (!canvas) return;
|
||||
@@ -112,7 +133,7 @@
|
||||
context.fillStyle = '#111411';
|
||||
context.fillRect(0, 0, width, height);
|
||||
|
||||
if (!data?.ready || !Array.isArray(data.cells)) {
|
||||
if (!data?.ready || !data.cellIndices || !Array.isArray(data.paletteColors)) {
|
||||
$('minimap-caption').textContent = 'Waiting for loaded terrain';
|
||||
updateMinimapControls();
|
||||
return;
|
||||
@@ -131,10 +152,12 @@
|
||||
const rasterContext = raster.getContext('2d');
|
||||
const rasterPixels = rasterContext.createImageData(columns, rows);
|
||||
for (let rowIndex = 0; rowIndex < rows; rowIndex++) {
|
||||
const row = data.cells[rowIndex] || [];
|
||||
for (let columnIndex = 0; columnIndex < columns; columnIndex++) {
|
||||
const surface = row[columnIndex];
|
||||
const color = surface ? minimapBlockColor(surface.block) : '#171b17';
|
||||
const flatIndex = rowIndex * columns + columnIndex;
|
||||
const paletteIndex = data.cellIndices[flatIndex];
|
||||
const color = paletteIndex
|
||||
? data.paletteColors[paletteIndex - 1]
|
||||
: '#171b17';
|
||||
const value = Number.parseInt(color.slice(1), 16);
|
||||
const pixel = (rowIndex * columns + columnIndex) * 4;
|
||||
rasterPixels.data[pixel] = value >> 16;
|
||||
@@ -698,7 +721,7 @@
|
||||
{cache: 'no-store'},
|
||||
);
|
||||
if (!response.ok) throw new Error(`API ${response.status}`);
|
||||
const data = await response.json();
|
||||
const data = decodeMinimapPalette(await response.json());
|
||||
const currentRadii = minimapRadii();
|
||||
if (requestedRadius !== minimapRadius ||
|
||||
requestKey !== `${currentRadii.x}:${currentRadii.z}` ||
|
||||
|
||||
@@ -81,7 +81,7 @@
|
||||
<div class="minimap-controls" aria-label="Minimap controls">
|
||||
<button id="minimap-zoom-out" type="button" aria-label="Zoom out" title="Zoom out">−</button>
|
||||
<button id="minimap-zoom-in" type="button" aria-label="Zoom in" title="Zoom in">+</button>
|
||||
<button id="minimap-north-lock" type="button" aria-label="Lock north to the top" aria-pressed="false" title="Lock north to the top">N ↑</button>
|
||||
<button id="minimap-north-lock" type="button" aria-label="Rotate with the bot" aria-pressed="true" title="Rotate with the bot">N ↑</button>
|
||||
<button id="minimap-size" type="button" aria-label="Enlarge minimap" aria-pressed="false" title="Enlarge minimap">⤢</button>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
Reference in new issue
Block a user