| 1 | import { CHUNK_SIZE } from "../../../shared/protocol/constants.js"; |
| 2 | import { tileToChunk, tileToLocal, chunkKey, maxLightChunkRadius, chebyshevChunks } from "../world/coordinates.js"; |
| 3 | import { cloneChunkData, collectLightSources, createEmptyChunkData, isChunkTilePassable } from "../world/chunkData.js"; |
| 4 | |
| 5 | export 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 | export function rayReaches (fromX, fromY, toX, toY, isPassable) |
| 38 | { |
| 39 | if (fromX === toX && fromY === toY) |
| 40 | { return isPassable(toX, toY); } |
| 41 | |
| 42 | if (!isPassable(toX, toY)) |
| 43 | { return false; } |
| 44 | |
| 45 | const tiles = bresenhamTiles(fromX, fromY, toX, toY); |
| 46 | for (let i = 1; i < tiles.length - 1; i++) |
| 47 | { |
| 48 | if (!isPassable(tiles[i].x, tiles[i].y)) |
| 49 | { return false; } |
| 50 | } |
| 51 | return true; |
| 52 | } |
| 53 | |
| 54 | export function falloffAtDistance (dist, range) |
| 55 | { |
| 56 | if (range <= 0 || dist > range) |
| 57 | { return 0; } |
| 58 | |
| 59 | const t = 1 - dist / range; |
| 60 | return t * t * (3 - 2 * t); |
| 61 | } |
| 62 | |
| 63 | export function encodeLight (dist, range) |
| 64 | { |
| 65 | const falloff = falloffAtDistance(dist, range); |
| 66 | if (falloff <= 0) |
| 67 | { return 0; } |
| 68 | |
| 69 | return Math.max(1, Math.round(255 * falloff)); |
| 70 | } |
| 71 | |
| 72 | export function lightIntensity (encoded) |
| 73 | { |
| 74 | return Math.min(1, Math.max(0, (encoded ?? 0) / 255)); |
| 75 | } |
| 76 | |
| 77 | export function maxDefinitionLightRange (blockDefs) |
| 78 | { |
| 79 | let max = 0; |
| 80 | for (const def of Object.values(blockDefs)) |
| 81 | { max = Math.max(max, def.lightRangeTiles ?? 0); } |
| 82 | return max; |
| 83 | } |
| 84 | |
| 85 | export function bakeLightMaps (centerChunkX, centerChunkY, loadChunk, maxRange) |
| 86 | { |
| 87 | const halo = maxLightChunkRadius(maxRange); |
| 88 | const affected = chebyshevChunks(centerChunkX, centerChunkY, halo); |
| 89 | const loaded = new Map(); |
| 90 | |
| 91 | function getData (chunkX, chunkY) |
| 92 | { |
| 93 | const key = chunkKey(chunkX, chunkY); |
| 94 | if (!loaded.has(key)) |
| 95 | { loaded.set(key, cloneChunkData(loadChunk(chunkX, chunkY))); } |
| 96 | return loaded.get(key); |
| 97 | } |
| 98 | |
| 99 | const sources = []; |
| 100 | for (const coord of chebyshevChunks(centerChunkX, centerChunkY, halo + 1)) |
| 101 | { |
| 102 | const data = getData(coord.chunkX, coord.chunkY); |
| 103 | sources.push(...collectLightSources(coord.chunkX, coord.chunkY, data)); |
| 104 | } |
| 105 | |
| 106 | function isPassable (tileX, tileY) |
| 107 | { |
| 108 | const data = getData(tileToChunk(tileX), tileToChunk(tileY)); |
| 109 | return isChunkTilePassable(data, tileToLocal(tileX), tileToLocal(tileY)); |
| 110 | } |
| 111 | |
| 112 | for (const coord of affected) |
| 113 | { |
| 114 | const data = getData(coord.chunkX, coord.chunkY); |
| 115 | for (let localY = 0; localY < CHUNK_SIZE; localY++) |
| 116 | { |
| 117 | for (let localX = 0; localX < CHUNK_SIZE; localX++) |
| 118 | { |
| 119 | const tileX = coord.chunkX * CHUNK_SIZE + localX; |
| 120 | const tileY = coord.chunkY * CHUNK_SIZE + localY; |
| 121 | const index = localY * CHUNK_SIZE + localX; |
| 122 | let best = 0; |
| 123 | if (isPassable(tileX, tileY)) |
| 124 | { |
| 125 | for (const source of sources) |
| 126 | { |
| 127 | const dist = Math.hypot(tileX - source.tileX, tileY - source.tileY); |
| 128 | if (dist > source.range) |
| 129 | { continue; } |
| 130 | if (rayReaches(source.tileX, source.tileY, tileX, tileY, isPassable)) |
| 131 | { |
| 132 | best = Math.max(best, encodeLight(dist, source.range)); |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | data.light[index] = best; |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | return affected.map((coord) => ({ |
| 142 | chunkX: coord.chunkX, |
| 143 | chunkY: coord.chunkY, |
| 144 | data: loaded.get(chunkKey(coord.chunkX, coord.chunkY)) |
| 145 | })); |
| 146 | } |
| 147 | |
| 148 | export function defaultLightChunk (groundTypeId) |
| 149 | { |
| 150 | return createEmptyChunkData(groundTypeId); |
| 151 | } |