Files
agentic-labs-challenge/underdepths/engine/map_gen.py
T
2026-08-13 07:28:55 +02:00

213 lines
6.8 KiB
Python

"""BSP dungeon generation.
Deterministic for a given (floor_index, rng): the only source of randomness is
the passed random.Random. The global random module is never touched.
"""
from __future__ import annotations
import random
from dataclasses import dataclass
from data.constants import (
MAP_W, MAP_H, BOSS_FLOOR,
BSP_SPLITS_MIN, BSP_SPLITS_MAX, SPLIT_MIN_DIM,
SPLIT_RATIO_MIN, SPLIT_RATIO_MAX,
ROOM_MIN_W, ROOM_MAX_W, ROOM_MIN_H, ROOM_MAX_H,
ROOM_SHRINK_MIN, ROOM_SHRINK_MAX,
BOSS_ROOM_W, BOSS_ROOM_H,
)
@dataclass
class Rect:
x: int
y: int
w: int
h: int
def center(self) -> tuple[int, int]:
return (self.x + self.w // 2, self.y + self.h // 2)
def inner(self) -> tuple[int, int, int, int]:
# x1, y1, x2, y2 inclusive of the carved floor area
return (self.x, self.y, self.x + self.w - 1, self.y + self.h - 1)
@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
floor_index: int
class _Node:
__slots__ = ("region", "left", "right", "room")
def __init__(self, region: Rect) -> None:
self.region: Rect = region
self.left: _Node | None = None
self.right: _Node | None = None
self.room: Rect | None = None
def _splittable(region: Rect, depth: int) -> bool:
vertical = (depth % 2 == 0)
extent = region.w if vertical else region.h
return extent >= SPLIT_MIN_DIM
def _split_region(region: Rect, depth: int, rng: random.Random) -> tuple[Rect, Rect]:
vertical = (depth % 2 == 0)
half = SPLIT_MIN_DIM // 2
if vertical:
cut = region.x + int(region.w * rng.uniform(SPLIT_RATIO_MIN, SPLIT_RATIO_MAX))
cut = max(region.x + half, min(cut, region.x + region.w - half))
a = Rect(region.x, region.y, cut - region.x, region.h)
b = Rect(cut, region.y, region.x + region.w - cut, region.h)
else:
cut = region.y + int(region.h * rng.uniform(SPLIT_RATIO_MIN, SPLIT_RATIO_MAX))
cut = max(region.y + half, min(cut, region.y + region.h - half))
a = Rect(region.x, region.y, region.w, cut - region.y)
b = Rect(region.x, cut, region.w, region.y + region.h - cut)
return a, b
def _make_room(region: Rect, rng: random.Random, tiles: list[list[int]]) -> Rect:
shrink = rng.randint(ROOM_SHRINK_MIN, ROOM_SHRINK_MAX)
avail_w = region.w - 2 * shrink
avail_h = region.h - 2 * shrink
if avail_w < ROOM_MIN_W or avail_h < ROOM_MIN_H:
shrink = max(0, min(shrink, (region.w - ROOM_MIN_W) // 2, (region.h - ROOM_MIN_H) // 2))
avail_w = max(ROOM_MIN_W, region.w - 2 * shrink)
avail_h = max(ROOM_MIN_H, region.h - 2 * shrink)
rw = min(avail_w, rng.randint(ROOM_MIN_W, ROOM_MAX_W))
rh = min(avail_h, rng.randint(ROOM_MIN_H, ROOM_MAX_H))
ox = rng.randint(0, avail_w - rw)
oy = rng.randint(0, avail_h - rh)
rx = region.x + shrink + ox
ry = region.y + shrink + oy
for yy in range(ry, ry + rh):
for xx in range(rx, rx + rw):
tiles[yy][xx] = 1
return Rect(rx, ry, rw, rh)
def _build(region: Rect, depth: int, rng: random.Random,
tiles: list[list[int]], budget: list[int]) -> _Node:
node = _Node(region)
if budget[0] > 0 and _splittable(region, depth):
a, b = _split_region(region, depth, rng)
budget[0] -= 1
node.left = _build(a, depth + 1, rng, tiles, budget)
node.right = _build(b, depth + 1, rng, tiles, budget)
else:
node.room = _make_room(region, rng, tiles)
return node
def _gather(node: _Node, out: list[Rect]) -> None:
if node.room is not None:
out.append(node.room)
return
assert node.left is not None and node.right is not None
_gather(node.left, out)
_gather(node.right, out)
def _hcarve(x1: int, x2: int, y: int, tiles: list[list[int]]) -> None:
for x in range(min(x1, x2), max(x1, x2) + 1):
tiles[y][x] = 1
def _vcarve(y1: int, y2: int, x: int, tiles: list[list[int]]) -> None:
for y in range(min(y1, y2), max(y1, y2) + 1):
tiles[y][x] = 1
def _corridor(a: Rect, b: Rect, rng: random.Random, tiles: list[list[int]]) -> None:
ax, ay = a.center()
bx, by = b.center()
if rng.random() < 0.5:
_hcarve(ax, bx, ay, tiles)
_vcarve(ay, by, bx, tiles)
else:
_vcarve(ay, by, ax, tiles)
_hcarve(ax, bx, by, tiles)
def _connect(node: _Node, rng: random.Random, tiles: list[list[int]]) -> Rect:
# Carves a corridor between a representative room of each subtree, for every
# internal node -> a spanning tree -> the whole floor is connected.
if node.room is not None:
return node.room
assert node.left is not None and node.right is not None
ra = _connect(node.left, rng, tiles)
rb = _connect(node.right, rng, tiles)
_corridor(ra, rb, rng, tiles)
return ra
def _empty_grids() -> tuple[list[list[bool]], list[list[bool]]]:
explored = [[False] * MAP_W for _ in range(MAP_H)]
visible = [[False] * MAP_W for _ in range(MAP_H)]
return explored, visible
def _boss_arena(floor_index: int) -> GameMap:
tiles = [[0] * MAP_W for _ in range(MAP_H)]
rx = (MAP_W - BOSS_ROOM_W) // 2
ry = (MAP_H - BOSS_ROOM_H) // 2
for yy in range(ry, ry + BOSS_ROOM_H):
for xx in range(rx, rx + BOSS_ROOM_W):
tiles[yy][xx] = 1
explored, visible = _empty_grids()
return GameMap(
width=MAP_W, height=MAP_H, tiles=tiles,
rooms=[Rect(rx, ry, BOSS_ROOM_W, BOSS_ROOM_H)],
explored=explored, visible=visible, entities=[],
stairs_down=None, floor_index=floor_index,
)
def generate_dungeon(floor_index: int, rng: random.Random) -> GameMap:
if floor_index == BOSS_FLOOR:
return _boss_arena(floor_index)
tiles = [[0] * MAP_W for _ in range(MAP_H)]
interior = Rect(1, 1, MAP_W - 2, MAP_H - 2)
budget = [rng.randint(BSP_SPLITS_MIN, BSP_SPLITS_MAX)]
root = _build(interior, 0, rng, tiles, budget)
rooms: list[Rect] = []
_gather(root, rooms)
_connect(root, rng, tiles)
spawn = rooms[0].center()
far = max(rooms, key=lambda r: abs(r.center()[0] - spawn[0]) + abs(r.center()[1] - spawn[1]))
stairs_down = far.center()
explored, visible = _empty_grids()
return GameMap(
width=MAP_W, height=MAP_H, tiles=tiles, rooms=rooms,
explored=explored, visible=visible, entities=[],
stairs_down=stairs_down, floor_index=floor_index,
)
def is_wall(gmap: GameMap, x: int, y: int) -> bool:
if x < 0 or y < 0 or x >= gmap.width or y >= gmap.height:
return True
return gmap.tiles[y][x] == 0
def walkable(gmap: GameMap, x: int, y: int) -> bool:
if x < 0 or y < 0 or x >= gmap.width or y >= gmap.height:
return False
return gmap.tiles[y][x] == 1