Improve minimap, switch to block palette, update faster

This commit is contained in:
Tanner committed 2026-11-14 18:40:42 +00:00
1 parent 8c75502966
commit 9be381c2de
6 files changed
+173 -45

No files matched your search

+79 -31
View File
@@ -1,3 +1,4 @@
import base64
import importlib
import json
import os
@@ -174,31 +175,54 @@ def api_global():
return response
def _minimap_surface_cell(chunks, x, z):
def _minimap_surface_cell(chunks, x, z, section_cache=None):
"""Return the uppermost solid/water block in a loaded world column."""
from mosfet.info import blocks
chunk_x, chunk_z = floor(x / 16), floor(z / 16)
chunk = chunks.chunks.get((chunk_x, chunk_z))
key = (chunk_x, chunk_z)
chunk = chunks.chunks.get(key)
if chunk is None:
return None
if section_cache is not None and key in section_cache:
sections = section_cache[key]
else:
sections = sorted(chunk.sections.items(), reverse=True)
if section_cache is not None:
section_cache[key] = sections
local_x, local_z = x - chunk_x * 16, z - chunk_z * 16
min_y = getattr(chunks, 'min_y', -64)
for section_y in sorted(chunk.sections, reverse=True):
if section_y * 16 + 15 < min_y:
for section_y, section in sections:
section_base = section_y * 16
if section_base + 15 < min_y:
continue
section = chunk.sections[section_y]
if section.get('non_empty_blocks') == 0:
continue
for y in range(section_y * 16 + 15, max(min_y, section_y * 16) - 1, -1):
state_id = chunk.get_block_at(local_x, y, local_z)
if state_id is None or state_id == blocks.AIR:
continue
if (state_id in blocks.NON_SOLID_IDS and
state_id not in blocks.WATER_IDS):
continue
return {'block': blocks.BLOCKS.get(state_id, 'unknown'), 'y': y}
section_blocks = section.get('blocks')
if section_blocks is not None and hasattr(section_blocks, 'get'):
min_local_y = max(0, min_y - section_base)
for local_y in range(15, min_local_y - 1, -1):
index = ((local_y * 16 + local_z) * 16 + local_x)
state_id = section_blocks.get(index)
y = section_base + local_y
if state_id is None or state_id == blocks.AIR:
continue
if (state_id in blocks.NON_SOLID_IDS and
state_id not in blocks.WATER_IDS):
continue
return {'block': blocks.BLOCKS.get(state_id, 'unknown'), 'y': y}
else:
for y in range(section_base + 15, max(min_y, section_base) - 1, -1):
state_id = chunk.get_block_at(local_x, y, local_z)
if state_id is None or state_id == blocks.AIR:
continue
if (state_id in blocks.NON_SOLID_IDS and
state_id not in blocks.WATER_IDS):
continue
return {'block': blocks.BLOCKS.get(state_id, 'unknown'), 'y': y}
return None
@@ -208,13 +232,13 @@ def api_minimap():
chunks = g.get('chunks')
if (not g.get('connected') or position is None or position is False or
chunks is None or not hasattr(chunks, 'chunks')):
return jsonify(ready=False, cells=[])
return jsonify(ready=False, palette=[], cell_bits=8, cells_b64='')
try:
radius = int(request.args.get('radius', 12))
except (TypeError, ValueError):
radius = 12
radius = max(4, min(32, radius))
radius = max(4, min(36, radius))
try:
radius_x = int(request.args.get('radius_x', radius))
except (TypeError, ValueError):
@@ -224,23 +248,42 @@ def api_minimap():
except (TypeError, ValueError):
radius_z = radius
radius_x = max(4, min(48, radius_x))
radius_z = max(4, min(32, radius_z))
radius_z = max(4, min(36, radius_z))
center_x, center_z = floor(position.x), floor(position.z)
center_y = floor(position.y)
section_cache = {}
palette = []
palette_indices = {}
cell_indices = []
lock = getattr(chunks, 'lock', None)
if lock is not None:
lock.acquire()
try:
cells = [
[_minimap_surface_cell(chunks, center_x + dx, center_z + dz)
for dx in range(-radius_x, radius_x + 1)]
for dz in range(-radius_z, radius_z + 1)
]
for dz in range(-radius_z, radius_z + 1):
for dx in range(-radius_x, radius_x + 1):
cell = _minimap_surface_cell(
chunks, center_x + dx, center_z + dz, section_cache)
if cell is None:
cell_indices.append(0)
else:
name = cell['block']
palette_index = palette_indices.get(name)
if palette_index is None:
palette_index = len(palette) + 1
palette_indices[name] = palette_index
palette.append(name)
cell_indices.append(palette_index)
finally:
if lock is not None:
lock.release()
ready = any(cell is not None for row in cells for cell in row)
ready = bool(palette)
cell_bits = 8 if len(palette) <= 255 else 16
if cell_bits == 8:
cell_data = bytes(cell_indices)
else:
cell_data = b''.join(index.to_bytes(2, 'little')
for index in cell_indices)
path_nodes = []
for node in (g.get('path') or ())[:512]:
try:
@@ -252,16 +295,21 @@ def api_minimap():
continue
path_nodes.append({'x': path_x, 'z': path_z})
return jsonify(
ready=ready,
center={'x': center_x, 'y': center_y, 'z': center_z},
yaw=float(g.get('yaw', 0.0) or 0.0),
radius=radius,
radius_x=radius_x,
radius_z=radius_z,
cells=cells if ready else [],
path=path_nodes,
response_data = {
'ready': ready,
'center': {'x': center_x, 'y': center_y, 'z': center_z},
'yaw': float(g.get('yaw', 0.0) or 0.0),
'radius': radius,
'radius_x': radius_x,
'radius_z': radius_z,
'path': path_nodes,
}
response_data.update(
palette=palette,
cell_bits=cell_bits,
cells_b64=base64.b64encode(cell_data).decode('ascii'),
)
return jsonify(response_data)
@app.route('/api/items')