Add new room type : rectangular rooms

This commit is contained in:
Charles Peyrat 2021-01-16 00:35:41 +01:00
parent c854d41579
commit 38878687c4
1 changed files with 45 additions and 7 deletions

View File

@ -25,8 +25,9 @@ DEFAULT_PARAMS = {
"loop_threshold": 15, "loop_threshold": 15,
"spawn_per_region": [1, 2], "spawn_per_region": [1, 2],
"room_chances" : { "room_chances" : {
"circular" : 5, "circular" : 3,
"chunks" : 1, "chunks" : 2,
"rectangle" : 1,
}, },
} }
@ -216,7 +217,8 @@ class Generator:
return False return False
# see if the path ahead is clear. needed in the case of non convex room # see if the path ahead is clear. needed in the case of non convex room
for i in range(length + 1): for i in range(length + 1):
if room[y + i * dy][x + i * dx] != Tile.EMPTY: if not(0 <= y + i * dy < rh and 0 <= x + i * dx < rw) \
or room[y + i * dy][x + i * dx] != Tile.EMPTY:
return False return False
for i in range(length): for i in range(length):
room[y + i * dy][x + i * dx] = Tile.FLOOR room[y + i * dy][x + i * dx] = Tile.FLOOR
@ -252,17 +254,15 @@ class Generator:
if room[y][x] == Tile.EMPTY and \ if room[y][x] == Tile.EMPTY and \
Generator.build_door(room, y, x, dy, dx, length): Generator.build_door(room, y, x, dy, dx, length):
break break
else:
return None, None
return y + length * dy, x + length * dx, dy, dx return y + length * dy, x + length * dx, dy, dx
def create_chunk_room(self, spawnable: bool = True) \ def create_chunk_room(self, spawnable: bool = True) \
-> Tuple[List[List[Tile]], int, int, int, int]: -> Tuple[List[List[Tile]], int, int, int, int]:
""" """
Create and return as a tile grid a room that is composed of multiple create and return as a tile grid a room that is composed of multiple
overlapping circles of the same radius overlapping circles of the same radius
Also return door info so we know how to place the room in the level also return door info so we know how to place the room in the level
""" """
height, width = 15, 15 height, width = 15, 15
nb_chunks, r = 6, 3 nb_chunks, r = 6, 3
@ -341,6 +341,42 @@ class Generator:
return room, door_y, door_x, dy, dx return room, door_y, door_x, dy, dx
def create_rectangle_room(self, spawnable: bool = True) \
-> Tuple[List[List[Tile]], int, int, int, int]:
"""
create and return as a tile grid a rectangular room
also return door info so we know how to place the room in the level
"""
shrt, lng = randint(3, 6), randint(6, 12)
if random() < .5:
height, width = shrt, lng
else:
height, width = lng, shrt
room = []
h_sup, w_sup, h_off, w_off = self.corr_meta_info()
min_w, max_w = w_off + 1, width + w_off
min_h, max_h = h_off + 1, height + h_off
for i in range(height + h_sup + 2):
room.append([])
for j in range(width + w_sup + 2):
if min_h <= i <= max_h and min_w <= j <= max_w:
room[-1].append(Tile.FLOOR)
else:
room[-1].append(Tile.EMPTY)
# log all placed tiles as spawn positions
if spawnable:
self.register_spawn_area(room)
# attach exit
door_y, door_x, dy, dx = self.attach_door(room, h_sup, w_sup,
h_off, w_off)
return room, door_y, door_x, dy, dx
def create_random_room(self, spawnable: bool = True) \ def create_random_room(self, spawnable: bool = True) \
-> Tuple[List[list], int, int, int, int]: -> Tuple[List[list], int, int, int, int]:
""" """
@ -361,6 +397,8 @@ class Generator:
return self.create_circular_room(spawnable = spawnable) return self.create_circular_room(spawnable = spawnable)
elif key == "chunks": elif key == "chunks":
return self.create_chunk_room(spawnable = spawnable) return self.create_chunk_room(spawnable = spawnable)
elif key == "rectangle":
return self.create_rectangle_room(spawnable = spawnable)
def register_spawn_area(self, area: List[List[Tile]]) -> None: def register_spawn_area(self, area: List[List[Tile]]) -> None:
""" """