BlueGit

timfa questward

Files

questward / server / src / editor / service.js

service.js

service.js 20.9 KiB Download Raw
1import { groundDefinition, blockDefinition, normalizeGround, normalizeBlock } from "../../../shared/definitions/tiles.js";
2import { CHUNK_SIZE } from "../../../shared/protocol/constants.js";
3import { localToTile } from "../world/coordinates.js";
4import { cloneChunkData, createEmptyChunkData, decodeChunkBlob, encodeChunkBlob, normalizeSpawner } from "../world/chunkData.js";
5import { BLOCK_DEFINITIONS } from "../../../shared/definitions/tiles.js";
6import { bakeLightMaps, maxDefinitionLightRange } from "../lighting/baker.js";
7import { closedNeighborEdges } from "../world/chunkFlags.js";
8import { coerceSettingValue, diffSettings, diffItemSeedValues, allSettingFields, itemSeedMapsEqual } from "../../../shared/definitions/worldSettings.js";
9import { normalizeItemSeedValues } from "../../../shared/shop/pricing.js";
10import { itemDefinition } from "../../../shared/definitions/items.js";
11import { DEFAULT_WORLD_SETTINGS } from "../../../shared/protocol/constants.js";
12import {
13 normalizeStarterLoadout,
14 starterLoadoutsEqual,
15 diffStarterLoadout
16} from "../../../shared/items/starterLoadout.js";
17import { normalizeQuestDef, normalizeDialogueTree } from "../../../shared/quests/normalize.js";
18import {
19 OVERWORLD_ID,
20 normalizeDimensionId,
21 normalizeDimensionRecord,
22 defaultDimensionRecord,
23 isOverworld,
24 normalizeLightingMode,
25 normalizeSeasonMode
26} from "../../../shared/world/dimensions.js";
27
28export class EditorService
29{
30 constructor (worldDb)
31 {
32 this.worldDb = worldDb;
33 }
34
35 #defaultGroundForDimension (dimensionId, revisionId = null)
36 {
37 const rev = revisionId ?? this.worldDb.getEditorRevisionId();
38 const dimId = normalizeDimensionId(dimensionId);
39 const settings = this.worldDb.getLiveSettings();
40 const record = this.worldDb.getDimension(rev, dimId)
41 ?? defaultDimensionRecord(dimId, { defaultGroundTypeId: settings.defaultGroundTypeId });
42 return record.defaultGroundTypeId;
43 }
44
45 loadChunk (chunkX, chunkY, dimensionId = OVERWORLD_ID)
46 {
47 const dimId = normalizeDimensionId(dimensionId);
48 const revisionId = this.worldDb.getEditorRevisionId();
49 const settings = this.worldDb.getLiveSettings();
50 const defaultGroundTypeId = this.#defaultGroundForDimension(dimId, revisionId);
51 const row = this.worldDb.getChunkVersion(chunkX, chunkY, revisionId, dimId);
52 const data = row
53 ? decodeChunkBlob(row.data_blob, defaultGroundTypeId, chunkX, chunkY)
54 : createEmptyChunkData(defaultGroundTypeId);
55 const isOpen = (x, y) =>
56 {
57 const neighbor = this.worldDb.getChunkVersion(x, y, revisionId, dimId);
58 if (!neighbor)
59 { return false; }
60 return !!decodeChunkBlob(neighbor.data_blob, defaultGroundTypeId, x, y).open;
61 };
62 return {
63 chunkX,
64 chunkY,
65 dimensionId: dimId,
66 revisionId,
67 pendingRevisionId: this.worldDb.getPendingRevisionId(),
68 activeRevisionId: this.worldDb.getActiveRevisionId(),
69 authored: !!row,
70 settings,
71 data,
72 closedEdges: closedNeighborEdges(chunkX, chunkY, isOpen),
73 changelog: this.worldDb.listChangelog(chunkX, chunkY, dimId)
74 };
75 }
76
77 saveChunk (chunkX, chunkY, draft, editorAdminId, dimensionId = OVERWORLD_ID)
78 {
79 const dimId = normalizeDimensionId(dimensionId);
80 const previous = this.loadChunk(chunkX, chunkY, dimId);
81 const next = cloneChunkData(previous.data);
82 const defaultGroundTypeId = this.#defaultGroundForDimension(dimId);
83 next.open = !!draft.open;
84 if (Array.isArray(draft.grounds))
85 { next.grounds = draft.grounds.map((ground) => normalizeGround(ground, defaultGroundTypeId)); }
86 if (Array.isArray(draft.blocks))
87 { next.blocks = draft.blocks.map((block) => normalizeBlock(block)); }
88 if (Array.isArray(draft.spawners))
89 { next.spawners = draft.spawners.map((entry) => normalizeSpawner(entry)).filter(Boolean); }
90
91 this.validateSpawnerDialogues(chunkX, chunkY, next.spawners);
92
93 const diffEntries = diffChunks(chunkX, chunkY, previous.data, next, previous.authored);
94 const revisionId = this.worldDb.ensurePendingRevision(editorAdminId);
95 const baked = bakeLightMaps(
96 chunkX,
97 chunkY,
98 (x, y) => this.#loadForBake(x, y, revisionId, defaultGroundTypeId, chunkX, chunkY, next, dimId),
99 maxDefinitionLightRange(BLOCK_DEFINITIONS)
100 );
101
102 for (const result of baked)
103 {
104 const shouldStore = result.chunkX === chunkX && result.chunkY === chunkY
105 || this.worldDb.getChunkVersion(result.chunkX, result.chunkY, revisionId, dimId)
106 || result.data.light.some((value) => value > 0);
107
108 if (!shouldStore)
109 { continue; }
110
111 this.worldDb.saveChunkVersion(
112 result.chunkX,
113 result.chunkY,
114 revisionId,
115 result.data.open ? 1 : 0,
116 encodeChunkBlob(result.data),
117 dimId
118 );
119 }
120
121 if (diffEntries.length > 0)
122 { this.worldDb.appendChangelog(chunkX, chunkY, revisionId, editorAdminId, diffEntries, dimId); }
123
124 return this.loadChunk(chunkX, chunkY, dimId);
125 }
126
127 listDimensions ()
128 {
129 const revisionId = this.worldDb.getEditorRevisionId();
130 return {
131 revisionId,
132 pendingRevisionId: this.worldDb.getPendingRevisionId(),
133 activeRevisionId: this.worldDb.getActiveRevisionId(),
134 dimensions: this.worldDb.listDimensions(revisionId)
135 };
136 }
137
138 createDimension (rawId, displayName, editorAdminId)
139 {
140 const trimmed = String(rawId ?? "").trim();
141 if (!trimmed)
142 {
143 const err = new Error("Dimension id is required.");
144 err.userFacing = true;
145 throw err;
146 }
147 const dimensionId = normalizeDimensionId(trimmed);
148 const revisionId = this.worldDb.ensurePendingRevision(editorAdminId);
149 if (this.worldDb.getDimension(revisionId, dimensionId))
150 {
151 const err = new Error(`Dimension "${dimensionId}" already exists.`);
152 err.userFacing = true;
153 throw err;
154 }
155 const settings = this.worldDb.getLiveSettings();
156 const record = defaultDimensionRecord(dimensionId, {
157 displayName: displayName != null && String(displayName).trim()
158 ? String(displayName).trim()
159 : dimensionId,
160 defaultGroundTypeId: settings.defaultGroundTypeId
161 });
162 this.worldDb.saveDimension(revisionId, record);
163 return this.listDimensions();
164 }
165
166 saveDimension (raw, editorAdminId)
167 {
168 if (!raw || typeof raw !== "object")
169 {
170 const err = new Error("Dimension payload required.");
171 err.userFacing = true;
172 throw err;
173 }
174 const dimensionId = normalizeDimensionId(raw.dimensionId);
175 const revisionId = this.worldDb.ensurePendingRevision(editorAdminId);
176 const settings = this.worldDb.getLiveSettings();
177 const existing = this.worldDb.getDimension(revisionId, dimensionId)
178 ?? defaultDimensionRecord(dimensionId, { defaultGroundTypeId: settings.defaultGroundTypeId });
179 const next = normalizeDimensionRecord({
180 ...existing,
181 displayName: Object.prototype.hasOwnProperty.call(raw, "displayName")
182 ? raw.displayName
183 : existing.displayName,
184 lightingMode: Object.prototype.hasOwnProperty.call(raw, "lightingMode")
185 ? normalizeLightingMode(raw.lightingMode)
186 : existing.lightingMode,
187 seasonMode: Object.prototype.hasOwnProperty.call(raw, "seasonMode")
188 ? normalizeSeasonMode(raw.seasonMode)
189 : existing.seasonMode,
190 defaultGroundTypeId: Object.prototype.hasOwnProperty.call(raw, "defaultGroundTypeId")
191 ? raw.defaultGroundTypeId
192 : existing.defaultGroundTypeId,
193 deletedAt: existing.deletedAt,
194 purgeAfter: existing.purgeAfter
195 });
196 this.worldDb.saveDimension(revisionId, next);
197 return this.listDimensions();
198 }
199
200 softDeleteDimension (dimensionId, editorAdminId)
201 {
202 const id = normalizeDimensionId(dimensionId);
203 if (isOverworld(id))
204 {
205 const err = new Error("Cannot delete overworld.");
206 err.userFacing = true;
207 throw err;
208 }
209 const revisionId = this.worldDb.ensurePendingRevision(editorAdminId);
210 const result = this.worldDb.softDeleteDimension(revisionId, id);
211 if (!result.ok)
212 {
213 const err = new Error(result.error ?? "Delete failed.");
214 err.userFacing = true;
215 throw err;
216 }
217 return this.listDimensions();
218 }
219
220 cancelDimensionDeletion (dimensionId, editorAdminId)
221 {
222 const id = normalizeDimensionId(dimensionId);
223 const revisionId = this.worldDb.ensurePendingRevision(editorAdminId);
224 const result = this.worldDb.cancelDimensionDeletion(revisionId, id);
225 if (!result.ok)
226 {
227 const err = new Error(result.error ?? "Cancel failed.");
228 err.userFacing = true;
229 throw err;
230 }
231 return this.listDimensions();
232 }
233
234 loadSettings ()
235 {
236 return {
237 settings: this.worldDb.getLiveSettings(),
238 changelog: this.worldDb.listSettingsChangelog()
239 };
240 }
241
242 saveSettings (partial, editorAdminId)
243 {
244 const current = this.worldDb.getLiveSettings();
245 const next = {
246 ...current,
247 itemSeedValues: {
248 ...(current.itemSeedValues && typeof current.itemSeedValues === "object"
249 ? current.itemSeedValues
250 : {})
251 }
252 };
253 for (const field of allSettingFields())
254 {
255 if (!Object.prototype.hasOwnProperty.call(partial, field.key))
256 { continue; }
257 const value = coerceSettingValue(field, partial[field.key]);
258 if (value == null)
259 { continue; }
260 next[field.key] = value;
261 }
262 if (Object.prototype.hasOwnProperty.call(partial, "itemSeedValues"))
263 {
264 next.itemSeedValues = normalizeItemSeedValues(partial.itemSeedValues);
265 }
266 if (Object.prototype.hasOwnProperty.call(partial, "starterEquipment")
267 || Object.prototype.hasOwnProperty.call(partial, "starterInventory"))
268 {
269 const loadout = normalizeStarterLoadout({
270 starterEquipment: Object.prototype.hasOwnProperty.call(partial, "starterEquipment")
271 ? partial.starterEquipment
272 : current.starterEquipment,
273 starterInventory: Object.prototype.hasOwnProperty.call(partial, "starterInventory")
274 ? partial.starterInventory
275 : current.starterInventory
276 });
277 next.starterEquipment = loadout.equipment;
278 next.starterInventory = loadout.inventory;
279 }
280 const { bullets: scalarBullets, changed } = diffSettings(current, next);
281 const seedBullets = Object.prototype.hasOwnProperty.call(partial, "itemSeedValues")
282 ? diffItemSeedValues(
283 normalizeItemSeedValues(current.itemSeedValues),
284 next.itemSeedValues,
285 (itemId) => itemDefinition(itemId)?.displayName ?? null
286 )
287 : [];
288 if (!itemSeedMapsEqual(current.itemSeedValues, next.itemSeedValues)
289 && Object.prototype.hasOwnProperty.call(partial, "itemSeedValues"))
290 {
291 changed.itemSeedValues = next.itemSeedValues;
292 }
293 const starterChanged = !starterLoadoutsEqual(current, next)
294 && (Object.prototype.hasOwnProperty.call(partial, "starterEquipment")
295 || Object.prototype.hasOwnProperty.call(partial, "starterInventory"));
296 if (starterChanged)
297 {
298 changed.starterEquipment = next.starterEquipment;
299 changed.starterInventory = next.starterInventory;
300 }
301 const starterBullets = starterChanged
302 ? diffStarterLoadout(current, next)
303 : [];
304 const bullets = [...scalarBullets, ...seedBullets, ...starterBullets];
305 if (Object.keys(changed).length === 0)
306 {
307 return {
308 settings: current,
309 changelog: this.worldDb.listSettingsChangelog(),
310 changed: {}
311 };
312 }
313 const settings = {
314 ...current,
315 ...changed,
316 itemSeedValues: changed.itemSeedValues
317 ?? normalizeItemSeedValues(current.itemSeedValues)
318 ?? { ...(DEFAULT_WORLD_SETTINGS.itemSeedValues ?? {}) },
319 starterEquipment: changed.starterEquipment ?? current.starterEquipment
320 ?? DEFAULT_WORLD_SETTINGS.starterEquipment,
321 starterInventory: changed.starterInventory ?? current.starterInventory
322 ?? DEFAULT_WORLD_SETTINGS.starterInventory
323 };
324 const normalized = normalizeStarterLoadout(settings);
325 settings.starterEquipment = normalized.equipment;
326 settings.starterInventory = normalized.inventory;
327 this.worldDb.saveLiveSettings(settings, editorAdminId);
328 this.worldDb.appendSettingsChangelog(editorAdminId, bullets);
329 return {
330 settings,
331 changelog: this.worldDb.listSettingsChangelog(),
332 changed,
333 diffEntries: bullets
334 };
335 }
336
337 applyPendingRevision ()
338 {
339 return this.worldDb.promotePendingRevision();
340 }
341
342 revisionStatus ()
343 {
344 return {
345 activeRevisionId: this.worldDb.getActiveRevisionId(),
346 pendingRevisionId: this.worldDb.getPendingRevisionId()
347 };
348 }
349
350 loadQuests ()
351 {
352 const revisionId = this.worldDb.getEditorRevisionId();
353 const quests = this.worldDb.listQuestDefs(revisionId).map((raw) => normalizeQuestDef(raw));
354 return {
355 revisionId,
356 pendingRevisionId: this.worldDb.getPendingRevisionId(),
357 activeRevisionId: this.worldDb.getActiveRevisionId(),
358 quests
359 };
360 }
361
362 saveQuest (raw, editorAdminId)
363 {
364 const def = normalizeQuestDef(raw);
365 const revisionId = this.worldDb.ensurePendingRevision(editorAdminId);
366 this.worldDb.saveQuestDef(revisionId, def);
367 return this.loadQuests();
368 }
369
370 deleteQuest (questId, editorAdminId)
371 {
372 const revisionId = this.worldDb.ensurePendingRevision(editorAdminId);
373 this.worldDb.deleteQuestDef(revisionId, String(questId));
374 return this.loadQuests();
375 }
376
377 loadDialogues ()
378 {
379 const revisionId = this.worldDb.getEditorRevisionId();
380 const trees = this.worldDb.listDialogueTrees(revisionId).map((raw) => normalizeDialogueTree(raw));
381 const assignmentCounts = this.#countDialogueAssignments(revisionId);
382 return {
383 revisionId,
384 pendingRevisionId: this.worldDb.getPendingRevisionId(),
385 activeRevisionId: this.worldDb.getActiveRevisionId(),
386 dialogues: trees.map((tree) => ({
387 ...tree,
388 assignedSpawnerCount: assignmentCounts.get(tree.dialogueId) ?? 0
389 }))
390 };
391 }
392
393 saveDialogue (raw, editorAdminId)
394 {
395 const tree = normalizeDialogueTree(raw);
396 const revisionId = this.worldDb.ensurePendingRevision(editorAdminId);
397 const assignmentCounts = this.#countDialogueAssignments(revisionId);
398 const assigned = assignmentCounts.get(tree.dialogueId) ?? 0;
399 if (tree.unique && assigned > 1)
400 {
401 const err = new Error("Unique dialogue can be assigned to at most one spawner.");
402 err.userFacing = true;
403 throw err;
404 }
405 if (tree.unique && assigned === 1)
406 {
407 const violation = this.#uniqueDialogueMaxAliveViolation(revisionId, tree.dialogueId);
408 if (violation)
409 {
410 const err = new Error(violation);
411 err.userFacing = true;
412 throw err;
413 }
414 }
415 this.worldDb.saveDialogueTree(revisionId, tree);
416 return this.loadDialogues();
417 }
418
419 deleteDialogue (dialogueId, editorAdminId)
420 {
421 const id = String(dialogueId);
422 const revisionId = this.worldDb.getEditorRevisionId();
423 const assigned = this.#countDialogueAssignments(revisionId).get(id) ?? 0;
424 if (assigned > 0)
425 {
426 const err = new Error(`Dialogue is still assigned to ${assigned} spawner(s). Unassign first.`);
427 err.userFacing = true;
428 throw err;
429 }
430 const pendingId = this.worldDb.ensurePendingRevision(editorAdminId);
431 this.worldDb.deleteDialogueTree(pendingId, id);
432 return this.loadDialogues();
433 }
434
435 /** Validate dialogueId / unique / maxAlive when saving a chunk's spawners. */
436 validateSpawnerDialogues (chunkX, chunkY, spawners)
437 {
438 const revisionId = this.worldDb.getEditorRevisionId();
439 const trees = new Map(
440 this.worldDb.listDialogueTrees(revisionId).map((raw) =>
441 {
442 const tree = normalizeDialogueTree(raw);
443 return [tree.dialogueId, tree];
444 })
445 );
446 for (const entry of spawners ?? [])
447 {
448 const dialogueId = entry?.config?.dialogueId;
449 if (!dialogueId)
450 { continue; }
451 const tree = trees.get(dialogueId);
452 if (!tree)
453 {
454 const err = new Error(`Unknown dialogue "${dialogueId}" on a spawner.`);
455 err.userFacing = true;
456 throw err;
457 }
458 if (tree.unique && (Math.max(1, Number(entry.config.maxAlive) || 1) !== 1))
459 {
460 const err = new Error(`Unique dialogue "${dialogueId}" requires spawner maxAlive = 1.`);
461 err.userFacing = true;
462 throw err;
463 }
464 }
465 const counts = this.#countDialogueAssignments(revisionId, { chunkX, chunkY, spawners });
466 for (const [dialogueId, tree] of trees)
467 {
468 const n = counts.get(dialogueId) ?? 0;
469 if (tree.unique && n > 1)
470 {
471 const err = new Error(`Unique dialogue "${dialogueId}" would be on ${n} spawners.`);
472 err.userFacing = true;
473 throw err;
474 }
475 }
476 }
477
478 #countDialogueAssignments (revisionId, replaceChunk = null)
479 {
480 const settings = this.worldDb.getLiveSettings();
481 const counts = new Map();
482 const coords = this.worldDb.listChunkCoords(revisionId);
483 for (const { chunk_x: cx, chunk_y: cy } of coords)
484 {
485 if (replaceChunk && cx === replaceChunk.chunkX && cy === replaceChunk.chunkY)
486 { continue; }
487 const row = this.worldDb.getChunkVersion(cx, cy, revisionId);
488 if (!row)
489 { continue; }
490 const data = decodeChunkBlob(row.data_blob, settings.defaultGroundTypeId, cx, cy);
491 for (const entry of data.spawners ?? [])
492 {
493 const id = entry?.config?.dialogueId;
494 if (!id)
495 { continue; }
496 counts.set(id, (counts.get(id) ?? 0) + 1);
497 }
498 }
499 if (replaceChunk?.spawners)
500 {
501 for (const entry of replaceChunk.spawners)
502 {
503 const id = entry?.config?.dialogueId;
504 if (!id)
505 { continue; }
506 counts.set(id, (counts.get(id) ?? 0) + 1);
507 }
508 }
509 return counts;
510 }
511
512 #uniqueDialogueMaxAliveViolation (revisionId, dialogueId)
513 {
514 const settings = this.worldDb.getLiveSettings();
515 const coords = this.worldDb.listChunkCoords(revisionId);
516 for (const { chunk_x: chunkX, chunk_y: chunkY } of coords)
517 {
518 const row = this.worldDb.getChunkVersion(chunkX, chunkY, revisionId);
519 if (!row)
520 { continue; }
521 const data = decodeChunkBlob(row.data_blob, settings.defaultGroundTypeId, chunkX, chunkY);
522 for (const entry of data.spawners ?? [])
523 {
524 if (entry?.config?.dialogueId !== dialogueId)
525 { continue; }
526 const maxAlive = Math.max(1, Number(entry.config.maxAlive) || 1);
527 if (maxAlive !== 1)
528 { return `Unique dialogue "${dialogueId}" requires spawner maxAlive = 1.`; }
529 }
530 }
531 return null;
532 }
533
534 #loadForBake (chunkX, chunkY, revisionId, defaultGroundTypeId, savedX, savedY, savedData, dimensionId = OVERWORLD_ID)
535 {
536 if (chunkX === savedX && chunkY === savedY)
537 { return savedData; }
538
539 const dimId = normalizeDimensionId(dimensionId);
540 const row = this.worldDb.getChunkVersion(chunkX, chunkY, revisionId, dimId);
541 return row
542 ? decodeChunkBlob(row.data_blob, defaultGroundTypeId, chunkX, chunkY)
543 : createEmptyChunkData(defaultGroundTypeId);
544 }
545}
546
547export function diffChunks (chunkX, chunkY, previous, next, previousAuthored)
548{
549 const bullets = [];
550 const prevOpen = previousAuthored ? !!previous.open : false;
551 if (prevOpen !== !!next.open)
552 { bullets.push(next.open ? "Chunk opened." : "Chunk closed."); }
553
554 for (let localY = 0; localY < CHUNK_SIZE; localY++)
555 {
556 for (let localX = 0; localX < CHUNK_SIZE; localX++)
557 {
558 const index = localY * CHUNK_SIZE + localX;
559 const tileX = localToTile(chunkX, localX);
560 const tileY = localToTile(chunkY, localY);
561 const prevGround = normalizeGround(previous.grounds[index]);
562 const nextGround = normalizeGround(next.grounds[index]);
563 if (prevGround.typeId !== nextGround.typeId)
564 {
565 bullets.push(
566 `Ground at ${tileX}, ${tileY} changed from ${nameGround(prevGround.typeId)} to ${nameGround(nextGround.typeId)}.`
567 );
568 }
569 else if ((prevGround.state ?? "") !== (nextGround.state ?? ""))
570 {
571 bullets.push(
572 `Ground ${nameGround(nextGround.typeId)} at ${tileX}, ${tileY} state changed from ${stateLabel(prevGround.state)} to ${stateLabel(nextGround.state)}.`
573 );
574 }
575
576 const prevBlock = normalizeBlock(previous.blocks[index]);
577 const nextBlock = normalizeBlock(next.blocks[index]);
578 const prevId = prevBlock?.typeId ?? null;
579 const nextId = nextBlock?.typeId ?? null;
580 const prevConfig = JSON.stringify(prevBlock?.config ?? null);
581 const nextConfig = JSON.stringify(nextBlock?.config ?? null);
582 if (prevId !== nextId || prevConfig !== nextConfig)
583 {
584 if (prevId == null && nextId != null)
585 { bullets.push(`Block ${nameBlock(nextId)} placed at ${tileX}, ${tileY}.`); }
586 else if (prevId != null && nextId == null)
587 { bullets.push(`Block ${nameBlock(prevId)} removed at ${tileX}, ${tileY}.`); }
588 else
589 { bullets.push(`Block at ${tileX}, ${tileY} changed from ${nameBlock(prevId)} to ${nameBlock(nextId)}.`); }
590 }
591 else if (prevBlock && nextBlock && (prevBlock.state ?? "") !== (nextBlock.state ?? ""))
592 {
593 bullets.push(
594 `Block ${nameBlock(nextId)} at ${tileX}, ${tileY} state changed from ${stateLabel(prevBlock.state)} to ${stateLabel(nextBlock.state)}.`
595 );
596 }
597 }
598 }
599
600 const prevSpawners = indexSpawners(previous.spawners);
601 const nextSpawners = indexSpawners(next.spawners);
602 for (const key of new Set([...prevSpawners.keys(), ...nextSpawners.keys()]))
603 {
604 const prev = prevSpawners.get(key);
605 const cur = nextSpawners.get(key);
606 const [localX, localY] = key.split(",").map(Number);
607 const tileX = localToTile(chunkX, localX);
608 const tileY = localToTile(chunkY, localY);
609 const prevConfig = JSON.stringify(prev?.config ?? null);
610 const nextConfig = JSON.stringify(cur?.config ?? null);
611 if (!prev && cur)
612 { bullets.push(`Spawner (${cur.config.mobTypeId}) placed at ${tileX}, ${tileY}.`); }
613 else if (prev && !cur)
614 { bullets.push(`Spawner removed at ${tileX}, ${tileY}.`); }
615 else if (prevConfig !== nextConfig)
616 { bullets.push(`Spawner at ${tileX}, ${tileY} config changed.`); }
617 }
618 return bullets;
619}
620
621function indexSpawners (spawners)
622{
623 const map = new Map();
624 for (const entry of spawners ?? [])
625 { map.set(`${entry.localX},${entry.localY}`, entry); }
626 return map;
627}
628
629function nameGround (typeId)
630{
631 return groundDefinition(typeId)?.displayName ?? `ground ${typeId}`;
632}
633
634function nameBlock (typeId)
635{
636 return blockDefinition(typeId)?.displayName ?? `block ${typeId}`;
637}
638
639function stateLabel (state)
640{
641 return state || "default";
642}