import { CHUNK_SIZE } from "../../../shared/protocol/constants.js"; import { tileToChunk, tileToLocal, chunkKey, maxLightChunkRadius, chebyshevChunks } from "../world/coordinates.js"; import { cloneChunkData, collectLightSources, createEmptyChunkData, isChunkTilePassable } from "../world/chunkData.js"; export function bresenhamTiles (x0, y0, x1, y1) { const tiles = []; let x = x0; let y = y0; const dx = Math.abs(x1 - x0); const dy = Math.abs(y1 - y0); const sx = x0 < x1 ? 1 : -1; const sy = y0 < y1 ? 1 : -1; let err = dx - dy; while (true) { tiles.push({ x, y }); if (x === x1 && y === y1) { break; } const e2 = 2 * err; if (e2 > -dy) { err -= dy; x += sx; } if (e2 < dx) { err += dx; y += sy; } } return tiles; } export function rayReaches (fromX, fromY, toX, toY, isPassable) { if (fromX === toX && fromY === toY) { return isPassable(toX, toY); } if (!isPassable(toX, toY)) { return false; } const tiles = bresenhamTiles(fromX, fromY, toX, toY); for (let i = 1; i < tiles.length - 1; i++) { if (!isPassable(tiles[i].x, tiles[i].y)) { return false; } } return true; } export function maxDefinitionLightRange (blockDefs) { let max = 0; for (const def of Object.values(blockDefs)) { max = Math.max(max, def.lightRangeTiles ?? 0); } return max; } export function bakeLightMaps (centerChunkX, centerChunkY, loadChunk, maxRange) { const halo = maxLightChunkRadius(maxRange); const affected = chebyshevChunks(centerChunkX, centerChunkY, halo); const loaded = new Map(); function getData (chunkX, chunkY) { const key = chunkKey(chunkX, chunkY); if (!loaded.has(key)) { loaded.set(key, cloneChunkData(loadChunk(chunkX, chunkY))); } return loaded.get(key); } const sources = []; for (const coord of chebyshevChunks(centerChunkX, centerChunkY, halo + 1)) { const data = getData(coord.chunkX, coord.chunkY); sources.push(...collectLightSources(coord.chunkX, coord.chunkY, data)); } function isPassable (tileX, tileY) { const data = getData(tileToChunk(tileX), tileToChunk(tileY)); return isChunkTilePassable(data, tileToLocal(tileX), tileToLocal(tileY)); } for (const coord of affected) { const data = getData(coord.chunkX, coord.chunkY); for (let localY = 0; localY < CHUNK_SIZE; localY++) { for (let localX = 0; localX < CHUNK_SIZE; localX++) { const tileX = coord.chunkX * CHUNK_SIZE + localX; const tileY = coord.chunkY * CHUNK_SIZE + localY; const index = localY * CHUNK_SIZE + localX; let best = 0; if (isPassable(tileX, tileY)) { for (const source of sources) { const dist = Math.hypot(tileX - source.tileX, tileY - source.tileY); if (dist > source.range) { continue; } if (rayReaches(source.tileX, source.tileY, tileX, tileY, isPassable)) { const encoded = Math.max(1, Math.round(255 * (1 - dist / (source.range + 1)))); best = Math.max(best, encoded); } } } data.light[index] = best; } } } return affected.map((coord) => ({ chunkX: coord.chunkX, chunkY: coord.chunkY, data: loaded.get(chunkKey(coord.chunkX, coord.chunkY)) })); } export function defaultLightChunk (groundTypeId) { return createEmptyChunkData(groundTypeId); }