import { CHUNK_SIZE, SPRITE_SIZE_PX } from "/shared/protocol/constants.js"; import { GROUND_DEFINITIONS, BLOCK_DEFINITIONS, GroundType, normalizeGround, normalizeBlock } from "/shared/definitions/tiles.js"; import { bloomImageName, spriteImageName } from "/shared/definitions/tileSprites.js"; import { resolveSeasonalVisualState, SeasonId } from "/shared/world/season.js"; /** * Bake immobile ≤16×16 ground/block/bloom tiles into chunk atlases. * Oversized or animated blocks/grounds are returned for individual sprites. */ export function bakeChunkAtlases ({ snapshot, catalog, images, preview = null, season = SeasonId.summer }) { const width = CHUNK_SIZE * SPRITE_SIZE_PX; const height = CHUNK_SIZE * SPRITE_SIZE_PX; const ground = makeCanvas(width, height); const bloom = makeCanvas(width, height); const blockRows = []; for (let row = 0; row < CHUNK_SIZE; row++) { blockRows.push(makeCanvas(width, SPRITE_SIZE_PX)); } const groundCtx = ground.getContext("2d"); const bloomCtx = bloom.getContext("2d"); const blockCtxs = blockRows.map((canvas) => canvas.getContext("2d")); for (const ctx of [groundCtx, bloomCtx, ...blockCtxs]) { ctx.imageSmoothingEnabled = false; ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height); } const tallBlocks = []; const animatedGrounds = []; const baseTileX = snapshot.chunkX * CHUNK_SIZE; const baseTileY = snapshot.chunkY * CHUNK_SIZE; 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; let groundEntry = snapshot.grounds[index]; let blockEntry = snapshot.blocks[index]; if (preview && preview.tileX === tileX && preview.tileY === tileY) { if (preview.ground !== undefined) { groundEntry = preview.ground; } if (preview.block !== undefined) { blockEntry = preview.block; } } const dx = localX * SPRITE_SIZE_PX; const dy = localY * SPRITE_SIZE_PX; const groundName = groundSpriteName(catalog, groundEntry, season); const groundMeta = groundCatalogMeta(catalog, groundEntry); if (isBakeable(images[groundName], groundMeta)) { blit(groundCtx, images[groundName], dx, dy); const groundBloom = groundBloomName(catalog, groundEntry, season); if (groundBloom) { blit(bloomCtx, images[groundBloom], dx, dy); } } else if (groundName) { animatedGrounds.push({ tileX, tileY, sprite: groundName, typeId: normalizeGround(groundEntry).typeId, bloom: groundBloomName(catalog, groundEntry, season) }); } const blockName = blockSpriteName(catalog, blockEntry, season); if (!blockName) { continue; } const blockMeta = blockCatalogMeta(catalog, blockEntry); if (isBakeable(images[blockName], blockMeta)) { blit(blockCtxs[localY], images[blockName], dx, 0); const blockBloom = blockBloomName(catalog, blockEntry, season); if (blockBloom) { blit(bloomCtx, images[blockBloom], dx, dy); } } else { tallBlocks.push({ tileX, tileY, sprite: blockName, bloom: blockBloomName(catalog, blockEntry, season) }); } } } return { ground, blockRows, bloom, tallBlocks, animatedGrounds }; } export function isBakeable (sprite, meta = null) { if (!sprite?.g_image) { return false; } if (sprite.width > SPRITE_SIZE_PX || sprite.height > SPRITE_SIZE_PX) { return false; } const frames = Number(sprite.subImages) || Number(sprite.subImgTotal) || (meta?.frames ?? 1); const speed = sprite._animationSpeed ?? meta?.animationSpeed ?? 0; if (frames > 1) { return false; } if (speed > 0) { return false; } return true; } function makeCanvas (width, height) { const canvas = document.createElement("canvas"); canvas.width = width; canvas.height = height; return canvas; } function blit (ctx, sprite, dx, dy) { if (!sprite?.g_image) { return; } const sub = sprite.getSubImage(0); ctx.drawImage( sprite.g_image, sub.sx, sub.sy, sub.sw, sub.sh, dx, dy, SPRITE_SIZE_PX, SPRITE_SIZE_PX ); } function groundSpriteName (catalog, ground, season = SeasonId.summer) { 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 spriteImageName("ground", def.key, state); } function blockSpriteName (catalog, block, season = SeasonId.summer) { const entry = normalizeBlock(block); if (!entry) { return null; } const def = BLOCK_DEFINITIONS[entry.typeId]; if (!def || def.editorHidden) { return null; } const states = catalog.blocks?.[def.key]?.states ?? []; const state = resolveSeasonalVisualState(states, entry.state, season); return spriteImageName("block", def.key, state); } function groundBloomName (catalog, ground, season = SeasonId.summer) { 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 = SeasonId.summer) { 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); } function blockCatalogMeta (catalog, block) { const entry = normalizeBlock(block); if (!entry) { return null; } const def = BLOCK_DEFINITIONS[entry.typeId]; if (!def) { return null; } return catalog.blocks?.[def.key] ?? null; } function groundCatalogMeta (catalog, ground) { const entry = normalizeGround(ground); const def = GROUND_DEFINITIONS[entry.typeId] ?? GROUND_DEFINITIONS[GroundType.grass]; return catalog.grounds?.[def.key] ?? { frames: def.frames, animationSpeed: def.animationSpeed }; }