BlueGit

timfa questward

Files

questward / shared / graphics / settings.js

settings.js

settings.js 2.9 KiB Download Raw
1/** Client graphics preferences (persisted in localStorage). */
2
3export const ViewDistance = Object.freeze({
4 long: "long",
5 short: "short"
6});
7
8export const LightingMode = Object.freeze({
9 soft: "soft",
10 simple: "simple"
11});
12
13export const BloomMode = Object.freeze({
14 on: "on",
15 off: "off"
16});
17
18/** Soft edge blend between blendable ground tiles (grass, dirt, …). */
19export const SoftTerrainMode = Object.freeze({
20 on: "on",
21 off: "off"
22});
23
24/**
25 * Wet sand shoreline width (% of the tile facing liquid water).
26 * Stored as percent strings for the settings UI.
27 */
28export const WetSandBand = Object.freeze({
29 off: "0",
30 narrow: "30",
31 medium: "60",
32 wide: "100"
33});
34
35/** Auto walk/run by path distance (client-only; ignored while following). */
36export const AutorunMode = Object.freeze({
37 on: "on",
38 off: "off"
39});
40
41export const DEFAULT_GRAPHICS_SETTINGS = Object.freeze({
42 viewDistance: ViewDistance.long,
43 lighting: LightingMode.soft,
44 bloom: BloomMode.on,
45 softTerrain: SoftTerrainMode.on,
46 wetSand: WetSandBand.medium,
47 autorun: AutorunMode.off
48});
49
50const WET_SAND_BAND_VALUES = new Set(Object.values(WetSandBand));
51
52const STORAGE_KEY = "questward-graphics-settings";
53
54/** Map a wetSand settings value to a 0–1 tile fraction. */
55export function wetSandBandFraction (value)
56{
57 const raw = String(value ?? WetSandBand.medium);
58 const pct = Number(raw);
59 if (!Number.isFinite(pct) || pct <= 0)
60 { return 0; }
61 return Math.min(1, pct / 100);
62}
63
64export function normalizeGraphicsSettings (value)
65{
66 const raw = value && typeof value === "object" ? value : {};
67 const viewDistance = raw.viewDistance === ViewDistance.short
68 ? ViewDistance.short
69 : ViewDistance.long;
70 const lighting = raw.lighting === LightingMode.simple
71 ? LightingMode.simple
72 : LightingMode.soft;
73 const bloom = raw.bloom === BloomMode.off
74 ? BloomMode.off
75 : BloomMode.on;
76 const softTerrain = raw.softTerrain === SoftTerrainMode.off
77 ? SoftTerrainMode.off
78 : SoftTerrainMode.on;
79 const wetSand = WET_SAND_BAND_VALUES.has(String(raw.wetSand))
80 ? String(raw.wetSand)
81 : WetSandBand.medium;
82 const autorun = raw.autorun === AutorunMode.on
83 ? AutorunMode.on
84 : AutorunMode.off;
85 return { viewDistance, lighting, bloom, softTerrain, wetSand, autorun };
86}
87
88export function loadGraphicsSettings ()
89{
90 try
91 {
92 const text = localStorage.getItem(STORAGE_KEY);
93 if (!text)
94 { return { ...DEFAULT_GRAPHICS_SETTINGS }; }
95 return normalizeGraphicsSettings(JSON.parse(text));
96 }
97 catch
98 {
99 return { ...DEFAULT_GRAPHICS_SETTINGS };
100 }
101}
102
103export function saveGraphicsSettings (settings)
104{
105 const next = normalizeGraphicsSettings(settings);
106 try
107 { localStorage.setItem(STORAGE_KEY, JSON.stringify(next)); }
108 catch
109 { /* ignore quota / private mode */ }
110 return next;
111}
112
113/** Map-ring Chebyshev radius for chunk subscriptions (entities always use VISIBLE_CHUNK_RADIUS). */
114export function staticChunkRadiusForViewDistance (viewDistance, longRadius, shortRadius)
115{
116 return viewDistance === ViewDistance.short ? shortRadius : longRadius;
117}