BlueGit

timfa questward

Files

questward / shared / lighting / enclosed.js

enclosed.js

enclosed.js 6.6 KiB Download Raw
1import { CHUNK_SIZE } from "../protocol/constants.js";
2import {
3 BlockType,
4 LightOcclusion,
5 blockDefinition,
6 isBlockPassable,
7 isGroundPassable,
8 normalizeGround
9} from "../definitions/tiles.js";
10import { tileLightOcclusionAt } from "./chunkOcclusion.js";
11
12/** Chebyshev chunk radius for enclosed bake window (3×3 chunks). */
13export const ENCLOSED_BAKE_CHUNK_RADIUS = 1;
14
15/**
16 * Door tiles are always enclosure boundaries (open or closed).
17 * Uses BlockType.door / def.door — not light-occlusion-of-open-passage.
18 */
19export function isEnclosedDoorBlock (block)
20{
21 if (!block)
22 { return false; }
23 if ((block.typeId | 0) === BlockType.door)
24 { return true; }
25 return !!blockDefinition(block.typeId)?.door;
26}
27
28/**
29 * Boundary = (solid LoS ∧ impassable) ∨ door.
30 * Water (impassable walk, clear LoS) is NOT boundary → interior candidate.
31 * Doors ignore open/closed passability; they are always boundary.
32 */
33export function isEnclosedBoundaryTile (grounds, blocks, localX, localY)
34{
35 const index = localY * CHUNK_SIZE + localX;
36 const block = blocks?.[index] ?? null;
37 if (isEnclosedDoorBlock(block))
38 { return true; }
39
40 const solidLos = tileLightOcclusionAt(grounds, blocks, localX, localY) === LightOcclusion.solid;
41 if (!solidLos)
42 { return false; }
43
44 const ground = normalizeGround(grounds?.[index]);
45 if (!isGroundPassable(ground.typeId))
46 { return true; }
47 if (!block)
48 { return false; }
49 return !isBlockPassable(block.typeId, block.state);
50}
51
52/** Interior candidate = not a boundary tile. */
53export function isEnclosedInteriorCandidate (grounds, blocks, localX, localY)
54{
55 return !isEnclosedBoundaryTile(grounds, blocks, localX, localY);
56}
57
58/**
59 * Bake enclosed masks for the 3×3 chunk window around `centerChunkX/Y`.
60 * Floods "outside" from the window edge through interior tiles; remaining
61 * interior components (plus their shared-edge boundary tiles) are enclosed.
62 * Regions that touch the window edge as interior are treated as outdoors.
63 *
64 * @param {number} centerChunkX
65 * @param {number} centerChunkY
66 * @param {(chunkX: number, chunkY: number) => { grounds: any[], blocks: any[] } | null | undefined} loadChunk
67 * @returns {{ chunkX: number, chunkY: number, enclosed: number[] }[]}
68 */
69export function bakeEnclosedWindow (centerChunkX, centerChunkY, loadChunk)
70{
71 const radius = ENCLOSED_BAKE_CHUNK_RADIUS;
72 const minChunkX = centerChunkX - radius;
73 const minChunkY = centerChunkY - radius;
74 const maxChunkX = centerChunkX + radius;
75 const maxChunkY = centerChunkY + radius;
76 const winChunks = 2 * radius + 1;
77 const size = winChunks * CHUNK_SIZE;
78 const minTileX = minChunkX * CHUNK_SIZE;
79 const minTileY = minChunkY * CHUNK_SIZE;
80
81 const chunkCache = new Map();
82 function chunkAt (chunkX, chunkY)
83 {
84 const key = `${chunkX},${chunkY}`;
85 if (!chunkCache.has(key))
86 { chunkCache.set(key, loadChunk(chunkX, chunkY) ?? null); }
87 return chunkCache.get(key);
88 }
89
90 function tileGroundsBlocks (tileX, tileY)
91 {
92 const chunkX = Math.floor(tileX / CHUNK_SIZE);
93 const chunkY = Math.floor(tileY / CHUNK_SIZE);
94 const data = chunkAt(chunkX, chunkY);
95 if (!data)
96 { return null; }
97 const localX = tileX - chunkX * CHUNK_SIZE;
98 const localY = tileY - chunkY * CHUNK_SIZE;
99 return { grounds: data.grounds, blocks: data.blocks, localX, localY };
100 }
101
102 const boundary = new Uint8Array(size * size);
103 const interior = new Uint8Array(size * size);
104 for (let wy = 0; wy < size; wy++)
105 {
106 for (let wx = 0; wx < size; wx++)
107 {
108 const tileX = minTileX + wx;
109 const tileY = minTileY + wy;
110 const loc = tileGroundsBlocks(tileX, tileY);
111 const idx = wy * size + wx;
112 if (!loc)
113 {
114 // Missing chunk → treat as open outdoors (interior) so edge flood escapes.
115 interior[idx] = 1;
116 continue;
117 }
118 if (isEnclosedBoundaryTile(loc.grounds, loc.blocks, loc.localX, loc.localY))
119 { boundary[idx] = 1; }
120 else
121 { interior[idx] = 1; }
122 }
123 }
124
125 const outside = new Uint8Array(size * size);
126 const queue = [];
127 function trySeed (wx, wy)
128 {
129 const idx = wy * size + wx;
130 if (!interior[idx] || outside[idx])
131 { return; }
132 outside[idx] = 1;
133 queue.push(idx);
134 }
135 for (let wx = 0; wx < size; wx++)
136 {
137 trySeed(wx, 0);
138 trySeed(wx, size - 1);
139 }
140 for (let wy = 1; wy < size - 1; wy++)
141 {
142 trySeed(0, wy);
143 trySeed(size - 1, wy);
144 }
145
146 const neighbors = [[1, 0], [-1, 0], [0, 1], [0, -1]];
147 while (queue.length > 0)
148 {
149 const idx = queue.pop();
150 const wx = idx % size;
151 const wy = (idx / size) | 0;
152 for (const [dx, dy] of neighbors)
153 {
154 const nx = wx + dx;
155 const ny = wy + dy;
156 if (nx < 0 || ny < 0 || nx >= size || ny >= size)
157 { continue; }
158 const nIdx = ny * size + nx;
159 if (!interior[nIdx] || outside[nIdx])
160 { continue; }
161 outside[nIdx] = 1;
162 queue.push(nIdx);
163 }
164 }
165
166 const enclosedWin = new Uint8Array(size * size);
167 for (let idx = 0; idx < size * size; idx++)
168 {
169 if (interior[idx] && !outside[idx])
170 { enclosedWin[idx] = 1; }
171 }
172 // Expand onto boundary tiles that enclose interiors (8-neighborhood so
173 // rectangular room corners are included; one step only — no wall-strip flood).
174 const encloseNeighbors = [
175 [1, 0], [-1, 0], [0, 1], [0, -1],
176 [1, 1], [1, -1], [-1, 1], [-1, -1]
177 ];
178 for (let wy = 0; wy < size; wy++)
179 {
180 for (let wx = 0; wx < size; wx++)
181 {
182 const idx = wy * size + wx;
183 if (!enclosedWin[idx] || !interior[idx])
184 { continue; }
185 for (const [dx, dy] of encloseNeighbors)
186 {
187 const nx = wx + dx;
188 const ny = wy + dy;
189 if (nx < 0 || ny < 0 || nx >= size || ny >= size)
190 { continue; }
191 const nIdx = ny * size + nx;
192 if (boundary[nIdx])
193 { enclosedWin[nIdx] = 1; }
194 }
195 }
196 }
197
198 const results = [];
199 for (let cy = minChunkY; cy <= maxChunkY; cy++)
200 {
201 for (let cx = minChunkX; cx <= maxChunkX; cx++)
202 {
203 const enclosed = new Array(CHUNK_SIZE * CHUNK_SIZE).fill(0);
204 const baseWX = (cx - minChunkX) * CHUNK_SIZE;
205 const baseWY = (cy - minChunkY) * CHUNK_SIZE;
206 for (let localY = 0; localY < CHUNK_SIZE; localY++)
207 {
208 for (let localX = 0; localX < CHUNK_SIZE; localX++)
209 {
210 const wIdx = (baseWY + localY) * size + (baseWX + localX);
211 enclosed[localY * CHUNK_SIZE + localX] = enclosedWin[wIdx] ? 1 : 0;
212 }
213 }
214 results.push({ chunkX: cx, chunkY: cy, enclosed });
215 }
216 }
217 return results;
218}
219
220/** Ambient darkness for a tile: base ambient + enclosed bonus, capped at night peak. */
221export function enclosedAmbientDarkness (baseAmbient, enclosedFlag, {
222 enclosedDarknessBonus = 0.10,
223 nightDarkness = 0.72
224} = {})
225{
226 const base = Math.min(1, Math.max(0, baseAmbient ?? 0));
227 if (!enclosedFlag)
228 { return base; }
229 const bonus = Math.min(1, Math.max(0, enclosedDarknessBonus ?? 0));
230 const peak = Math.min(1, Math.max(0, nightDarkness ?? 0.72));
231 return Math.min(peak, base + bonus);
232}