Made Map.neighbourhood a static method

This commit is contained in:
Charles Peyrat 2021-01-08 04:36:57 +01:00
parent 3229eb8ea7
commit ffa7641b21
2 changed files with 6 additions and 3 deletions

View File

@ -192,16 +192,19 @@ class Map:
dictclasses = Entity.get_all_entity_classes_in_a_dict() dictclasses = Entity.get_all_entity_classes_in_a_dict()
for entisave in d["entities"]: for entisave in d["entities"]:
self.add_entity(dictclasses[entisave["type"]](**entisave)) self.add_entity(dictclasses[entisave["type"]](**entisave))
def neighbourhood(self, y, x, large=False):
@staticmethod
def neighbourhood(grid, y, x, large=False):
""" """
Returns up to 8 nearby coordinates, in a 3x3 square around the input coordinate if large is Returns up to 8 nearby coordinates, in a 3x3 square around the input coordinate if large is
set to True, or in a 5-square cross by default. Does not return coordinates if they are out set to True, or in a 5-square cross by default. Does not return coordinates if they are out
of bounds. of bounds.
""" """
height, width = len(grid), len(grid[0])
neighbours = [] neighbours = []
dyxs = product([-1, 0, 1], [-1, 0, 1]) if large else [[0, -1], [0, 1], [-1, 0], [1, 0]] dyxs = product([-1, 0, 1], [-1, 0, 1]) if large else [[0, -1], [0, 1], [-1, 0], [1, 0]]
for dy, dx in dyxs: for dy, dx in dyxs:
if 0 < y+dy < self.height and 0 < x+dx < self.width: if 0 < y+dy < height and 0 < x+dx < width:
neighbours.append([y+dy, x+dx]) neighbours.append([y+dy, x+dx])
return neighbours return neighbours

View File

@ -106,7 +106,7 @@ class Generator:
for x in range(width): for x in range(width):
for y in range(height): for y in range(height):
if grid[y][x] == Tile.EMPTY: if grid[y][x] == Tile.EMPTY:
c = sum([1 if grid[j][i] == Tile.FLOOR else 0 for j, i in result.neighbourhood(y, x, large=True)]) c = sum([1 if grid[j][i] == Tile.FLOOR else 0 for j, i in Map.neighbourhood(grid, y, x, large=True)])
if c == 4 and self.params["no_lone_walls"]: if c == 4 and self.params["no_lone_walls"]:
result.tiles[y][x] = Tile.FLOOR result.tiles[y][x] = Tile.FLOOR
elif c > 0: elif c > 0: