2022-01-02 15:23:25 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
import random
|
|
|
|
from typing import Tuple, Iterator, TYPE_CHECKING, List
|
|
|
|
|
|
|
|
import tcod.los
|
|
|
|
|
|
|
|
import tile_types
|
2022-01-02 17:19:43 +00:00
|
|
|
import entity_factories
|
2022-01-02 15:23:25 +00:00
|
|
|
from game_map import GameMap
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
2022-01-02 17:50:33 +00:00
|
|
|
from engine import Engine
|
2022-01-02 15:23:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
class RectangularRoom:
|
|
|
|
def __init__(self, x: int, y: int, width: int, height: int):
|
|
|
|
self.x1 = x
|
|
|
|
self.y1 = y
|
|
|
|
self.x2 = x + width
|
|
|
|
self.y2 = y + height
|
|
|
|
|
|
|
|
@property
|
|
|
|
def center(self) -> Tuple[int, int]:
|
|
|
|
return (self.x1 + self.x2) // 2, (self.y1 + self.y2) // 2
|
|
|
|
|
|
|
|
@property
|
|
|
|
def inner(self) -> Tuple[slice, slice]:
|
|
|
|
return slice(self.x1 + 1, self.x2), slice(self.y1 + 1, self.y2)
|
|
|
|
|
|
|
|
def intersects(self, other: RectangularRoom) -> bool:
|
|
|
|
"""Return True if this room overlaps with another RectangularRoom."""
|
|
|
|
return (
|
2022-01-02 17:19:43 +00:00
|
|
|
self.x1 <= other.x2
|
|
|
|
and self.x2 >= other.x1
|
|
|
|
and self.y1 <= other.y2
|
|
|
|
and self.y2 >= other.y1
|
2022-01-02 15:23:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def tunnel_between(
|
2022-01-02 17:19:43 +00:00
|
|
|
start: Tuple[int, int], end: Tuple[int, int]
|
2022-01-02 15:23:25 +00:00
|
|
|
) -> Iterator[Tuple[int, int]]:
|
2022-01-02 17:19:43 +00:00
|
|
|
"""return an L shape tunel between points"""
|
2022-01-02 15:23:25 +00:00
|
|
|
x1, y1 = start
|
|
|
|
x2, y2 = end
|
|
|
|
if random.random() < 0.5:
|
|
|
|
corner_x, corner_y = x2, y1
|
|
|
|
else:
|
|
|
|
corner_x, corner_y = x1, y2
|
|
|
|
|
|
|
|
for x, y in tcod.los.bresenham((x1, y1), (corner_x, corner_y)).tolist():
|
|
|
|
yield x, y
|
|
|
|
|
|
|
|
for x, y in tcod.los.bresenham((corner_x, corner_y), (x2, y2)).tolist():
|
|
|
|
yield x, y
|
|
|
|
|
2022-01-02 21:43:44 +00:00
|
|
|
|
2022-01-02 17:19:43 +00:00
|
|
|
def place_entities(
|
2022-01-02 21:43:44 +00:00
|
|
|
room: RectangularRoom, dungeon: GameMap, maximum_monsters: int
|
|
|
|
) -> None:
|
2022-01-02 17:19:43 +00:00
|
|
|
number_of_monsters = random.randint(0, maximum_monsters)
|
|
|
|
|
|
|
|
for i in range(number_of_monsters):
|
2022-01-02 21:43:44 +00:00
|
|
|
x = random.randint(room.x1 + 1, room.x2 - 1)
|
|
|
|
y = random.randint(room.y1 + 1, room.y2 - 1)
|
2022-01-02 17:19:43 +00:00
|
|
|
if not any(entity.x == x and entity.y == y for entity in dungeon.entities):
|
|
|
|
if random.random() <= 0.8:
|
2022-01-02 21:43:44 +00:00
|
|
|
entity_factories.orc.spawn(dungeon, x, y)
|
2022-01-02 17:19:43 +00:00
|
|
|
else:
|
2022-01-02 21:43:44 +00:00
|
|
|
entity_factories.troll.spawn(dungeon, x, y)
|
|
|
|
|
2022-01-02 15:23:25 +00:00
|
|
|
|
|
|
|
def generate_dungeon(
|
2022-01-02 17:19:43 +00:00
|
|
|
max_rooms: int,
|
|
|
|
room_min_size: int,
|
|
|
|
room_max_size: int,
|
|
|
|
map_width: int,
|
|
|
|
map_height: int,
|
|
|
|
max_monsters_per_room: int,
|
2022-01-02 17:50:33 +00:00
|
|
|
engine: Engine,
|
2022-01-02 15:23:25 +00:00
|
|
|
) -> GameMap:
|
2022-01-02 17:50:33 +00:00
|
|
|
player = engine.player
|
|
|
|
dungeon = GameMap(engine, map_width, map_height, entities=[player])
|
2022-01-02 15:23:25 +00:00
|
|
|
rooms: List[RectangularRoom] = []
|
|
|
|
|
|
|
|
for room in range(max_rooms):
|
|
|
|
room_width = random.randint(room_min_size, room_max_size)
|
|
|
|
room_height = random.randint(room_min_size, room_max_size)
|
|
|
|
x = random.randint(0, dungeon.width - room_width - 1)
|
|
|
|
y = random.randint(0, dungeon.height - room_height - 1)
|
|
|
|
|
2022-01-02 17:19:43 +00:00
|
|
|
new_room = RectangularRoom(x, y, room_width, room_height)
|
2022-01-02 15:23:25 +00:00
|
|
|
|
|
|
|
if any(new_room.intersects(other_room) for other_room in rooms):
|
|
|
|
continue
|
|
|
|
|
|
|
|
dungeon.tiles[new_room.inner] = tile_types.floor
|
|
|
|
|
|
|
|
if len(rooms) == 0:
|
|
|
|
# first room
|
2022-01-02 17:50:33 +00:00
|
|
|
player.place(*new_room.center, dungeon)
|
2022-01-02 15:23:25 +00:00
|
|
|
else:
|
2022-01-02 17:19:43 +00:00
|
|
|
for x, y in tunnel_between(rooms[-1].center, new_room.center):
|
|
|
|
dungeon.tiles[x, y] = tile_types.floor
|
|
|
|
|
|
|
|
place_entities(new_room, dungeon, max_monsters_per_room)
|
2022-01-02 15:23:25 +00:00
|
|
|
|
|
|
|
rooms.append(new_room)
|
|
|
|
|
|
|
|
return dungeon
|