squirrel-battle/squirrelbattle/mapgeneration/broguelike.py

151 lines
5.2 KiB
Python

# Copyright (C) 2020 by ÿnérant, eichhornchen, nicomarg, charlse
# SPDX-License-Identifier: GPL-3.0-or-later
from enum import auto, Enum
from random import choice, random, randint, shuffle
from ..interfaces import Map, Tile
DEFAULT_PARAMS = {
"width" : 80,
"height" : 40,
"tries" : 600,
"max_rooms" : 99,
"max_room_tries" : 15,
"cross_room" : 1,
"corridor_chance" : .8,
"min_v_corr" : 2,
"max_v_corr" : 6,
"min_h_corr" : 4,
"max_h_corr" : 12,
"large_circular_room" : .10,
"circular_holes" : .5,
}
class Generator:
def __init__(self, params: dict = DEFAULT_PARAMS):
self.params = params
@staticmethod
def room_fits(level, y, x, room, door_y, door_x, dy, dx):
if level[y][x] != Tile.EMPTY or level[y-dy][x-dx] != Tile.FLOOR:
return False
lh, lw = len(level), len(level[0])
rh, rw = len(room), len(room[0])
for ry in range(rh):
for rx in range(rw):
if room[y][x] == Tile.FLOOR:
ly, lx = ry - door_y, rx - door_x
if not(0 <= ly <= rh and 0 <= lx <= rw) or \
level[ly][lx] == Tile.FLOOR:
return False
return True
def corr_meta_info(self):
if random() < self.params["corridor_chance"]:
h_sup = randint(self.params["min_h_corr"], \
self.params["max_h_corr"]) if random() < .5 else 0
w_sup = 0 if h_sup else randint(self.params["min_w_corr"], \
self.params["max_w_coor"])
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
yxs = [i for i in range(len(room) * len(room[0]))]
shuffle(xys)
for pos in yxs:
y, x = pos // len(room), pos % len(room)
if room[y][x] == Tile.EMPTY:
if room[y-dy][x-dx] == Tile.FLOOR:
build_here = True
for i in range(l):
if room[y+i*dy][x+i*dx] != Tile.EMPTY:
build_here = False
break
if build_here:
for i in range(l):
room[y+i*dy][x+i*dx] == Tile.FLOOR
break
return y+l*dy, x+l*dx, dy, dx
def create_circular_room(self):
if random() < self.params["large_circular_room"]:
r = randint(5, 10)**2
else:
r = randint(2, 4)**2
room = []
h_sup, w_sup, h_off, w_off = self.corr_meta_info()
height = 2*r+2
width = 2*r+2
make_hole = random() < self.params["circular_holes"]
if make_hole:
r2 = randint(3, r-3)
for i in range(height+h_sup):
room.append([])
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):
room[-1].append(Tile.FLOOR)
else:
room[-1].append(Tile.EMPTY)
door_y, door_x, dy, dx = self.attach_doors(room, h_sup, w_sup, h_off, w_off)
return room, doory, doorx, dy, dx
def create_random_room(self):
return create_circular_room(self)
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
mem, self.params["corridor"] = self.params["corridor"], 0
starting_room, _, _, _, _ = self.create_random_room()
self.place_room(level, height//2, width//2, 0, 0, starting_room)
self.params["corridor"] = mem
# 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
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()
positions = [i for i in range()]
shuffle(positions)
for pos in positions:
y, x = pos // height, pos % width
if self.room_fits(level, y, x, room, door_y, door_x, dy, dx):
self.place_room(level, y, x, door_y, door_x, room)
# post-processing
self.place_walls(level)
return Map(width, height, level, sy, sx)