This commit is contained in:
Yohann D'ANELLO 2021-01-10 23:49:43 +01:00
parent 2031d7fa67
commit 588357e5bf
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
1 changed files with 21 additions and 24 deletions

View File

@ -26,9 +26,11 @@ DEFAULT_PARAMS = {
"spawn_per_region": [1, 2], "spawn_per_region": [1, 2],
} }
def dist(level, y1, x1, y2, x2):
def dist(level: List[List[Tile]], y1: int, x1: int, y2: int, x2: int) -> int:
""" """
Compute the minimum walking distance between points (y1, x1) and (y2, x2) on a Tile grid Compute the minimum walking distance between points (y1, x1) and (y2, x2)
on a Tile grid
""" """
# simple breadth first search # simple breadth first search
copy = [[t for t in row] for row in level] copy = [[t for t in row] for row in level]
@ -60,9 +62,9 @@ class Generator:
room: List[List[Tile]], door_y: int, door_x: int, room: List[List[Tile]], door_y: int, door_x: int,
dy: int, dx: int) -> bool: dy: int, dx: int) -> bool:
""" """
Using point (door_y, door_x) in the room as a reference and placing it Using point (door_y, door_x) in the room as a reference and placing it
over point (y, x) in the level, returns whether or not the room fits over point (y, x) in the level, returns whether or not the room fits
here here
""" """
lh, lw = len(level), len(level[0]) lh, lw = len(level), len(level[0])
rh, rw = len(room), len(room[0]) rh, rw = len(room), len(room[0])
@ -93,7 +95,7 @@ class Generator:
def place_room(level: List[List[Tile]], y: int, x: int, def place_room(level: List[List[Tile]], y: int, x: int,
room: List[List[Tile]], door_y: int, door_x: int) -> None: room: List[List[Tile]], door_y: int, door_x: int) -> None:
""" """
Mutates level in place to add the room. Placement is determined by Mutates level in place to add the room. Placement is determined by
making (door_y, door_x) in the room correspond with (y, x) in the level making (door_y, door_x) in the room correspond with (y, x) in the level
""" """
rh, rw = len(room), len(room[0]) rh, rw = len(room), len(room[0])
@ -106,11 +108,11 @@ class Generator:
@staticmethod @staticmethod
def add_loop(level: List[List[Tile]], y: int, x: int) -> bool: def add_loop(level: List[List[Tile]], y: int, x: int) -> bool:
""" """
Try to add a corridor between two far apart floor tiles, passing Try to add a corridor between two far apart floor tiles, passing
through point (y, x). through point (y, x).
""" """
h, w = len(level), len(level[0]) h, w = len(level), len(level[0])
if level[y][x] != Tile.EMPTY: if level[y][x] != Tile.EMPTY:
return False return False
@ -128,8 +130,8 @@ class Generator:
continue continue
def verify_sides() -> bool: def verify_sides() -> bool:
# switching up dy and dx here pivots the axis, so # switching up dy and dx here pivots the axis, so
# (y+dx, x+dy) and (y-dx, x-dy) are the tiles adjacent to # (y+dx, x+dy) and (y-dx, x-dy) are the tiles adjacent to
# (y, x), but not on the original axis # (y, x), but not on the original axis
for delta_x, delta_y in [[dy, dx], [-dy, -dx]]: for delta_x, delta_y in [[dy, dx], [-dy, -dx]]:
for i in range(1, y2 - y1 + x2 - x1): for i in range(1, y2 - y1 + x2 - x1):
@ -194,8 +196,8 @@ class Generator:
dy: int, dx: int, length: int) -> bool: dy: int, dx: int, length: int) -> bool:
""" """
Tries to build the exit from the room at given coordinates Tries to build the exit from the room at given coordinates
Depending on parameter length, it will either attempt to build a Depending on parameter length, it will either attempt to build a
simple door, or a long corridor. Return value is a boolean simple door, or a long corridor. Return value is a boolean
signifying whether or not the exit was successfully built signifying whether or not the exit was successfully built
""" """
rh, rw = len(room), len(room[0]) rh, rw = len(room), len(room[0])
@ -247,15 +249,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: else: # pragma: no cover
return None, None return None, None, None, None
return y + length * dy, x + length * dx, dy, dx return y + length * dy, x + length * dx, dy, dx
def create_circular_room(self, spawnable: bool = True) \ def create_circular_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 circular in shape, and Create and return as a tile grid a room that is circular in shape, and
may have a center, also circular hole may have a center, also circular hole
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
""" """
@ -298,7 +300,7 @@ class Generator:
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]:
""" """
Randomly select a room shape and return one such room along with its Randomly select a room shape and return one such room along with its
door info. Set spawnable to False is the room should be marked as a door info. Set spawnable to False is the room should be marked as a
potential spawning region on the map potential spawning region on the map
""" """
@ -319,12 +321,12 @@ class Generator:
def update_spawnable(self, y: int, x: int) -> None: def update_spawnable(self, y: int, x: int) -> None:
""" """
Convert previous spawn positions relative to the room grid to actual Convert previous spawn positions relative to the room grid to actual
actual spawn positions on the level grid, using the position of the 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 top left corner of the room on the level, then log them as a
spawnable region spawnable region
""" """
if self.queued_area != None: if self.queued_area is not None:
translated_area = [[y+ry, x+rx] for ry, rx in self.queued_area] translated_area = [[y + ry, x + rx] for ry, rx in self.queued_area]
self.spawn_areas.append(translated_area) self.spawn_areas.append(translated_area)
self.queued_area = None self.queued_area = None
@ -333,11 +335,6 @@ class Generator:
Populate every spawnable area with some randomly chosen, randomly Populate every spawnable area with some randomly chosen, randomly
placed entity placed entity
""" """
if self.queued_area is not None:
translated_area = [[y + ry, x + rx] for ry, rx in self.queued_area]
self.spawn_areas.append(translated_area)
self.queued_area = None
min_c, max_c = self.params["spawn_per_region"] min_c, max_c = self.params["spawn_per_region"]
for region in self.spawn_areas: for region in self.spawn_areas:
entity_count = randint(min_c, max_c) entity_count = randint(min_c, max_c)