BlueGit

timfa questward

Files

questward / shared / render / chunkAtlasKeys.js

chunkAtlasKeys.js

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