| 1 | import { groundDefinition, blockDefinition } 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 | |
| 9 | export class EditorService |
| 10 | { |
| 11 | constructor (worldDb) |
| 12 | { |
| 13 | this.worldDb = worldDb; |
| 14 | } |
| 15 | |
| 16 | loadChunk (chunkX, chunkY) |
| 17 | { |
| 18 | const revisionId = this.worldDb.getEditorRevisionId(); |
| 19 | const settings = this.worldDb.getSettings(revisionId); |
| 20 | const row = this.worldDb.getChunkVersion(chunkX, chunkY, revisionId); |
| 21 | const data = row |
| 22 | ? decodeChunkBlob(row.data_blob, settings.defaultGroundTypeId) |
| 23 | : createEmptyChunkData(settings.defaultGroundTypeId); |
| 24 | const isOpen = (x, y) => |
| 25 | { |
| 26 | const neighbor = this.worldDb.getChunkVersion(x, y, revisionId); |
| 27 | if (!neighbor) |
| 28 | { return false; } |
| 29 | return !!decodeChunkBlob(neighbor.data_blob, settings.defaultGroundTypeId).open; |
| 30 | }; |
| 31 | return { |
| 32 | chunkX, |
| 33 | chunkY, |
| 34 | revisionId, |
| 35 | pendingRevisionId: this.worldDb.getPendingRevisionId(), |
| 36 | activeRevisionId: this.worldDb.getActiveRevisionId(), |
| 37 | authored: !!row, |
| 38 | settings, |
| 39 | data, |
| 40 | closedEdges: closedNeighborEdges(chunkX, chunkY, isOpen), |
| 41 | changelog: this.worldDb.listChangelog(chunkX, chunkY) |
| 42 | }; |
| 43 | } |
| 44 | |
| 45 | saveChunk (chunkX, chunkY, draft, editorAdminId) |
| 46 | { |
| 47 | const previous = this.loadChunk(chunkX, chunkY); |
| 48 | const next = cloneChunkData(previous.data); |
| 49 | next.open = !!draft.open; |
| 50 | if (Array.isArray(draft.grounds)) |
| 51 | { next.grounds = draft.grounds.slice(); } |
| 52 | if (Array.isArray(draft.blocks)) |
| 53 | { next.blocks = draft.blocks.map((block) => (block ? { typeId: block.typeId, config: block.config ?? null } : null)); } |
| 54 | |
| 55 | const diffEntries = diffChunks(chunkX, chunkY, previous.data, next, previous.authored); |
| 56 | const revisionId = this.worldDb.ensurePendingRevision(editorAdminId); |
| 57 | const settings = this.worldDb.getSettings(revisionId); |
| 58 | const baked = bakeLightMaps( |
| 59 | chunkX, |
| 60 | chunkY, |
| 61 | (x, y) => this.#loadForBake(x, y, revisionId, settings.defaultGroundTypeId, chunkX, chunkY, next), |
| 62 | maxDefinitionLightRange(BLOCK_DEFINITIONS) |
| 63 | ); |
| 64 | |
| 65 | for (const result of baked) |
| 66 | { |
| 67 | const shouldStore = result.chunkX === chunkX && result.chunkY === chunkY |
| 68 | || this.worldDb.getChunkVersion(result.chunkX, result.chunkY, revisionId) |
| 69 | || result.data.light.some((value) => value > 0); |
| 70 | |
| 71 | if (!shouldStore) |
| 72 | { continue; } |
| 73 | |
| 74 | this.worldDb.saveChunkVersion( |
| 75 | result.chunkX, |
| 76 | result.chunkY, |
| 77 | revisionId, |
| 78 | result.data.open ? 1 : 0, |
| 79 | encodeChunkBlob(result.data) |
| 80 | ); |
| 81 | } |
| 82 | |
| 83 | if (diffEntries.length > 0) |
| 84 | { this.worldDb.appendChangelog(chunkX, chunkY, revisionId, editorAdminId, diffEntries); } |
| 85 | |
| 86 | return this.loadChunk(chunkX, chunkY); |
| 87 | } |
| 88 | |
| 89 | saveSettings (partial, editorAdminId) |
| 90 | { |
| 91 | const revisionId = this.worldDb.ensurePendingRevision(editorAdminId); |
| 92 | const current = this.worldDb.getSettings(revisionId); |
| 93 | const next = { ...current, ...partial }; |
| 94 | this.worldDb.saveSettings(revisionId, next); |
| 95 | return { revisionId, settings: next }; |
| 96 | } |
| 97 | |
| 98 | applyPendingRevision () |
| 99 | { |
| 100 | return this.worldDb.promotePendingRevision(); |
| 101 | } |
| 102 | |
| 103 | #loadForBake (chunkX, chunkY, revisionId, defaultGroundTypeId, savedX, savedY, savedData) |
| 104 | { |
| 105 | if (chunkX === savedX && chunkY === savedY) |
| 106 | { return savedData; } |
| 107 | |
| 108 | const row = this.worldDb.getChunkVersion(chunkX, chunkY, revisionId); |
| 109 | return row |
| 110 | ? decodeChunkBlob(row.data_blob, defaultGroundTypeId) |
| 111 | : createEmptyChunkData(defaultGroundTypeId); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | export function diffChunks (chunkX, chunkY, previous, next, previousAuthored) |
| 116 | { |
| 117 | const bullets = []; |
| 118 | const prevOpen = previousAuthored ? !!previous.open : false; |
| 119 | if (prevOpen !== !!next.open) |
| 120 | { bullets.push(next.open ? "Chunk opened." : "Chunk closed."); } |
| 121 | |
| 122 | for (let localY = 0; localY < CHUNK_SIZE; localY++) |
| 123 | { |
| 124 | for (let localX = 0; localX < CHUNK_SIZE; localX++) |
| 125 | { |
| 126 | const index = localY * CHUNK_SIZE + localX; |
| 127 | const tileX = localToTile(chunkX, localX); |
| 128 | const tileY = localToTile(chunkY, localY); |
| 129 | const prevGround = previous.grounds[index]; |
| 130 | const nextGround = next.grounds[index]; |
| 131 | if (prevGround !== nextGround) |
| 132 | { |
| 133 | bullets.push( |
| 134 | `Ground at ${tileX}, ${tileY} changed from ${nameGround(prevGround)} to ${nameGround(nextGround)}.` |
| 135 | ); |
| 136 | } |
| 137 | |
| 138 | const prevBlock = previous.blocks[index]; |
| 139 | const nextBlock = next.blocks[index]; |
| 140 | const prevId = prevBlock?.typeId ?? null; |
| 141 | const nextId = nextBlock?.typeId ?? null; |
| 142 | const prevConfig = JSON.stringify(prevBlock?.config ?? null); |
| 143 | const nextConfig = JSON.stringify(nextBlock?.config ?? null); |
| 144 | if (prevId !== nextId || prevConfig !== nextConfig) |
| 145 | { |
| 146 | if (prevId == null && nextId != null) |
| 147 | { bullets.push(`Block ${nameBlock(nextId)} placed at ${tileX}, ${tileY}.`); } |
| 148 | else if (prevId != null && nextId == null) |
| 149 | { bullets.push(`Block ${nameBlock(prevId)} removed at ${tileX}, ${tileY}.`); } |
| 150 | else |
| 151 | { bullets.push(`Block at ${tileX}, ${tileY} changed from ${nameBlock(prevId)} to ${nameBlock(nextId)}.`); } |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | return bullets; |
| 156 | } |
| 157 | |
| 158 | function nameGround (typeId) |
| 159 | { |
| 160 | return groundDefinition(typeId)?.displayName ?? `ground ${typeId}`; |
| 161 | } |
| 162 | |
| 163 | function nameBlock (typeId) |
| 164 | { |
| 165 | return blockDefinition(typeId)?.displayName ?? `block ${typeId}`; |
| 166 | } |