BlueGit

timfa questward

Files

questward / shared / lighting / combatLight.js

combatLight.js

combatLight.js 3.1 KiB Download Raw
1import { CHUNK_SIZE } from "../protocol/constants.js";
2import { lightIntensity, falloffAtDistance } from "./raycast.js";
3import { equippedLightRadiusTiles } from "../definitions/items.js";
4import { statusExtraLightRadius } from "../statusEffects/rules.js";
5import { mobDefinition } from "../definitions/mobs.js";
6
7/** Night ambient darkness 0–1 (matches client `nightFactor`). */
8export function nightFactor (hour, nightDarkness = 0.72)
9{
10 const peak = Math.min(1, Math.max(0, nightDarkness));
11 if (hour >= 8 && hour <= 16)
12 { return 0; }
13 if (hour >= 22 || hour <= 5)
14 { return peak; }
15 if (hour < 8)
16 { return lerp(peak, 0, (hour - 5) / 3); }
17 return lerp(0, peak, (hour - 16) / 6);
18}
19
20function lerp (a, b, t)
21{
22 return a + (b - a) * Math.min(1, Math.max(0, t));
23}
24
25function chebyshev (ax, ay, bx, by)
26{
27 return Math.max(Math.abs(ax - bx), Math.abs(ay - by));
28}
29
30/** Baked static light 0–1 at a tile (0 when unlit / unbaked). */
31export function bakedTileLight01 (lightArray, index)
32{
33 if (!lightArray || index < 0 || index >= lightArray.length)
34 { return 0; }
35 return lightIntensity(lightArray[index] ?? 0);
36}
37
38/**
39 * Combined target-tile lighting 0–1 for combat
40 * (static + entity/spell lights, time of day, then spell darkness).
41 * Dark subtracts from perceived light so it works in daylight as well as at night.
42 * Matches client shade: min(1, ambientDark×(1−lights) + spellDark).
43 */
44export function tileCombatLight01 ({
45 bakedLight01 = 0,
46 entityLight01 = 0,
47 spellLight01 = 0,
48 spellDark01 = 0,
49 worldHour = 12,
50 nightDarkness = 0.72
51} = {})
52{
53 const lights = Math.max(
54 0,
55 Math.min(1, Math.max(bakedLight01, entityLight01, spellLight01))
56 );
57 const ambientDark = nightFactor(worldHour, nightDarkness);
58 const dark = Math.max(0, Math.min(1, Number(spellDark01) || 0));
59 const shade = Math.min(1, ambientDark * (1 - lights) + dark);
60 return 1 - shade;
61}
62
63/** Best dynamic entity light reaching a tile (0–1). */
64export function entityLightAtTile (ecs, Component, tileX, tileY, now = Date.now())
65{
66 let best = 0;
67 for (const entity of ecs.query(Component.position))
68 {
69 const radius = entityLightRadiusTiles(ecs, entity.handle, Component, now);
70 if (radius <= 0)
71 { continue; }
72 const dist = chebyshev(entity.position.tileX, entity.position.tileY, tileX, tileY);
73 if (dist > radius)
74 { continue; }
75 best = Math.max(best, falloffAtDistance(dist, radius));
76 }
77 return best;
78}
79
80function entityLightRadiusTiles (ecs, handle, Component, now)
81{
82 const equipment = ecs.get(handle, Component.equipment);
83 const gear = equippedLightRadiusTiles(equipment);
84 const status = statusExtraLightRadius(ecs.get(handle, Component.statusEffects), now);
85 const creature = ecs.get(handle, Component.creature);
86 const creatureLight = creature?.lightRadiusTiles
87 ?? (creature ? (mobDefinition(creature.mobTypeId)?.lightRadiusTiles ?? 0) : 0);
88 return Math.max(gear, status, creatureLight);
89}
90
91export function tileIndexLocal (tileX, tileY, chunkSize = CHUNK_SIZE)
92{
93 const localX = ((tileX % chunkSize) + chunkSize) % chunkSize;
94 const localY = ((tileY % chunkSize) + chunkSize) % chunkSize;
95 return localY * chunkSize + localX;
96}