BlueGit

timfa questward

Files

questward / shared / lighting / combatLight.js

combatLight.js

combatLight.js 2.9 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 − spell darkness + time of day).
41 * Matches client shade: ambientDark × (1 − max(0, max(lights) − dark)).
42 */
43export function tileCombatLight01 ({
44 bakedLight01 = 0,
45 entityLight01 = 0,
46 spellLight01 = 0,
47 spellDark01 = 0,
48 worldHour = 12,
49 nightDarkness = 0.72
50} = {})
51{
52 const localLit = Math.max(
53 0,
54 Math.min(1, Math.max(bakedLight01, entityLight01, spellLight01) - (Number(spellDark01) || 0))
55 );
56 const ambientDark = nightFactor(worldHour, nightDarkness);
57 return 1 - ambientDark * (1 - localLit);
58}
59
60/** Best dynamic entity light reaching a tile (0–1). */
61export function entityLightAtTile (ecs, Component, tileX, tileY, now = Date.now())
62{
63 let best = 0;
64 for (const entity of ecs.query(Component.position))
65 {
66 const radius = entityLightRadiusTiles(ecs, entity.handle, Component, now);
67 if (radius <= 0)
68 { continue; }
69 const dist = chebyshev(entity.position.tileX, entity.position.tileY, tileX, tileY);
70 if (dist > radius)
71 { continue; }
72 best = Math.max(best, falloffAtDistance(dist, radius));
73 }
74 return best;
75}
76
77function entityLightRadiusTiles (ecs, handle, Component, now)
78{
79 const equipment = ecs.get(handle, Component.equipment);
80 const gear = equippedLightRadiusTiles(equipment);
81 const status = statusExtraLightRadius(ecs.get(handle, Component.statusEffects), now);
82 const creature = ecs.get(handle, Component.creature);
83 const creatureLight = creature?.lightRadiusTiles
84 ?? (creature ? (mobDefinition(creature.mobTypeId)?.lightRadiusTiles ?? 0) : 0);
85 return Math.max(gear, status, creatureLight);
86}
87
88export function tileIndexLocal (tileX, tileY, chunkSize = CHUNK_SIZE)
89{
90 const localX = ((tileX % chunkSize) + chunkSize) % chunkSize;
91 const localY = ((tileY % chunkSize) + chunkSize) % chunkSize;
92 return localY * chunkSize + localX;
93}