import { Component } from "../ecs/components.js"; import { tileKey } from "../world/coordinates.js"; import { LIGHT_FADE_MS, LIGHT_MAX_DURATION_MS, LIGHT_OWNER_DISTANCE_CAP_MS, SPELL_LIGHT_INTENSITY, SPELL_LIGHT_RANGE_TILES, COMBAT_RANGE_TILES } from "../../../shared/spells/constants.js"; /** * Authoritative temporary spell lights keyed by tile. * @typedef {object} SpellLight * @property {number} tileX * @property {number} tileY * @property {number} ownerEntityId * @property {number} expiresAt * @property {number} maxDurationMs * @property {number} baseRange * @property {number} baseIntensity */ export class SpellLightRegistry { constructor () { /** @type {Map} */ this.byTile = new Map(); this.nextLightId = 1; } getAt (tileX, tileY) { return this.byTile.get(tileKey(tileX, tileY)) ?? null; } /** @returns {{ light: SpellLight, extended: boolean }} */ placeOrExtend ({ tileX, tileY, ownerEntityId, durationMs, now = Date.now() }) { const key = tileKey(tileX, tileY); const addMs = Math.max(0, Math.min(LIGHT_MAX_DURATION_MS, durationMs | 0)); const existing = this.byTile.get(key); if (existing) { const remaining = Math.max(0, existing.expiresAt - now); const next = Math.min(LIGHT_MAX_DURATION_MS, remaining + addMs); existing.expiresAt = now + next; existing.maxDurationMs = Math.max(existing.maxDurationMs, next); existing.ownerEntityId = ownerEntityId; return { light: existing, extended: true }; } const light = { lightId: this.nextLightId++, tileX, tileY, ownerEntityId, expiresAt: now + addMs, maxDurationMs: addMs, baseRange: SPELL_LIGHT_RANGE_TILES, baseIntensity: SPELL_LIGHT_INTENSITY }; this.byTile.set(key, light); return { light, extended: false }; } removeAt (tileX, tileY) { const key = tileKey(tileX, tileY); const existing = this.byTile.get(key); if (!existing) { return null; } this.byTile.delete(key); return existing; } /** Tick ownership distance penalty and expiry. Returns replication events. */ tick (world, now = Date.now()) { const events = []; for (const [, light] of [...this.byTile.entries()]) { if (now >= light.expiresAt) { this.byTile.delete(tileKey(light.tileX, light.tileY)); events.push({ type: "spell-light-remove", tileX: light.tileX, tileY: light.tileY }); continue; } const ownerHandle = world.ecs.handleOf(light.ownerEntityId); const ownerPos = ownerHandle != null ? world.ecs.get(ownerHandle, Component.position) : null; if (ownerPos) { const dist = Math.max( Math.abs(ownerPos.tileX - light.tileX), Math.abs(ownerPos.tileY - light.tileY) ); if (dist > COMBAT_RANGE_TILES) { const remaining = light.expiresAt - now; const capped = Math.min( remaining, LIGHT_OWNER_DISTANCE_CAP_MS); if (capped !== remaining) { light.expiresAt = now + capped; events.push({ type: "spell-light-update", ...publicSpellLight(light, now) }); } } } } return events; } listInBounds (minX, minY, maxX, maxY, now = Date.now()) { const out = []; for (const light of this.byTile.values()) { if (light.tileX < minX || light.tileX > maxX || light.tileY < minY || light.tileY > maxY) { continue; } if (now >= light.expiresAt) { continue; } out.push(publicSpellLight(light, now)); } return out; } } export function publicSpellLight (light, now = Date.now()) { const remainingMs = Math.max(0, light.expiresAt - now); const fadeT = remainingMs <= LIGHT_FADE_MS ? remainingMs / LIGHT_FADE_MS : 1; const range = light.baseRange * fadeT; const intensity = light.baseIntensity * fadeT; return { tileX: light.tileX, tileY: light.tileY, ownerEntityId: light.ownerEntityId, remainingMs, rangeTiles: range, intensity, expiresAt: light.expiresAt }; } export function spellLightsForDynamicLighting (registry, now = Date.now()) { const out = []; for (const light of registry.byTile.values()) { if (now >= light.expiresAt) { continue; } const pub = publicSpellLight(light, now); if (pub.intensity <= 0 || pub.rangeTiles <= 0) { continue; } out.push({ tileX: light.tileX, tileY: light.tileY, range: pub.rangeTiles, intensity: pub.intensity }); } return out; }