Changed Map.large_neighbourhood so we can also request only immediate neighbours, ignoring diagonals

This commit is contained in:
Charles Peyrat 2020-12-11 18:33:16 +01:00
parent d3c14a48ee
commit 7667079aa3
2 changed files with 7 additions and 6 deletions

View File

@ -180,14 +180,15 @@ class Map:
dictclasses = Entity.get_all_entity_classes_in_a_dict()
for entisave in d["entities"]:
self.add_entity(dictclasses[entisave["type"]](**entisave))
def large_neighbourhood(self, y, x):
def neighbourhood(self, y, x, large=False):
"""
Returns up to 8 nearby coordinates, in a 3x3 square around the input coordinate.
Does not return coordinates if they are out of bounds.
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
of bounds.
"""
neighbours = []
for dy, dx in product([-1, 0, 1], [-1, 0, 1]):
dyxs = product([-1, 0, 1], [-1, 0, 1]) if large else [[0, -1], [0, 1], [-1, 0], [1, 0]]
for dy, dx in dyxs:
if 0 < y+dy < self.height and 0 < x+dx < self.width:
neighbours.append([y+dy, x+dx])
return neighbours

View File

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