/** * World dimensions (overworld + interiors). * Spatial identity is { dimensionId, tileX, tileY }. */ export const OVERWORLD_ID = "overworld"; export const DimensionLightingMode = Object.freeze({ dayNight: "dayNight", alwaysLit: "alwaysLit", alwaysDark: "alwaysDark" }); /** Authoring values; autumn→fall for SeasonId; spring keeps its own art when present. */ export const DimensionSeasonMode = Object.freeze({ auto: "auto", summer: "summer", spring: "spring", autumn: "autumn", winter: "winter" }); export const DIMENSION_PURGE_MS = 7 * 24 * 60 * 60 * 1000; export function normalizeDimensionId (raw) { const id = String(raw ?? "").trim().toLowerCase(); if (!id) { return OVERWORLD_ID; } // Slug: letters, digits, underscore, hyphen const cleaned = id.replace(/[^a-z0-9_-]+/g, "-").replace(/^-+|-+$/g, ""); return cleaned || OVERWORLD_ID; } export function isOverworld (dimensionId) { return normalizeDimensionId(dimensionId) === OVERWORLD_ID; } export function normalizeLightingMode (raw) { const mode = String(raw ?? "").trim(); if (mode === DimensionLightingMode.alwaysLit || mode === DimensionLightingMode.alwaysDark) { return mode; } return DimensionLightingMode.dayNight; } export function normalizeSeasonMode (raw) { const mode = String(raw ?? "").trim(); if ( mode === DimensionSeasonMode.summer || mode === DimensionSeasonMode.spring || mode === DimensionSeasonMode.autumn || mode === DimensionSeasonMode.winter ) { return mode; } return DimensionSeasonMode.auto; } /** * Map dimension season mode to a SeasonId for sprite bake, or null for calendar auto. * @returns {null | "spring" | "summer" | "fall" | "winter"} */ export function resolveDimensionSeasonId (seasonMode) { const mode = normalizeSeasonMode(seasonMode); if (mode === DimensionSeasonMode.auto) { return null; } if (mode === DimensionSeasonMode.autumn) { return "fall"; } if (mode === DimensionSeasonMode.spring) { return "spring"; } if (mode === DimensionSeasonMode.summer) { return "summer"; } return "winter"; } /** * Night darkness factor override for a dimension (null = use calendar/world hour). * @returns {null | number} 0 = fully lit, 1 = fully dark ambient */ export function resolveDimensionDarknessFactor (lightingMode, calendarNightFactor) { const mode = normalizeLightingMode(lightingMode); if (mode === DimensionLightingMode.alwaysLit) { return 0; } if (mode === DimensionLightingMode.alwaysDark) { return 1; } return calendarNightFactor ?? 0; } export function defaultDimensionRecord (dimensionId, { displayName = null, lightingMode = DimensionLightingMode.dayNight, seasonMode = DimensionSeasonMode.auto, defaultGroundTypeId = 1, deletedAt = null, purgeAfter = null } = {}) { const id = normalizeDimensionId(dimensionId); return { dimensionId: id, displayName: displayName != null ? String(displayName) : id, lightingMode: normalizeLightingMode(lightingMode), seasonMode: normalizeSeasonMode(seasonMode), defaultGroundTypeId: Math.max(1, Number(defaultGroundTypeId) || 1) | 0, deletedAt: deletedAt == null ? null : Number(deletedAt), purgeAfter: purgeAfter == null ? null : Number(purgeAfter) }; } export function normalizeDimensionRecord (raw) { if (!raw || typeof raw !== "object") { return defaultDimensionRecord(OVERWORLD_ID); } return defaultDimensionRecord(raw.dimensionId ?? raw.dimension_id ?? OVERWORLD_ID, { displayName: raw.displayName ?? raw.display_name, lightingMode: raw.lightingMode ?? raw.lighting_mode, seasonMode: raw.seasonMode ?? raw.season_mode, defaultGroundTypeId: raw.defaultGroundTypeId ?? raw.default_ground_type_id, deletedAt: raw.deletedAt ?? raw.deleted_at, purgeAfter: raw.purgeAfter ?? raw.purge_after }); } export function isDimensionUsable (record, now = Date.now()) { const dim = normalizeDimensionRecord(record); if (dim.deletedAt != null) { return false; } if (dim.purgeAfter != null && dim.purgeAfter <= now) { return false; } return true; }