BlueGit

timfa questward

Files

questward / shared / lighting / combatLight.js

combatLight.js

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