going-rogue/roguebasin/map.py

133 lines
4.2 KiB
Python
Raw Normal View History

2022-04-30 23:29:52 -07:00
#!/usr/bin/env python3
# Eryn Wells <eryn@erynwells.me>
import logging
import numpy as np
2022-04-30 23:29:52 -07:00
import tcod
from .geometry import Point, Rect, Size
from .tile import Floor, Wall
from typing import List, Optional
2022-04-30 23:29:52 -07:00
LOG = logging.getLogger('map')
class Map:
def __init__(self, size: Size):
self.size = size
self.generator = RoomsAndCorridorsGenerator(size=size)
self.tiles = self.generator.generate()
2022-04-30 23:29:52 -07:00
def tile_is_in_bounds(self, point: Point) -> bool:
return 0 <= point.x < self.size.width and 0 <= point.y < self.size.height
def tile_is_walkable(self, point: Point) -> bool:
return self.tiles[point.x, point.y]['walkable']
2022-04-30 23:29:52 -07:00
def print_to_console(self, console: tcod.Console) -> None:
size = self.size
console.tiles_rgb[0:size.width, 0:size.height] = self.tiles["dark"]
class MapGenerator:
def __init__(self, *, size: Size):
self.size = size
def generate(self) -> np.ndarray:
'''
Generate a tile grid
Subclasses should implement this and fill in their specific map
generation algorithm.
'''
raise NotImplementedError()
class RoomsAndCorridorsGenerator(MapGenerator):
'''Generate a rooms-and-corridors style map with BSP.'''
class Configuration:
def __init__(self, min_room_size: Size):
self.minimum_room_size = min_room_size
DefaultConfiguration = Configuration(
min_room_size=Size(8, 8)
)
def __init__(self, *, size: Size, config: Optional[Configuration] = None):
super().__init__(size=size)
self.configuration = config if config else RoomsAndCorridorsGenerator.DefaultConfiguration
self.rng: tcod.random.Random = tcod.random.Random()
self.rooms: List['RectanularRoom'] = []
self.tiles: Optional[np.ndarray] = None
def generate(self) -> np.ndarray:
if self.tiles:
return self.tiles
minimum_room_size = self.configuration.minimum_room_size
# Recursively divide the map into squares of various sizes to place rooms in.
2022-04-30 23:29:52 -07:00
bsp = tcod.bsp.BSP(x=0, y=0, width=self.size.width, height=self.size.height)
bsp.split_recursive(
depth=4,
min_width=minimum_room_size.width, min_height=minimum_room_size.height,
2022-04-30 23:29:52 -07:00
max_horizontal_ratio=1.5, max_vertical_ratio=1.5)
tiles = np.full(tuple(self.size), fill_value=Wall, order='F')
# Generate the rooms
rooms: List['RectangularRoom'] = []
2022-04-30 23:29:52 -07:00
# For nicer debug logging
indent = 0
for node in bsp.pre_order():
node_bounds = self.__rect_from_bsp_node(node)
2022-04-30 23:29:52 -07:00
if node.children:
if LOG.getEffectiveLevel() == logging.DEBUG:
LOG.debug(f'{" " * indent}{node_bounds}')
2022-04-30 23:29:52 -07:00
indent += 2
# TODO: Connect the two child rooms
else:
LOG.debug(f'{" " * indent}{node_bounds} (room) {node}')
2022-04-30 23:29:52 -07:00
size = Size(self.rng.randint(5, min(15, max(5, node.width - 2))),
self.rng.randint(5, min(15, max(5, node.height - 2))))
origin = Point(node.x + self.rng.randint(1, max(1, node.width - size.width - 1)),
node.y + self.rng.randint(1, max(1, node.height - size.height - 1)))
bounds = Rect(origin, size)
2022-04-30 23:29:52 -07:00
LOG.debug(f'{" " * indent}`-> {bounds}')
room = RectangularRoom(bounds)
2022-04-30 23:29:52 -07:00
rooms.append(room)
if LOG.getEffectiveLevel() == logging.DEBUG:
indent -= 2
self.rooms = rooms
2022-04-30 23:29:52 -07:00
for room in rooms:
bounds = room.bounds
tiles[bounds.min_x:bounds.max_x, bounds.min_y:bounds.max_y] = Floor
2022-04-30 23:29:52 -07:00
self.tiles = tiles
2022-04-30 23:29:52 -07:00
return tiles
2022-05-01 17:34:57 -07:00
def generate_tunnel(self, start_room_bounds: Rect, end_room_bounds: Rect):
pass
def __rect_from_bsp_node(self, node: tcod.bsp.BSP) -> Rect:
return Rect(Point(node.x, node.y), Size(node.width, node.height))
class RectangularRoom:
def __init__(self, bounds: Rect):
self.bounds = bounds
@property
def center(self) -> Point:
2022-05-01 17:34:57 -07:00
return self.bounds.midpoint
def __repr__(self) -> str:
return f'{self.__class__.__name__}({self.bounds})'