BlueGit

timfa questward

Files

questward / shared / world / dimensions.js

dimensions.js

dimensions.js 3.9 KiB Download Raw
1/**
2 * World dimensions (overworld + interiors).
3 * Spatial identity is { dimensionId, tileX, tileY }.
4 */
5
6export const OVERWORLD_ID = "overworld";
7
8export const DimensionLightingMode = Object.freeze({
9 dayNight: "dayNight",
10 alwaysLit: "alwaysLit",
11 alwaysDark: "alwaysDark"
12});
13
14/** Authoring values; autumn→fall for SeasonId; spring keeps its own art when present. */
15export const DimensionSeasonMode = Object.freeze({
16 auto: "auto",
17 summer: "summer",
18 spring: "spring",
19 autumn: "autumn",
20 winter: "winter"
21});
22
23export const DIMENSION_PURGE_MS = 7 * 24 * 60 * 60 * 1000;
24
25export function normalizeDimensionId (raw)
26{
27 const id = String(raw ?? "").trim().toLowerCase();
28 if (!id)
29 { return OVERWORLD_ID; }
30 // Slug: letters, digits, underscore, hyphen
31 const cleaned = id.replace(/[^a-z0-9_-]+/g, "-").replace(/^-+|-+$/g, "");
32 return cleaned || OVERWORLD_ID;
33}
34
35export function isOverworld (dimensionId)
36{
37 return normalizeDimensionId(dimensionId) === OVERWORLD_ID;
38}
39
40export function normalizeLightingMode (raw)
41{
42 const mode = String(raw ?? "").trim();
43 if (mode === DimensionLightingMode.alwaysLit || mode === DimensionLightingMode.alwaysDark)
44 { return mode; }
45 return DimensionLightingMode.dayNight;
46}
47
48export function normalizeSeasonMode (raw)
49{
50 const mode = String(raw ?? "").trim();
51 if (
52 mode === DimensionSeasonMode.summer
53 || mode === DimensionSeasonMode.spring
54 || mode === DimensionSeasonMode.autumn
55 || mode === DimensionSeasonMode.winter
56 )
57 { return mode; }
58 return DimensionSeasonMode.auto;
59}
60
61/**
62 * Map dimension season mode to a SeasonId for sprite bake, or null for calendar auto.
63 * @returns {null | "spring" | "summer" | "fall" | "winter"}
64 */
65export function resolveDimensionSeasonId (seasonMode)
66{
67 const mode = normalizeSeasonMode(seasonMode);
68 if (mode === DimensionSeasonMode.auto)
69 { return null; }
70 if (mode === DimensionSeasonMode.autumn)
71 { return "fall"; }
72 if (mode === DimensionSeasonMode.spring)
73 { return "spring"; }
74 if (mode === DimensionSeasonMode.summer)
75 { return "summer"; }
76 return "winter";
77}
78
79/**
80 * Night darkness factor override for a dimension (null = use calendar/world hour).
81 * @returns {null | number} 0 = fully lit, 1 = fully dark ambient
82 */
83export function resolveDimensionDarknessFactor (lightingMode, calendarNightFactor)
84{
85 const mode = normalizeLightingMode(lightingMode);
86 if (mode === DimensionLightingMode.alwaysLit)
87 { return 0; }
88 if (mode === DimensionLightingMode.alwaysDark)
89 { return 1; }
90 return calendarNightFactor ?? 0;
91}
92
93export function defaultDimensionRecord (dimensionId, {
94 displayName = null,
95 lightingMode = DimensionLightingMode.dayNight,
96 seasonMode = DimensionSeasonMode.auto,
97 defaultGroundTypeId = 1,
98 deletedAt = null,
99 purgeAfter = null
100} = {})
101{
102 const id = normalizeDimensionId(dimensionId);
103 return {
104 dimensionId: id,
105 displayName: displayName != null ? String(displayName) : id,
106 lightingMode: normalizeLightingMode(lightingMode),
107 seasonMode: normalizeSeasonMode(seasonMode),
108 defaultGroundTypeId: Math.max(1, Number(defaultGroundTypeId) || 1) | 0,
109 deletedAt: deletedAt == null ? null : Number(deletedAt),
110 purgeAfter: purgeAfter == null ? null : Number(purgeAfter)
111 };
112}
113
114export function normalizeDimensionRecord (raw)
115{
116 if (!raw || typeof raw !== "object")
117 { return defaultDimensionRecord(OVERWORLD_ID); }
118 return defaultDimensionRecord(raw.dimensionId ?? raw.dimension_id ?? OVERWORLD_ID, {
119 displayName: raw.displayName ?? raw.display_name,
120 lightingMode: raw.lightingMode ?? raw.lighting_mode,
121 seasonMode: raw.seasonMode ?? raw.season_mode,
122 defaultGroundTypeId: raw.defaultGroundTypeId ?? raw.default_ground_type_id,
123 deletedAt: raw.deletedAt ?? raw.deleted_at,
124 purgeAfter: raw.purgeAfter ?? raw.purge_after
125 });
126}
127
128export function isDimensionUsable (record, now = Date.now())
129{
130 const dim = normalizeDimensionRecord(record);
131 if (dim.deletedAt != null)
132 { return false; }
133 if (dim.purgeAfter != null && dim.purgeAfter <= now)
134 { return false; }
135 return true;
136}