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, diffItemSeedValues, allSettingFields, itemSeedMapsEqual } from "../../../shared/definitions/worldSettings.js"; import { normalizeItemSeedValues } from "../../../shared/shop/pricing.js"; import { itemDefinition } from "../../../shared/definitions/items.js"; import { DEFAULT_WORLD_SETTINGS } from "../../../shared/protocol/constants.js"; import { normalizeStarterLoadout, starterLoadoutsEqual, diffStarterLoadout } from "../../../shared/items/starterLoadout.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, chunkX, chunkY) : 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, x, y).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, itemSeedValues: { ...(current.itemSeedValues && typeof current.itemSeedValues === "object" ? current.itemSeedValues : {}) } }; 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; } if (Object.prototype.hasOwnProperty.call(partial, "itemSeedValues")) { next.itemSeedValues = normalizeItemSeedValues(partial.itemSeedValues); } if (Object.prototype.hasOwnProperty.call(partial, "starterEquipment") || Object.prototype.hasOwnProperty.call(partial, "starterInventory")) { const loadout = normalizeStarterLoadout({ starterEquipment: Object.prototype.hasOwnProperty.call(partial, "starterEquipment") ? partial.starterEquipment : current.starterEquipment, starterInventory: Object.prototype.hasOwnProperty.call(partial, "starterInventory") ? partial.starterInventory : current.starterInventory }); next.starterEquipment = loadout.equipment; next.starterInventory = loadout.inventory; } const { bullets: scalarBullets, changed } = diffSettings(current, next); const seedBullets = Object.prototype.hasOwnProperty.call(partial, "itemSeedValues") ? diffItemSeedValues( normalizeItemSeedValues(current.itemSeedValues), next.itemSeedValues, (itemId) => itemDefinition(itemId)?.displayName ?? null ) : []; if (!itemSeedMapsEqual(current.itemSeedValues, next.itemSeedValues) && Object.prototype.hasOwnProperty.call(partial, "itemSeedValues")) { changed.itemSeedValues = next.itemSeedValues; } const starterChanged = !starterLoadoutsEqual(current, next) && (Object.prototype.hasOwnProperty.call(partial, "starterEquipment") || Object.prototype.hasOwnProperty.call(partial, "starterInventory")); if (starterChanged) { changed.starterEquipment = next.starterEquipment; changed.starterInventory = next.starterInventory; } const starterBullets = starterChanged ? diffStarterLoadout(current, next) : []; const bullets = [...scalarBullets, ...seedBullets, ...starterBullets]; if (Object.keys(changed).length === 0) { return { settings: current, changelog: this.worldDb.listSettingsChangelog(), changed: {} }; } const settings = { ...current, ...changed, itemSeedValues: changed.itemSeedValues ?? normalizeItemSeedValues(current.itemSeedValues) ?? { ...(DEFAULT_WORLD_SETTINGS.itemSeedValues ?? {}) }, starterEquipment: changed.starterEquipment ?? current.starterEquipment ?? DEFAULT_WORLD_SETTINGS.starterEquipment, starterInventory: changed.starterInventory ?? current.starterInventory ?? DEFAULT_WORLD_SETTINGS.starterInventory }; const normalized = normalizeStarterLoadout(settings); settings.starterEquipment = normalized.equipment; settings.starterInventory = normalized.inventory; 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, chunkX, chunkY) : 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"; }