BlueGit

timfa questward

Files

questward / shared / lighting / raycast.js

raycast.js

raycast.js 4.2 KiB Download Raw
1/** Shared lighting raycast and falloff — used by server bake and client dynamic lights. */
2
3import { LightOcclusion } from "../definitions/tiles.js";
4
5export function bresenhamTiles (x0, y0, x1, y1)
6{
7 const tiles = [];
8 let x = x0;
9 let y = y0;
10 const dx = Math.abs(x1 - x0);
11 const dy = Math.abs(y1 - y0);
12 const sx = x0 < x1 ? 1 : -1;
13 const sy = y0 < y1 ? 1 : -1;
14 let err = dx - dy;
15
16 while (true)
17 {
18 tiles.push({ x, y });
19 if (x === x1 && y === y1)
20 { break; }
21
22 const e2 = 2 * err;
23 if (e2 > -dy)
24 {
25 err -= dy;
26 x += sx;
27 }
28 if (e2 < dx)
29 {
30 err += dx;
31 y += sy;
32 }
33 }
34 return tiles;
35}
36
37/**
38 * Binary LOS for combat / legacy callers (`true` = clear).
39 * Destination may be blocked and still "reached" (facing surface).
40 */
41export function rayReaches (fromX, fromY, toX, toY, isClear)
42{
43 const ray = evaluateLightRay(fromX, fromY, toX, toY, (x, y) =>
44 (isClear(x, y) ? LightOcclusion.clear : LightOcclusion.solid));
45 return ray.reaches;
46}
47
48/**
49 * Light transmission along a Bresenham ray.
50 * Walks the line without allocating a tile array (hot path for dynamic lights).
51 * @param {(x: number, y: number) => number} occlusionAt
52 * {@link LightOcclusion.clear|partial|solid}
53 * @returns {{ reaches: boolean, extraDistance: number }}
54 * `extraDistance` is added to geometric hypot for falloff (partial tiles
55 * cost +1 each, i.e. that step falls off twice as fast).
56 */
57export function evaluateLightRay (fromX, fromY, toX, toY, occlusionAt)
58{
59 if (fromX === toX && fromY === toY)
60 {
61 return {
62 reaches: occlusionAt(toX, toY) !== LightOcclusion.solid,
63 extraDistance: 0
64 };
65 }
66
67 let x = fromX;
68 let y = fromY;
69 const dx = Math.abs(toX - fromX);
70 const dy = Math.abs(toY - fromY);
71 const sx = fromX < toX ? 1 : -1;
72 const sy = fromY < toY ? 1 : -1;
73 let err = dx - dy;
74 let extraDistance = 0;
75
76 // Only tiles strictly between source and target can occlude / add penalty.
77 while (x !== toX || y !== toY)
78 {
79 const e2 = 2 * err;
80 if (e2 > -dy)
81 {
82 err -= dy;
83 x += sx;
84 }
85 if (e2 < dx)
86 {
87 err += dx;
88 y += sy;
89 }
90 if (x === toX && y === toY)
91 { break; }
92 const level = occlusionAt(x, y) | 0;
93 if (level >= LightOcclusion.solid)
94 { return { reaches: false, extraDistance: 0 }; }
95 if (level === LightOcclusion.partial)
96 { extraDistance += 1; }
97 }
98 return { reaches: true, extraDistance };
99}
100
101export function falloffAtDistance (dist, range)
102{
103 if (range <= 0 || dist > range)
104 { return 0; }
105
106 const t = 1 - dist / range;
107 return t * t * (3 - 2 * t);
108}
109
110export function encodeLight (dist, range)
111{
112 const falloff = falloffAtDistance(dist, range);
113 if (falloff <= 0)
114 { return 0; }
115
116 return Math.max(1, Math.round(255 * falloff));
117}
118
119export function lightIntensity (encoded)
120{
121 return Math.min(1, Math.max(0, (encoded ?? 0) / 255));
122}
123
124/**
125 * Best dynamic light at a tile (0–1).
126 * @param {Array<{ tileX: number, tileY: number, range: number, intensity?: number }>} sources
127 * @param {(x: number, y: number) => number|boolean} occlusionAt
128 * LightOcclusion level, or legacy boolean (`true` = clear).
129 */
130export function dynamicLightAtTile (tileX, tileY, sources, occlusionAt)
131{
132 if (!sources?.length)
133 { return 0; }
134 const occlude = normalizeOcclusionFn(occlusionAt);
135 let best = 0;
136 for (const source of sources)
137 {
138 const range = source.range;
139 if (!(range > 0))
140 { continue; }
141 const geo = Math.hypot(tileX - source.tileX, tileY - source.tileY);
142 // Partial occlusion only *increases* distance — never shortens it.
143 if (geo > range)
144 { continue; }
145 const ray = evaluateLightRay(source.tileX, source.tileY, tileX, tileY, occlude);
146 if (!ray.reaches)
147 { continue; }
148 const dist = geo + ray.extraDistance;
149 if (dist > range)
150 { continue; }
151 best = Math.max(best, falloffAtDistance(dist, range) * (source.intensity ?? 1));
152 }
153 return best;
154}
155
156/** Best dynamic darkness contribution at a tile (0–1), same falloff as light. */
157export function dynamicDarknessAtTile (tileX, tileY, sources, occlusionAt)
158{
159 return dynamicLightAtTile(tileX, tileY, sources, occlusionAt);
160}
161
162function normalizeOcclusionFn (occlusionAt)
163{
164 return (x, y) =>
165 {
166 const value = occlusionAt(x, y);
167 if (value === true)
168 { return LightOcclusion.clear; }
169 if (value === false)
170 { return LightOcclusion.solid; }
171 return value | 0;
172 };
173}