Files
2026-08-16 19:10:31 +02:00

186 lines
5.9 KiB
Python

from __future__ import annotations
from array import array
from typing import Final
from data.constants import TILE_WALL
from engine.map_gen import GameMap, Grid, Point
# ponytail: FOV tuning lives here, not constants.py, so parallel Phase 2
# workers (pathfinding/combat) never touch the same file; merge later if desired.
SIGHT_RADIUS: Final = 8
_OCTANTS: Final = (
(1, 0, 0, -1, -1, 0, 0, 1),
(0, 1, -1, 0, 0, -1, 1, 0),
(0, 1, 1, 0, 0, -1, -1, 0),
(1, 0, 0, 1, -1, 0, 0, -1),
)
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:
if start < end:
return
new_start = 0.0
for distance in range(row, radius + 1):
dx = -distance - 1
dy = -distance
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 * radius and game_map.in_bounds(x, y):
game_map.visible[y][x] = 1
game_map.explored[y][x] = 1
if blocked:
if game_map.tile_at(x, y) == TILE_WALL:
new_start = r_slope
continue
blocked = False
start = new_start
elif game_map.tile_at(x, y) == TILE_WALL and distance < radius:
_cast_light(
game_map, cx, cy, distance + 1, start, l_slope, radius, xx, xy, yx, yy
)
blocked = True
new_start = r_slope
if blocked:
break
def compute_fov(game_map: GameMap, origin: Point, radius: int = SIGHT_RADIUS) -> None:
"""Refresh game_map.visible via recursive shadowcasting, merging into explored.
Walls occlude but are themselves lit when reached by light. Visible is
rebuilt from scratch every call; explored only ever grows. Never raises
on any game state: an out-of-bounds origin just leaves visible cleared.
"""
for grid_row in game_map.visible:
grid_row[:] = array("b", bytes(len(grid_row)))
ox, oy = origin
if not game_map.in_bounds(ox, oy):
return
game_map.visible[oy][ox] = 1
game_map.explored[oy][ox] = 1
for octant in range(8):
_cast_light(
game_map,
ox,
oy,
1,
1.0,
0.0,
radius,
_OCTANTS[0][octant],
_OCTANTS[1][octant],
_OCTANTS[2][octant],
_OCTANTS[3][octant],
)
if __name__ == "__main__":
# Run from the repo root: python -m engine.fov
from data.constants import TILE_FLOOR
from engine.map_gen import generate_map
def manual_map(width: int, height: int, floors: list[Point]) -> GameMap:
tiles = [array("b", [TILE_WALL] * width) for _ in range(height)]
for x, y in floors:
tiles[y][x] = TILE_FLOOR
return GameMap(
width=width,
height=height,
tiles=tiles,
rooms=[],
junctions=[],
corridors=[],
explored=[array("b", [0] * width) for _ in range(height)],
visible=[array("b", [0] * width) for _ in range(height)],
entities=[],
stairs_down=None,
floor_index=0,
spawn=floors[0],
)
def lit(game_map: GameMap) -> set[Point]:
return {
(x, y)
for y, row in enumerate(game_map.visible)
for x, seen in enumerate(row)
if seen
}
def lit_floors(game_map: GameMap) -> set[Point]:
return {point for point in lit(game_map) if game_map.tile_at(*point) == TILE_FLOOR}
sealed = manual_map(5, 5, [(2, 2)])
compute_fov(sealed, (2, 2))
assert lit_floors(sealed) == {(2, 2)}, "walled-in player must see no other floor"
assert all(
sealed.visible[2 + dy][2 + dx]
for dy in (-1, 0, 1)
for dx in (-1, 0, 1)
if (dx, dy) != (0, 0)
), "walls adjacent to the visible origin floor must be lit"
size = 19
disc = manual_map(size, size, [(x, y) for y in range(size) for x in range(size)])
center = (9, 9)
compute_fov(disc, center)
expected = {
(x, y)
for y in range(size)
for x in range(size)
if (x - center[0]) ** 2 + (y - center[1]) ** 2 <= SIGHT_RADIUS**2
}
assert lit(disc) == expected, "open area must light exactly the radius-8 disc"
compute_fov(disc, (9, 16))
assert (9, 7) not in lit(disc), "visible must be rebuilt, not accumulated"
assert disc.explored[7][9], "previously seen tiles stay explored"
assert disc.explored[9][9] and disc.visible[16][9]
corridor = manual_map(11, 3, [(x, 1) for x in range(11) if x != 5])
compute_fov(corridor, (1, 1))
assert (4, 1) in lit(corridor), "tile before the wall must be visible"
assert (6, 1) not in lit(corridor), "wall must occlude the tile behind it"
game_map = generate_map(0, seed=0)
compute_fov(game_map, game_map.spawn)
ox, oy = game_map.spawn
assert all(
(x - ox) ** 2 + (y - oy) ** 2 <= SIGHT_RADIUS**2 for x, y in lit(game_map)
), "no visible tile may exceed the sight radius"
assert len(lit_floors(game_map)) > 20, "the spawn room must be broadly visible"
far = next(
(
(x, y)
for y, tiles in enumerate(game_map.tiles)
for x, tile in enumerate(tiles)
if tile == TILE_FLOOR and (x - ox) ** 2 + (y - oy) ** 2 > SIGHT_RADIUS**2
),
None,
)
if far is not None:
assert far not in lit(game_map), "tiles beyond radius must stay dark"
print("fov self-check: PASS")