BlueGit

timfa questward

Files

questward / server / src / editor / service.js

service.js

spawner tweaks
timfa Tim Falken · pushed by timfa · 2 months ago
661b81e5 History
service.js 8.2 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, allSettingFields } from "../../../shared/definitions/worldSettings.js";
9
10export class EditorService
11{
12 constructor (worldDb)
13 {
14 this.worldDb = worldDb;
15 }
16
17 loadChunk (chunkX, chunkY)
18 {
19 const revisionId = this.worldDb.getEditorRevisionId();
20 const settings = this.worldDb.getLiveSettings();
21 const row = this.worldDb.getChunkVersion(chunkX, chunkY, revisionId);
22 const data = row
23 ? decodeChunkBlob(row.data_blob, settings.defaultGroundTypeId)
24 : createEmptyChunkData(settings.defaultGroundTypeId);
25 const isOpen = (x, y) =>
26 {
27 const neighbor = this.worldDb.getChunkVersion(x, y, revisionId);
28 if (!neighbor)
29 { return false; }
30 return !!decodeChunkBlob(neighbor.data_blob, settings.defaultGroundTypeId).open;
31 };
32 return {
33 chunkX,
34 chunkY,
35 revisionId,
36 pendingRevisionId: this.worldDb.getPendingRevisionId(),
37 activeRevisionId: this.worldDb.getActiveRevisionId(),
38 authored: !!row,
39 settings,
40 data,
41 closedEdges: closedNeighborEdges(chunkX, chunkY, isOpen),
42 changelog: this.worldDb.listChangelog(chunkX, chunkY)
43 };
44 }
45
46 saveChunk (chunkX, chunkY, draft, editorAdminId)
47 {
48 const previous = this.loadChunk(chunkX, chunkY);
49 const next = cloneChunkData(previous.data);
50 const settings = previous.settings ?? this.worldDb.getLiveSettings();
51 next.open = !!draft.open;
52 if (Array.isArray(draft.grounds))
53 { next.grounds = draft.grounds.map((ground) => normalizeGround(ground, settings.defaultGroundTypeId)); }
54 if (Array.isArray(draft.blocks))
55 { next.blocks = draft.blocks.map((block) => normalizeBlock(block)); }
56 if (Array.isArray(draft.spawners))
57 { next.spawners = draft.spawners.map((entry) => normalizeSpawner(entry)).filter(Boolean); }
58
59 const diffEntries = diffChunks(chunkX, chunkY, previous.data, next, previous.authored);
60 const revisionId = this.worldDb.ensurePendingRevision(editorAdminId);
61 const baked = bakeLightMaps(
62 chunkX,
63 chunkY,
64 (x, y) => this.#loadForBake(x, y, revisionId, settings.defaultGroundTypeId, chunkX, chunkY, next),
65 maxDefinitionLightRange(BLOCK_DEFINITIONS)
66 );
67
68 for (const result of baked)
69 {
70 const shouldStore = result.chunkX === chunkX && result.chunkY === chunkY
71 || this.worldDb.getChunkVersion(result.chunkX, result.chunkY, revisionId)
72 || result.data.light.some((value) => value > 0);
73
74 if (!shouldStore)
75 { continue; }
76
77 this.worldDb.saveChunkVersion(
78 result.chunkX,
79 result.chunkY,
80 revisionId,
81 result.data.open ? 1 : 0,
82 encodeChunkBlob(result.data)
83 );
84 }
85
86 if (diffEntries.length > 0)
87 { this.worldDb.appendChangelog(chunkX, chunkY, revisionId, editorAdminId, diffEntries); }
88
89 return this.loadChunk(chunkX, chunkY);
90 }
91
92 loadSettings ()
93 {
94 return {
95 settings: this.worldDb.getLiveSettings(),
96 changelog: this.worldDb.listSettingsChangelog()
97 };
98 }
99
100 saveSettings (partial, editorAdminId)
101 {
102 const current = this.worldDb.getLiveSettings();
103 const next = { ...current };
104 for (const field of allSettingFields())
105 {
106 if (!Object.prototype.hasOwnProperty.call(partial, field.key))
107 { continue; }
108 const value = coerceSettingValue(field, partial[field.key]);
109 if (value == null)
110 { continue; }
111 next[field.key] = value;
112 }
113 const { bullets, changed } = diffSettings(current, next);
114 if (Object.keys(changed).length === 0)
115 {
116 return {
117 settings: current,
118 changelog: this.worldDb.listSettingsChangelog(),
119 changed: {}
120 };
121 }
122 const settings = { ...current, ...changed };
123 this.worldDb.saveLiveSettings(settings, editorAdminId);
124 this.worldDb.appendSettingsChangelog(editorAdminId, bullets);
125 return {
126 settings,
127 changelog: this.worldDb.listSettingsChangelog(),
128 changed,
129 diffEntries: bullets
130 };
131 }
132
133 applyPendingRevision ()
134 {
135 return this.worldDb.promotePendingRevision();
136 }
137
138 revisionStatus ()
139 {
140 return {
141 activeRevisionId: this.worldDb.getActiveRevisionId(),
142 pendingRevisionId: this.worldDb.getPendingRevisionId()
143 };
144 }
145
146 #loadForBake (chunkX, chunkY, revisionId, defaultGroundTypeId, savedX, savedY, savedData)
147 {
148 if (chunkX === savedX && chunkY === savedY)
149 { return savedData; }
150
151 const row = this.worldDb.getChunkVersion(chunkX, chunkY, revisionId);
152 return row
153 ? decodeChunkBlob(row.data_blob, defaultGroundTypeId)
154 : createEmptyChunkData(defaultGroundTypeId);
155 }
156}
157
158export function diffChunks (chunkX, chunkY, previous, next, previousAuthored)
159{
160 const bullets = [];
161 const prevOpen = previousAuthored ? !!previous.open : false;
162 if (prevOpen !== !!next.open)
163 { bullets.push(next.open ? "Chunk opened." : "Chunk closed."); }
164
165 for (let localY = 0; localY < CHUNK_SIZE; localY++)
166 {
167 for (let localX = 0; localX < CHUNK_SIZE; localX++)
168 {
169 const index = localY * CHUNK_SIZE + localX;
170 const tileX = localToTile(chunkX, localX);
171 const tileY = localToTile(chunkY, localY);
172 const prevGround = normalizeGround(previous.grounds[index]);
173 const nextGround = normalizeGround(next.grounds[index]);
174 if (prevGround.typeId !== nextGround.typeId)
175 {
176 bullets.push(
177 `Ground at ${tileX}, ${tileY} changed from ${nameGround(prevGround.typeId)} to ${nameGround(nextGround.typeId)}.`
178 );
179 }
180 else if ((prevGround.state ?? "") !== (nextGround.state ?? ""))
181 {
182 bullets.push(
183 `Ground ${nameGround(nextGround.typeId)} at ${tileX}, ${tileY} state changed from ${stateLabel(prevGround.state)} to ${stateLabel(nextGround.state)}.`
184 );
185 }
186
187 const prevBlock = normalizeBlock(previous.blocks[index]);
188 const nextBlock = normalizeBlock(next.blocks[index]);
189 const prevId = prevBlock?.typeId ?? null;
190 const nextId = nextBlock?.typeId ?? null;
191 const prevConfig = JSON.stringify(prevBlock?.config ?? null);
192 const nextConfig = JSON.stringify(nextBlock?.config ?? null);
193 if (prevId !== nextId || prevConfig !== nextConfig)
194 {
195 if (prevId == null && nextId != null)
196 { bullets.push(`Block ${nameBlock(nextId)} placed at ${tileX}, ${tileY}.`); }
197 else if (prevId != null && nextId == null)
198 { bullets.push(`Block ${nameBlock(prevId)} removed at ${tileX}, ${tileY}.`); }
199 else
200 { bullets.push(`Block at ${tileX}, ${tileY} changed from ${nameBlock(prevId)} to ${nameBlock(nextId)}.`); }
201 }
202 else if (prevBlock && nextBlock && (prevBlock.state ?? "") !== (nextBlock.state ?? ""))
203 {
204 bullets.push(
205 `Block ${nameBlock(nextId)} at ${tileX}, ${tileY} state changed from ${stateLabel(prevBlock.state)} to ${stateLabel(nextBlock.state)}.`
206 );
207 }
208 }
209 }
210
211 const prevSpawners = indexSpawners(previous.spawners);
212 const nextSpawners = indexSpawners(next.spawners);
213 for (const key of new Set([...prevSpawners.keys(), ...nextSpawners.keys()]))
214 {
215 const prev = prevSpawners.get(key);
216 const cur = nextSpawners.get(key);
217 const [localX, localY] = key.split(",").map(Number);
218 const tileX = localToTile(chunkX, localX);
219 const tileY = localToTile(chunkY, localY);
220 const prevConfig = JSON.stringify(prev?.config ?? null);
221 const nextConfig = JSON.stringify(cur?.config ?? null);
222 if (!prev && cur)
223 { bullets.push(`Spawner (${cur.config.mobTypeId}) placed at ${tileX}, ${tileY}.`); }
224 else if (prev && !cur)
225 { bullets.push(`Spawner removed at ${tileX}, ${tileY}.`); }
226 else if (prevConfig !== nextConfig)
227 { bullets.push(`Spawner at ${tileX}, ${tileY} config changed.`); }
228 }
229 return bullets;
230}
231
232function indexSpawners (spawners)
233{
234 const map = new Map();
235 for (const entry of spawners ?? [])
236 { map.set(`${entry.localX},${entry.localY}`, entry); }
237 return map;
238}
239
240function nameGround (typeId)
241{
242 return groundDefinition(typeId)?.displayName ?? `ground ${typeId}`;
243}
244
245function nameBlock (typeId)
246{
247 return blockDefinition(typeId)?.displayName ?? `block ${typeId}`;
248}
249
250function stateLabel (state)
251{
252 return state || "default";
253}