| 1 | import { CHUNK_SIZE, DEFAULT_WORLD_SETTINGS } from "../../../shared/protocol/constants.js"; |
| 2 | import { GroundType, isBlockPassable, blockLightRange, normalizeGround, normalizeBlock } from "../../../shared/definitions/tiles.js"; |
| 3 | import { tileIndex } from "./coordinates.js"; |
| 4 | |
| 5 | export function createEmptyChunkData (groundTypeId = DEFAULT_WORLD_SETTINGS.defaultGroundTypeId) |
| 6 | { |
| 7 | const tileCount = CHUNK_SIZE * CHUNK_SIZE; |
| 8 | const grounds = new Array(tileCount).fill(null).map(() => normalizeGround(groundTypeId, groundTypeId)); |
| 9 | const blocks = new Array(tileCount).fill(null); |
| 10 | const light = new Array(tileCount).fill(0); |
| 11 | return { |
| 12 | open: false, |
| 13 | grounds, |
| 14 | blocks, |
| 15 | light |
| 16 | }; |
| 17 | } |
| 18 | |
| 19 | export function cloneChunkData (data) |
| 20 | { |
| 21 | return { |
| 22 | open: data.open, |
| 23 | grounds: data.grounds.map((ground) => normalizeGround(ground)), |
| 24 | blocks: data.blocks.map((block) => normalizeBlock(block)), |
| 25 | light: data.light.slice() |
| 26 | }; |
| 27 | } |
| 28 | |
| 29 | export function encodeChunkBlob (data) |
| 30 | { |
| 31 | return JSON.stringify({ |
| 32 | open: !!data.open, |
| 33 | grounds: data.grounds.map((ground) => normalizeGround(ground)), |
| 34 | blocks: data.blocks.map((block) => normalizeBlock(block)), |
| 35 | light: data.light |
| 36 | }); |
| 37 | } |
| 38 | |
| 39 | export function decodeChunkBlob (blob, groundTypeId = DEFAULT_WORLD_SETTINGS.defaultGroundTypeId) |
| 40 | { |
| 41 | if (!blob) |
| 42 | { return createEmptyChunkData(groundTypeId); } |
| 43 | |
| 44 | const parsed = typeof blob === "string" ? JSON.parse(blob) : blob; |
| 45 | const fallback = createEmptyChunkData(groundTypeId); |
| 46 | const tileCount = CHUNK_SIZE * CHUNK_SIZE; |
| 47 | const grounds = Array.isArray(parsed.grounds) ? parsed.grounds.slice(0, tileCount) : fallback.grounds; |
| 48 | const blocks = Array.isArray(parsed.blocks) ? parsed.blocks.slice(0, tileCount) : fallback.blocks; |
| 49 | const light = Array.isArray(parsed.light) ? parsed.light.slice(0, tileCount) : fallback.light; |
| 50 | |
| 51 | while (grounds.length < tileCount) |
| 52 | { grounds.push(normalizeGround(groundTypeId, groundTypeId)); } |
| 53 | while (blocks.length < tileCount) |
| 54 | { blocks.push(null); } |
| 55 | while (light.length < tileCount) |
| 56 | { light.push(0); } |
| 57 | |
| 58 | return { |
| 59 | open: !!parsed.open, |
| 60 | grounds: grounds.map((ground) => normalizeGround(ground, groundTypeId)), |
| 61 | blocks: blocks.map((block) => normalizeBlock(block)), |
| 62 | light |
| 63 | }; |
| 64 | } |
| 65 | |
| 66 | export function getTile (data, localX, localY) |
| 67 | { |
| 68 | const index = tileIndex(localX, localY); |
| 69 | const ground = normalizeGround(data.grounds[index]); |
| 70 | return { |
| 71 | groundTypeId: ground.typeId, |
| 72 | groundState: ground.state, |
| 73 | block: data.blocks[index], |
| 74 | light: data.light[index] |
| 75 | }; |
| 76 | } |
| 77 | |
| 78 | export function setGround (data, localX, localY, groundTypeId, state = null) |
| 79 | { |
| 80 | data.grounds[tileIndex(localX, localY)] = normalizeGround({ typeId: groundTypeId, state }); |
| 81 | } |
| 82 | |
| 83 | export function setBlock (data, localX, localY, block) |
| 84 | { |
| 85 | data.blocks[tileIndex(localX, localY)] = normalizeBlock(block); |
| 86 | } |
| 87 | |
| 88 | export function isChunkTilePassable (data, localX, localY) |
| 89 | { |
| 90 | const block = data.blocks[tileIndex(localX, localY)]; |
| 91 | return isBlockPassable(block?.typeId ?? null); |
| 92 | } |
| 93 | |
| 94 | export function collectLightSources (chunkX, chunkY, data) |
| 95 | { |
| 96 | const sources = []; |
| 97 | for (let localY = 0; localY < CHUNK_SIZE; localY++) |
| 98 | { |
| 99 | for (let localX = 0; localX < CHUNK_SIZE; localX++) |
| 100 | { |
| 101 | const block = data.blocks[tileIndex(localX, localY)]; |
| 102 | const range = blockLightRange(block?.typeId ?? null); |
| 103 | if (range > 0) |
| 104 | { |
| 105 | sources.push({ |
| 106 | tileX: chunkX * CHUNK_SIZE + localX, |
| 107 | tileY: chunkY * CHUNK_SIZE + localY, |
| 108 | range |
| 109 | }); |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | return sources; |
| 114 | } |
| 115 | |
| 116 | export { GroundType, normalizeGround, normalizeBlock }; |