(Underdepths): First pass
This commit is contained in:
31 files changed
+3554
No files matched your search
@@ -0,0 +1,103 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from data.constants import FOV_RADIUS, TILE_FLOOR
|
||||
from engine.map_gen import GameMap
|
||||
|
||||
_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: GameMap,
|
||||
origin_x: int,
|
||||
origin_y: int,
|
||||
radius: int = FOV_RADIUS,
|
||||
) -> None:
|
||||
for index in range(len(game_map.visible)):
|
||||
game_map.visible[index] = 0
|
||||
if not game_map.in_bounds(origin_x, origin_y):
|
||||
return
|
||||
|
||||
_mark_visible(game_map, origin_x, origin_y)
|
||||
for xx, xy, yx, yy in _OCTANTS:
|
||||
_cast_light(game_map, origin_x, origin_y, 1, 1.0, 0.0, radius, xx, xy, yx, yy)
|
||||
|
||||
for index, visible in enumerate(game_map.visible):
|
||||
if visible:
|
||||
game_map.explored[index] = 1
|
||||
|
||||
|
||||
def _cast_light(
|
||||
game_map: GameMap,
|
||||
origin_x: int,
|
||||
origin_y: int,
|
||||
row: int,
|
||||
start_slope: float,
|
||||
end_slope: float,
|
||||
radius: int,
|
||||
xx: int,
|
||||
xy: int,
|
||||
yx: int,
|
||||
yy: int,
|
||||
) -> None:
|
||||
if start_slope < end_slope:
|
||||
return
|
||||
|
||||
radius_squared = radius * radius
|
||||
next_start_slope = start_slope
|
||||
for distance in range(row, radius + 1):
|
||||
blocked = False
|
||||
delta_y = -distance
|
||||
for delta_x in range(-distance, 1):
|
||||
map_x = origin_x + delta_x * xx + delta_y * xy
|
||||
map_y = origin_y + delta_x * yx + delta_y * yy
|
||||
left_slope = (delta_x - 0.5) / (delta_y + 0.5)
|
||||
right_slope = (delta_x + 0.5) / (delta_y - 0.5)
|
||||
|
||||
if start_slope < right_slope:
|
||||
continue
|
||||
if end_slope > left_slope:
|
||||
break
|
||||
|
||||
in_bounds = game_map.in_bounds(map_x, map_y)
|
||||
opaque = not in_bounds or game_map.tiles[game_map.index(map_x, map_y)] != TILE_FLOOR
|
||||
if in_bounds and delta_x * delta_x + delta_y * delta_y <= radius_squared:
|
||||
_mark_visible(game_map, map_x, map_y)
|
||||
|
||||
if blocked:
|
||||
if opaque:
|
||||
next_start_slope = right_slope
|
||||
continue
|
||||
blocked = False
|
||||
start_slope = next_start_slope
|
||||
elif opaque and distance < radius:
|
||||
blocked = True
|
||||
_cast_light(
|
||||
game_map,
|
||||
origin_x,
|
||||
origin_y,
|
||||
distance + 1,
|
||||
start_slope,
|
||||
left_slope,
|
||||
radius,
|
||||
xx,
|
||||
xy,
|
||||
yx,
|
||||
yy,
|
||||
)
|
||||
next_start_slope = right_slope
|
||||
|
||||
if blocked:
|
||||
break
|
||||
|
||||
|
||||
def _mark_visible(game_map: GameMap, x: int, y: int) -> None:
|
||||
game_map.visible[game_map.index(x, y)] = 1
|
||||
Reference in new issue
Block a user