import { CHUNK_SIZE } from "../protocol/constants.js"; import { lightIntensity, falloffAtDistance } from "./raycast.js"; import { equippedLightRadiusTiles } from "../definitions/items.js"; import { statusExtraLightRadius } from "../statusEffects/rules.js"; /** Night ambient darkness 0–1 (matches client `nightFactor`). */ export function nightFactor (hour, nightDarkness = 0.72) { const peak = Math.min(1, Math.max(0, nightDarkness)); if (hour >= 8 && hour <= 16) { return 0; } if (hour >= 22 || hour <= 5) { return peak; } if (hour < 8) { return lerp(peak, 0, (hour - 5) / 3); } return lerp(0, peak, (hour - 16) / 6); } function lerp (a, b, t) { return a + (b - a) * Math.min(1, Math.max(0, t)); } function chebyshev (ax, ay, bx, by) { return Math.max(Math.abs(ax - bx), Math.abs(ay - by)); } /** Baked static light 0–1 at a tile (0 when unlit / unbaked). */ export function bakedTileLight01 (lightArray, index) { if (!lightArray || index < 0 || index >= lightArray.length) { return 0; } return lightIntensity(lightArray[index] ?? 0); } /** Combined target-tile lighting 0–1 for combat (static + entity lights + time of day). */ export function tileCombatLight01 ({ bakedLight01 = 0, entityLight01 = 0, worldHour = 12, nightDarkness = 0.72 } = {}) { const localLit = Math.min(1, Math.max(bakedLight01, entityLight01)); const ambientDark = nightFactor(worldHour, nightDarkness); return 1 - ambientDark * (1 - localLit); } /** Best dynamic entity light reaching a tile (0–1). */ export function entityLightAtTile (ecs, Component, tileX, tileY, now = Date.now()) { let best = 0; for (const entity of ecs.query(Component.position)) { const radius = entityLightRadiusTiles(ecs, entity.handle, Component, now); if (radius <= 0) { continue; } const dist = chebyshev(entity.position.tileX, entity.position.tileY, tileX, tileY); if (dist > radius) { continue; } best = Math.max(best, falloffAtDistance(dist, radius)); } return best; } function entityLightRadiusTiles (ecs, handle, Component, now) { const equipment = ecs.get(handle, Component.equipment); const gear = equippedLightRadiusTiles(equipment); const status = statusExtraLightRadius(ecs.get(handle, Component.statusEffects), now); return Math.max(gear, status); } export function tileIndexLocal (tileX, tileY, chunkSize = CHUNK_SIZE) { const localX = ((tileX % chunkSize) + chunkSize) % chunkSize; const localY = ((tileY % chunkSize) + chunkSize) % chunkSize; return localY * chunkSize + localX; }