| 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 regionKey (regionX, regionY) |
| 47 | { |
| 48 | return `${regionX},${regionY}`; |
| 49 | } |
| 50 | |
| 51 | export function parseRegionKey (key) |
| 52 | { |
| 53 | const [regionX, regionY] = key.split(",").map(Number); |
| 54 | return { regionX, regionY }; |
| 55 | } |
| 56 | |
| 57 | export function tileToRegion (tile) |
| 58 | { |
| 59 | return chunkToRegion(tileToChunk(tile)); |
| 60 | } |
| 61 | |
| 62 | export function regionOriginChunk (region) |
| 63 | { |
| 64 | return region * REGION_CHUNKS; |
| 65 | } |
| 66 | |
| 67 | export function chunksInRegion (regionX, regionY) |
| 68 | { |
| 69 | const originX = regionOriginChunk(regionX); |
| 70 | const originY = regionOriginChunk(regionY); |
| 71 | const coords = []; |
| 72 | for (let localY = 0; localY < REGION_CHUNKS; localY++) |
| 73 | { |
| 74 | for (let localX = 0; localX < REGION_CHUNKS; localX++) |
| 75 | { coords.push({ chunkX: originX + localX, chunkY: originY + localY }); } |
| 76 | } |
| 77 | return coords; |
| 78 | } |
| 79 | |
| 80 | export function regionTileBounds (regionX, regionY) |
| 81 | { |
| 82 | const minChunkX = regionOriginChunk(regionX); |
| 83 | const minChunkY = regionOriginChunk(regionY); |
| 84 | return { |
| 85 | minX: minChunkX * CHUNK_SIZE, |
| 86 | minY: minChunkY * CHUNK_SIZE, |
| 87 | maxX: (minChunkX + REGION_CHUNKS) * CHUNK_SIZE - 1, |
| 88 | maxY: (minChunkY + REGION_CHUNKS) * CHUNK_SIZE - 1 |
| 89 | }; |
| 90 | } |
| 91 | |
| 92 | export function visibleRegionCoords (regionX, regionY, radius = 1) |
| 93 | { |
| 94 | const coords = []; |
| 95 | for (let dy = -radius; dy <= radius; dy++) |
| 96 | { |
| 97 | for (let dx = -radius; dx <= radius; dx++) |
| 98 | { coords.push({ regionX: regionX + dx, regionY: regionY + dy }); } |
| 99 | } |
| 100 | return coords; |
| 101 | } |
| 102 | |
| 103 | export function chunkKey (chunkX, chunkY) |
| 104 | { |
| 105 | return `${chunkX},${chunkY}`; |
| 106 | } |
| 107 | |
| 108 | export function parseChunkKey (key) |
| 109 | { |
| 110 | const [chunkX, chunkY] = key.split(",").map(Number); |
| 111 | return { chunkX, chunkY }; |
| 112 | } |
| 113 | |
| 114 | export function tileKey (tileX, tileY) |
| 115 | { |
| 116 | return `${tileX},${tileY}`; |
| 117 | } |
| 118 | |
| 119 | export function visibleChunkCoords (chunkX, chunkY, radius = 1) |
| 120 | { |
| 121 | const coords = []; |
| 122 | for (let dy = -radius; dy <= radius; dy++) |
| 123 | { |
| 124 | for (let dx = -radius; dx <= radius; dx++) |
| 125 | { |
| 126 | coords.push({ chunkX: chunkX + dx, chunkY: chunkY + dy }); |
| 127 | } |
| 128 | } |
| 129 | return coords; |
| 130 | } |
| 131 | |
| 132 | export function chebyshevChunks (chunkX, chunkY, chunkRadius) |
| 133 | { |
| 134 | return visibleChunkCoords(chunkX, chunkY, chunkRadius); |
| 135 | } |
| 136 | |
| 137 | export function maxLightChunkRadius (lightRangeTiles) |
| 138 | { |
| 139 | return Math.ceil(Math.max(0, lightRangeTiles) / CHUNK_SIZE); |
| 140 | } |