BlueGit

timfa questward

Files

questward / shared / render / chunkAtlasKeys.js

chunkAtlasKeys.js

chunkAtlasKeys.js 9.4 KiB Download Raw
1import { CHUNK_SIZE } from "../protocol/constants.js";
2import {
3 GROUND_DEFINITIONS,
4 BLOCK_DEFINITIONS,
5 GroundType,
6 normalizeGround,
7 normalizeBlock,
8 isChunkAtlasBakedBlock,
9 isLiquidWaterGround
10} from "../definitions/tiles.js";
11import { bloomImageName } from "../definitions/tileSprites.js";
12import { resolveSeasonalVisualState } from "../world/season.js";
13import { SOFT_TERRAIN_NEIGHBOR_DIRS } from "./softTerrain.js";
14import { WET_SAND_BAND_FRACTION, WET_SAND_NEIGHBOR_DIRS } from "./wetSand.js";
15
16export const EMPTY_BLOCK_ROW_KEY = "blockRow|empty";
17export const EMPTY_BLOOM_KEY = "bloom|empty";
18
19/**
20 * Fingerprints for chunk ground / block-row / bloom atlases.
21 * Identical keys mean identical baked pixels (ignoring chunk coordinates).
22 */
23export function computeChunkAtlasKeys ({
24 snapshot,
25 catalog,
26 preview = null,
27 season,
28 softTerrain = false,
29 wetSandBandFraction = WET_SAND_BAND_FRACTION,
30 resolveGround = null
31})
32{
33 const baseTileX = snapshot.chunkX * CHUNK_SIZE;
34 const baseTileY = snapshot.chunkY * CHUNK_SIZE;
35 const groundFps = [];
36 for (let localY = 0; localY < CHUNK_SIZE; localY++)
37 {
38 for (let localX = 0; localX < CHUNK_SIZE; localX++)
39 {
40 groundFps.push(groundFingerprint(
41 catalog,
42 tileGroundEntry(snapshot, preview, localX, localY),
43 season
44 ));
45 }
46 }
47 let groundBody = compactFingerprint("ground", groundFps);
48 if (softTerrain)
49 {
50 groundBody += `|soft:${softBorderGroundFingerprint({
51 snapshot,
52 catalog,
53 season,
54 preview,
55 resolveGround
56 })}`;
57 }
58 const wetBand = Math.max(0, Number(wetSandBandFraction) || 0);
59 if (wetBand > 0)
60 {
61 groundBody += `|wetBand:${wetBand}|wet:${wetSandBorderFingerprint({
62 snapshot,
63 season,
64 preview,
65 resolveGround
66 })}`;
67 }
68 else
69 { groundBody += "|wetBand:0"; }
70 const groundKey = `ground|${season}|${groundBody}`;
71
72 const blockRowKeys = [];
73 for (let row = 0; row < CHUNK_SIZE; row++)
74 {
75 const rowFps = [];
76 for (let localX = 0; localX < CHUNK_SIZE; localX++)
77 {
78 const index = row * CHUNK_SIZE + localX;
79 const tileX = baseTileX + localX;
80 const tileY = baseTileY + row;
81 rowFps.push(blockFingerprint(
82 catalog,
83 tileBlockEntry(snapshot, preview, tileX, tileY, index),
84 season
85 ));
86 }
87 blockRowKeys.push(compactRowFingerprint("blockRow", rowFps));
88 }
89
90 const bloomFps = [];
91 for (let localY = 0; localY < CHUNK_SIZE; localY++)
92 {
93 for (let localX = 0; localX < CHUNK_SIZE; localX++)
94 {
95 const index = localY * CHUNK_SIZE + localX;
96 const tileX = baseTileX + localX;
97 const tileY = baseTileY + localY;
98 bloomFps.push(bloomFingerprint(
99 catalog,
100 tileGroundEntry(snapshot, preview, localX, localY),
101 tileBlockEntry(snapshot, preview, tileX, tileY, index),
102 season
103 ));
104 }
105 }
106 const bloomKey = compactRowFingerprint("bloom", bloomFps);
107
108 return { groundKey, blockRowKeys, bloomKey };
109}
110
111function tileGroundEntry (snapshot, preview, localX, localY)
112{
113 const index = localY * CHUNK_SIZE + localX;
114 const baseTileX = snapshot.chunkX * CHUNK_SIZE;
115 const baseTileY = snapshot.chunkY * CHUNK_SIZE;
116 const tileX = baseTileX + localX;
117 const tileY = baseTileY + localY;
118 let groundEntry = snapshot.grounds[index];
119 if (preview && preview.tileX === tileX && preview.tileY === tileY && preview.ground !== undefined)
120 { groundEntry = preview.ground; }
121 return groundEntry;
122}
123
124function tileBlockEntry (snapshot, preview, tileX, tileY, index)
125{
126 let blockEntry = snapshot.blocks[index];
127 if (preview && preview.tileX === tileX && preview.tileY === tileY && preview.block !== undefined)
128 { blockEntry = preview.block; }
129 return blockEntry;
130}
131
132function compactFingerprint (prefix, fps)
133{
134 if (!fps.length)
135 { return `${prefix}|empty`; }
136 const first = fps[0];
137 if (fps.every((fp) => fp === first))
138 { return `${prefix}|u:${first}`; }
139 return `${prefix}|m:${fps.join(",")}`;
140}
141
142function compactRowFingerprint (prefix, fps)
143{
144 if (!fps.length || fps.every((fp) => fp === "_"))
145 { return prefix === "blockRow" ? EMPTY_BLOCK_ROW_KEY : EMPTY_BLOOM_KEY; }
146 const first = fps[0];
147 if (fps.every((fp) => fp === first))
148 { return `${prefix}|u:${first}`; }
149 return `${prefix}|m:${fps.join(",")}`;
150}
151
152function groundFingerprint (catalog, ground, season)
153{
154 return groundVisualKey(catalog, ground, season);
155}
156
157function blockFingerprint (catalog, block, season)
158{
159 const entry = normalizeBlock(block);
160 if (!entry)
161 { return "_"; }
162 if (!isChunkAtlasBakedBlock(entry))
163 { return "_"; }
164 const def = BLOCK_DEFINITIONS[entry.typeId];
165 if (!def || def.editorHidden)
166 { return "_"; }
167 const states = catalog.blocks?.[def.key]?.states ?? [];
168 const state = resolveSeasonalVisualState(states, entry.state, season);
169 return `${entry.typeId}:${state ?? ""}`;
170}
171
172function bloomFingerprint (catalog, groundEntry, blockEntry, season)
173{
174 const parts = [];
175 const groundBloom = groundBloomName(catalog, groundEntry, season);
176 if (groundBloom)
177 { parts.push(`g:${groundBloom}`); }
178 if (isChunkAtlasBakedBlock(normalizeBlock(blockEntry)))
179 {
180 const blockBloom = blockBloomName(catalog, blockEntry, season);
181 if (blockBloom)
182 { parts.push(`b:${blockBloom}`); }
183 }
184 return parts.length ? parts.join("+") : "_";
185}
186
187function softBorderGroundFingerprint ({
188 snapshot,
189 catalog,
190 season,
191 preview,
192 resolveGround
193})
194{
195 if (typeof resolveGround !== "function")
196 { return "none"; }
197 const baseTileX = snapshot.chunkX * CHUNK_SIZE;
198 const baseTileY = snapshot.chunkY * CHUNK_SIZE;
199 const parts = [];
200 for (let localY = 0; localY < CHUNK_SIZE; localY++)
201 {
202 for (let localX = 0; localX < CHUNK_SIZE; localX++)
203 {
204 const onBorder = localX === 0 || localY === 0 || localX === CHUNK_SIZE - 1 || localY === CHUNK_SIZE - 1;
205 if (!onBorder)
206 { continue; }
207 const tileX = baseTileX + localX;
208 const tileY = baseTileY + localY;
209 const groundEntry = tileGroundEntry(snapshot, preview, localX, localY);
210 if (!isGroundBlendable(groundEntry))
211 { continue; }
212 const selfKey = groundVisualKey(catalog, groundEntry, season);
213 for (const { ndx, ndy } of SOFT_TERRAIN_NEIGHBOR_DIRS)
214 {
215 const nx = tileX + ndx;
216 const ny = tileY + ndy;
217 if (nx >= baseTileX && nx < baseTileX + CHUNK_SIZE
218 && ny >= baseTileY && ny < baseTileY + CHUNK_SIZE)
219 { continue; }
220 const neighbor = resolveGround(nx, ny);
221 if (neighbor == null)
222 { continue; }
223 const neighborKey = groundVisualKey(catalog, neighbor, season);
224 if (neighborKey === selfKey)
225 { continue; }
226 const nbrTypeId = normalizeGround(neighbor).typeId;
227 const nbrDef = GROUND_DEFINITIONS[nbrTypeId];
228 if (nbrDef?.proceduralWaves || isLiquidWaterGround(nbrTypeId, season))
229 { continue; }
230 parts.push(`${tileX},${tileY}:${ndx},${ndy}:${neighborKey}`);
231 }
232 }
233 }
234 return parts.length ? parts.join(";") : "flat";
235}
236
237/**
238 * Cross-chunk liquid-water adjacency for wet-shore sand (8 dirs).
239 * Winter ice (shallow water) is excluded via isLiquidWaterGround.
240 */
241function wetSandBorderFingerprint ({
242 snapshot,
243 season,
244 preview,
245 resolveGround
246})
247{
248 if (typeof resolveGround !== "function")
249 { return "none"; }
250 const baseTileX = snapshot.chunkX * CHUNK_SIZE;
251 const baseTileY = snapshot.chunkY * CHUNK_SIZE;
252 const parts = [];
253 for (let localY = 0; localY < CHUNK_SIZE; localY++)
254 {
255 for (let localX = 0; localX < CHUNK_SIZE; localX++)
256 {
257 const onBorder = localX === 0 || localY === 0 || localX === CHUNK_SIZE - 1 || localY === CHUNK_SIZE - 1;
258 if (!onBorder)
259 { continue; }
260 const tileX = baseTileX + localX;
261 const tileY = baseTileY + localY;
262 const groundEntry = tileGroundEntry(snapshot, preview, localX, localY);
263 if (!isWetShoreGround(groundEntry))
264 { continue; }
265 for (const { ndx, ndy } of WET_SAND_NEIGHBOR_DIRS)
266 {
267 const nx = tileX + ndx;
268 const ny = tileY + ndy;
269 if (nx >= baseTileX && nx < baseTileX + CHUNK_SIZE
270 && ny >= baseTileY && ny < baseTileY + CHUNK_SIZE)
271 { continue; }
272 const neighbor = resolveGround(nx, ny);
273 if (neighbor == null)
274 { continue; }
275 const typeId = normalizeGround(neighbor).typeId;
276 if (!isLiquidWaterGround(typeId, season))
277 { continue; }
278 parts.push(`${tileX},${tileY}:${ndx},${ndy}:${typeId}`);
279 }
280 }
281 }
282 return parts.length ? parts.join(";") : "flat";
283}
284
285function isGroundBlendable (ground)
286{
287 const entry = normalizeGround(ground);
288 return !!GROUND_DEFINITIONS[entry.typeId]?.blendable;
289}
290
291function isWetShoreGround (ground)
292{
293 const entry = normalizeGround(ground);
294 return !!GROUND_DEFINITIONS[entry.typeId]?.wetShore;
295}
296
297function groundVisualKey (catalog, ground, season)
298{
299 const entry = normalizeGround(ground);
300 const def = GROUND_DEFINITIONS[entry.typeId] ?? GROUND_DEFINITIONS[GroundType.grass];
301 const states = catalog.grounds?.[def.key]?.states ?? [];
302 const state = resolveSeasonalVisualState(states, entry.state, season);
303 return `${entry.typeId}:${state ?? ""}`;
304}
305
306function groundBloomName (catalog, ground, season)
307{
308 const entry = normalizeGround(ground);
309 const def = GROUND_DEFINITIONS[entry.typeId] ?? GROUND_DEFINITIONS[GroundType.grass];
310 const type = catalog.grounds?.[def.key];
311 const state = resolveSeasonalVisualState(type?.states ?? [], entry.state, season);
312 if (!state || !type?.blooms?.[state])
313 { return null; }
314 return bloomImageName("ground", def.key, state);
315}
316
317function blockBloomName (catalog, block, season)
318{
319 const entry = normalizeBlock(block);
320 if (!entry)
321 { return null; }
322 const def = BLOCK_DEFINITIONS[entry.typeId];
323 if (!def)
324 { return null; }
325 const type = catalog.blocks?.[def.key];
326 const state = resolveSeasonalVisualState(type?.states ?? [], entry.state, season);
327 if (!state || !type?.blooms?.[state])
328 { return null; }
329 return bloomImageName("block", def.key, state);
330}