From 7667079aa3c026d8fb067f8453cda69bc7c3f7ef Mon Sep 17 00:00:00 2001 From: Charles Peyrat Date: Fri, 11 Dec 2020 18:33:16 +0100 Subject: [PATCH] Changed Map.large_neighbourhood so we can also request only immediate neighbours, ignoring diagonals --- squirrelbattle/interfaces.py | 11 ++++++----- squirrelbattle/mapgeneration/randomwalk.py | 2 +- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/squirrelbattle/interfaces.py b/squirrelbattle/interfaces.py index 9d13046..41b11f0 100644 --- a/squirrelbattle/interfaces.py +++ b/squirrelbattle/interfaces.py @@ -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 diff --git a/squirrelbattle/mapgeneration/randomwalk.py b/squirrelbattle/mapgeneration/randomwalk.py index b913d1c..bd3a20d 100644 --- a/squirrelbattle/mapgeneration/randomwalk.py +++ b/squirrelbattle/mapgeneration/randomwalk.py @@ -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: