(Underdepths) Pass 5
This commit is contained in:
1 parent
05e340dec2
commit
b6d2a058e3
53 files changed
+5020
No files matched your search
@@ -0,0 +1,207 @@
|
||||
"""BSP dungeon generation.
|
||||
|
||||
Floors 0..8: a region recursively split into non-overlapping leaf regions,
|
||||
each gets a carved room, rooms are chained by L-corridors (guarantees a
|
||||
connected walkable graph), spawn in the first room, stairs in the room
|
||||
farthest from spawn. Reachability is flood-fill-verified with a fallback
|
||||
direct corridor.
|
||||
|
||||
Floor 9 (boss arena): a single large room, no descent.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import random
|
||||
from collections import deque
|
||||
from dataclasses import dataclass
|
||||
|
||||
from data.constants import (
|
||||
MAP_W, MAP_H, FLOOR_COUNT, WALL, FLOOR,
|
||||
ROOM_MIN_W, ROOM_MIN_H, ROOM_MAX_W, ROOM_MAX_H,
|
||||
BSP_MIN_HALF, BSP_LEAVES_MIN, BSP_LEAVES_MAX,
|
||||
BOSS_ROOM_W, BOSS_ROOM_H,
|
||||
)
|
||||
|
||||
|
||||
@dataclass
|
||||
class Rect:
|
||||
x: int
|
||||
y: int
|
||||
w: int
|
||||
h: int
|
||||
|
||||
@property
|
||||
def center(self) -> tuple[int, int]:
|
||||
return (self.x + self.w // 2, self.y + self.h // 2)
|
||||
|
||||
def intersects(self, other: "Rect") -> bool:
|
||||
return (self.x < other.x + other.w
|
||||
and self.x + self.w > other.x
|
||||
and self.y < other.y + other.h
|
||||
and self.y + self.h > other.y)
|
||||
|
||||
|
||||
@dataclass(slots=True)
|
||||
class GameMap:
|
||||
width: int
|
||||
height: int
|
||||
tiles: list[list[int]]
|
||||
rooms: list[Rect]
|
||||
explored: list[list[bool]]
|
||||
visible: list[list[bool]]
|
||||
entities: list
|
||||
stairs_down: tuple[int, int] | None
|
||||
spawn: tuple[int, int] | None
|
||||
floor_index: int
|
||||
|
||||
def in_bounds(self, x: int, y: int) -> bool:
|
||||
return 0 <= x < self.width and 0 <= y < self.height
|
||||
|
||||
def walkable(self, x: int, y: int) -> bool:
|
||||
return self.in_bounds(x, y) and self.tiles[y][x] == FLOOR
|
||||
|
||||
|
||||
# --------------------------------------------------------------------- carving
|
||||
def _carve_rect(tiles: list[list[int]], r: Rect) -> None:
|
||||
for y in range(r.y, r.y + r.h):
|
||||
for x in range(r.x, r.x + r.w):
|
||||
tiles[y][x] = FLOOR
|
||||
|
||||
|
||||
def _carve_h(tiles: list[list[int]], x1: int, x2: int, y: int) -> None:
|
||||
if x2 < x1:
|
||||
x1, x2 = x2, x1
|
||||
for x in range(x1, x2 + 1):
|
||||
tiles[y][x] = FLOOR
|
||||
|
||||
|
||||
def _carve_v(tiles: list[list[int]], y1: int, y2: int, x: int) -> None:
|
||||
if y2 < y1:
|
||||
y1, y2 = y2, y1
|
||||
for y in range(y1, y2 + 1):
|
||||
tiles[y][x] = FLOOR
|
||||
|
||||
|
||||
def _carve_corridor(tiles: list[list[int]], a: tuple[int, int],
|
||||
b: tuple[int, int], rng: random.Random) -> None:
|
||||
ax, ay = a
|
||||
bx, by = b
|
||||
if rng.random() < 0.5:
|
||||
_carve_h(tiles, ax, bx, ay)
|
||||
_carve_v(tiles, ay, by, bx)
|
||||
else:
|
||||
_carve_v(tiles, ay, by, ax)
|
||||
_carve_h(tiles, ax, bx, by)
|
||||
|
||||
|
||||
# --------------------------------------------------------------------- bsp tree
|
||||
def _bsp(root: Rect, rng: random.Random) -> list[Rect]:
|
||||
target = rng.randint(BSP_LEAVES_MIN, BSP_LEAVES_MAX)
|
||||
leaves: list[Rect] = []
|
||||
frontier: list[Rect] = [root]
|
||||
while frontier:
|
||||
node = frontier.pop()
|
||||
if len(leaves) + len(frontier) + 1 >= target:
|
||||
leaves.append(node)
|
||||
continue
|
||||
can_v = node.w >= 2 * BSP_MIN_HALF
|
||||
can_h = node.h >= 2 * BSP_MIN_HALF
|
||||
if not (can_v or can_h):
|
||||
leaves.append(node)
|
||||
continue
|
||||
vertical = can_v and (not can_h or rng.random() < node.w / (node.w + node.h))
|
||||
if vertical:
|
||||
s = rng.randint(BSP_MIN_HALF, node.w - BSP_MIN_HALF)
|
||||
frontier.append(Rect(node.x, node.y, s, node.h))
|
||||
frontier.append(Rect(node.x + s, node.y, node.w - s, node.h))
|
||||
else:
|
||||
s = rng.randint(BSP_MIN_HALF, node.h - BSP_MIN_HALF)
|
||||
frontier.append(Rect(node.x, node.y, node.w, s))
|
||||
frontier.append(Rect(node.x, node.y + s, node.w, node.h - s))
|
||||
return leaves
|
||||
|
||||
|
||||
def _room_in(leaf: Rect, tiles: list[list[int]], rng: random.Random) -> Rect:
|
||||
max_w = leaf.w - 2
|
||||
max_h = leaf.h - 2
|
||||
w = rng.randint(ROOM_MIN_W, min(ROOM_MAX_W, max_w))
|
||||
h = rng.randint(ROOM_MIN_H, min(ROOM_MAX_H, max_h))
|
||||
x = rng.randint(leaf.x + 1, leaf.x + 1 + max(0, max_w - w))
|
||||
y = rng.randint(leaf.y + 1, leaf.y + 1 + max(0, max_h - h))
|
||||
room = Rect(x, y, w, h)
|
||||
_carve_rect(tiles, room)
|
||||
return room
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- reachability
|
||||
def _flood(tiles: list[list[int]], start: tuple[int, int],
|
||||
width: int, height: int) -> set[tuple[int, int]]:
|
||||
sx, sy = start
|
||||
seen = {(sx, sy)}
|
||||
dq = deque([(sx, sy)])
|
||||
while dq:
|
||||
x, y = dq.popleft()
|
||||
for nx, ny in ((x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1)):
|
||||
if (nx, ny) in seen:
|
||||
continue
|
||||
if 0 <= nx < width and 0 <= ny < height and tiles[ny][nx] == FLOOR:
|
||||
seen.add((nx, ny))
|
||||
dq.append((nx, ny))
|
||||
return seen
|
||||
|
||||
|
||||
def _dist2(a: tuple[int, int], b: tuple[int, int]) -> int:
|
||||
return (a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2
|
||||
|
||||
|
||||
# --------------------------------------------------------------------- builders
|
||||
def _boss_floor(floor_index: int) -> GameMap:
|
||||
width, height = MAP_W, MAP_H
|
||||
tiles = [[WALL] * width for _ in range(height)]
|
||||
explored = [[False] * width for _ in range(height)]
|
||||
visible = [[False] * width for _ in range(height)]
|
||||
rw, rh = BOSS_ROOM_W, BOSS_ROOM_H
|
||||
rx = (width - rw) // 2
|
||||
ry = (height - rh) // 2
|
||||
room = Rect(rx, ry, rw, rh)
|
||||
_carve_rect(tiles, room)
|
||||
spawn = (rx + 1, ry + rh // 2)
|
||||
return GameMap(width, height, tiles, [room], explored, visible,
|
||||
[], None, spawn, floor_index)
|
||||
|
||||
|
||||
def _standard_floor(floor_index: int, rng: random.Random) -> GameMap:
|
||||
width, height = MAP_W, MAP_H
|
||||
tiles = [[WALL] * width for _ in range(height)]
|
||||
explored = [[False] * width for _ in range(height)]
|
||||
visible = [[False] * width for _ in range(height)]
|
||||
|
||||
leaves = _bsp(Rect(1, 1, width - 2, height - 2), rng)
|
||||
rooms = [_room_in(leaf, tiles, rng) for leaf in leaves]
|
||||
|
||||
# Chain-connect consecutive rooms -> connected spanning path of floor tiles.
|
||||
for i in range(len(rooms) - 1):
|
||||
_carve_corridor(tiles, rooms[i].center, rooms[i + 1].center, rng)
|
||||
|
||||
spawn = rooms[0].center
|
||||
far = max(rooms, key=lambda r: _dist2(r.center, spawn))
|
||||
stairs = far.center
|
||||
|
||||
# Reachability safety net (chain already guarantees it; re-carve if a
|
||||
# future tweak ever breaks connectivity).
|
||||
reach = _flood(tiles, spawn, width, height)
|
||||
if stairs not in reach:
|
||||
_carve_corridor(tiles, spawn, stairs, rng)
|
||||
|
||||
return GameMap(width, height, tiles, rooms, explored, visible,
|
||||
[], stairs, spawn, floor_index)
|
||||
|
||||
|
||||
def generate_floor(floor_index: int, rng: random.Random) -> GameMap:
|
||||
if floor_index == FLOOR_COUNT - 1:
|
||||
return _boss_floor(floor_index)
|
||||
return _standard_floor(floor_index, rng)
|
||||
|
||||
|
||||
def generate_dungeon(seed: int) -> list[GameMap]:
|
||||
rng = random.Random(seed)
|
||||
return [generate_floor(i, rng) for i in range(FLOOR_COUNT)]
|
||||
Reference in new issue
Block a user