import { groundDefinition, blockDefinition } from "../../../shared/definitions/tiles.js"; import { CHUNK_SIZE } from "../../../shared/protocol/constants.js"; import { localToTile } from "../world/coordinates.js"; import { cloneChunkData, createEmptyChunkData, decodeChunkBlob, encodeChunkBlob } from "../world/chunkData.js"; import { BLOCK_DEFINITIONS } from "../../../shared/definitions/tiles.js"; import { bakeLightMaps, maxDefinitionLightRange } from "../lighting/baker.js"; export class EditorService { constructor (worldDb) { this.worldDb = worldDb; } loadChunk (chunkX, chunkY) { const revisionId = this.worldDb.getEditorRevisionId(); const settings = this.worldDb.getSettings(revisionId); const row = this.worldDb.getChunkVersion(chunkX, chunkY, revisionId); const data = row ? decodeChunkBlob(row.data_blob, settings.defaultGroundTypeId) : createEmptyChunkData(settings.defaultGroundTypeId); return { chunkX, chunkY, revisionId, pendingRevisionId: this.worldDb.getPendingRevisionId(), activeRevisionId: this.worldDb.getActiveRevisionId(), authored: !!row, settings, data, changelog: this.worldDb.listChangelog(chunkX, chunkY) }; } saveChunk (chunkX, chunkY, draft, editorAdminId) { const previous = this.loadChunk(chunkX, chunkY); const next = cloneChunkData(previous.data); next.open = !!draft.open; if (Array.isArray(draft.grounds)) { next.grounds = draft.grounds.slice(); } if (Array.isArray(draft.blocks)) { next.blocks = draft.blocks.map((block) => (block ? { typeId: block.typeId, config: block.config ?? null } : null)); } const diffEntries = diffChunks(chunkX, chunkY, previous.data, next, previous.authored); const revisionId = this.worldDb.ensurePendingRevision(editorAdminId); const settings = this.worldDb.getSettings(revisionId); const baked = bakeLightMaps( chunkX, chunkY, (x, y) => this.#loadForBake(x, y, revisionId, settings.defaultGroundTypeId, chunkX, chunkY, next), maxDefinitionLightRange(BLOCK_DEFINITIONS) ); for (const result of baked) { const shouldStore = result.chunkX === chunkX && result.chunkY === chunkY || this.worldDb.getChunkVersion(result.chunkX, result.chunkY, revisionId) || result.data.light.some((value) => value > 0); if (!shouldStore) { continue; } this.worldDb.saveChunkVersion( result.chunkX, result.chunkY, revisionId, result.data.open ? 1 : 0, encodeChunkBlob(result.data) ); } if (diffEntries.length > 0) { this.worldDb.appendChangelog(chunkX, chunkY, revisionId, editorAdminId, diffEntries); } return this.loadChunk(chunkX, chunkY); } saveSettings (partial, editorAdminId) { const revisionId = this.worldDb.ensurePendingRevision(editorAdminId); const current = this.worldDb.getSettings(revisionId); const next = { ...current, ...partial }; this.worldDb.saveSettings(revisionId, next); return { revisionId, settings: next }; } applyPendingRevision () { return this.worldDb.promotePendingRevision(); } #loadForBake (chunkX, chunkY, revisionId, defaultGroundTypeId, savedX, savedY, savedData) { if (chunkX === savedX && chunkY === savedY) { return savedData; } const row = this.worldDb.getChunkVersion(chunkX, chunkY, revisionId); return row ? decodeChunkBlob(row.data_blob, defaultGroundTypeId) : createEmptyChunkData(defaultGroundTypeId); } } export function diffChunks (chunkX, chunkY, previous, next, previousAuthored) { const bullets = []; const prevOpen = previousAuthored ? !!previous.open : false; if (prevOpen !== !!next.open) { bullets.push(next.open ? "Chunk opened." : "Chunk closed."); } for (let localY = 0; localY < CHUNK_SIZE; localY++) { for (let localX = 0; localX < CHUNK_SIZE; localX++) { const index = localY * CHUNK_SIZE + localX; const tileX = localToTile(chunkX, localX); const tileY = localToTile(chunkY, localY); const prevGround = previous.grounds[index]; const nextGround = next.grounds[index]; if (prevGround !== nextGround) { bullets.push( `Ground at ${tileX}, ${tileY} changed from ${nameGround(prevGround)} to ${nameGround(nextGround)}.` ); } const prevBlock = previous.blocks[index]; const nextBlock = next.blocks[index]; const prevId = prevBlock?.typeId ?? null; const nextId = nextBlock?.typeId ?? null; const prevConfig = JSON.stringify(prevBlock?.config ?? null); const nextConfig = JSON.stringify(nextBlock?.config ?? null); if (prevId !== nextId || prevConfig !== nextConfig) { if (prevId == null && nextId != null) { bullets.push(`Block ${nameBlock(nextId)} placed at ${tileX}, ${tileY}.`); } else if (prevId != null && nextId == null) { bullets.push(`Block ${nameBlock(prevId)} removed at ${tileX}, ${tileY}.`); } else { bullets.push(`Block at ${tileX}, ${tileY} changed from ${nameBlock(prevId)} to ${nameBlock(nextId)}.`); } } } } return bullets; } function nameGround (typeId) { return groundDefinition(typeId)?.displayName ?? `ground ${typeId}`; } function nameBlock (typeId) { return blockDefinition(typeId)?.displayName ?? `block ${typeId}`; }