| 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 } 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, allSettingFields } from "../../../shared/definitions/worldSettings.js"; |
| 9 | |
| 10 | export 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 | |
| 57 | const diffEntries = diffChunks(chunkX, chunkY, previous.data, next, previous.authored); |
| 58 | const revisionId = this.worldDb.ensurePendingRevision(editorAdminId); |
| 59 | const baked = bakeLightMaps( |
| 60 | chunkX, |
| 61 | chunkY, |
| 62 | (x, y) => this.#loadForBake(x, y, revisionId, settings.defaultGroundTypeId, chunkX, chunkY, next), |
| 63 | maxDefinitionLightRange(BLOCK_DEFINITIONS) |
| 64 | ); |
| 65 | |
| 66 | for (const result of baked) |
| 67 | { |
| 68 | const shouldStore = result.chunkX === chunkX && result.chunkY === chunkY |
| 69 | || this.worldDb.getChunkVersion(result.chunkX, result.chunkY, revisionId) |
| 70 | || result.data.light.some((value) => value > 0); |
| 71 | |
| 72 | if (!shouldStore) |
| 73 | { continue; } |
| 74 | |
| 75 | this.worldDb.saveChunkVersion( |
| 76 | result.chunkX, |
| 77 | result.chunkY, |
| 78 | revisionId, |
| 79 | result.data.open ? 1 : 0, |
| 80 | encodeChunkBlob(result.data) |
| 81 | ); |
| 82 | } |
| 83 | |
| 84 | if (diffEntries.length > 0) |
| 85 | { this.worldDb.appendChangelog(chunkX, chunkY, revisionId, editorAdminId, diffEntries); } |
| 86 | |
| 87 | return this.loadChunk(chunkX, chunkY); |
| 88 | } |
| 89 | |
| 90 | loadSettings () |
| 91 | { |
| 92 | return { |
| 93 | settings: this.worldDb.getLiveSettings(), |
| 94 | changelog: this.worldDb.listSettingsChangelog() |
| 95 | }; |
| 96 | } |
| 97 | |
| 98 | saveSettings (partial, editorAdminId) |
| 99 | { |
| 100 | const current = this.worldDb.getLiveSettings(); |
| 101 | const next = { ...current }; |
| 102 | for (const field of allSettingFields()) |
| 103 | { |
| 104 | if (!Object.prototype.hasOwnProperty.call(partial, field.key)) |
| 105 | { continue; } |
| 106 | const value = coerceSettingValue(field, partial[field.key]); |
| 107 | if (value == null) |
| 108 | { continue; } |
| 109 | next[field.key] = value; |
| 110 | } |
| 111 | const { bullets, changed } = diffSettings(current, next); |
| 112 | if (Object.keys(changed).length === 0) |
| 113 | { |
| 114 | return { |
| 115 | settings: current, |
| 116 | changelog: this.worldDb.listSettingsChangelog(), |
| 117 | changed: {} |
| 118 | }; |
| 119 | } |
| 120 | const settings = { ...current, ...changed }; |
| 121 | this.worldDb.saveLiveSettings(settings, editorAdminId); |
| 122 | this.worldDb.appendSettingsChangelog(editorAdminId, bullets); |
| 123 | return { |
| 124 | settings, |
| 125 | changelog: this.worldDb.listSettingsChangelog(), |
| 126 | changed, |
| 127 | diffEntries: bullets |
| 128 | }; |
| 129 | } |
| 130 | |
| 131 | applyPendingRevision () |
| 132 | { |
| 133 | return this.worldDb.promotePendingRevision(); |
| 134 | } |
| 135 | |
| 136 | #loadForBake (chunkX, chunkY, revisionId, defaultGroundTypeId, savedX, savedY, savedData) |
| 137 | { |
| 138 | if (chunkX === savedX && chunkY === savedY) |
| 139 | { return savedData; } |
| 140 | |
| 141 | const row = this.worldDb.getChunkVersion(chunkX, chunkY, revisionId); |
| 142 | return row |
| 143 | ? decodeChunkBlob(row.data_blob, defaultGroundTypeId) |
| 144 | : createEmptyChunkData(defaultGroundTypeId); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | export function diffChunks (chunkX, chunkY, previous, next, previousAuthored) |
| 149 | { |
| 150 | const bullets = []; |
| 151 | const prevOpen = previousAuthored ? !!previous.open : false; |
| 152 | if (prevOpen !== !!next.open) |
| 153 | { bullets.push(next.open ? "Chunk opened." : "Chunk closed."); } |
| 154 | |
| 155 | for (let localY = 0; localY < CHUNK_SIZE; localY++) |
| 156 | { |
| 157 | for (let localX = 0; localX < CHUNK_SIZE; localX++) |
| 158 | { |
| 159 | const index = localY * CHUNK_SIZE + localX; |
| 160 | const tileX = localToTile(chunkX, localX); |
| 161 | const tileY = localToTile(chunkY, localY); |
| 162 | const prevGround = normalizeGround(previous.grounds[index]); |
| 163 | const nextGround = normalizeGround(next.grounds[index]); |
| 164 | if (prevGround.typeId !== nextGround.typeId) |
| 165 | { |
| 166 | bullets.push( |
| 167 | `Ground at ${tileX}, ${tileY} changed from ${nameGround(prevGround.typeId)} to ${nameGround(nextGround.typeId)}.` |
| 168 | ); |
| 169 | } |
| 170 | else if ((prevGround.state ?? "") !== (nextGround.state ?? "")) |
| 171 | { |
| 172 | bullets.push( |
| 173 | `Ground ${nameGround(nextGround.typeId)} at ${tileX}, ${tileY} state changed from ${stateLabel(prevGround.state)} to ${stateLabel(nextGround.state)}.` |
| 174 | ); |
| 175 | } |
| 176 | |
| 177 | const prevBlock = normalizeBlock(previous.blocks[index]); |
| 178 | const nextBlock = normalizeBlock(next.blocks[index]); |
| 179 | const prevId = prevBlock?.typeId ?? null; |
| 180 | const nextId = nextBlock?.typeId ?? null; |
| 181 | const prevConfig = JSON.stringify(prevBlock?.config ?? null); |
| 182 | const nextConfig = JSON.stringify(nextBlock?.config ?? null); |
| 183 | if (prevId !== nextId || prevConfig !== nextConfig) |
| 184 | { |
| 185 | if (prevId == null && nextId != null) |
| 186 | { bullets.push(`Block ${nameBlock(nextId)} placed at ${tileX}, ${tileY}.`); } |
| 187 | else if (prevId != null && nextId == null) |
| 188 | { bullets.push(`Block ${nameBlock(prevId)} removed at ${tileX}, ${tileY}.`); } |
| 189 | else |
| 190 | { bullets.push(`Block at ${tileX}, ${tileY} changed from ${nameBlock(prevId)} to ${nameBlock(nextId)}.`); } |
| 191 | } |
| 192 | else if (prevBlock && nextBlock && (prevBlock.state ?? "") !== (nextBlock.state ?? "")) |
| 193 | { |
| 194 | bullets.push( |
| 195 | `Block ${nameBlock(nextId)} at ${tileX}, ${tileY} state changed from ${stateLabel(prevBlock.state)} to ${stateLabel(nextBlock.state)}.` |
| 196 | ); |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | return bullets; |
| 201 | } |
| 202 | |
| 203 | function nameGround (typeId) |
| 204 | { |
| 205 | return groundDefinition(typeId)?.displayName ?? `ground ${typeId}`; |
| 206 | } |
| 207 | |
| 208 | function nameBlock (typeId) |
| 209 | { |
| 210 | return blockDefinition(typeId)?.displayName ?? `block ${typeId}`; |
| 211 | } |
| 212 | |
| 213 | function stateLabel (state) |
| 214 | { |
| 215 | return state || "default"; |
| 216 | } |