| 1 | import { CHUNK_SIZE } from "../protocol/constants.js"; |
| 2 | import { |
| 3 | GROUND_DEFINITIONS, |
| 4 | BLOCK_DEFINITIONS, |
| 5 | GroundType, |
| 6 | normalizeGround, |
| 7 | normalizeBlock, |
| 8 | isChunkAtlasBakedBlock, |
| 9 | isLiquidWaterGround |
| 10 | } from "../definitions/tiles.js"; |
| 11 | import { bloomImageName } from "../definitions/tileSprites.js"; |
| 12 | import { resolveSeasonalVisualState } from "../world/season.js"; |
| 13 | import { SOFT_TERRAIN_NEIGHBOR_DIRS } from "./softTerrain.js"; |
| 14 | import { WET_SAND_BAND_FRACTION, WET_SAND_NEIGHBOR_DIRS } from "./wetSand.js"; |
| 15 | |
| 16 | export const EMPTY_BLOCK_ROW_KEY = "blockRow|empty"; |
| 17 | export const EMPTY_BLOOM_KEY = "bloom|empty"; |
| 18 | |
| 19 | /** |
| 20 | * Fingerprints for chunk ground / block-row / bloom atlases. |
| 21 | * Identical keys mean identical baked pixels (ignoring chunk coordinates). |
| 22 | */ |
| 23 | export function computeChunkAtlasKeys ({ |
| 24 | snapshot, |
| 25 | catalog, |
| 26 | preview = null, |
| 27 | season, |
| 28 | softTerrain = false, |
| 29 | wetSandBandFraction = WET_SAND_BAND_FRACTION, |
| 30 | resolveGround = null |
| 31 | }) |
| 32 | { |
| 33 | const baseTileX = snapshot.chunkX * CHUNK_SIZE; |
| 34 | const baseTileY = snapshot.chunkY * CHUNK_SIZE; |
| 35 | const groundFps = []; |
| 36 | for (let localY = 0; localY < CHUNK_SIZE; localY++) |
| 37 | { |
| 38 | for (let localX = 0; localX < CHUNK_SIZE; localX++) |
| 39 | { |
| 40 | groundFps.push(groundFingerprint( |
| 41 | catalog, |
| 42 | tileGroundEntry(snapshot, preview, localX, localY), |
| 43 | season |
| 44 | )); |
| 45 | } |
| 46 | } |
| 47 | let groundBody = compactFingerprint("ground", groundFps); |
| 48 | if (softTerrain) |
| 49 | { |
| 50 | groundBody += `|soft:${softBorderGroundFingerprint({ |
| 51 | snapshot, |
| 52 | catalog, |
| 53 | season, |
| 54 | preview, |
| 55 | resolveGround |
| 56 | })}`; |
| 57 | } |
| 58 | const wetBand = Math.max(0, Number(wetSandBandFraction) || 0); |
| 59 | if (wetBand > 0) |
| 60 | { |
| 61 | groundBody += `|wetBand:${wetBand}|wet:${wetSandBorderFingerprint({ |
| 62 | snapshot, |
| 63 | season, |
| 64 | preview, |
| 65 | resolveGround |
| 66 | })}`; |
| 67 | } |
| 68 | else |
| 69 | { groundBody += "|wetBand:0"; } |
| 70 | const groundKey = `ground|${season}|${groundBody}`; |
| 71 | |
| 72 | const blockRowKeys = []; |
| 73 | for (let row = 0; row < CHUNK_SIZE; row++) |
| 74 | { |
| 75 | const rowFps = []; |
| 76 | for (let localX = 0; localX < CHUNK_SIZE; localX++) |
| 77 | { |
| 78 | const index = row * CHUNK_SIZE + localX; |
| 79 | const tileX = baseTileX + localX; |
| 80 | const tileY = baseTileY + row; |
| 81 | rowFps.push(blockFingerprint( |
| 82 | catalog, |
| 83 | tileBlockEntry(snapshot, preview, tileX, tileY, index), |
| 84 | season |
| 85 | )); |
| 86 | } |
| 87 | blockRowKeys.push(compactRowFingerprint("blockRow", rowFps)); |
| 88 | } |
| 89 | |
| 90 | const bloomFps = []; |
| 91 | for (let localY = 0; localY < CHUNK_SIZE; localY++) |
| 92 | { |
| 93 | for (let localX = 0; localX < CHUNK_SIZE; localX++) |
| 94 | { |
| 95 | const index = localY * CHUNK_SIZE + localX; |
| 96 | const tileX = baseTileX + localX; |
| 97 | const tileY = baseTileY + localY; |
| 98 | bloomFps.push(bloomFingerprint( |
| 99 | catalog, |
| 100 | tileGroundEntry(snapshot, preview, localX, localY), |
| 101 | tileBlockEntry(snapshot, preview, tileX, tileY, index), |
| 102 | season |
| 103 | )); |
| 104 | } |
| 105 | } |
| 106 | const bloomKey = compactRowFingerprint("bloom", bloomFps); |
| 107 | |
| 108 | return { groundKey, blockRowKeys, bloomKey }; |
| 109 | } |
| 110 | |
| 111 | function tileGroundEntry (snapshot, preview, localX, localY) |
| 112 | { |
| 113 | const index = localY * CHUNK_SIZE + localX; |
| 114 | const baseTileX = snapshot.chunkX * CHUNK_SIZE; |
| 115 | const baseTileY = snapshot.chunkY * CHUNK_SIZE; |
| 116 | const tileX = baseTileX + localX; |
| 117 | const tileY = baseTileY + localY; |
| 118 | let groundEntry = snapshot.grounds[index]; |
| 119 | if (preview && preview.tileX === tileX && preview.tileY === tileY && preview.ground !== undefined) |
| 120 | { groundEntry = preview.ground; } |
| 121 | return groundEntry; |
| 122 | } |
| 123 | |
| 124 | function tileBlockEntry (snapshot, preview, tileX, tileY, index) |
| 125 | { |
| 126 | let blockEntry = snapshot.blocks[index]; |
| 127 | if (preview && preview.tileX === tileX && preview.tileY === tileY && preview.block !== undefined) |
| 128 | { blockEntry = preview.block; } |
| 129 | return blockEntry; |
| 130 | } |
| 131 | |
| 132 | function compactFingerprint (prefix, fps) |
| 133 | { |
| 134 | if (!fps.length) |
| 135 | { return `${prefix}|empty`; } |
| 136 | const first = fps[0]; |
| 137 | if (fps.every((fp) => fp === first)) |
| 138 | { return `${prefix}|u:${first}`; } |
| 139 | return `${prefix}|m:${fps.join(",")}`; |
| 140 | } |
| 141 | |
| 142 | function compactRowFingerprint (prefix, fps) |
| 143 | { |
| 144 | if (!fps.length || fps.every((fp) => fp === "_")) |
| 145 | { return prefix === "blockRow" ? EMPTY_BLOCK_ROW_KEY : EMPTY_BLOOM_KEY; } |
| 146 | const first = fps[0]; |
| 147 | if (fps.every((fp) => fp === first)) |
| 148 | { return `${prefix}|u:${first}`; } |
| 149 | return `${prefix}|m:${fps.join(",")}`; |
| 150 | } |
| 151 | |
| 152 | function groundFingerprint (catalog, ground, season) |
| 153 | { |
| 154 | return groundVisualKey(catalog, ground, season); |
| 155 | } |
| 156 | |
| 157 | function blockFingerprint (catalog, block, season) |
| 158 | { |
| 159 | const entry = normalizeBlock(block); |
| 160 | if (!entry) |
| 161 | { return "_"; } |
| 162 | if (!isChunkAtlasBakedBlock(entry)) |
| 163 | { return "_"; } |
| 164 | const def = BLOCK_DEFINITIONS[entry.typeId]; |
| 165 | if (!def || def.editorHidden) |
| 166 | { return "_"; } |
| 167 | const states = catalog.blocks?.[def.key]?.states ?? []; |
| 168 | const state = resolveSeasonalVisualState(states, entry.state, season); |
| 169 | return `${entry.typeId}:${state ?? ""}`; |
| 170 | } |
| 171 | |
| 172 | function bloomFingerprint (catalog, groundEntry, blockEntry, season) |
| 173 | { |
| 174 | const parts = []; |
| 175 | const groundBloom = groundBloomName(catalog, groundEntry, season); |
| 176 | if (groundBloom) |
| 177 | { parts.push(`g:${groundBloom}`); } |
| 178 | if (isChunkAtlasBakedBlock(normalizeBlock(blockEntry))) |
| 179 | { |
| 180 | const blockBloom = blockBloomName(catalog, blockEntry, season); |
| 181 | if (blockBloom) |
| 182 | { parts.push(`b:${blockBloom}`); } |
| 183 | } |
| 184 | return parts.length ? parts.join("+") : "_"; |
| 185 | } |
| 186 | |
| 187 | function softBorderGroundFingerprint ({ |
| 188 | snapshot, |
| 189 | catalog, |
| 190 | season, |
| 191 | preview, |
| 192 | resolveGround |
| 193 | }) |
| 194 | { |
| 195 | if (typeof resolveGround !== "function") |
| 196 | { return "none"; } |
| 197 | const baseTileX = snapshot.chunkX * CHUNK_SIZE; |
| 198 | const baseTileY = snapshot.chunkY * CHUNK_SIZE; |
| 199 | const parts = []; |
| 200 | for (let localY = 0; localY < CHUNK_SIZE; localY++) |
| 201 | { |
| 202 | for (let localX = 0; localX < CHUNK_SIZE; localX++) |
| 203 | { |
| 204 | const onBorder = localX === 0 || localY === 0 || localX === CHUNK_SIZE - 1 || localY === CHUNK_SIZE - 1; |
| 205 | if (!onBorder) |
| 206 | { continue; } |
| 207 | const tileX = baseTileX + localX; |
| 208 | const tileY = baseTileY + localY; |
| 209 | const groundEntry = tileGroundEntry(snapshot, preview, localX, localY); |
| 210 | if (!isGroundBlendable(groundEntry)) |
| 211 | { continue; } |
| 212 | const selfKey = groundVisualKey(catalog, groundEntry, season); |
| 213 | for (const { ndx, ndy } of SOFT_TERRAIN_NEIGHBOR_DIRS) |
| 214 | { |
| 215 | const nx = tileX + ndx; |
| 216 | const ny = tileY + ndy; |
| 217 | if (nx >= baseTileX && nx < baseTileX + CHUNK_SIZE |
| 218 | && ny >= baseTileY && ny < baseTileY + CHUNK_SIZE) |
| 219 | { continue; } |
| 220 | const neighbor = resolveGround(nx, ny); |
| 221 | if (neighbor == null) |
| 222 | { continue; } |
| 223 | const neighborKey = groundVisualKey(catalog, neighbor, season); |
| 224 | if (neighborKey === selfKey) |
| 225 | { continue; } |
| 226 | parts.push(`${tileX},${tileY}:${ndx},${ndy}:${neighborKey}`); |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | return parts.length ? parts.join(";") : "flat"; |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Cross-chunk liquid-water adjacency for wet-shore sand (8 dirs). |
| 235 | * Winter ice (shallow water) is excluded via isLiquidWaterGround. |
| 236 | */ |
| 237 | function wetSandBorderFingerprint ({ |
| 238 | snapshot, |
| 239 | season, |
| 240 | preview, |
| 241 | resolveGround |
| 242 | }) |
| 243 | { |
| 244 | if (typeof resolveGround !== "function") |
| 245 | { return "none"; } |
| 246 | const baseTileX = snapshot.chunkX * CHUNK_SIZE; |
| 247 | const baseTileY = snapshot.chunkY * CHUNK_SIZE; |
| 248 | const parts = []; |
| 249 | for (let localY = 0; localY < CHUNK_SIZE; localY++) |
| 250 | { |
| 251 | for (let localX = 0; localX < CHUNK_SIZE; localX++) |
| 252 | { |
| 253 | const onBorder = localX === 0 || localY === 0 || localX === CHUNK_SIZE - 1 || localY === CHUNK_SIZE - 1; |
| 254 | if (!onBorder) |
| 255 | { continue; } |
| 256 | const tileX = baseTileX + localX; |
| 257 | const tileY = baseTileY + localY; |
| 258 | const groundEntry = tileGroundEntry(snapshot, preview, localX, localY); |
| 259 | if (!isWetShoreGround(groundEntry)) |
| 260 | { continue; } |
| 261 | for (const { ndx, ndy } of WET_SAND_NEIGHBOR_DIRS) |
| 262 | { |
| 263 | const nx = tileX + ndx; |
| 264 | const ny = tileY + ndy; |
| 265 | if (nx >= baseTileX && nx < baseTileX + CHUNK_SIZE |
| 266 | && ny >= baseTileY && ny < baseTileY + CHUNK_SIZE) |
| 267 | { continue; } |
| 268 | const neighbor = resolveGround(nx, ny); |
| 269 | if (neighbor == null) |
| 270 | { continue; } |
| 271 | const typeId = normalizeGround(neighbor).typeId; |
| 272 | if (!isLiquidWaterGround(typeId, season)) |
| 273 | { continue; } |
| 274 | parts.push(`${tileX},${tileY}:${ndx},${ndy}:${typeId}`); |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | return parts.length ? parts.join(";") : "flat"; |
| 279 | } |
| 280 | |
| 281 | function isGroundBlendable (ground) |
| 282 | { |
| 283 | const entry = normalizeGround(ground); |
| 284 | return !!GROUND_DEFINITIONS[entry.typeId]?.blendable; |
| 285 | } |
| 286 | |
| 287 | function isWetShoreGround (ground) |
| 288 | { |
| 289 | const entry = normalizeGround(ground); |
| 290 | return !!GROUND_DEFINITIONS[entry.typeId]?.wetShore; |
| 291 | } |
| 292 | |
| 293 | function groundVisualKey (catalog, ground, season) |
| 294 | { |
| 295 | const entry = normalizeGround(ground); |
| 296 | const def = GROUND_DEFINITIONS[entry.typeId] ?? GROUND_DEFINITIONS[GroundType.grass]; |
| 297 | const states = catalog.grounds?.[def.key]?.states ?? []; |
| 298 | const state = resolveSeasonalVisualState(states, entry.state, season); |
| 299 | return `${entry.typeId}:${state ?? ""}`; |
| 300 | } |
| 301 | |
| 302 | function groundBloomName (catalog, ground, season) |
| 303 | { |
| 304 | const entry = normalizeGround(ground); |
| 305 | const def = GROUND_DEFINITIONS[entry.typeId] ?? GROUND_DEFINITIONS[GroundType.grass]; |
| 306 | const type = catalog.grounds?.[def.key]; |
| 307 | const state = resolveSeasonalVisualState(type?.states ?? [], entry.state, season); |
| 308 | if (!state || !type?.blooms?.[state]) |
| 309 | { return null; } |
| 310 | return bloomImageName("ground", def.key, state); |
| 311 | } |
| 312 | |
| 313 | function blockBloomName (catalog, block, season) |
| 314 | { |
| 315 | const entry = normalizeBlock(block); |
| 316 | if (!entry) |
| 317 | { return null; } |
| 318 | const def = BLOCK_DEFINITIONS[entry.typeId]; |
| 319 | if (!def) |
| 320 | { return null; } |
| 321 | const type = catalog.blocks?.[def.key]; |
| 322 | const state = resolveSeasonalVisualState(type?.states ?? [], entry.state, season); |
| 323 | if (!state || !type?.blooms?.[state]) |
| 324 | { return null; } |
| 325 | return bloomImageName("block", def.key, state); |
| 326 | } |