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
|
|
|
|
|
|
|
|
from enum import auto, Enum
|
2021-01-07 06:06:08 +00:00
|
|
|
from random import choice, random, randint, shuffle
|
2020-12-18 19:02:37 +00:00
|
|
|
|
|
|
|
from ..interfaces import Map, Tile
|
|
|
|
|
|
|
|
|
|
|
|
DEFAULT_PARAMS = {
|
|
|
|
"width" : 80,
|
|
|
|
"height" : 40,
|
|
|
|
"tries" : 600,
|
|
|
|
"max_rooms" : 99,
|
2021-01-07 06:06:08 +00:00
|
|
|
"max_room_tries" : 15,
|
2020-12-18 19:02:37 +00:00
|
|
|
"cross_room" : 1,
|
2021-01-07 06:06:08 +00:00
|
|
|
"corridor_chance" : .8,
|
|
|
|
"min_v_corr" : 2,
|
|
|
|
"max_v_corr" : 6,
|
|
|
|
"min_h_corr" : 4,
|
|
|
|
"max_h_corr" : 12,
|
2020-12-18 19:02:37 +00:00
|
|
|
"large_circular_room" : .10,
|
|
|
|
"circular_holes" : .5,
|
|
|
|
}
|
|
|
|
|
|
|
|
class Generator:
|
|
|
|
def __init__(self, params: dict = DEFAULT_PARAMS):
|
|
|
|
self.params = params
|
|
|
|
|
2021-01-08 02:38:37 +00:00
|
|
|
@staticmethod
|
|
|
|
def room_fits(level, y, x, room, door_y, door_x, dy, dx):
|
|
|
|
lh, lw = len(level), len(level[0])
|
|
|
|
rh, rw = len(room), len(room[0])
|
2021-01-08 05:54:01 +00:00
|
|
|
if not(0 < y+dy < lh and 0 < x+dx < lw):
|
|
|
|
return False
|
|
|
|
if level[y][x] != Tile.EMPTY or level[y+dy][x+dx] != Tile.FLOOR:
|
|
|
|
return False
|
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
|
|
|
|
for ny, nx in Map.neighbourhood(level, ly, lx, large=True, oob=True):
|
|
|
|
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 05:58:02 +00:00
|
|
|
def place_room(level, y, x, room, door_y, door_x):
|
2021-01-08 02:45:26 +00:00
|
|
|
rh, rw = len(room), len(room[0])
|
|
|
|
# maybe place Tile.DOOR here ?
|
2021-01-08 05:58:02 +00:00
|
|
|
level[y][x] = Tile.FLOOR
|
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 05:58:02 +00:00
|
|
|
level[y-door_y+ry][x-door_x+rx] = Tile.FLOOR
|
2021-01-08 03:43:10 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def place_walls(level):
|
|
|
|
h, w = len(level), len(level[0])
|
|
|
|
for y in range(h):
|
|
|
|
for x in range(w):
|
|
|
|
if level[y][x] == Tile.FLOOR:
|
2021-01-08 06:03:49 +00:00
|
|
|
for ny, nx in Map.neighbourhood(level, y, x):
|
|
|
|
if level[ny][nx] == Tile.EMPTY:
|
|
|
|
level[ny][nx] = Tile.WALL
|
2021-01-08 03:43:10 +00:00
|
|
|
|
2021-01-07 06:06:08 +00:00
|
|
|
def corr_meta_info(self):
|
|
|
|
if random() < self.params["corridor_chance"]:
|
2021-01-08 06:05:02 +00:00
|
|
|
h_sup = randint(self.params["min_v_corr"], \
|
|
|
|
self.params["max_v_corr"]) if random() < .5 else 0
|
|
|
|
w_sup = 0 if h_sup else randint(self.params["min_h_corr"], \
|
|
|
|
self.params["max_h_corr"])
|
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
|
|
|
|
|
|
|
|
def attach_door(self, room, h_sup, w_sup, h_off, w_off):
|
|
|
|
l = h_sup + w_sup
|
|
|
|
dy, dx = 0, 0
|
|
|
|
if l > 0:
|
|
|
|
if h_sup:
|
|
|
|
dy = -1 if h_off else 1
|
|
|
|
else:
|
|
|
|
dx = -1 if w_off else 1
|
|
|
|
else:
|
|
|
|
if random() < .5:
|
|
|
|
dy = -1 if random() < .5 else 1
|
|
|
|
else:
|
|
|
|
dx = -1 if random() < .5 else 1
|
|
|
|
|
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-07 06:06:08 +00:00
|
|
|
if room[y][x] == Tile.EMPTY:
|
2021-01-08 06:36:31 +00:00
|
|
|
# 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:
|
|
|
|
continue
|
|
|
|
# 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:
|
|
|
|
break
|
|
|
|
else:
|
2021-01-07 06:06:08 +00:00
|
|
|
for i in range(l):
|
|
|
|
if room[y+i*dy][x+i*dx] != Tile.EMPTY:
|
|
|
|
break
|
2021-01-08 06:36:31 +00:00
|
|
|
else:
|
2021-01-07 06:06:08 +00:00
|
|
|
for i in range(l):
|
2021-01-08 06:41:00 +00:00
|
|
|
room[y+i*dy][x+i*dx] = Tile.FLOOR
|
2021-01-07 06:06:08 +00:00
|
|
|
break
|
2021-01-08 02:43:20 +00:00
|
|
|
return y+l*dy, x+l*dx, dy, dx
|
2021-01-07 06:06:08 +00:00
|
|
|
|
|
|
|
|
2021-01-07 04:02:49 +00:00
|
|
|
def create_circular_room(self):
|
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-07 06:06:08 +00:00
|
|
|
|
|
|
|
h_sup, w_sup, h_off, w_off = self.corr_meta_info()
|
|
|
|
|
|
|
|
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"]
|
2020-12-18 19:02:37 +00:00
|
|
|
if make_hole:
|
|
|
|
r2 = randint(3, r-3)
|
2021-01-07 06:06:08 +00:00
|
|
|
for i in range(height+h_sup):
|
2020-12-18 19:02:37 +00:00
|
|
|
room.append([])
|
2021-01-07 06:06:08 +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-08 04:14:46 +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-08 02:43:20 +00:00
|
|
|
def create_random_room(self):
|
2021-01-08 03:48:32 +00:00
|
|
|
return self.create_circular_room()
|
2021-01-08 02:43:20 +00:00
|
|
|
|
2021-01-08 02:19:59 +00:00
|
|
|
def run(self):
|
|
|
|
height, width = self.params["height"], self.params["width"]
|
|
|
|
level = [[Tile.EMPTY for i in range(width)] for j in range(height)]
|
|
|
|
|
|
|
|
# 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-08 02:19:59 +00:00
|
|
|
starting_room, _, _, _, _ = self.create_random_room()
|
2021-01-08 05:58:02 +00:00
|
|
|
self.place_room(level, height//2, width//2, starting_room, 0, 0)
|
2021-01-08 06:36:31 +00:00
|
|
|
level[0][0] = Tile.EMPTY
|
2021-01-08 03:48:32 +00:00
|
|
|
self.params["corridor_chance"] = mem
|
2021-01-08 05:58:02 +00:00
|
|
|
|
2021-01-08 02:37:10 +00:00
|
|
|
# find a starting position
|
|
|
|
sy, sx = randint(0, height-1), randint(0, width-1)
|
|
|
|
while level[sy][sx] != Tile.FLOOR:
|
|
|
|
sy, sx = randint(0, height-1), randint(0, width-1)
|
|
|
|
|
|
|
|
# now we loop until we've tried enough, or we've added enough rooms
|
2021-01-08 02:19:59 +00:00
|
|
|
tries, rooms_built = 0, 0
|
|
|
|
while tries < self.params["tries"] and rooms_built < self.params["max_rooms"]:
|
|
|
|
|
|
|
|
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-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
|
|
|
|
tries += 1
|
2021-01-08 05:58:02 +00:00
|
|
|
|
2021-01-08 02:19:59 +00:00
|
|
|
# post-processing
|
|
|
|
self.place_walls(level)
|
|
|
|
|
2021-01-08 02:37:10 +00:00
|
|
|
return Map(width, height, level, sy, sx)
|