| 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 | }); |
| 71 | |
| 72 | test("wet sand border fingerprint differs for liquid water neighbors", () => |
| 73 | { |
| 74 | const catalogSand = { |
| 75 | grounds: { |
| 76 | sand: { states: ["sand", "sand-wet"] }, |
| 77 | water: { states: ["summer"] }, |
| 78 | "deep-water": { states: ["summer"] } |
| 79 | }, |
| 80 | blocks: {} |
| 81 | }; |
| 82 | const snapshot = { |
| 83 | chunkX: 1, |
| 84 | chunkY: 1, |
| 85 | grounds: Array.from({ length: CHUNK_SIZE * CHUNK_SIZE }, () => ({ typeId: GroundType.sand })), |
| 86 | blocks: Array.from({ length: CHUNK_SIZE * CHUNK_SIZE }, () => null) |
| 87 | }; |
| 88 | const dry = computeChunkAtlasKeys({ |
| 89 | snapshot, |
| 90 | catalog: catalogSand, |
| 91 | season: "summer", |
| 92 | softTerrain: false, |
| 93 | resolveGround: () => ({ typeId: GroundType.sand }) |
| 94 | }); |
| 95 | const besideWater = computeChunkAtlasKeys({ |
| 96 | snapshot, |
| 97 | catalog: catalogSand, |
| 98 | season: "summer", |
| 99 | softTerrain: false, |
| 100 | resolveGround: (tileX, tileY) => |
| 101 | { |
| 102 | if (tileX < snapshot.chunkX * CHUNK_SIZE) |
| 103 | { return { typeId: GroundType.water }; } |
| 104 | return { typeId: GroundType.sand }; |
| 105 | } |
| 106 | }); |
| 107 | assert.notEqual(dry.groundKey, besideWater.groundKey); |
| 108 | |
| 109 | const winterShallow = computeChunkAtlasKeys({ |
| 110 | snapshot, |
| 111 | catalog: catalogSand, |
| 112 | season: "winter", |
| 113 | softTerrain: false, |
| 114 | resolveGround: (tileX, tileY) => |
| 115 | { |
| 116 | if (tileX < snapshot.chunkX * CHUNK_SIZE) |
| 117 | { return { typeId: GroundType.water }; } |
| 118 | return { typeId: GroundType.sand }; |
| 119 | } |
| 120 | }); |
| 121 | const winterDeep = computeChunkAtlasKeys({ |
| 122 | snapshot, |
| 123 | catalog: catalogSand, |
| 124 | season: "winter", |
| 125 | softTerrain: false, |
| 126 | resolveGround: (tileX, tileY) => |
| 127 | { |
| 128 | if (tileX < snapshot.chunkX * CHUNK_SIZE) |
| 129 | { return { typeId: GroundType.deepWater }; } |
| 130 | return { typeId: GroundType.sand }; |
| 131 | } |
| 132 | }); |
| 133 | // Frozen shallow water should not wet the shoreline; deep still does. |
| 134 | assert.equal(winterShallow.groundKey.includes("wet:flat") || winterShallow.groundKey.includes("wetBand:0.6"), true); |
| 135 | assert.ok(winterShallow.groundKey.includes("wet:flat")); |
| 136 | assert.notEqual(winterShallow.groundKey, winterDeep.groundKey); |
| 137 | }); |
| 138 | |
| 139 | test("wet sand band fraction is part of the ground atlas key", () => |
| 140 | { |
| 141 | const catalogSand = { |
| 142 | grounds: { sand: { states: ["sand", "sand-wet"] } }, |
| 143 | blocks: {} |
| 144 | }; |
| 145 | const snapshot = { |
| 146 | chunkX: 0, |
| 147 | chunkY: 0, |
| 148 | grounds: Array.from({ length: CHUNK_SIZE * CHUNK_SIZE }, () => ({ typeId: GroundType.sand })), |
| 149 | blocks: Array.from({ length: CHUNK_SIZE * CHUNK_SIZE }, () => null) |
| 150 | }; |
| 151 | const medium = computeChunkAtlasKeys({ |
| 152 | snapshot, |
| 153 | catalog: catalogSand, |
| 154 | season: "summer", |
| 155 | wetSandBandFraction: 0.6, |
| 156 | resolveGround: () => ({ typeId: GroundType.sand }) |
| 157 | }); |
| 158 | const off = computeChunkAtlasKeys({ |
| 159 | snapshot, |
| 160 | catalog: catalogSand, |
| 161 | season: "summer", |
| 162 | wetSandBandFraction: 0, |
| 163 | resolveGround: () => ({ typeId: GroundType.sand }) |
| 164 | }); |
| 165 | assert.notEqual(medium.groundKey, off.groundKey); |
| 166 | assert.ok(medium.groundKey.includes("wetBand:0.6")); |
| 167 | assert.ok(off.groundKey.includes("wetBand:0")); |
| 168 | }); |