(Underdepths) Pass 4

This commit is contained in:
AlexKucera committed 2026-08-13 15:23:35 +02:00
1 parent a219ad17d5
commit 6147ab3121
29 files changed
+4888

No files matched your search

+87
View File
@@ -0,0 +1,87 @@
"""Recursive shadowcasting field of view over 8 octants (plan.md §2).
Written from scratch. Walls block sight, but a wall reached by a light ray is
itself visible (you see the wall face). `compute_fov` mutates `game_map.visible`
to match the set it returns.
"""
from __future__ import annotations
# Per-octant transforms: (xx, xy, yx, yy) map scan offsets (dx, dy) to map space.
_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 compute_fov(game_map, ox: int, oy: int, radius: int = 8) -> set[tuple[int, int]]:
"""Return the set of visible tiles from (ox, oy); also set game_map.visible."""
width, height = game_map.width, game_map.height
tiles = game_map.tiles
# Fresh visible grid every call so stale tiles never linger.
for y in range(height):
vrow = game_map.visible[y]
for x in range(width):
vrow[x] = False
visible: set[tuple[int, int]] = set()
def blocks(x: int, y: int) -> bool:
if 0 <= x < width and 0 <= y < height:
return tiles[y][x] == 0
return True # out of bounds behaves as wall
def light(x: int, y: int) -> None:
if 0 <= x < width and 0 <= y < height:
visible.add((x, y))
if not (0 <= ox < width and 0 <= oy < height):
return visible
light(ox, oy) # origin is always seen
r2 = radius * radius
def cast(row: int, start: float, end: float,
xx: int, xy: int, yx: int, yy: int) -> None:
if start < end:
return
new_start = start
for j in range(row, radius + 1):
dy = -j
dx = -j - 1
blocked = False
while dx <= 0:
dx += 1
mx = ox + dx * xx + dy * xy
my = oy + 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
elif end > l_slope:
break
else:
if dx * dx + dy * dy <= r2:
light(mx, my)
if blocked:
if blocks(mx, my):
new_start = r_slope
continue
else:
blocked = False
start = new_start
else:
if blocks(mx, my) and j < radius:
blocked = True
cast(j + 1, start, l_slope, xx, xy, yx, yy)
new_start = r_slope
if blocked:
break
for (xx, xy, yx, yy) in _OCTANTS:
cast(1, 1.0, 0.0, xx, xy, yx, yy)
for (x, y) in visible:
game_map.visible[y][x] = True
return visible