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