BlueGit

timfa questward

Files

questward / shared / world / dimensions.js

dimensions.js

dimensions.js 3.8 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; render resolves spring→summer, autumn→fall. */
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 | "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 || mode === DimensionSeasonMode.summer)
73 { return "summer"; }
74 return "winter";
75}
76
77/**
78 * Night darkness factor override for a dimension (null = use calendar/world hour).
79 * @returns {null | number} 0 = fully lit, 1 = fully dark ambient
80 */
81export function resolveDimensionDarknessFactor (lightingMode, calendarNightFactor)
82{
83 const mode = normalizeLightingMode(lightingMode);
84 if (mode === DimensionLightingMode.alwaysLit)
85 { return 0; }
86 if (mode === DimensionLightingMode.alwaysDark)
87 { return 1; }
88 return calendarNightFactor ?? 0;
89}
90
91export function defaultDimensionRecord (dimensionId, {
92 displayName = null,
93 lightingMode = DimensionLightingMode.dayNight,
94 seasonMode = DimensionSeasonMode.auto,
95 defaultGroundTypeId = 1,
96 deletedAt = null,
97 purgeAfter = null
98} = {})
99{
100 const id = normalizeDimensionId(dimensionId);
101 return {
102 dimensionId: id,
103 displayName: displayName != null ? String(displayName) : id,
104 lightingMode: normalizeLightingMode(lightingMode),
105 seasonMode: normalizeSeasonMode(seasonMode),
106 defaultGroundTypeId: Math.max(1, Number(defaultGroundTypeId) || 1) | 0,
107 deletedAt: deletedAt == null ? null : Number(deletedAt),
108 purgeAfter: purgeAfter == null ? null : Number(purgeAfter)
109 };
110}
111
112export function normalizeDimensionRecord (raw)
113{
114 if (!raw || typeof raw !== "object")
115 { return defaultDimensionRecord(OVERWORLD_ID); }
116 return defaultDimensionRecord(raw.dimensionId ?? raw.dimension_id ?? OVERWORLD_ID, {
117 displayName: raw.displayName ?? raw.display_name,
118 lightingMode: raw.lightingMode ?? raw.lighting_mode,
119 seasonMode: raw.seasonMode ?? raw.season_mode,
120 defaultGroundTypeId: raw.defaultGroundTypeId ?? raw.default_ground_type_id,
121 deletedAt: raw.deletedAt ?? raw.deleted_at,
122 purgeAfter: raw.purgeAfter ?? raw.purge_after
123 });
124}
125
126export function isDimensionUsable (record, now = Date.now())
127{
128 const dim = normalizeDimensionRecord(record);
129 if (dim.deletedAt != null)
130 { return false; }
131 if (dim.purgeAfter != null && dim.purgeAfter <= now)
132 { return false; }
133 return true;
134}