| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { CHUNK_SIZE } from "../../shared/protocol/constants.js"; |
| 4 | import { GroundType } from "../../shared/definitions/tiles.js"; |
| 5 | import { |
| 6 | computeChunkAtlasKeys, |
| 7 | EMPTY_BLOCK_ROW_KEY, |
| 8 | EMPTY_BLOOM_KEY |
| 9 | } from "../../shared/render/chunkAtlasKeys.js"; |
| 10 | |
| 11 | const catalog = { |
| 12 | grounds: { grass: { states: ["summer"] } }, |
| 13 | blocks: {} |
| 14 | }; |
| 15 | |
| 16 | function grassSnapshot (chunkX = 0, chunkY = 0) |
| 17 | { |
| 18 | return { |
| 19 | chunkX, |
| 20 | chunkY, |
| 21 | grounds: Array.from({ length: CHUNK_SIZE * CHUNK_SIZE }, () => ({ typeId: GroundType.grass })), |
| 22 | blocks: Array.from({ length: CHUNK_SIZE * CHUNK_SIZE }, () => null) |
| 23 | }; |
| 24 | } |
| 25 | |
| 26 | test("uniform grass chunks share atlas keys regardless of chunk coords", () => |
| 27 | { |
| 28 | const a = computeChunkAtlasKeys({ |
| 29 | snapshot: grassSnapshot(0, 0), |
| 30 | catalog, |
| 31 | season: "summer", |
| 32 | softTerrain: false |
| 33 | }); |
| 34 | const b = computeChunkAtlasKeys({ |
| 35 | snapshot: grassSnapshot(3, 7), |
| 36 | catalog, |
| 37 | season: "summer", |
| 38 | softTerrain: false |
| 39 | }); |
| 40 | assert.equal(a.groundKey, b.groundKey); |
| 41 | assert.equal(a.bloomKey, b.bloomKey); |
| 42 | assert.equal(a.blockRowKeys.length, CHUNK_SIZE); |
| 43 | assert.ok(a.blockRowKeys.every((key) => key === EMPTY_BLOCK_ROW_KEY)); |
| 44 | assert.equal(a.bloomKey, EMPTY_BLOOM_KEY); |
| 45 | }); |
| 46 | |
| 47 | test("soft border fingerprint differs when neighbor ground changes", () => |
| 48 | { |
| 49 | const snapshot = grassSnapshot(1, 1); |
| 50 | const flat = computeChunkAtlasKeys({ |
| 51 | snapshot, |
| 52 | catalog, |
| 53 | season: "summer", |
| 54 | softTerrain: true, |
| 55 | resolveGround: () => ({ typeId: GroundType.grass }) |
| 56 | }); |
| 57 | const edgeDirt = computeChunkAtlasKeys({ |
| 58 | snapshot, |
| 59 | catalog, |
| 60 | season: "summer", |
| 61 | softTerrain: true, |
| 62 | resolveGround: (tileX, tileY) => |
| 63 | { |
| 64 | if (tileX < snapshot.chunkX * CHUNK_SIZE) |
| 65 | { return { typeId: GroundType.dirt }; } |
| 66 | return { typeId: GroundType.grass }; |
| 67 | } |
| 68 | }); |
| 69 | assert.notEqual(flat.groundKey, edgeDirt.groundKey); |
| 70 | }); |