import { groundDefinition, blockDefinition, normalizeGround, normalizeBlock } from "../../../shared/definitions/tiles.js"; import { CHUNK_SIZE } from "../../../shared/protocol/constants.js"; import { localToTile } from "../world/coordinates.js"; import { cloneChunkData, createEmptyChunkData, decodeChunkBlob, encodeChunkBlob, normalizeSpawner } from "../world/chunkData.js"; import { BLOCK_DEFINITIONS } from "../../../shared/definitions/tiles.js"; import { bakeLightMaps, maxDefinitionLightRange } from "../lighting/baker.js"; import { closedNeighborEdges } from "../world/chunkFlags.js"; import { coerceSettingValue, diffSettings, allSettingFields } from "../../../shared/definitions/worldSettings.js"; export class EditorService { constructor (worldDb) { this.worldDb = worldDb; } loadChunk (chunkX, chunkY) { const revisionId = this.worldDb.getEditorRevisionId(); const settings = this.worldDb.getLiveSettings(); const row = this.worldDb.getChunkVersion(chunkX, chunkY, revisionId); const data = row ? decodeChunkBlob(row.data_blob, settings.defaultGroundTypeId) : createEmptyChunkData(settings.defaultGroundTypeId); const isOpen = (x, y) => { const neighbor = this.worldDb.getChunkVersion(x, y, revisionId); if (!neighbor) { return false; } return !!decodeChunkBlob(neighbor.data_blob, settings.defaultGroundTypeId).open; }; return { chunkX, chunkY, revisionId, pendingRevisionId: this.worldDb.getPendingRevisionId(), activeRevisionId: this.worldDb.getActiveRevisionId(), authored: !!row, settings, data, closedEdges: closedNeighborEdges(chunkX, chunkY, isOpen), changelog: this.worldDb.listChangelog(chunkX, chunkY) }; } saveChunk (chunkX, chunkY, draft, editorAdminId) { const previous = this.loadChunk(chunkX, chunkY); const next = cloneChunkData(previous.data); const settings = previous.settings ?? this.worldDb.getLiveSettings(); next.open = !!draft.open; if (Array.isArray(draft.grounds)) { next.grounds = draft.grounds.map((ground) => normalizeGround(ground, settings.defaultGroundTypeId)); } if (Array.isArray(draft.blocks)) { next.blocks = draft.blocks.map((block) => normalizeBlock(block)); } if (Array.isArray(draft.spawners)) { next.spawners = draft.spawners.map((entry) => normalizeSpawner(entry)).filter(Boolean); } const diffEntries = diffChunks(chunkX, chunkY, previous.data, next, previous.authored); const revisionId = this.worldDb.ensurePendingRevision(editorAdminId); 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); } loadSettings () { return { settings: this.worldDb.getLiveSettings(), changelog: this.worldDb.listSettingsChangelog() }; } saveSettings (partial, editorAdminId) { const current = this.worldDb.getLiveSettings(); const next = { ...current }; for (const field of allSettingFields()) { if (!Object.prototype.hasOwnProperty.call(partial, field.key)) { continue; } const value = coerceSettingValue(field, partial[field.key]); if (value == null) { continue; } next[field.key] = value; } const { bullets, changed } = diffSettings(current, next); if (Object.keys(changed).length === 0) { return { settings: current, changelog: this.worldDb.listSettingsChangelog(), changed: {} }; } const settings = { ...current, ...changed }; this.worldDb.saveLiveSettings(settings, editorAdminId); this.worldDb.appendSettingsChangelog(editorAdminId, bullets); return { settings, changelog: this.worldDb.listSettingsChangelog(), changed, diffEntries: bullets }; } applyPendingRevision () { return this.worldDb.promotePendingRevision(); } revisionStatus () { return { activeRevisionId: this.worldDb.getActiveRevisionId(), pendingRevisionId: this.worldDb.getPendingRevisionId() }; } #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 = normalizeGround(previous.grounds[index]); const nextGround = normalizeGround(next.grounds[index]); if (prevGround.typeId !== nextGround.typeId) { bullets.push( `Ground at ${tileX}, ${tileY} changed from ${nameGround(prevGround.typeId)} to ${nameGround(nextGround.typeId)}.` ); } else if ((prevGround.state ?? "") !== (nextGround.state ?? "")) { bullets.push( `Ground ${nameGround(nextGround.typeId)} at ${tileX}, ${tileY} state changed from ${stateLabel(prevGround.state)} to ${stateLabel(nextGround.state)}.` ); } const prevBlock = normalizeBlock(previous.blocks[index]); const nextBlock = normalizeBlock(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)}.`); } } else if (prevBlock && nextBlock && (prevBlock.state ?? "") !== (nextBlock.state ?? "")) { bullets.push( `Block ${nameBlock(nextId)} at ${tileX}, ${tileY} state changed from ${stateLabel(prevBlock.state)} to ${stateLabel(nextBlock.state)}.` ); } } } const prevSpawners = indexSpawners(previous.spawners); const nextSpawners = indexSpawners(next.spawners); for (const key of new Set([...prevSpawners.keys(), ...nextSpawners.keys()])) { const prev = prevSpawners.get(key); const cur = nextSpawners.get(key); const [localX, localY] = key.split(",").map(Number); const tileX = localToTile(chunkX, localX); const tileY = localToTile(chunkY, localY); const prevConfig = JSON.stringify(prev?.config ?? null); const nextConfig = JSON.stringify(cur?.config ?? null); if (!prev && cur) { bullets.push(`Spawner (${cur.config.mobTypeId}) placed at ${tileX}, ${tileY}.`); } else if (prev && !cur) { bullets.push(`Spawner removed at ${tileX}, ${tileY}.`); } else if (prevConfig !== nextConfig) { bullets.push(`Spawner at ${tileX}, ${tileY} config changed.`); } } return bullets; } function indexSpawners (spawners) { const map = new Map(); for (const entry of spawners ?? []) { map.set(`${entry.localX},${entry.localY}`, entry); } return map; } function nameGround (typeId) { return groundDefinition(typeId)?.displayName ?? `ground ${typeId}`; } function nameBlock (typeId) { return blockDefinition(typeId)?.displayName ?? `block ${typeId}`; } function stateLabel (state) { return state || "default"; }