129 lines
3.7 KiB
Python
129 lines
3.7 KiB
Python
from __future__ import annotations
|
|
|
|
from data.constants import SIGHT_RADIUS, TILE_WALL
|
|
from engine.map_gen import GameMap
|
|
|
|
# Row/column multipliers for the 8 shadowcasting octants (Bergström layout).
|
|
_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 _set_visible(game_map: GameMap, x: int, y: int) -> None:
|
|
game_map.visible[y][x] = 1
|
|
game_map.explored[y][x] = 1
|
|
|
|
|
|
def _is_blocked(game_map: GameMap, x: int, y: int) -> bool:
|
|
return game_map.tile_at(x, y) == TILE_WALL
|
|
|
|
|
|
def _cast_light(
|
|
game_map: GameMap,
|
|
cx: int,
|
|
cy: int,
|
|
row: int,
|
|
start: float,
|
|
end: float,
|
|
radius: int,
|
|
xx: int,
|
|
xy: int,
|
|
yx: int,
|
|
yy: int,
|
|
) -> None:
|
|
"""Scan one octant. Rows are dy; dx ascends from axis to diagonal, so
|
|
cell slopes rise through the call. The visible cone is [end, start];
|
|
each wall recurses for the light passing left of it, then narrows the
|
|
cone to the light passing right."""
|
|
if start <= end:
|
|
return
|
|
|
|
radius2 = radius * radius
|
|
for j in range(row, radius + 1):
|
|
if start <= end:
|
|
return
|
|
dx = 0
|
|
while dx <= j:
|
|
x, y = cx + dx * xx + j * xy, cy + dx * yx + j * yy
|
|
l_slope = (dx - 0.5) / (j + 0.5)
|
|
r_slope = (dx + 0.5) / (j - 0.5)
|
|
if r_slope <= end:
|
|
dx += 1
|
|
continue
|
|
if l_slope >= start:
|
|
break
|
|
if dx * dx + j * j <= radius2 and game_map.in_bounds(x, y):
|
|
_set_visible(game_map, x, y)
|
|
if _is_blocked(game_map, x, y):
|
|
if l_slope > end and j < radius:
|
|
_cast_light(
|
|
game_map,
|
|
cx,
|
|
cy,
|
|
j + 1,
|
|
l_slope,
|
|
end,
|
|
radius,
|
|
xx,
|
|
xy,
|
|
yx,
|
|
yy,
|
|
)
|
|
end = r_slope
|
|
if end >= start:
|
|
break
|
|
dx += 1
|
|
|
|
|
|
def compute_fov(game_map: GameMap, x: int, y: int, radius: int = SIGHT_RADIUS) -> None:
|
|
"""Recursive shadowcasting: fills visible, merges into explored."""
|
|
for row in game_map.visible:
|
|
for i in range(len(row)):
|
|
row[i] = 0
|
|
|
|
if game_map.in_bounds(x, y):
|
|
_set_visible(game_map, x, y)
|
|
|
|
for xx, xy, yx, yy in _OCTANTS:
|
|
_cast_light(game_map, x, y, 1, 1.0, 0.0, radius, xx, xy, yx, yy)
|
|
|
|
_reveal_wall_faces(game_map)
|
|
|
|
|
|
def _reveal_wall_faces(game_map: GameMap) -> None:
|
|
"""Walls adjacent to a visible floor tile are themselves visible."""
|
|
lit: list[tuple[int, int]] = []
|
|
for wy in range(game_map.height):
|
|
for wx in range(game_map.width):
|
|
if game_map.visible[wy][wx]:
|
|
continue
|
|
if game_map.tile_at(wx, wy) != TILE_WALL:
|
|
continue
|
|
for ax, ay in ((-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)):
|
|
fx, fy = wx + ax, wy + ay
|
|
if (
|
|
game_map.in_bounds(fx, fy)
|
|
and game_map.visible[fy][fx]
|
|
and game_map.tile_at(fx, fy) != TILE_WALL
|
|
):
|
|
lit.append((wx, wy))
|
|
break
|
|
for wx, wy in lit:
|
|
_set_visible(game_map, wx, wy)
|
|
|
|
|
|
def visible_tiles(game_map: GameMap) -> list[tuple[int, int]]:
|
|
return [
|
|
(x, y_coord)
|
|
for y_coord, row in enumerate(game_map.visible)
|
|
for x, cell in enumerate(row)
|
|
if cell
|
|
]
|