2020-12-18 19:02:37 +00:00
|
|
|
# Copyright (C) 2020 by ÿnérant, eichhornchen, nicomarg, charlse
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2021-01-10 21:19:15 +00:00
|
|
|
from random import choice, choices, randint, random, shuffle
|
2021-01-08 15:55:02 +00:00
|
|
|
from typing import List, Tuple
|
2020-12-18 19:02:37 +00:00
|
|
|
|
2021-01-10 21:19:15 +00:00
|
|
|
from ..interfaces import Entity, Map, Tile
|
2020-12-18 19:02:37 +00:00
|
|
|
|
|
|
|
DEFAULT_PARAMS = {
|
2021-01-08 15:55:02 +00:00
|
|
|
"width": 120,
|
|
|
|
"height": 35,
|
|
|
|
"tries": 300,
|
|
|
|
"max_rooms": 20,
|
|
|
|
"max_room_tries": 15,
|
|
|
|
"cross_room": 1,
|
|
|
|
"corridor_chance": .6,
|
|
|
|
"min_v_corr": 2,
|
|
|
|
"max_v_corr": 6,
|
|
|
|
"min_h_corr": 4,
|
|
|
|
"max_h_corr": 12,
|
|
|
|
"large_circular_room": .10,
|
|
|
|
"circular_holes": .5,
|
2021-01-10 21:08:42 +00:00
|
|
|
"loop_tries": 40,
|
|
|
|
"loop_max": 5,
|
|
|
|
"loop_threshold": 15,
|
|
|
|
"spawn_per_region": [1, 2],
|
2020-12-18 19:02:37 +00:00
|
|
|
}
|
|
|
|
|
2021-01-10 22:49:43 +00:00
|
|
|
|
|
|
|
def dist(level: List[List[Tile]], y1: int, x1: int, y2: int, x2: int) -> int:
|
2021-01-10 21:54:48 +00:00
|
|
|
"""
|
2021-01-10 22:49:43 +00:00
|
|
|
Compute the minimum walking distance between points (y1, x1) and (y2, x2)
|
|
|
|
on a Tile grid
|
2021-01-10 21:54:48 +00:00
|
|
|
"""
|
|
|
|
# simple breadth first search
|
2021-01-10 21:08:42 +00:00
|
|
|
copy = [[t for t in row] for row in level]
|
2021-01-10 20:30:18 +00:00
|
|
|
dist = -1
|
|
|
|
queue, next_queue = [[y1, x1]], [0]
|
|
|
|
while next_queue:
|
|
|
|
next_queue = []
|
|
|
|
dist += 1
|
|
|
|
while queue:
|
|
|
|
y, x = queue.pop()
|
|
|
|
copy[y][x] = Tile.EMPTY
|
2021-01-10 21:08:42 +00:00
|
|
|
if y == y2 and x == x2:
|
2021-01-10 20:30:18 +00:00
|
|
|
return dist
|
|
|
|
for y, x in Map.neighbourhood(copy, y, x):
|
|
|
|
if copy[y][x].can_walk():
|
|
|
|
next_queue.append([y, x])
|
|
|
|
queue = next_queue
|
|
|
|
return -1
|
2021-01-08 15:55:02 +00:00
|
|
|
|
2021-01-10 21:08:42 +00:00
|
|
|
|
2020-12-18 19:02:37 +00:00
|
|
|
class Generator:
|
2021-01-08 15:55:02 +00:00
|
|
|
def __init__(self, params: dict = None):
|
|
|
|
self.params = params or DEFAULT_PARAMS
|
2021-01-10 20:49:39 +00:00
|
|
|
self.spawn_areas = []
|
|
|
|
self.queued_area = None
|
2020-12-18 19:02:37 +00:00
|
|
|
|
2021-01-08 02:38:37 +00:00
|
|
|
@staticmethod
|
2021-01-08 15:55:02 +00:00
|
|
|
def room_fits(level: List[List[Tile]], y: int, x: int,
|
|
|
|
room: List[List[Tile]], door_y: int, door_x: int,
|
|
|
|
dy: int, dx: int) -> bool:
|
2021-01-10 21:54:48 +00:00
|
|
|
"""
|
2021-01-10 22:49:43 +00:00
|
|
|
Using point (door_y, door_x) in the room as a reference and placing it
|
2021-01-10 21:54:48 +00:00
|
|
|
over point (y, x) in the level, returns whether or not the room fits
|
2021-01-10 22:49:43 +00:00
|
|
|
here
|
2021-01-10 21:54:48 +00:00
|
|
|
"""
|
2021-01-08 02:38:37 +00:00
|
|
|
lh, lw = len(level), len(level[0])
|
|
|
|
rh, rw = len(room), len(room[0])
|
2021-01-08 15:55:02 +00:00
|
|
|
if not(0 < y + dy < lh and 0 < x + dx < lw):
|
2021-01-08 05:54:01 +00:00
|
|
|
return False
|
2021-01-10 21:54:48 +00:00
|
|
|
# door must be placed on an empty tile, and point into a floor tile
|
2021-01-08 15:55:02 +00:00
|
|
|
if level[y][x] != Tile.EMPTY or level[y + dy][x + dx] != Tile.FLOOR:
|
2021-01-08 05:54:01 +00:00
|
|
|
return False
|
2021-01-10 21:54:48 +00:00
|
|
|
# now we verify floor tiles in both grids do not overlap
|
2021-01-08 02:38:37 +00:00
|
|
|
for ry in range(rh):
|
|
|
|
for rx in range(rw):
|
2021-01-08 04:14:32 +00:00
|
|
|
if room[ry][rx] == Tile.FLOOR:
|
|
|
|
ly, lx = y + ry - door_y, x + rx - door_x
|
2021-01-08 06:38:47 +00:00
|
|
|
# tile must be in bounds and empty
|
2021-01-08 05:54:01 +00:00
|
|
|
if not(0 <= ly < lh and 0 <= lx < lw) or \
|
2021-01-08 02:38:37 +00:00
|
|
|
level[ly][lx] == Tile.FLOOR:
|
|
|
|
return False
|
2021-01-08 06:38:47 +00:00
|
|
|
# so do all neighbouring tiles bc we may
|
|
|
|
# need to place walls there eventually
|
2021-01-08 15:55:02 +00:00
|
|
|
for ny, nx in Map.neighbourhood(level, ly, lx,
|
|
|
|
large=True, oob=True):
|
2021-01-08 06:38:47 +00:00
|
|
|
if not(0 <= ny < lh and 0 <= nx < lw) or \
|
|
|
|
level[ny][nx] != Tile.EMPTY:
|
|
|
|
return False
|
2021-01-08 02:38:37 +00:00
|
|
|
return True
|
2021-01-08 02:45:26 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2021-01-08 15:55:02 +00:00
|
|
|
def place_room(level: List[List[Tile]], y: int, x: int,
|
|
|
|
room: List[List[Tile]], door_y: int, door_x: int) -> None:
|
2021-01-10 21:54:48 +00:00
|
|
|
"""
|
2021-01-10 22:49:43 +00:00
|
|
|
Mutates level in place to add the room. Placement is determined by
|
2021-01-10 21:54:48 +00:00
|
|
|
making (door_y, door_x) in the room correspond with (y, x) in the level
|
|
|
|
"""
|
2021-01-08 02:45:26 +00:00
|
|
|
rh, rw = len(room), len(room[0])
|
2021-01-10 21:51:01 +00:00
|
|
|
level[y][x] = Tile.DOOR
|
2021-01-08 02:45:26 +00:00
|
|
|
for ry in range(rh):
|
|
|
|
for rx in range(rw):
|
2021-01-08 04:14:32 +00:00
|
|
|
if room[ry][rx] == Tile.FLOOR:
|
2021-01-08 15:55:02 +00:00
|
|
|
level[y - door_y + ry][x - door_x + rx] = Tile.FLOOR
|
2021-01-08 03:43:10 +00:00
|
|
|
|
2021-01-10 20:30:18 +00:00
|
|
|
@staticmethod
|
2021-01-10 21:08:42 +00:00
|
|
|
def add_loop(level: List[List[Tile]], y: int, x: int) -> bool:
|
2021-01-10 21:54:48 +00:00
|
|
|
"""
|
2021-01-10 22:49:43 +00:00
|
|
|
Try to add a corridor between two far apart floor tiles, passing
|
2021-01-10 21:54:48 +00:00
|
|
|
through point (y, x).
|
|
|
|
"""
|
2021-01-10 20:30:18 +00:00
|
|
|
h, w = len(level), len(level[0])
|
2021-01-10 22:49:43 +00:00
|
|
|
|
2021-01-10 20:30:18 +00:00
|
|
|
if level[y][x] != Tile.EMPTY:
|
|
|
|
return False
|
2021-01-10 21:54:48 +00:00
|
|
|
|
|
|
|
# loop over both directions, trying to place both veritcal
|
|
|
|
# and horizontal corridors
|
2021-01-10 20:30:18 +00:00
|
|
|
for dx, dy in [[0, 1], [1, 0]]:
|
2021-01-10 21:54:48 +00:00
|
|
|
# then we find two floor tiles, one on each side of (y, x)
|
|
|
|
# exiting if we don't find two (reach the edge of the map before)
|
2021-01-10 20:30:18 +00:00
|
|
|
y1, x1, y2, x2 = y, x, y, x
|
|
|
|
while x1 >= 0 and y1 >= 0 and level[y1][x1] == Tile.EMPTY:
|
|
|
|
y1, x1 = y1 - dy, x1 - dx
|
|
|
|
while x2 < w and y2 < h and level[y2][x2] == Tile.EMPTY:
|
|
|
|
y2, x2 = y2 + dy, x2 + dx
|
|
|
|
if not(0 <= x1 <= x2 < w and 0 <= y1 <= y2 < h):
|
|
|
|
continue
|
|
|
|
|
2021-01-10 21:08:42 +00:00
|
|
|
def verify_sides() -> bool:
|
2021-01-10 22:49:43 +00:00
|
|
|
# switching up dy and dx here pivots the axis, so
|
|
|
|
# (y+dx, x+dy) and (y-dx, x-dy) are the tiles adjacent to
|
2021-01-10 21:54:48 +00:00
|
|
|
# (y, x), but not on the original axis
|
2021-01-10 21:08:42 +00:00
|
|
|
for delta_x, delta_y in [[dy, dx], [-dy, -dx]]:
|
|
|
|
for i in range(1, y2 - y1 + x2 - x1):
|
|
|
|
if not (0 <= y1 + delta_y + i * dy < h
|
|
|
|
and 0 <= x1 + delta_x + i * dx < w) or \
|
|
|
|
level[y1 + delta_y + i * dy][x1 + delta_x
|
|
|
|
+ i * dx]\
|
|
|
|
.can_walk():
|
2021-01-10 20:30:18 +00:00
|
|
|
return False
|
|
|
|
return True
|
2021-01-10 21:54:48 +00:00
|
|
|
# if adding the path would make the two tiles significantly closer
|
|
|
|
# and its sides don't touch already placed terrain, build it
|
2021-01-10 20:30:18 +00:00
|
|
|
if dist(level, y1, x1, y2, x2) < 20 and verify_sides():
|
2021-01-10 21:08:42 +00:00
|
|
|
y, x = y1 + dy, x1 + dx
|
2021-01-10 20:30:18 +00:00
|
|
|
while level[y][x] == Tile.EMPTY:
|
|
|
|
level[y][x] = Tile.FLOOR
|
2021-01-10 21:08:42 +00:00
|
|
|
y, x = y + dy, x + dx
|
2021-01-10 20:30:18 +00:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2021-01-08 03:43:10 +00:00
|
|
|
@staticmethod
|
2021-01-08 15:55:02 +00:00
|
|
|
def place_walls(level: List[List[Tile]]) -> None:
|
2021-01-10 21:54:48 +00:00
|
|
|
"""
|
|
|
|
Place wall tiles on every empty tile that is adjacent (in the largest
|
|
|
|
sense), to a floor tile
|
|
|
|
"""
|
2021-01-08 03:43:10 +00:00
|
|
|
h, w = len(level), len(level[0])
|
|
|
|
for y in range(h):
|
|
|
|
for x in range(w):
|
2021-01-08 16:10:30 +00:00
|
|
|
if not level[y][x].is_wall():
|
|
|
|
for ny, nx in Map.neighbourhood(level, y, x, large=True):
|
2021-01-08 06:03:49 +00:00
|
|
|
if level[ny][nx] == Tile.EMPTY:
|
|
|
|
level[ny][nx] = Tile.WALL
|
2021-01-08 15:55:02 +00:00
|
|
|
|
|
|
|
def corr_meta_info(self) -> Tuple[int, int, int, int]:
|
2021-01-10 21:54:48 +00:00
|
|
|
"""
|
|
|
|
Return info about the extra grid space that should be allocated for the
|
|
|
|
room, and where the room should be built along this extra grid space.
|
|
|
|
Because grids are usually thight around the room, this gives us extra
|
|
|
|
place to add a corridor later. Corridor length and orientation is
|
|
|
|
implicitly derived from this info.
|
|
|
|
|
|
|
|
h_sup and w_sup represent the extra needed space along each axis,
|
|
|
|
and h_off and w_off are the offset at which to build the room
|
|
|
|
"""
|
2021-01-07 06:06:08 +00:00
|
|
|
if random() < self.params["corridor_chance"]:
|
2021-01-08 15:55:02 +00:00
|
|
|
h_sup = randint(self.params["min_v_corr"],
|
|
|
|
self.params["max_v_corr"]) if random() < .5 else 0
|
2021-01-10 21:54:48 +00:00
|
|
|
# we only allow extra space allocation along one axis,
|
|
|
|
# because there won't more than one exit corridor
|
2021-01-08 15:55:02 +00:00
|
|
|
w_sup = 0 if h_sup else randint(self.params["min_h_corr"],
|
|
|
|
self.params["max_h_corr"])
|
2021-01-10 21:54:48 +00:00
|
|
|
# implicitly choose which direction along the axis
|
|
|
|
# the corridor will be pointing to
|
2021-01-07 06:06:08 +00:00
|
|
|
h_off = h_sup if random() < .5 else 0
|
|
|
|
w_off = w_sup if random() < .5 else 0
|
|
|
|
return h_sup, w_sup, h_off, w_off
|
|
|
|
return 0, 0, 0, 0
|
|
|
|
|
2021-01-10 20:30:18 +00:00
|
|
|
@staticmethod
|
2021-01-10 21:08:42 +00:00
|
|
|
def build_door(room: List[List[Tile]], y: int, x: int,
|
|
|
|
dy: int, dx: int, length: int) -> bool:
|
2021-01-10 21:54:48 +00:00
|
|
|
"""
|
|
|
|
Tries to build the exit from the room at given coordinates
|
2021-01-10 22:49:43 +00:00
|
|
|
Depending on parameter length, it will either attempt to build a
|
|
|
|
simple door, or a long corridor. Return value is a boolean
|
2021-01-10 21:54:48 +00:00
|
|
|
signifying whether or not the exit was successfully built
|
|
|
|
"""
|
2021-01-10 20:30:18 +00:00
|
|
|
rh, rw = len(room), len(room[0])
|
|
|
|
# verify we are pointing away from a floor tile
|
|
|
|
if not(0 <= y - dy < rh and 0 <= x - dx < rw) \
|
|
|
|
or room[y - dy][x - dx] != Tile.FLOOR:
|
|
|
|
return False
|
|
|
|
# verify there's no other floor tile around us
|
|
|
|
for ny, nx in [[y + dy, x + dx], [y - dx, x - dy],
|
|
|
|
[y + dx, x + dy]]:
|
|
|
|
if 0 <= ny < rh and 0 <= nx < rw \
|
|
|
|
and room[ny][nx] != Tile.EMPTY:
|
|
|
|
return False
|
2021-01-10 21:54:48 +00:00
|
|
|
# see if the path ahead is clear. needed in the case of non convex room
|
2021-01-10 21:08:42 +00:00
|
|
|
for i in range(length + 1):
|
2021-01-10 20:30:18 +00:00
|
|
|
if room[y + i * dy][x + i * dx] != Tile.EMPTY:
|
|
|
|
return False
|
|
|
|
for i in range(length):
|
|
|
|
room[y + i * dy][x + i * dx] = Tile.FLOOR
|
|
|
|
return True
|
|
|
|
|
|
|
|
@staticmethod
|
2021-01-10 21:08:42 +00:00
|
|
|
def attach_door(room: List[List[Tile]], h_sup: int, w_sup: int,
|
|
|
|
h_off: int, w_off: int) -> Tuple[int, int, int, int]:
|
2021-01-10 21:54:48 +00:00
|
|
|
"""
|
|
|
|
Attach an exit to the room. If extra space was allocated to
|
|
|
|
the grid, make sure a corridor is properly built
|
|
|
|
"""
|
2021-01-08 15:55:02 +00:00
|
|
|
length = h_sup + w_sup
|
2021-01-07 06:06:08 +00:00
|
|
|
dy, dx = 0, 0
|
2021-01-08 15:55:02 +00:00
|
|
|
if length > 0:
|
2021-01-07 06:06:08 +00:00
|
|
|
if h_sup:
|
|
|
|
dy = -1 if h_off else 1
|
|
|
|
else:
|
|
|
|
dx = -1 if w_off else 1
|
|
|
|
else:
|
2021-01-10 21:54:48 +00:00
|
|
|
# determine door direction for rooms without corridors
|
2021-01-07 06:06:08 +00:00
|
|
|
if random() < .5:
|
|
|
|
dy = -1 if random() < .5 else 1
|
|
|
|
else:
|
|
|
|
dx = -1 if random() < .5 else 1
|
|
|
|
|
2021-01-10 21:54:48 +00:00
|
|
|
# loop over all possible positions in a random order
|
2021-01-08 06:36:31 +00:00
|
|
|
rh, rw = len(room), len(room[0])
|
|
|
|
yxs = [i for i in range(rh * rw)]
|
2021-01-08 04:14:46 +00:00
|
|
|
shuffle(yxs)
|
2021-01-08 02:43:20 +00:00
|
|
|
for pos in yxs:
|
2021-01-08 06:36:31 +00:00
|
|
|
y, x = pos // rw, pos % rw
|
2021-01-10 20:30:18 +00:00
|
|
|
if room[y][x] == Tile.EMPTY and \
|
|
|
|
Generator.build_door(room, y, x, dy, dx, length):
|
|
|
|
break
|
2021-01-10 22:49:43 +00:00
|
|
|
else: # pragma: no cover
|
|
|
|
return None, None, None, None
|
2021-01-10 20:30:18 +00:00
|
|
|
|
2021-01-08 15:55:02 +00:00
|
|
|
return y + length * dy, x + length * dx, dy, dx
|
2021-01-07 06:06:08 +00:00
|
|
|
|
2021-01-10 20:49:39 +00:00
|
|
|
def create_circular_room(self, spawnable: bool = True) \
|
|
|
|
-> Tuple[List[List[Tile]], int, int, int, int]:
|
2021-01-10 21:54:48 +00:00
|
|
|
"""
|
2021-01-10 22:49:43 +00:00
|
|
|
Create and return as a tile grid a room that is circular in shape, and
|
2021-01-10 21:54:48 +00:00
|
|
|
may have a center, also circular hole
|
|
|
|
Also return door info so we know how to place the room in the level
|
|
|
|
"""
|
2020-12-18 19:02:37 +00:00
|
|
|
if random() < self.params["large_circular_room"]:
|
2021-01-08 03:51:20 +00:00
|
|
|
r = randint(5, 10)
|
2020-12-18 19:02:37 +00:00
|
|
|
else:
|
2021-01-08 03:51:20 +00:00
|
|
|
r = randint(2, 4)
|
2020-12-18 19:02:37 +00:00
|
|
|
|
|
|
|
room = []
|
2021-01-08 15:55:02 +00:00
|
|
|
|
2021-01-07 06:06:08 +00:00
|
|
|
h_sup, w_sup, h_off, w_off = self.corr_meta_info()
|
|
|
|
|
2021-01-08 15:55:02 +00:00
|
|
|
height = 2 * r + 2
|
|
|
|
width = 2 * r + 2
|
2021-01-08 03:51:20 +00:00
|
|
|
make_hole = r > 6 and random() < self.params["circular_holes"]
|
2021-01-08 15:55:02 +00:00
|
|
|
r2 = 0
|
2020-12-18 19:02:37 +00:00
|
|
|
if make_hole:
|
2021-01-08 15:55:02 +00:00
|
|
|
r2 = randint(3, r - 3)
|
|
|
|
for i in range(height + h_sup):
|
2020-12-18 19:02:37 +00:00
|
|
|
room.append([])
|
2021-01-08 15:55:02 +00:00
|
|
|
d = (i - h_off - height // 2) ** 2
|
|
|
|
for j in range(width + w_sup):
|
|
|
|
if d + (j - w_off - width // 2) ** 2 < r ** 2 and \
|
|
|
|
(not make_hole
|
|
|
|
or d + (j - w_off - width // 2) ** 2 >= r2 ** 2):
|
2020-12-18 19:02:37 +00:00
|
|
|
room[-1].append(Tile.FLOOR)
|
|
|
|
else:
|
|
|
|
room[-1].append(Tile.EMPTY)
|
2021-01-07 06:06:08 +00:00
|
|
|
|
2021-01-10 21:54:48 +00:00
|
|
|
# log all placed tiles as spawn positions
|
2021-01-10 20:49:39 +00:00
|
|
|
if spawnable:
|
|
|
|
self.register_spawn_area(room)
|
2021-01-10 21:54:48 +00:00
|
|
|
|
|
|
|
# attach exit
|
2021-01-08 15:55:02 +00:00
|
|
|
door_y, door_x, dy, dx = self.attach_door(room, h_sup, w_sup,
|
|
|
|
h_off, w_off)
|
2021-01-07 06:06:08 +00:00
|
|
|
|
2021-01-08 04:14:46 +00:00
|
|
|
return room, door_y, door_x, dy, dx
|
2021-01-07 06:06:08 +00:00
|
|
|
|
2021-01-10 20:49:39 +00:00
|
|
|
def create_random_room(self, spawnable: bool = True) \
|
|
|
|
-> Tuple[List[list], int, int, int, int]:
|
2021-01-10 21:54:48 +00:00
|
|
|
"""
|
2021-01-10 22:49:43 +00:00
|
|
|
Randomly select a room shape and return one such room along with its
|
2021-01-10 21:54:48 +00:00
|
|
|
door info. Set spawnable to False is the room should be marked as a
|
|
|
|
potential spawning region on the map
|
|
|
|
"""
|
2021-01-08 03:48:32 +00:00
|
|
|
return self.create_circular_room()
|
2021-01-08 15:55:02 +00:00
|
|
|
|
2021-01-10 21:08:42 +00:00
|
|
|
def register_spawn_area(self, area: List[List[Tile]]) -> None:
|
2021-01-10 21:54:48 +00:00
|
|
|
"""
|
|
|
|
Register all floor positions relative to the input grid
|
|
|
|
for later use
|
|
|
|
"""
|
2021-01-10 20:49:39 +00:00
|
|
|
spawn_positions = []
|
|
|
|
for y, line in enumerate(area):
|
|
|
|
for x, tile in enumerate(line):
|
|
|
|
if tile == Tile.FLOOR:
|
|
|
|
spawn_positions.append([y, x])
|
|
|
|
self.queued_area = spawn_positions
|
|
|
|
|
2021-01-10 21:08:42 +00:00
|
|
|
def update_spawnable(self, y: int, x: int) -> None:
|
2021-01-10 21:54:48 +00:00
|
|
|
"""
|
|
|
|
Convert previous spawn positions relative to the room grid to actual
|
2021-01-10 22:49:43 +00:00
|
|
|
actual spawn positions on the level grid, using the position of the
|
|
|
|
top left corner of the room on the level, then log them as a
|
2021-01-10 21:54:48 +00:00
|
|
|
spawnable region
|
|
|
|
"""
|
2021-01-10 22:49:43 +00:00
|
|
|
if self.queued_area is not None:
|
|
|
|
translated_area = [[y + ry, x + rx] for ry, rx in self.queued_area]
|
2021-01-10 20:49:39 +00:00
|
|
|
self.spawn_areas.append(translated_area)
|
|
|
|
self.queued_area = None
|
|
|
|
|
2021-01-10 22:02:34 +00:00
|
|
|
def populate(self, rv: Map) -> None:
|
2021-01-10 21:54:48 +00:00
|
|
|
"""
|
|
|
|
Populate every spawnable area with some randomly chosen, randomly
|
|
|
|
placed entity
|
|
|
|
"""
|
2021-01-10 20:49:39 +00:00
|
|
|
min_c, max_c = self.params["spawn_per_region"]
|
|
|
|
for region in self.spawn_areas:
|
|
|
|
entity_count = randint(min_c, max_c)
|
|
|
|
for _dummy in range(entity_count):
|
|
|
|
entity = choices(Entity.get_all_entity_classes(),
|
|
|
|
weights=Entity.get_weights(), k=1)[0]()
|
|
|
|
y, x = choice(region)
|
|
|
|
entity.move(y, x)
|
|
|
|
rv.add_entity(entity)
|
|
|
|
|
2021-01-08 15:55:02 +00:00
|
|
|
def run(self) -> Map:
|
2021-01-10 21:54:48 +00:00
|
|
|
"""
|
|
|
|
Using procedural generation, build and return a full map, populated
|
|
|
|
with entities
|
|
|
|
"""
|
2021-01-08 02:19:59 +00:00
|
|
|
height, width = self.params["height"], self.params["width"]
|
2021-01-08 15:55:02 +00:00
|
|
|
level = [width * [Tile.EMPTY] for _ignored in range(height)]
|
2021-01-08 02:19:59 +00:00
|
|
|
|
|
|
|
# the starting room must have no corridor
|
2021-01-08 03:48:32 +00:00
|
|
|
mem, self.params["corridor_chance"] = self.params["corridor_chance"], 0
|
2021-01-10 21:08:42 +00:00
|
|
|
starting_room, _, _, _, _ = self.create_random_room(spawnable=False)
|
2021-01-08 14:06:38 +00:00
|
|
|
dim_v, dim_h = len(starting_room), len(starting_room[0])
|
2021-01-10 21:54:48 +00:00
|
|
|
# because Generator.room_fits checks that the exit door is correctly
|
|
|
|
# placed, but the starting room has no exit door, we find a positoin
|
|
|
|
# manually
|
2021-01-08 15:55:02 +00:00
|
|
|
pos_y, pos_x = randint(0, height - dim_v - 1),\
|
|
|
|
randint(0, width - dim_h - 1)
|
2021-01-08 14:06:38 +00:00
|
|
|
self.place_room(level, pos_y, pos_x, starting_room, 0, 0)
|
2021-01-10 21:54:48 +00:00
|
|
|
# remove the door that was placed
|
2021-01-08 14:18:13 +00:00
|
|
|
if starting_room[0][0] != Tile.FLOOR:
|
|
|
|
level[pos_y][pos_x] = Tile.EMPTY
|
2021-01-08 03:48:32 +00:00
|
|
|
self.params["corridor_chance"] = mem
|
2021-01-08 15:55:02 +00:00
|
|
|
|
2021-01-10 21:54:48 +00:00
|
|
|
# find a starting position for the player
|
2021-01-08 15:55:02 +00:00
|
|
|
sy, sx = randint(0, height - 1), randint(0, width - 1)
|
2021-01-08 02:37:10 +00:00
|
|
|
while level[sy][sx] != Tile.FLOOR:
|
2021-01-08 15:55:02 +00:00
|
|
|
sy, sx = randint(0, height - 1), randint(0, width - 1)
|
2021-01-08 16:10:42 +00:00
|
|
|
level[sy][sx] = Tile.LADDER
|
2021-01-08 02:37:10 +00:00
|
|
|
|
2021-01-10 21:54:48 +00:00
|
|
|
# now we loop until we're bored, or we've added enough rooms
|
2021-01-08 02:19:59 +00:00
|
|
|
tries, rooms_built = 0, 0
|
2021-01-08 15:55:02 +00:00
|
|
|
while tries < self.params["tries"] \
|
|
|
|
and rooms_built < self.params["max_rooms"]:
|
2021-01-08 02:19:59 +00:00
|
|
|
|
2021-01-10 21:54:48 +00:00
|
|
|
# build a room, try to fit it everywhere in a random order, and
|
|
|
|
# place it at the first possible position
|
2021-01-08 02:19:59 +00:00
|
|
|
room, door_y, door_x, dy, dx = self.create_random_room()
|
2021-01-08 05:58:02 +00:00
|
|
|
positions = [i for i in range(height * width)]
|
2021-01-08 02:19:59 +00:00
|
|
|
shuffle(positions)
|
|
|
|
for pos in positions:
|
2021-01-08 04:21:31 +00:00
|
|
|
y, x = pos // width, pos % width
|
2021-01-08 02:19:59 +00:00
|
|
|
if self.room_fits(level, y, x, room, door_y, door_x, dy, dx):
|
2021-01-10 20:49:39 +00:00
|
|
|
self.update_spawnable(y - door_y, x - door_x)
|
2021-01-08 05:58:02 +00:00
|
|
|
self.place_room(level, y, x, room, door_y, door_x)
|
2021-01-08 06:04:24 +00:00
|
|
|
rooms_built += 1
|
2021-01-08 15:51:04 +00:00
|
|
|
break
|
2021-01-08 06:04:24 +00:00
|
|
|
tries += 1
|
2021-01-08 15:55:02 +00:00
|
|
|
|
2021-01-08 02:19:59 +00:00
|
|
|
# post-processing
|
|
|
|
self.place_walls(level)
|
2021-01-10 21:54:48 +00:00
|
|
|
|
|
|
|
# because when a room is placed, it leads to exactly one previously
|
|
|
|
# placed room, the level has a tree like structure with the starting
|
|
|
|
# room as the root
|
|
|
|
# to avoid boring player backtracking, we add some cycles to the room
|
|
|
|
# graph in post processing by placing additional corridors
|
2021-01-10 20:30:18 +00:00
|
|
|
tries, loops = 0, 0
|
|
|
|
while tries < self.params["loop_tries"] and \
|
|
|
|
loops < self.params["loop_max"]:
|
|
|
|
tries += 1
|
2021-01-10 21:08:42 +00:00
|
|
|
y, x = randint(0, height - 1), randint(0, width - 1)
|
2021-01-10 20:30:18 +00:00
|
|
|
loops += self.add_loop(level, y, x)
|
2021-01-08 02:19:59 +00:00
|
|
|
|
2021-01-08 16:25:52 +00:00
|
|
|
# place an exit ladder
|
|
|
|
y, x = randint(0, height - 1), randint(0, width - 1)
|
|
|
|
while level[y][x] != Tile.FLOOR or \
|
2021-01-08 18:20:56 +00:00
|
|
|
any([level[j][i].is_wall() for j, i
|
|
|
|
in Map.neighbourhood(level, y, x, large=True)]):
|
2021-01-08 16:25:52 +00:00
|
|
|
y, x = randint(0, height - 1), randint(0, width - 1)
|
2021-01-08 18:20:56 +00:00
|
|
|
level[y][x] = Tile.LADDER
|
2021-01-08 16:25:52 +00:00
|
|
|
|
2021-01-10 20:49:39 +00:00
|
|
|
# spawn entities
|
|
|
|
rv = Map(width, height, level, sy, sx)
|
|
|
|
self.populate(rv)
|
|
|
|
|
|
|
|
return rv
|