| 1 | import { CHUNK_SIZE, SPRITE_SIZE_PX } from "/shared/protocol/constants.js"; |
| 2 | import { |
| 3 | GROUND_DEFINITIONS, |
| 4 | BLOCK_DEFINITIONS, |
| 5 | GroundType, |
| 6 | normalizeGround, |
| 7 | normalizeBlock, |
| 8 | isChunkAtlasBakedBlock, |
| 9 | isLiquidWaterGround |
| 10 | } from "/shared/definitions/tiles.js"; |
| 11 | import { bloomImageName, spriteImageName } from "/shared/definitions/tileSprites.js"; |
| 12 | import { resolveSeasonalVisualState, SeasonId } from "/shared/world/season.js"; |
| 13 | import { |
| 14 | SOFT_TERRAIN_NEIGHBOR_DIRS, |
| 15 | softTerrainBlendWeight, |
| 16 | softTerrainNeighborSample, |
| 17 | mixSoftTerrainPixel |
| 18 | } from "/shared/render/softTerrain.js"; |
| 19 | import { |
| 20 | WET_SAND_NEIGHBOR_DIRS, |
| 21 | wetSandWetness, |
| 22 | mixWetSandPixel |
| 23 | } from "/shared/render/wetSand.js"; |
| 24 | import { computeChunkAtlasKeys } from "/shared/render/chunkAtlasKeys.js"; |
| 25 | |
| 26 | /** |
| 27 | * Bake immobile ≤16×16 ground/block/bloom tiles into chunk atlases. |
| 28 | * Oversized or animated blocks/grounds are returned for individual sprites. |
| 29 | * When `cache` is set, identical ground/row/bloom layouts reuse canvases already in GPU memory. |
| 30 | * @param {{ |
| 31 | * snapshot: object, |
| 32 | * catalog: object, |
| 33 | * images: object, |
| 34 | * preview?: object | null, |
| 35 | * season?: string, |
| 36 | * softTerrain?: boolean, |
| 37 | * resolveGround?: (tileX: number, tileY: number) => object | number | null, |
| 38 | * cache?: ReturnType<typeof import("./chunkAtlasCache.js").createChunkAtlasCache> |
| 39 | * }} options |
| 40 | */ |
| 41 | export function bakeChunkAtlases ({ |
| 42 | snapshot, |
| 43 | catalog, |
| 44 | images, |
| 45 | preview = null, |
| 46 | season = SeasonId.summer, |
| 47 | softTerrain = false, |
| 48 | resolveGround = null, |
| 49 | cache = null |
| 50 | }) |
| 51 | { |
| 52 | const extras = collectChunkSpriteExtras({ |
| 53 | snapshot, |
| 54 | catalog, |
| 55 | images, |
| 56 | preview, |
| 57 | season |
| 58 | }); |
| 59 | const keys = computeChunkAtlasKeys({ |
| 60 | snapshot, |
| 61 | catalog, |
| 62 | preview, |
| 63 | season, |
| 64 | softTerrain, |
| 65 | resolveGround |
| 66 | }); |
| 67 | const bakeCtx = { |
| 68 | snapshot, |
| 69 | catalog, |
| 70 | images, |
| 71 | preview, |
| 72 | season, |
| 73 | softTerrain, |
| 74 | resolveGround |
| 75 | }; |
| 76 | |
| 77 | let groundEntry; |
| 78 | let blockRowEntries; |
| 79 | let bloomEntry; |
| 80 | |
| 81 | if (cache) |
| 82 | { |
| 83 | groundEntry = cache.acquireGround(keys.groundKey, () => bakeGroundCanvas(bakeCtx)); |
| 84 | blockRowEntries = keys.blockRowKeys.map((key, row) => |
| 85 | cache.acquireBlockRow(key, () => bakeBlockRowCanvas(bakeCtx, row))); |
| 86 | bloomEntry = cache.acquireBloom(keys.bloomKey, () => bakeBloomCanvas(bakeCtx)); |
| 87 | } |
| 88 | else |
| 89 | { |
| 90 | groundEntry = { canvas: bakeGroundCanvas(bakeCtx) }; |
| 91 | blockRowEntries = keys.blockRowKeys.map((_key, row) => ({ |
| 92 | canvas: bakeBlockRowCanvas(bakeCtx, row) |
| 93 | })); |
| 94 | bloomEntry = { canvas: bakeBloomCanvas(bakeCtx) }; |
| 95 | } |
| 96 | |
| 97 | return { |
| 98 | ground: groundEntry.canvas, |
| 99 | blockRows: blockRowEntries.map((entry) => entry.canvas), |
| 100 | bloom: bloomEntry.canvas, |
| 101 | tallBlocks: extras.tallBlocks, |
| 102 | animatedGrounds: extras.animatedGrounds, |
| 103 | cacheRefs: cache |
| 104 | ? { |
| 105 | ground: groundEntry, |
| 106 | blockRows: blockRowEntries, |
| 107 | bloom: bloomEntry |
| 108 | } |
| 109 | : null |
| 110 | }; |
| 111 | } |
| 112 | |
| 113 | export { computeChunkAtlasKeys } from "/shared/render/chunkAtlasKeys.js"; |
| 114 | |
| 115 | export function collectChunkSpriteExtras ({ |
| 116 | snapshot, |
| 117 | catalog, |
| 118 | images, |
| 119 | preview = null, |
| 120 | season = SeasonId.summer |
| 121 | }) |
| 122 | { |
| 123 | const tallBlocks = []; |
| 124 | const animatedGrounds = []; |
| 125 | const baseTileX = snapshot.chunkX * CHUNK_SIZE; |
| 126 | const baseTileY = snapshot.chunkY * CHUNK_SIZE; |
| 127 | |
| 128 | for (let localY = 0; localY < CHUNK_SIZE; localY++) |
| 129 | { |
| 130 | for (let localX = 0; localX < CHUNK_SIZE; localX++) |
| 131 | { |
| 132 | const index = localY * CHUNK_SIZE + localX; |
| 133 | const tileX = baseTileX + localX; |
| 134 | const tileY = baseTileY + localY; |
| 135 | const groundEntry = tileGroundEntry(snapshot, preview, localX, localY); |
| 136 | const blockEntry = tileBlockEntry(snapshot, preview, tileX, tileY, index); |
| 137 | |
| 138 | const groundName = groundSpriteName(catalog, groundEntry, season); |
| 139 | const groundMeta = groundCatalogMeta(catalog, groundEntry); |
| 140 | if (groundName && !isBakeable(images[groundName], groundMeta)) |
| 141 | { |
| 142 | animatedGrounds.push({ |
| 143 | tileX, |
| 144 | tileY, |
| 145 | sprite: groundName, |
| 146 | typeId: normalizeGround(groundEntry).typeId, |
| 147 | bloom: groundBloomName(catalog, groundEntry, season) |
| 148 | }); |
| 149 | } |
| 150 | |
| 151 | const blockName = blockSpriteName(catalog, blockEntry, season); |
| 152 | if (!blockName) |
| 153 | { continue; } |
| 154 | const blockMeta = blockCatalogMeta(catalog, blockEntry); |
| 155 | if (!isBakeable(images[blockName], blockMeta) || !isChunkAtlasBakedBlock(blockEntry)) |
| 156 | { |
| 157 | tallBlocks.push({ |
| 158 | tileX, |
| 159 | tileY, |
| 160 | sprite: blockName, |
| 161 | bloom: blockBloomName(catalog, blockEntry, season) |
| 162 | }); |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | return { tallBlocks, animatedGrounds }; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Per-tile block sprite when the block is not represented in the chunk atlas row. |
| 172 | * @returns {{ sprite: string, bloom: string | null } | null} |
| 173 | */ |
| 174 | export function tileBlockSpriteEntry (catalog, images, blockEntry, season = SeasonId.summer) |
| 175 | { |
| 176 | if (!blockEntry) |
| 177 | { return null; } |
| 178 | const blockName = blockSpriteName(catalog, blockEntry, season); |
| 179 | if (!blockName) |
| 180 | { return null; } |
| 181 | const blockMeta = blockCatalogMeta(catalog, blockEntry); |
| 182 | const bakeable = isBakeable(images[blockName], blockMeta); |
| 183 | if (bakeable && isChunkAtlasBakedBlock(blockEntry)) |
| 184 | { return null; } |
| 185 | return { |
| 186 | sprite: blockName, |
| 187 | bloom: blockBloomName(catalog, blockEntry, season) |
| 188 | }; |
| 189 | } |
| 190 | |
| 191 | function bakeGroundCanvas ({ |
| 192 | snapshot, |
| 193 | catalog, |
| 194 | images, |
| 195 | preview, |
| 196 | season, |
| 197 | softTerrain, |
| 198 | resolveGround |
| 199 | }) |
| 200 | { |
| 201 | const width = CHUNK_SIZE * SPRITE_SIZE_PX; |
| 202 | const height = CHUNK_SIZE * SPRITE_SIZE_PX; |
| 203 | const ground = makeCanvas(width, height); |
| 204 | const groundCtx = ground.getContext("2d", { willReadFrequently: true }); |
| 205 | groundCtx.imageSmoothingEnabled = false; |
| 206 | groundCtx.clearRect(0, 0, width, height); |
| 207 | const pixelCache = new Map(); |
| 208 | const baseTileX = snapshot.chunkX * CHUNK_SIZE; |
| 209 | const baseTileY = snapshot.chunkY * CHUNK_SIZE; |
| 210 | |
| 211 | for (let localY = 0; localY < CHUNK_SIZE; localY++) |
| 212 | { |
| 213 | for (let localX = 0; localX < CHUNK_SIZE; localX++) |
| 214 | { |
| 215 | const tileX = baseTileX + localX; |
| 216 | const tileY = baseTileY + localY; |
| 217 | const groundEntry = tileGroundEntry(snapshot, preview, localX, localY); |
| 218 | const dx = localX * SPRITE_SIZE_PX; |
| 219 | const dy = localY * SPRITE_SIZE_PX; |
| 220 | const groundName = groundSpriteName(catalog, groundEntry, season); |
| 221 | const groundMeta = groundCatalogMeta(catalog, groundEntry); |
| 222 | if (!isBakeable(images[groundName], groundMeta)) |
| 223 | { continue; } |
| 224 | const wetShore = isWetShoreGround(groundEntry); |
| 225 | const blendable = softTerrain && isGroundBlendable(groundEntry); |
| 226 | if (wetShore || blendable) |
| 227 | { |
| 228 | blitBlendedGround( |
| 229 | groundCtx, |
| 230 | images, |
| 231 | catalog, |
| 232 | groundEntry, |
| 233 | groundName, |
| 234 | dx, |
| 235 | dy, |
| 236 | tileX, |
| 237 | tileY, |
| 238 | season, |
| 239 | resolveGround, |
| 240 | pixelCache, |
| 241 | { softTerrain: blendable, wetShore } |
| 242 | ); |
| 243 | } |
| 244 | else |
| 245 | { blit(groundCtx, images[groundName], dx, dy); } |
| 246 | } |
| 247 | } |
| 248 | return ground; |
| 249 | } |
| 250 | |
| 251 | function bakeBlockRowCanvas ({ |
| 252 | snapshot, |
| 253 | catalog, |
| 254 | images, |
| 255 | preview, |
| 256 | season |
| 257 | }, row) |
| 258 | { |
| 259 | const width = CHUNK_SIZE * SPRITE_SIZE_PX; |
| 260 | const canvas = makeCanvas(width, SPRITE_SIZE_PX); |
| 261 | const ctx = canvas.getContext("2d"); |
| 262 | ctx.imageSmoothingEnabled = false; |
| 263 | ctx.clearRect(0, 0, width, SPRITE_SIZE_PX); |
| 264 | const baseTileX = snapshot.chunkX * CHUNK_SIZE; |
| 265 | const baseTileY = snapshot.chunkY * CHUNK_SIZE; |
| 266 | |
| 267 | for (let localX = 0; localX < CHUNK_SIZE; localX++) |
| 268 | { |
| 269 | const index = row * CHUNK_SIZE + localX; |
| 270 | const tileX = baseTileX + localX; |
| 271 | const tileY = baseTileY + row; |
| 272 | const blockEntry = tileBlockEntry(snapshot, preview, tileX, tileY, index); |
| 273 | const blockName = blockSpriteName(catalog, blockEntry, season); |
| 274 | if (!blockName) |
| 275 | { continue; } |
| 276 | const blockMeta = blockCatalogMeta(catalog, blockEntry); |
| 277 | if (!isBakeable(images[blockName], blockMeta) || !isChunkAtlasBakedBlock(blockEntry)) |
| 278 | { continue; } |
| 279 | blit(ctx, images[blockName], localX * SPRITE_SIZE_PX, 0); |
| 280 | } |
| 281 | return canvas; |
| 282 | } |
| 283 | |
| 284 | function bakeBloomCanvas ({ |
| 285 | snapshot, |
| 286 | catalog, |
| 287 | images, |
| 288 | preview, |
| 289 | season |
| 290 | }) |
| 291 | { |
| 292 | const width = CHUNK_SIZE * SPRITE_SIZE_PX; |
| 293 | const bloom = makeCanvas(width, width); |
| 294 | const bloomCtx = bloom.getContext("2d"); |
| 295 | bloomCtx.imageSmoothingEnabled = false; |
| 296 | bloomCtx.clearRect(0, 0, width, width); |
| 297 | const baseTileX = snapshot.chunkX * CHUNK_SIZE; |
| 298 | const baseTileY = snapshot.chunkY * CHUNK_SIZE; |
| 299 | |
| 300 | for (let localY = 0; localY < CHUNK_SIZE; localY++) |
| 301 | { |
| 302 | for (let localX = 0; localX < CHUNK_SIZE; localX++) |
| 303 | { |
| 304 | const index = localY * CHUNK_SIZE + localX; |
| 305 | const tileX = baseTileX + localX; |
| 306 | const tileY = baseTileY + localY; |
| 307 | const dx = localX * SPRITE_SIZE_PX; |
| 308 | const dy = localY * SPRITE_SIZE_PX; |
| 309 | const groundEntry = tileGroundEntry(snapshot, preview, localX, localY); |
| 310 | const blockEntry = tileBlockEntry(snapshot, preview, tileX, tileY, index); |
| 311 | |
| 312 | const groundBloom = groundBloomName(catalog, groundEntry, season); |
| 313 | if (groundBloom) |
| 314 | { blit(bloomCtx, images[groundBloom], dx, dy); } |
| 315 | |
| 316 | const blockName = blockSpriteName(catalog, blockEntry, season); |
| 317 | if (!blockName) |
| 318 | { continue; } |
| 319 | const blockMeta = blockCatalogMeta(catalog, blockEntry); |
| 320 | if (!isBakeable(images[blockName], blockMeta) || !isChunkAtlasBakedBlock(blockEntry)) |
| 321 | { continue; } |
| 322 | const blockBloom = blockBloomName(catalog, blockEntry, season); |
| 323 | if (blockBloom) |
| 324 | { blit(bloomCtx, images[blockBloom], dx, dy); } |
| 325 | } |
| 326 | } |
| 327 | return bloom; |
| 328 | } |
| 329 | |
| 330 | export function isBakeable (sprite, meta = null) |
| 331 | { |
| 332 | if (!sprite?.g_image) |
| 333 | { return false; } |
| 334 | if (sprite.width > SPRITE_SIZE_PX || sprite.height > SPRITE_SIZE_PX) |
| 335 | { return false; } |
| 336 | const frames = Number(sprite.subImages) || Number(sprite.subImgTotal) || (meta?.frames ?? 1); |
| 337 | const speed = sprite._animationSpeed ?? meta?.animationSpeed ?? 0; |
| 338 | if (frames > 1) |
| 339 | { return false; } |
| 340 | if (speed > 0) |
| 341 | { return false; } |
| 342 | return true; |
| 343 | } |
| 344 | |
| 345 | function tileGroundEntry (snapshot, preview, localX, localY) |
| 346 | { |
| 347 | const index = localY * CHUNK_SIZE + localX; |
| 348 | const baseTileX = snapshot.chunkX * CHUNK_SIZE; |
| 349 | const baseTileY = snapshot.chunkY * CHUNK_SIZE; |
| 350 | const tileX = baseTileX + localX; |
| 351 | const tileY = baseTileY + localY; |
| 352 | let groundEntry = snapshot.grounds[index]; |
| 353 | if (preview && preview.tileX === tileX && preview.tileY === tileY && preview.ground !== undefined) |
| 354 | { groundEntry = preview.ground; } |
| 355 | return groundEntry; |
| 356 | } |
| 357 | |
| 358 | function tileBlockEntry (snapshot, preview, tileX, tileY, index) |
| 359 | { |
| 360 | let blockEntry = snapshot.blocks[index]; |
| 361 | if (preview && preview.tileX === tileX && preview.tileY === tileY && preview.block !== undefined) |
| 362 | { blockEntry = preview.block; } |
| 363 | return blockEntry; |
| 364 | } |
| 365 | |
| 366 | function makeCanvas (width, height) |
| 367 | { |
| 368 | const canvas = document.createElement("canvas"); |
| 369 | canvas.width = width; |
| 370 | canvas.height = height; |
| 371 | return canvas; |
| 372 | } |
| 373 | |
| 374 | function blit (ctx, sprite, dx, dy) |
| 375 | { |
| 376 | if (!sprite?.g_image) |
| 377 | { return; } |
| 378 | const sub = sprite.getSubImage(0); |
| 379 | ctx.drawImage( |
| 380 | sprite.g_image, |
| 381 | sub.sx, |
| 382 | sub.sy, |
| 383 | sub.sw, |
| 384 | sub.sh, |
| 385 | dx, |
| 386 | dy, |
| 387 | SPRITE_SIZE_PX, |
| 388 | SPRITE_SIZE_PX |
| 389 | ); |
| 390 | } |
| 391 | |
| 392 | function isGroundBlendable (ground) |
| 393 | { |
| 394 | const entry = normalizeGround(ground); |
| 395 | return !!GROUND_DEFINITIONS[entry.typeId]?.blendable; |
| 396 | } |
| 397 | |
| 398 | function isWetShoreGround (ground) |
| 399 | { |
| 400 | const entry = normalizeGround(ground); |
| 401 | return !!GROUND_DEFINITIONS[entry.typeId]?.wetShore; |
| 402 | } |
| 403 | |
| 404 | function wetShoreSpriteName (catalog, ground) |
| 405 | { |
| 406 | const entry = normalizeGround(ground); |
| 407 | const def = GROUND_DEFINITIONS[entry.typeId]; |
| 408 | if (!def?.wetShore) |
| 409 | { return null; } |
| 410 | const wetState = `${def.key}-wet`; |
| 411 | const states = catalog.grounds?.[def.key]?.states ?? []; |
| 412 | if (!states.includes(wetState)) |
| 413 | { return null; } |
| 414 | return spriteImageName("ground", def.key, wetState); |
| 415 | } |
| 416 | |
| 417 | function groundVisualKey (catalog, ground, season) |
| 418 | { |
| 419 | const entry = normalizeGround(ground); |
| 420 | const def = GROUND_DEFINITIONS[entry.typeId] ?? GROUND_DEFINITIONS[GroundType.grass]; |
| 421 | const states = catalog.grounds?.[def.key]?.states ?? []; |
| 422 | const state = resolveSeasonalVisualState(states, entry.state, season); |
| 423 | return `${entry.typeId}:${state ?? ""}`; |
| 424 | } |
| 425 | |
| 426 | function blitBlendedGround ( |
| 427 | ctx, |
| 428 | images, |
| 429 | catalog, |
| 430 | groundEntry, |
| 431 | groundName, |
| 432 | dx, |
| 433 | dy, |
| 434 | tileX, |
| 435 | tileY, |
| 436 | season, |
| 437 | resolveGround, |
| 438 | pixelCache, |
| 439 | { softTerrain = false, wetShore = false } = {} |
| 440 | ) |
| 441 | { |
| 442 | const dryPixels = spritePixels(images[groundName], pixelCache); |
| 443 | if (!dryPixels) |
| 444 | { |
| 445 | blit(ctx, images[groundName], dx, dy); |
| 446 | return; |
| 447 | } |
| 448 | |
| 449 | const waterDirs = []; |
| 450 | if (wetShore) |
| 451 | { |
| 452 | for (const { ndx, ndy } of WET_SAND_NEIGHBOR_DIRS) |
| 453 | { |
| 454 | const neighborGround = resolveNeighborGround( |
| 455 | tileX + ndx, |
| 456 | tileY + ndy, |
| 457 | resolveGround |
| 458 | ); |
| 459 | if (neighborGround == null) |
| 460 | { continue; } |
| 461 | const typeId = normalizeGround(neighborGround).typeId; |
| 462 | if (!isLiquidWaterGround(typeId, season)) |
| 463 | { continue; } |
| 464 | waterDirs.push({ ndx, ndy }); |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | const wetName = wetShore && waterDirs.length ? wetShoreSpriteName(catalog, groundEntry) : null; |
| 469 | const wetPixels = wetName ? spritePixels(images[wetName], pixelCache) : null; |
| 470 | |
| 471 | const selfKey = groundVisualKey(catalog, groundEntry, season); |
| 472 | const neighborSprites = []; |
| 473 | if (softTerrain) |
| 474 | { |
| 475 | for (const { ndx, ndy } of SOFT_TERRAIN_NEIGHBOR_DIRS) |
| 476 | { |
| 477 | const neighborGround = resolveNeighborGround( |
| 478 | tileX + ndx, |
| 479 | tileY + ndy, |
| 480 | resolveGround |
| 481 | ); |
| 482 | if (neighborGround == null) |
| 483 | { continue; } |
| 484 | if (groundVisualKey(catalog, neighborGround, season) === selfKey) |
| 485 | { continue; } |
| 486 | const neighborName = groundSpriteName(catalog, neighborGround, season); |
| 487 | const pixels = spritePixels(images[neighborName], pixelCache); |
| 488 | if (!pixels) |
| 489 | { continue; } |
| 490 | neighborSprites.push({ |
| 491 | ndx, |
| 492 | ndy, |
| 493 | pixels, |
| 494 | neighborBlendable: isGroundBlendable(neighborGround) |
| 495 | }); |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | if (!waterDirs.length && !neighborSprites.length) |
| 500 | { |
| 501 | blit(ctx, images[groundName], dx, dy); |
| 502 | return; |
| 503 | } |
| 504 | |
| 505 | const out = ctx.createImageData(SPRITE_SIZE_PX, SPRITE_SIZE_PX); |
| 506 | const dst = out.data; |
| 507 | for (let py = 0; py < SPRITE_SIZE_PX; py++) |
| 508 | { |
| 509 | for (let px = 0; px < SPRITE_SIZE_PX; px++) |
| 510 | { |
| 511 | let self = readPixel(dryPixels, px, py); |
| 512 | if (wetPixels && waterDirs.length) |
| 513 | { |
| 514 | const wetness = wetSandWetness(px, py, waterDirs, SPRITE_SIZE_PX); |
| 515 | if (wetness > 0) |
| 516 | { self = mixWetSandPixel(self, readPixel(wetPixels, px, py), wetness); } |
| 517 | } |
| 518 | const contribs = []; |
| 519 | for (const neighbor of neighborSprites) |
| 520 | { |
| 521 | const weight = softTerrainBlendWeight(px, py, neighbor.ndx, neighbor.ndy, SPRITE_SIZE_PX, { |
| 522 | neighborBlendable: neighbor.neighborBlendable |
| 523 | }); |
| 524 | if (weight <= 0) |
| 525 | { continue; } |
| 526 | const sample = softTerrainNeighborSample(px, py, neighbor.ndx, neighbor.ndy, SPRITE_SIZE_PX); |
| 527 | contribs.push({ |
| 528 | weight, |
| 529 | color: readPixel(neighbor.pixels, sample.sx, sample.sy) |
| 530 | }); |
| 531 | } |
| 532 | const mixed = mixSoftTerrainPixel(self, contribs); |
| 533 | const i = (py * SPRITE_SIZE_PX + px) * 4; |
| 534 | dst[i] = mixed.r; |
| 535 | dst[i + 1] = mixed.g; |
| 536 | dst[i + 2] = mixed.b; |
| 537 | dst[i + 3] = mixed.a; |
| 538 | } |
| 539 | } |
| 540 | ctx.putImageData(out, dx, dy); |
| 541 | } |
| 542 | |
| 543 | function resolveNeighborGround (tileX, tileY, resolveGround) |
| 544 | { |
| 545 | if (typeof resolveGround === "function") |
| 546 | { return resolveGround(tileX, tileY); } |
| 547 | return null; |
| 548 | } |
| 549 | |
| 550 | function spritePixels (sprite, cache) |
| 551 | { |
| 552 | if (!sprite?.g_image) |
| 553 | { return null; } |
| 554 | const key = sprite.g_image?.src ?? sprite; |
| 555 | if (cache?.has(key)) |
| 556 | { return cache.get(key); } |
| 557 | const sub = sprite.getSubImage(0); |
| 558 | const canvas = makeCanvas(SPRITE_SIZE_PX, SPRITE_SIZE_PX); |
| 559 | const ctx = canvas.getContext("2d", { willReadFrequently: true }); |
| 560 | ctx.imageSmoothingEnabled = false; |
| 561 | ctx.drawImage( |
| 562 | sprite.g_image, |
| 563 | sub.sx, |
| 564 | sub.sy, |
| 565 | sub.sw, |
| 566 | sub.sh, |
| 567 | 0, |
| 568 | 0, |
| 569 | SPRITE_SIZE_PX, |
| 570 | SPRITE_SIZE_PX |
| 571 | ); |
| 572 | const data = ctx.getImageData(0, 0, SPRITE_SIZE_PX, SPRITE_SIZE_PX); |
| 573 | cache?.set(key, data); |
| 574 | return data; |
| 575 | } |
| 576 | |
| 577 | function readPixel (imageData, x, y) |
| 578 | { |
| 579 | const i = (y * imageData.width + x) * 4; |
| 580 | const d = imageData.data; |
| 581 | return { r: d[i], g: d[i + 1], b: d[i + 2], a: d[i + 3] }; |
| 582 | } |
| 583 | |
| 584 | function groundSpriteName (catalog, ground, season = SeasonId.summer) |
| 585 | { |
| 586 | const entry = normalizeGround(ground); |
| 587 | const def = GROUND_DEFINITIONS[entry.typeId] ?? GROUND_DEFINITIONS[GroundType.grass]; |
| 588 | const states = catalog.grounds?.[def.key]?.states ?? []; |
| 589 | const state = resolveSeasonalVisualState(states, entry.state, season); |
| 590 | return spriteImageName("ground", def.key, state); |
| 591 | } |
| 592 | |
| 593 | function blockSpriteName (catalog, block, season = SeasonId.summer) |
| 594 | { |
| 595 | const entry = normalizeBlock(block); |
| 596 | if (!entry) |
| 597 | { return null; } |
| 598 | const def = BLOCK_DEFINITIONS[entry.typeId]; |
| 599 | if (!def || def.editorHidden) |
| 600 | { return null; } |
| 601 | const states = catalog.blocks?.[def.key]?.states ?? []; |
| 602 | const state = resolveSeasonalVisualState(states, entry.state, season); |
| 603 | return spriteImageName("block", def.key, state); |
| 604 | } |
| 605 | |
| 606 | function groundBloomName (catalog, ground, season = SeasonId.summer) |
| 607 | { |
| 608 | const entry = normalizeGround(ground); |
| 609 | const def = GROUND_DEFINITIONS[entry.typeId] ?? GROUND_DEFINITIONS[GroundType.grass]; |
| 610 | const type = catalog.grounds?.[def.key]; |
| 611 | const state = resolveSeasonalVisualState(type?.states ?? [], entry.state, season); |
| 612 | if (!state || !type?.blooms?.[state]) |
| 613 | { return null; } |
| 614 | return bloomImageName("ground", def.key, state); |
| 615 | } |
| 616 | |
| 617 | function blockBloomName (catalog, block, season = SeasonId.summer) |
| 618 | { |
| 619 | const entry = normalizeBlock(block); |
| 620 | if (!entry) |
| 621 | { return null; } |
| 622 | const def = BLOCK_DEFINITIONS[entry.typeId]; |
| 623 | if (!def) |
| 624 | { return null; } |
| 625 | const type = catalog.blocks?.[def.key]; |
| 626 | const state = resolveSeasonalVisualState(type?.states ?? [], entry.state, season); |
| 627 | if (!state || !type?.blooms?.[state]) |
| 628 | { return null; } |
| 629 | return bloomImageName("block", def.key, state); |
| 630 | } |
| 631 | |
| 632 | function blockCatalogMeta (catalog, block) |
| 633 | { |
| 634 | const entry = normalizeBlock(block); |
| 635 | if (!entry) |
| 636 | { return null; } |
| 637 | const def = BLOCK_DEFINITIONS[entry.typeId]; |
| 638 | if (!def) |
| 639 | { return null; } |
| 640 | return catalog.blocks?.[def.key] ?? null; |
| 641 | } |
| 642 | |
| 643 | function groundCatalogMeta (catalog, ground) |
| 644 | { |
| 645 | const entry = normalizeGround(ground); |
| 646 | const def = GROUND_DEFINITIONS[entry.typeId] ?? GROUND_DEFINITIONS[GroundType.grass]; |
| 647 | return catalog.grounds?.[def.key] ?? { |
| 648 | frames: def.frames, |
| 649 | animationSpeed: def.animationSpeed |
| 650 | }; |
| 651 | } |