import { CHUNK_SIZE } from "../protocol/constants.js"; import { GROUND_DEFINITIONS, BLOCK_DEFINITIONS, GroundType, normalizeGround, normalizeBlock, isChunkAtlasBakedBlock, isLiquidWaterGround } from "../definitions/tiles.js"; import { bloomImageName } from "../definitions/tileSprites.js"; import { resolveSeasonalVisualState } from "../world/season.js"; import { SOFT_TERRAIN_NEIGHBOR_DIRS } from "./softTerrain.js"; import { WET_SAND_BAND_FRACTION, WET_SAND_NEIGHBOR_DIRS } from "./wetSand.js"; export const EMPTY_BLOCK_ROW_KEY = "blockRow|empty"; export const EMPTY_BLOOM_KEY = "bloom|empty"; /** * Fingerprints for chunk ground / block-row / bloom atlases. * Identical keys mean identical baked pixels (ignoring chunk coordinates). */ export function computeChunkAtlasKeys ({ snapshot, catalog, preview = null, season, softTerrain = false, wetSandBandFraction = WET_SAND_BAND_FRACTION, resolveGround = null }) { const baseTileX = snapshot.chunkX * CHUNK_SIZE; const baseTileY = snapshot.chunkY * CHUNK_SIZE; const groundFps = []; for (let localY = 0; localY < CHUNK_SIZE; localY++) { for (let localX = 0; localX < CHUNK_SIZE; localX++) { groundFps.push(groundFingerprint( catalog, tileGroundEntry(snapshot, preview, localX, localY), season )); } } let groundBody = compactFingerprint("ground", groundFps); if (softTerrain) { groundBody += `|soft:${softBorderGroundFingerprint({ snapshot, catalog, season, preview, resolveGround })}`; } const wetBand = Math.max(0, Number(wetSandBandFraction) || 0); if (wetBand > 0) { groundBody += `|wetBand:${wetBand}|wet:${wetSandBorderFingerprint({ snapshot, season, preview, resolveGround })}`; } else { groundBody += "|wetBand:0"; } const groundKey = `ground|${season}|${groundBody}`; const blockRowKeys = []; for (let row = 0; row < CHUNK_SIZE; row++) { const rowFps = []; for (let localX = 0; localX < CHUNK_SIZE; localX++) { const index = row * CHUNK_SIZE + localX; const tileX = baseTileX + localX; const tileY = baseTileY + row; rowFps.push(blockFingerprint( catalog, tileBlockEntry(snapshot, preview, tileX, tileY, index), season )); } blockRowKeys.push(compactRowFingerprint("blockRow", rowFps)); } const bloomFps = []; for (let localY = 0; localY < CHUNK_SIZE; localY++) { for (let localX = 0; localX < CHUNK_SIZE; localX++) { const index = localY * CHUNK_SIZE + localX; const tileX = baseTileX + localX; const tileY = baseTileY + localY; bloomFps.push(bloomFingerprint( catalog, tileGroundEntry(snapshot, preview, localX, localY), tileBlockEntry(snapshot, preview, tileX, tileY, index), season )); } } const bloomKey = compactRowFingerprint("bloom", bloomFps); return { groundKey, blockRowKeys, bloomKey }; } function tileGroundEntry (snapshot, preview, localX, localY) { const index = localY * CHUNK_SIZE + localX; const baseTileX = snapshot.chunkX * CHUNK_SIZE; const baseTileY = snapshot.chunkY * CHUNK_SIZE; const tileX = baseTileX + localX; const tileY = baseTileY + localY; let groundEntry = snapshot.grounds[index]; if (preview && preview.tileX === tileX && preview.tileY === tileY && preview.ground !== undefined) { groundEntry = preview.ground; } return groundEntry; } function tileBlockEntry (snapshot, preview, tileX, tileY, index) { let blockEntry = snapshot.blocks[index]; if (preview && preview.tileX === tileX && preview.tileY === tileY && preview.block !== undefined) { blockEntry = preview.block; } return blockEntry; } function compactFingerprint (prefix, fps) { if (!fps.length) { return `${prefix}|empty`; } const first = fps[0]; if (fps.every((fp) => fp === first)) { return `${prefix}|u:${first}`; } return `${prefix}|m:${fps.join(",")}`; } function compactRowFingerprint (prefix, fps) { if (!fps.length || fps.every((fp) => fp === "_")) { return prefix === "blockRow" ? EMPTY_BLOCK_ROW_KEY : EMPTY_BLOOM_KEY; } const first = fps[0]; if (fps.every((fp) => fp === first)) { return `${prefix}|u:${first}`; } return `${prefix}|m:${fps.join(",")}`; } function groundFingerprint (catalog, ground, season) { return groundVisualKey(catalog, ground, season); } function blockFingerprint (catalog, block, season) { const entry = normalizeBlock(block); if (!entry) { return "_"; } if (!isChunkAtlasBakedBlock(entry)) { return "_"; } const def = BLOCK_DEFINITIONS[entry.typeId]; if (!def || def.editorHidden) { return "_"; } const states = catalog.blocks?.[def.key]?.states ?? []; const state = resolveSeasonalVisualState(states, entry.state, season); return `${entry.typeId}:${state ?? ""}`; } function bloomFingerprint (catalog, groundEntry, blockEntry, season) { const parts = []; const groundBloom = groundBloomName(catalog, groundEntry, season); if (groundBloom) { parts.push(`g:${groundBloom}`); } if (isChunkAtlasBakedBlock(normalizeBlock(blockEntry))) { const blockBloom = blockBloomName(catalog, blockEntry, season); if (blockBloom) { parts.push(`b:${blockBloom}`); } } return parts.length ? parts.join("+") : "_"; } function softBorderGroundFingerprint ({ snapshot, catalog, season, preview, resolveGround }) { if (typeof resolveGround !== "function") { return "none"; } const baseTileX = snapshot.chunkX * CHUNK_SIZE; const baseTileY = snapshot.chunkY * CHUNK_SIZE; const parts = []; for (let localY = 0; localY < CHUNK_SIZE; localY++) { for (let localX = 0; localX < CHUNK_SIZE; localX++) { const onBorder = localX === 0 || localY === 0 || localX === CHUNK_SIZE - 1 || localY === CHUNK_SIZE - 1; if (!onBorder) { continue; } const tileX = baseTileX + localX; const tileY = baseTileY + localY; const groundEntry = tileGroundEntry(snapshot, preview, localX, localY); if (!isGroundBlendable(groundEntry)) { continue; } const selfKey = groundVisualKey(catalog, groundEntry, season); for (const { ndx, ndy } of SOFT_TERRAIN_NEIGHBOR_DIRS) { const nx = tileX + ndx; const ny = tileY + ndy; if (nx >= baseTileX && nx < baseTileX + CHUNK_SIZE && ny >= baseTileY && ny < baseTileY + CHUNK_SIZE) { continue; } const neighbor = resolveGround(nx, ny); if (neighbor == null) { continue; } const neighborKey = groundVisualKey(catalog, neighbor, season); if (neighborKey === selfKey) { continue; } const nbrTypeId = normalizeGround(neighbor).typeId; const nbrDef = GROUND_DEFINITIONS[nbrTypeId]; if (nbrDef?.proceduralWaves || isLiquidWaterGround(nbrTypeId, season)) { continue; } parts.push(`${tileX},${tileY}:${ndx},${ndy}:${neighborKey}`); } } } return parts.length ? parts.join(";") : "flat"; } /** * Cross-chunk liquid-water adjacency for wet-shore sand (8 dirs). * Winter ice (shallow water) is excluded via isLiquidWaterGround. */ function wetSandBorderFingerprint ({ snapshot, season, preview, resolveGround }) { if (typeof resolveGround !== "function") { return "none"; } const baseTileX = snapshot.chunkX * CHUNK_SIZE; const baseTileY = snapshot.chunkY * CHUNK_SIZE; const parts = []; for (let localY = 0; localY < CHUNK_SIZE; localY++) { for (let localX = 0; localX < CHUNK_SIZE; localX++) { const onBorder = localX === 0 || localY === 0 || localX === CHUNK_SIZE - 1 || localY === CHUNK_SIZE - 1; if (!onBorder) { continue; } const tileX = baseTileX + localX; const tileY = baseTileY + localY; const groundEntry = tileGroundEntry(snapshot, preview, localX, localY); if (!isWetShoreGround(groundEntry)) { continue; } for (const { ndx, ndy } of WET_SAND_NEIGHBOR_DIRS) { const nx = tileX + ndx; const ny = tileY + ndy; if (nx >= baseTileX && nx < baseTileX + CHUNK_SIZE && ny >= baseTileY && ny < baseTileY + CHUNK_SIZE) { continue; } const neighbor = resolveGround(nx, ny); if (neighbor == null) { continue; } const typeId = normalizeGround(neighbor).typeId; if (!isLiquidWaterGround(typeId, season)) { continue; } parts.push(`${tileX},${tileY}:${ndx},${ndy}:${typeId}`); } } } return parts.length ? parts.join(";") : "flat"; } function isGroundBlendable (ground) { const entry = normalizeGround(ground); return !!GROUND_DEFINITIONS[entry.typeId]?.blendable; } function isWetShoreGround (ground) { const entry = normalizeGround(ground); return !!GROUND_DEFINITIONS[entry.typeId]?.wetShore; } function groundVisualKey (catalog, ground, season) { const entry = normalizeGround(ground); const def = GROUND_DEFINITIONS[entry.typeId] ?? GROUND_DEFINITIONS[GroundType.grass]; const states = catalog.grounds?.[def.key]?.states ?? []; const state = resolveSeasonalVisualState(states, entry.state, season); return `${entry.typeId}:${state ?? ""}`; } function groundBloomName (catalog, ground, season) { const entry = normalizeGround(ground); const def = GROUND_DEFINITIONS[entry.typeId] ?? GROUND_DEFINITIONS[GroundType.grass]; const type = catalog.grounds?.[def.key]; const state = resolveSeasonalVisualState(type?.states ?? [], entry.state, season); if (!state || !type?.blooms?.[state]) { return null; } return bloomImageName("ground", def.key, state); } function blockBloomName (catalog, block, season) { const entry = normalizeBlock(block); if (!entry) { return null; } const def = BLOCK_DEFINITIONS[entry.typeId]; if (!def) { return null; } const type = catalog.blocks?.[def.key]; const state = resolveSeasonalVisualState(type?.states ?? [], entry.state, season); if (!state || !type?.blooms?.[state]) { return null; } return bloomImageName("block", def.key, state); }