BlueGit

timfa questward

Files

questward / server / src / spells / lights.js

lights.js

lights.js 6.2 KiB Download Raw
1import { Component } from "../ecs/components.js";
2import { tileKey } from "../world/coordinates.js";
3import { OVERWORLD_ID, normalizeDimensionId } from "../../../shared/world/dimensions.js";
4import
5 {
6 LIGHT_FADE_MS,
7 LIGHT_MAX_DURATION_MS,
8 LIGHT_OWNER_DISTANCE_CAP_MS,
9 SPELL_DARK_INTENSITY,
10 SPELL_LIGHT_INTENSITY,
11 SPELL_LIGHT_RANGE_TILES,
12 COMBAT_RANGE_TILES
13 } from "../../../shared/spells/constants.js";
14
15/**
16 * Authoritative temporary spell lights / darkness keyed by tile + dimension.
17 * @typedef {object} SpellLight
18 * @property {number} tileX
19 * @property {number} tileY
20 * @property {string} dimensionId
21 * @property {number} ownerEntityId
22 * @property {number} expiresAt
23 * @property {number} maxDurationMs
24 * @property {number} baseRange
25 * @property {number} baseIntensity
26 * @property {"light"|"dark"} kind
27 */
28
29export class SpellLightRegistry
30{
31 constructor()
32 {
33 /** @type {Map<string, SpellLight>} */
34 this.byTile = new Map();
35 this.nextLightId = 1;
36 }
37
38 getAt (tileX, tileY, dimensionId = OVERWORLD_ID)
39 {
40 const dim = normalizeDimensionId(dimensionId);
41 return this.byTile.get(tileKey(tileX, tileY, dim)) ?? null;
42 }
43
44 /** @returns {{ light: SpellLight, extended: boolean }} */
45 placeOrExtend ({
46 tileX,
47 tileY,
48 ownerEntityId,
49 durationMs,
50 baseRange = SPELL_LIGHT_RANGE_TILES,
51 baseIntensity = SPELL_LIGHT_INTENSITY,
52 kind = "light",
53 dimensionId = OVERWORLD_ID,
54 now = Date.now()
55 })
56 {
57 const dim = normalizeDimensionId(dimensionId);
58 const key = tileKey(tileX, tileY, dim);
59 const addMs = Math.max(0, Math.min(LIGHT_MAX_DURATION_MS, durationMs | 0));
60 const range = Math.max(0, Number(baseRange) || SPELL_LIGHT_RANGE_TILES);
61 const intensity = Math.max(0, Number(baseIntensity)
62 || (kind === "dark" ? SPELL_DARK_INTENSITY : SPELL_LIGHT_INTENSITY));
63 const existing = this.byTile.get(key);
64 if (existing && existing.kind === kind)
65 {
66 const remaining = Math.max(0, existing.expiresAt - now);
67 const next = Math.min(LIGHT_MAX_DURATION_MS, remaining + addMs);
68 existing.expiresAt = now + next;
69 existing.maxDurationMs = Math.max(existing.maxDurationMs, next);
70 existing.ownerEntityId = ownerEntityId;
71 existing.baseRange = Math.max(existing.baseRange ?? 0, range);
72 existing.baseIntensity = Math.max(existing.baseIntensity ?? 0, intensity);
73 return { light: existing, extended: true };
74 }
75 const light = {
76 lightId: this.nextLightId++,
77 tileX,
78 tileY,
79 dimensionId: dim,
80 ownerEntityId,
81 expiresAt: now + addMs,
82 maxDurationMs: addMs,
83 baseRange: range,
84 baseIntensity: intensity,
85 kind: kind === "dark" ? "dark" : "light"
86 };
87 this.byTile.set(key, light);
88 return { light, extended: false };
89 }
90
91 removeAt (tileX, tileY, dimensionId = OVERWORLD_ID)
92 {
93 const dim = normalizeDimensionId(dimensionId);
94 const key = tileKey(tileX, tileY, dim);
95 const existing = this.byTile.get(key);
96 if (!existing)
97 { return null; }
98 this.byTile.delete(key);
99 return existing;
100 }
101
102 /** Tick ownership distance penalty and expiry. Returns replication events. */
103 tick (world, now = Date.now())
104 {
105 const events = [];
106 for (const [, light] of [...this.byTile.entries()])
107 {
108 const dim = normalizeDimensionId(light.dimensionId ?? OVERWORLD_ID);
109 if (now >= light.expiresAt)
110 {
111 this.byTile.delete(tileKey(light.tileX, light.tileY, dim));
112 events.push({
113 type: light.kind === "dark" ? "spell-dark-remove" : "spell-light-remove",
114 tileX: light.tileX,
115 tileY: light.tileY,
116 dimensionId: dim
117 });
118 continue;
119 }
120 const ownerHandle = world.ecs.handleOf(light.ownerEntityId);
121 const ownerPos = ownerHandle != null ? world.ecs.get(ownerHandle, Component.position) : null;
122 if (ownerPos)
123 {
124 const ownerDim = normalizeDimensionId(ownerPos.dimensionId ?? OVERWORLD_ID);
125 if (ownerDim !== dim)
126 { continue; }
127 const dist = Math.max(
128 Math.abs(ownerPos.tileX - light.tileX),
129 Math.abs(ownerPos.tileY - light.tileY)
130 );
131 if (dist > COMBAT_RANGE_TILES)
132 {
133 const remaining = light.expiresAt - now;
134 const capped = Math.min(remaining, LIGHT_OWNER_DISTANCE_CAP_MS);
135 if (capped !== remaining)
136 {
137 light.expiresAt = now + capped;
138 const eventType = light.kind === "dark" ? "spell-dark-update" : "spell-light-update";
139 events.push({ type: eventType, ...publicSpellLight(light, now) });
140 }
141 }
142 }
143 }
144 return events;
145 }
146
147 listInBounds (minX, minY, maxX, maxY, now = Date.now(), dimensionId = null)
148 {
149 const dimFilter = dimensionId != null ? normalizeDimensionId(dimensionId) : null;
150 const out = [];
151 for (const light of this.byTile.values())
152 {
153 if (dimFilter != null
154 && normalizeDimensionId(light.dimensionId ?? OVERWORLD_ID) !== dimFilter)
155 { continue; }
156 if (light.tileX < minX || light.tileX > maxX || light.tileY < minY || light.tileY > maxY)
157 { continue; }
158 if (now >= light.expiresAt)
159 { continue; }
160 out.push(publicSpellLight(light, now));
161 }
162 return out;
163 }
164}
165
166export function publicSpellLight (light, now = Date.now())
167{
168 const remainingMs = Math.max(0, light.expiresAt - now);
169 const fadeT = remainingMs <= LIGHT_FADE_MS ? remainingMs / LIGHT_FADE_MS : 1;
170 const range = light.baseRange * fadeT;
171 const intensity = light.baseIntensity * fadeT;
172 return {
173 tileX: light.tileX,
174 tileY: light.tileY,
175 dimensionId: normalizeDimensionId(light.dimensionId ?? OVERWORLD_ID),
176 ownerEntityId: light.ownerEntityId,
177 remainingMs,
178 rangeTiles: range,
179 intensity,
180 expiresAt: light.expiresAt,
181 kind: light.kind === "dark" ? "dark" : "light"
182 };
183}
184
185export function spellLightsForDynamicLighting (registry, now = Date.now(), dimensionId = null)
186{
187 const lights = [];
188 const darks = [];
189 if (!registry)
190 { return { lights, darks }; }
191 const dimFilter = dimensionId != null ? normalizeDimensionId(dimensionId) : null;
192 for (const light of registry.byTile.values())
193 {
194 if (dimFilter != null
195 && normalizeDimensionId(light.dimensionId ?? OVERWORLD_ID) !== dimFilter)
196 { continue; }
197 if (now >= light.expiresAt)
198 { continue; }
199 const pub = publicSpellLight(light, now);
200 if (pub.intensity <= 0 || pub.rangeTiles <= 0)
201 { continue; }
202 const entry = {
203 tileX: light.tileX,
204 tileY: light.tileY,
205 range: pub.rangeTiles,
206 intensity: pub.intensity
207 };
208 if (light.kind === "dark")
209 { darks.push(entry); }
210 else
211 { lights.push(entry); }
212 }
213 return { lights, darks };
214}