| 1 | import { CHUNK_SIZE, REGION_CHUNKS } from "../../../shared/protocol/constants.js"; |
| 2 | |
| 3 | export function floorDiv (value, divisor) |
| 4 | { |
| 5 | return Math.floor(value / divisor); |
| 6 | } |
| 7 | |
| 8 | export function tileToChunk (tile) |
| 9 | { |
| 10 | return floorDiv(tile, CHUNK_SIZE); |
| 11 | } |
| 12 | |
| 13 | export function chunkToRegion (chunk) |
| 14 | { |
| 15 | return floorDiv(chunk, REGION_CHUNKS); |
| 16 | } |
| 17 | |
| 18 | export function chunkOriginTile (chunk) |
| 19 | { |
| 20 | return chunk * CHUNK_SIZE; |
| 21 | } |
| 22 | |
| 23 | export function tileToLocal (tile) |
| 24 | { |
| 25 | return ((tile % CHUNK_SIZE) + CHUNK_SIZE) % CHUNK_SIZE; |
| 26 | } |
| 27 | |
| 28 | export function localToTile (chunk, local) |
| 29 | { |
| 30 | return chunkOriginTile(chunk) + local; |
| 31 | } |
| 32 | |
| 33 | export function tileIndex (localX, localY) |
| 34 | { |
| 35 | return localY * CHUNK_SIZE + localX; |
| 36 | } |
| 37 | |
| 38 | export function indexToLocal (index) |
| 39 | { |
| 40 | return { |
| 41 | localX: index % CHUNK_SIZE, |
| 42 | localY: Math.floor(index / CHUNK_SIZE) |
| 43 | }; |
| 44 | } |
| 45 | |
| 46 | export function chunkKey (chunkX, chunkY) |
| 47 | { |
| 48 | return `${chunkX},${chunkY}`; |
| 49 | } |
| 50 | |
| 51 | export function parseChunkKey (key) |
| 52 | { |
| 53 | const [chunkX, chunkY] = key.split(",").map(Number); |
| 54 | return { chunkX, chunkY }; |
| 55 | } |
| 56 | |
| 57 | export function tileKey (tileX, tileY) |
| 58 | { |
| 59 | return `${tileX},${tileY}`; |
| 60 | } |
| 61 | |
| 62 | export function visibleChunkCoords (chunkX, chunkY, radius = 1) |
| 63 | { |
| 64 | const coords = []; |
| 65 | for (let dy = -radius; dy <= radius; dy++) |
| 66 | { |
| 67 | for (let dx = -radius; dx <= radius; dx++) |
| 68 | { |
| 69 | coords.push({ chunkX: chunkX + dx, chunkY: chunkY + dy }); |
| 70 | } |
| 71 | } |
| 72 | return coords; |
| 73 | } |
| 74 | |
| 75 | export function chebyshevChunks (chunkX, chunkY, chunkRadius) |
| 76 | { |
| 77 | return visibleChunkCoords(chunkX, chunkY, chunkRadius); |
| 78 | } |
| 79 | |
| 80 | export function maxLightChunkRadius (lightRangeTiles) |
| 81 | { |
| 82 | return Math.ceil(Math.max(0, lightRangeTiles) / CHUNK_SIZE); |
| 83 | } |