BlueGit

timfa questward

Files

questward / server / src / spells / lights.js

lights.js

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