109 lines
3.3 KiB
Python
109 lines
3.3 KiB
Python
"""Recursive shadowcasting field of view.
|
|
|
|
8-octant recursive shadowcasting (Bjorn Bergstrom). A floor tile is visible when
|
|
an unblocked line of sight reaches it; walls block and cast shadows. A wall next
|
|
to a visible floor tile is itself visible so room boundaries render. The origin
|
|
never lands in the lit set, so a player sealed inside a single floor tile has no
|
|
lit floor neighbour and sees only themselves.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
if TYPE_CHECKING:
|
|
from engine.map_gen import GameMap
|
|
|
|
|
|
# (xx, xy, yx, yy) transforms map a single octant scan onto all 8 octants.
|
|
_FOV_OCTANTS: tuple[tuple[int, int, int, int], ...] = (
|
|
(1, 0, 0, 1), (0, 1, 1, 0), (0, -1, 1, 0), (-1, 0, 0, 1),
|
|
(-1, 0, 0, -1), (0, -1, -1, 0), (0, 1, -1, 0), (1, 0, 0, -1),
|
|
)
|
|
|
|
|
|
def _blocks(gmap: GameMap, x: int, y: int) -> bool:
|
|
if x < 0 or y < 0 or x >= gmap.width or y >= gmap.height:
|
|
return True
|
|
return gmap.tiles[y][x] == 0
|
|
|
|
|
|
def _cast_light(
|
|
gmap: GameMap,
|
|
cx: int,
|
|
cy: int,
|
|
row: int,
|
|
start: float,
|
|
end: float,
|
|
radius: int,
|
|
radius_sq: int,
|
|
xx: int,
|
|
xy: int,
|
|
yx: int,
|
|
yy: int,
|
|
light: set[tuple[int, int]],
|
|
) -> None:
|
|
if start < end:
|
|
return
|
|
width, height, tiles = gmap.width, gmap.height, gmap.tiles
|
|
new_start = 0.0
|
|
j = row
|
|
while j <= radius:
|
|
dx = -j - 1
|
|
dy = -j
|
|
blocked = False
|
|
while dx <= 0:
|
|
dx += 1
|
|
x = cx + dx * xx + dy * xy
|
|
y = cy + dx * yx + dy * yy
|
|
l_slope = (dx - 0.5) / (dy + 0.5)
|
|
r_slope = (dx + 0.5) / (dy - 0.5)
|
|
if start < r_slope:
|
|
continue
|
|
if end > l_slope:
|
|
break
|
|
if dx * dx + dy * dy <= radius_sq and 0 <= x < width and 0 <= y < height:
|
|
light.add((x, y))
|
|
if blocked:
|
|
if _blocks(gmap, x, y):
|
|
new_start = r_slope
|
|
continue
|
|
blocked = False
|
|
start = new_start
|
|
elif _blocks(gmap, x, y) and j < radius:
|
|
blocked = True
|
|
_cast_light(
|
|
gmap, cx, cy, j + 1, start, l_slope,
|
|
radius, radius_sq, xx, xy, yx, yy, light,
|
|
)
|
|
new_start = r_slope
|
|
if blocked:
|
|
break
|
|
j += 1
|
|
|
|
|
|
def compute_fov(gmap: GameMap, ox: int, oy: int, radius: int) -> set[tuple[int, int]]:
|
|
radius_sq = radius * radius
|
|
light: set[tuple[int, int]] = set()
|
|
for xx, xy, yx, yy in _FOV_OCTANTS:
|
|
_cast_light(gmap, ox, oy, 1, 1.0, 0.0, radius, radius_sq, xx, xy, yx, yy, light)
|
|
|
|
width, height, tiles = gmap.width, gmap.height, gmap.tiles
|
|
lit_floors = {(x, y) for (x, y) in light if tiles[y][x] == 1}
|
|
|
|
result: set[tuple[int, int]] = {(ox, oy)} | lit_floors
|
|
for fx, fy in lit_floors:
|
|
for ddx in (-1, 0, 1):
|
|
wx = fx + ddx
|
|
if wx < 0 or wx >= width:
|
|
continue
|
|
for ddy in (-1, 0, 1):
|
|
if not ddx and not ddy:
|
|
continue
|
|
wy = fy + ddy
|
|
if wy < 0 or wy >= height:
|
|
continue
|
|
if tiles[wy][wx] == 0 and max(abs(wx - ox), abs(wy - oy)) <= radius:
|
|
result.add((wx, wy))
|
|
return result
|