| 1 | import { OVERWORLD_ID, normalizeDimensionId } from "../../../shared/world/dimensions.js"; |
| 2 | import { bakeEnclosedWindow, ENCLOSED_BAKE_CHUNK_RADIUS } from "../../../shared/lighting/enclosed.js"; |
| 3 | import { chunkKey, chebyshevChunks } from "../world/coordinates.js"; |
| 4 | import { cloneChunkData } from "../world/chunkData.js"; |
| 5 | |
| 6 | export { ENCLOSED_BAKE_CHUNK_RADIUS }; |
| 7 | |
| 8 | /** |
| 9 | * Bake enclosed masks into cloned chunk data for the 3×3 window around the saved chunk. |
| 10 | * Same return shape as bakeLightMaps for easy merge in editor save. |
| 11 | */ |
| 12 | export function bakeEnclosedMaps (centerChunkX, centerChunkY, loadChunk, dimensionId = OVERWORLD_ID) |
| 13 | { |
| 14 | const dim = normalizeDimensionId(dimensionId); |
| 15 | const loaded = new Map(); |
| 16 | |
| 17 | function getData (chunkX, chunkY) |
| 18 | { |
| 19 | const key = chunkKey(chunkX, chunkY, dim); |
| 20 | if (!loaded.has(key)) |
| 21 | { loaded.set(key, cloneChunkData(loadChunk(chunkX, chunkY))); } |
| 22 | return loaded.get(key); |
| 23 | } |
| 24 | |
| 25 | // Prefetch window so clone exists before mask write. |
| 26 | for (const coord of chebyshevChunks(centerChunkX, centerChunkY, ENCLOSED_BAKE_CHUNK_RADIUS)) |
| 27 | { getData(coord.chunkX, coord.chunkY); } |
| 28 | |
| 29 | const baked = bakeEnclosedWindow(centerChunkX, centerChunkY, (chunkX, chunkY) => getData(chunkX, chunkY)); |
| 30 | for (const entry of baked) |
| 31 | { |
| 32 | const data = getData(entry.chunkX, entry.chunkY); |
| 33 | data.enclosed = entry.enclosed; |
| 34 | } |
| 35 | |
| 36 | return baked.map((entry) => ({ |
| 37 | chunkX: entry.chunkX, |
| 38 | chunkY: entry.chunkY, |
| 39 | dimensionId: dim, |
| 40 | data: loaded.get(chunkKey(entry.chunkX, entry.chunkY, dim)) |
| 41 | })); |
| 42 | } |