| 1 | import { groundDefinition, blockDefinition, normalizeGround, normalizeBlock } from "../../../shared/definitions/tiles.js"; |
| 2 | import { CHUNK_SIZE } from "../../../shared/protocol/constants.js"; |
| 3 | import { localToTile } from "../world/coordinates.js"; |
| 4 | import { cloneChunkData, createEmptyChunkData, decodeChunkBlob, encodeChunkBlob, normalizeSpawner } from "../world/chunkData.js"; |
| 5 | import { BLOCK_DEFINITIONS } from "../../../shared/definitions/tiles.js"; |
| 6 | import { bakeLightMaps, maxDefinitionLightRange } from "../lighting/baker.js"; |
| 7 | import { closedNeighborEdges } from "../world/chunkFlags.js"; |
| 8 | import { coerceSettingValue, diffSettings, diffItemSeedValues, allSettingFields, itemSeedMapsEqual } from "../../../shared/definitions/worldSettings.js"; |
| 9 | import { normalizeItemSeedValues } from "../../../shared/shop/pricing.js"; |
| 10 | import { itemDefinition } from "../../../shared/definitions/items.js"; |
| 11 | import { DEFAULT_WORLD_SETTINGS } from "../../../shared/protocol/constants.js"; |
| 12 | import { |
| 13 | normalizeStarterLoadout, |
| 14 | starterLoadoutsEqual, |
| 15 | diffStarterLoadout |
| 16 | } from "../../../shared/items/starterLoadout.js"; |
| 17 | |
| 18 | export class EditorService |
| 19 | { |
| 20 | constructor (worldDb) |
| 21 | { |
| 22 | this.worldDb = worldDb; |
| 23 | } |
| 24 | |
| 25 | loadChunk (chunkX, chunkY) |
| 26 | { |
| 27 | const revisionId = this.worldDb.getEditorRevisionId(); |
| 28 | const settings = this.worldDb.getLiveSettings(); |
| 29 | const row = this.worldDb.getChunkVersion(chunkX, chunkY, revisionId); |
| 30 | const data = row |
| 31 | ? decodeChunkBlob(row.data_blob, settings.defaultGroundTypeId, chunkX, chunkY) |
| 32 | : createEmptyChunkData(settings.defaultGroundTypeId); |
| 33 | const isOpen = (x, y) => |
| 34 | { |
| 35 | const neighbor = this.worldDb.getChunkVersion(x, y, revisionId); |
| 36 | if (!neighbor) |
| 37 | { return false; } |
| 38 | return !!decodeChunkBlob(neighbor.data_blob, settings.defaultGroundTypeId, x, y).open; |
| 39 | }; |
| 40 | return { |
| 41 | chunkX, |
| 42 | chunkY, |
| 43 | revisionId, |
| 44 | pendingRevisionId: this.worldDb.getPendingRevisionId(), |
| 45 | activeRevisionId: this.worldDb.getActiveRevisionId(), |
| 46 | authored: !!row, |
| 47 | settings, |
| 48 | data, |
| 49 | closedEdges: closedNeighborEdges(chunkX, chunkY, isOpen), |
| 50 | changelog: this.worldDb.listChangelog(chunkX, chunkY) |
| 51 | }; |
| 52 | } |
| 53 | |
| 54 | saveChunk (chunkX, chunkY, draft, editorAdminId) |
| 55 | { |
| 56 | const previous = this.loadChunk(chunkX, chunkY); |
| 57 | const next = cloneChunkData(previous.data); |
| 58 | const settings = previous.settings ?? this.worldDb.getLiveSettings(); |
| 59 | next.open = !!draft.open; |
| 60 | if (Array.isArray(draft.grounds)) |
| 61 | { next.grounds = draft.grounds.map((ground) => normalizeGround(ground, settings.defaultGroundTypeId)); } |
| 62 | if (Array.isArray(draft.blocks)) |
| 63 | { next.blocks = draft.blocks.map((block) => normalizeBlock(block)); } |
| 64 | if (Array.isArray(draft.spawners)) |
| 65 | { next.spawners = draft.spawners.map((entry) => normalizeSpawner(entry)).filter(Boolean); } |
| 66 | |
| 67 | const diffEntries = diffChunks(chunkX, chunkY, previous.data, next, previous.authored); |
| 68 | const revisionId = this.worldDb.ensurePendingRevision(editorAdminId); |
| 69 | const baked = bakeLightMaps( |
| 70 | chunkX, |
| 71 | chunkY, |
| 72 | (x, y) => this.#loadForBake(x, y, revisionId, settings.defaultGroundTypeId, chunkX, chunkY, next), |
| 73 | maxDefinitionLightRange(BLOCK_DEFINITIONS) |
| 74 | ); |
| 75 | |
| 76 | for (const result of baked) |
| 77 | { |
| 78 | const shouldStore = result.chunkX === chunkX && result.chunkY === chunkY |
| 79 | || this.worldDb.getChunkVersion(result.chunkX, result.chunkY, revisionId) |
| 80 | || result.data.light.some((value) => value > 0); |
| 81 | |
| 82 | if (!shouldStore) |
| 83 | { continue; } |
| 84 | |
| 85 | this.worldDb.saveChunkVersion( |
| 86 | result.chunkX, |
| 87 | result.chunkY, |
| 88 | revisionId, |
| 89 | result.data.open ? 1 : 0, |
| 90 | encodeChunkBlob(result.data) |
| 91 | ); |
| 92 | } |
| 93 | |
| 94 | if (diffEntries.length > 0) |
| 95 | { this.worldDb.appendChangelog(chunkX, chunkY, revisionId, editorAdminId, diffEntries); } |
| 96 | |
| 97 | return this.loadChunk(chunkX, chunkY); |
| 98 | } |
| 99 | |
| 100 | loadSettings () |
| 101 | { |
| 102 | return { |
| 103 | settings: this.worldDb.getLiveSettings(), |
| 104 | changelog: this.worldDb.listSettingsChangelog() |
| 105 | }; |
| 106 | } |
| 107 | |
| 108 | saveSettings (partial, editorAdminId) |
| 109 | { |
| 110 | const current = this.worldDb.getLiveSettings(); |
| 111 | const next = { |
| 112 | ...current, |
| 113 | itemSeedValues: { |
| 114 | ...(current.itemSeedValues && typeof current.itemSeedValues === "object" |
| 115 | ? current.itemSeedValues |
| 116 | : {}) |
| 117 | } |
| 118 | }; |
| 119 | for (const field of allSettingFields()) |
| 120 | { |
| 121 | if (!Object.prototype.hasOwnProperty.call(partial, field.key)) |
| 122 | { continue; } |
| 123 | const value = coerceSettingValue(field, partial[field.key]); |
| 124 | if (value == null) |
| 125 | { continue; } |
| 126 | next[field.key] = value; |
| 127 | } |
| 128 | if (Object.prototype.hasOwnProperty.call(partial, "itemSeedValues")) |
| 129 | { |
| 130 | next.itemSeedValues = normalizeItemSeedValues(partial.itemSeedValues); |
| 131 | } |
| 132 | if (Object.prototype.hasOwnProperty.call(partial, "starterEquipment") |
| 133 | || Object.prototype.hasOwnProperty.call(partial, "starterInventory")) |
| 134 | { |
| 135 | const loadout = normalizeStarterLoadout({ |
| 136 | starterEquipment: Object.prototype.hasOwnProperty.call(partial, "starterEquipment") |
| 137 | ? partial.starterEquipment |
| 138 | : current.starterEquipment, |
| 139 | starterInventory: Object.prototype.hasOwnProperty.call(partial, "starterInventory") |
| 140 | ? partial.starterInventory |
| 141 | : current.starterInventory |
| 142 | }); |
| 143 | next.starterEquipment = loadout.equipment; |
| 144 | next.starterInventory = loadout.inventory; |
| 145 | } |
| 146 | const { bullets: scalarBullets, changed } = diffSettings(current, next); |
| 147 | const seedBullets = Object.prototype.hasOwnProperty.call(partial, "itemSeedValues") |
| 148 | ? diffItemSeedValues( |
| 149 | normalizeItemSeedValues(current.itemSeedValues), |
| 150 | next.itemSeedValues, |
| 151 | (itemId) => itemDefinition(itemId)?.displayName ?? null |
| 152 | ) |
| 153 | : []; |
| 154 | if (!itemSeedMapsEqual(current.itemSeedValues, next.itemSeedValues) |
| 155 | && Object.prototype.hasOwnProperty.call(partial, "itemSeedValues")) |
| 156 | { |
| 157 | changed.itemSeedValues = next.itemSeedValues; |
| 158 | } |
| 159 | const starterChanged = !starterLoadoutsEqual(current, next) |
| 160 | && (Object.prototype.hasOwnProperty.call(partial, "starterEquipment") |
| 161 | || Object.prototype.hasOwnProperty.call(partial, "starterInventory")); |
| 162 | if (starterChanged) |
| 163 | { |
| 164 | changed.starterEquipment = next.starterEquipment; |
| 165 | changed.starterInventory = next.starterInventory; |
| 166 | } |
| 167 | const starterBullets = starterChanged |
| 168 | ? diffStarterLoadout(current, next) |
| 169 | : []; |
| 170 | const bullets = [...scalarBullets, ...seedBullets, ...starterBullets]; |
| 171 | if (Object.keys(changed).length === 0) |
| 172 | { |
| 173 | return { |
| 174 | settings: current, |
| 175 | changelog: this.worldDb.listSettingsChangelog(), |
| 176 | changed: {} |
| 177 | }; |
| 178 | } |
| 179 | const settings = { |
| 180 | ...current, |
| 181 | ...changed, |
| 182 | itemSeedValues: changed.itemSeedValues |
| 183 | ?? normalizeItemSeedValues(current.itemSeedValues) |
| 184 | ?? { ...(DEFAULT_WORLD_SETTINGS.itemSeedValues ?? {}) }, |
| 185 | starterEquipment: changed.starterEquipment ?? current.starterEquipment |
| 186 | ?? DEFAULT_WORLD_SETTINGS.starterEquipment, |
| 187 | starterInventory: changed.starterInventory ?? current.starterInventory |
| 188 | ?? DEFAULT_WORLD_SETTINGS.starterInventory |
| 189 | }; |
| 190 | const normalized = normalizeStarterLoadout(settings); |
| 191 | settings.starterEquipment = normalized.equipment; |
| 192 | settings.starterInventory = normalized.inventory; |
| 193 | this.worldDb.saveLiveSettings(settings, editorAdminId); |
| 194 | this.worldDb.appendSettingsChangelog(editorAdminId, bullets); |
| 195 | return { |
| 196 | settings, |
| 197 | changelog: this.worldDb.listSettingsChangelog(), |
| 198 | changed, |
| 199 | diffEntries: bullets |
| 200 | }; |
| 201 | } |
| 202 | |
| 203 | applyPendingRevision () |
| 204 | { |
| 205 | return this.worldDb.promotePendingRevision(); |
| 206 | } |
| 207 | |
| 208 | revisionStatus () |
| 209 | { |
| 210 | return { |
| 211 | activeRevisionId: this.worldDb.getActiveRevisionId(), |
| 212 | pendingRevisionId: this.worldDb.getPendingRevisionId() |
| 213 | }; |
| 214 | } |
| 215 | |
| 216 | #loadForBake (chunkX, chunkY, revisionId, defaultGroundTypeId, savedX, savedY, savedData) |
| 217 | { |
| 218 | if (chunkX === savedX && chunkY === savedY) |
| 219 | { return savedData; } |
| 220 | |
| 221 | const row = this.worldDb.getChunkVersion(chunkX, chunkY, revisionId); |
| 222 | return row |
| 223 | ? decodeChunkBlob(row.data_blob, defaultGroundTypeId, chunkX, chunkY) |
| 224 | : createEmptyChunkData(defaultGroundTypeId); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | export function diffChunks (chunkX, chunkY, previous, next, previousAuthored) |
| 229 | { |
| 230 | const bullets = []; |
| 231 | const prevOpen = previousAuthored ? !!previous.open : false; |
| 232 | if (prevOpen !== !!next.open) |
| 233 | { bullets.push(next.open ? "Chunk opened." : "Chunk closed."); } |
| 234 | |
| 235 | for (let localY = 0; localY < CHUNK_SIZE; localY++) |
| 236 | { |
| 237 | for (let localX = 0; localX < CHUNK_SIZE; localX++) |
| 238 | { |
| 239 | const index = localY * CHUNK_SIZE + localX; |
| 240 | const tileX = localToTile(chunkX, localX); |
| 241 | const tileY = localToTile(chunkY, localY); |
| 242 | const prevGround = normalizeGround(previous.grounds[index]); |
| 243 | const nextGround = normalizeGround(next.grounds[index]); |
| 244 | if (prevGround.typeId !== nextGround.typeId) |
| 245 | { |
| 246 | bullets.push( |
| 247 | `Ground at ${tileX}, ${tileY} changed from ${nameGround(prevGround.typeId)} to ${nameGround(nextGround.typeId)}.` |
| 248 | ); |
| 249 | } |
| 250 | else if ((prevGround.state ?? "") !== (nextGround.state ?? "")) |
| 251 | { |
| 252 | bullets.push( |
| 253 | `Ground ${nameGround(nextGround.typeId)} at ${tileX}, ${tileY} state changed from ${stateLabel(prevGround.state)} to ${stateLabel(nextGround.state)}.` |
| 254 | ); |
| 255 | } |
| 256 | |
| 257 | const prevBlock = normalizeBlock(previous.blocks[index]); |
| 258 | const nextBlock = normalizeBlock(next.blocks[index]); |
| 259 | const prevId = prevBlock?.typeId ?? null; |
| 260 | const nextId = nextBlock?.typeId ?? null; |
| 261 | const prevConfig = JSON.stringify(prevBlock?.config ?? null); |
| 262 | const nextConfig = JSON.stringify(nextBlock?.config ?? null); |
| 263 | if (prevId !== nextId || prevConfig !== nextConfig) |
| 264 | { |
| 265 | if (prevId == null && nextId != null) |
| 266 | { bullets.push(`Block ${nameBlock(nextId)} placed at ${tileX}, ${tileY}.`); } |
| 267 | else if (prevId != null && nextId == null) |
| 268 | { bullets.push(`Block ${nameBlock(prevId)} removed at ${tileX}, ${tileY}.`); } |
| 269 | else |
| 270 | { bullets.push(`Block at ${tileX}, ${tileY} changed from ${nameBlock(prevId)} to ${nameBlock(nextId)}.`); } |
| 271 | } |
| 272 | else if (prevBlock && nextBlock && (prevBlock.state ?? "") !== (nextBlock.state ?? "")) |
| 273 | { |
| 274 | bullets.push( |
| 275 | `Block ${nameBlock(nextId)} at ${tileX}, ${tileY} state changed from ${stateLabel(prevBlock.state)} to ${stateLabel(nextBlock.state)}.` |
| 276 | ); |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | const prevSpawners = indexSpawners(previous.spawners); |
| 282 | const nextSpawners = indexSpawners(next.spawners); |
| 283 | for (const key of new Set([...prevSpawners.keys(), ...nextSpawners.keys()])) |
| 284 | { |
| 285 | const prev = prevSpawners.get(key); |
| 286 | const cur = nextSpawners.get(key); |
| 287 | const [localX, localY] = key.split(",").map(Number); |
| 288 | const tileX = localToTile(chunkX, localX); |
| 289 | const tileY = localToTile(chunkY, localY); |
| 290 | const prevConfig = JSON.stringify(prev?.config ?? null); |
| 291 | const nextConfig = JSON.stringify(cur?.config ?? null); |
| 292 | if (!prev && cur) |
| 293 | { bullets.push(`Spawner (${cur.config.mobTypeId}) placed at ${tileX}, ${tileY}.`); } |
| 294 | else if (prev && !cur) |
| 295 | { bullets.push(`Spawner removed at ${tileX}, ${tileY}.`); } |
| 296 | else if (prevConfig !== nextConfig) |
| 297 | { bullets.push(`Spawner at ${tileX}, ${tileY} config changed.`); } |
| 298 | } |
| 299 | return bullets; |
| 300 | } |
| 301 | |
| 302 | function indexSpawners (spawners) |
| 303 | { |
| 304 | const map = new Map(); |
| 305 | for (const entry of spawners ?? []) |
| 306 | { map.set(`${entry.localX},${entry.localY}`, entry); } |
| 307 | return map; |
| 308 | } |
| 309 | |
| 310 | function nameGround (typeId) |
| 311 | { |
| 312 | return groundDefinition(typeId)?.displayName ?? `ground ${typeId}`; |
| 313 | } |
| 314 | |
| 315 | function nameBlock (typeId) |
| 316 | { |
| 317 | return blockDefinition(typeId)?.displayName ?? `block ${typeId}`; |
| 318 | } |
| 319 | |
| 320 | function stateLabel (state) |
| 321 | { |
| 322 | return state || "default"; |
| 323 | } |