From ffa7641b215b5ec47650a81b6432bde850509f14 Mon Sep 17 00:00:00 2001 From: Charles Peyrat Date: Fri, 8 Jan 2021 04:36:57 +0100 Subject: [PATCH] Made Map.neighbourhood a static method --- squirrelbattle/interfaces.py | 7 +++++-- squirrelbattle/mapgeneration/randomwalk.py | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/squirrelbattle/interfaces.py b/squirrelbattle/interfaces.py index ef74614..2a08abc 100644 --- a/squirrelbattle/interfaces.py +++ b/squirrelbattle/interfaces.py @@ -192,16 +192,19 @@ class Map: dictclasses = Entity.get_all_entity_classes_in_a_dict() for entisave in d["entities"]: 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 set to True, or in a 5-square cross by default. Does not return coordinates if they are out of bounds. """ + height, width = len(grid), len(grid[0]) neighbours = [] 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: + if 0 < y+dy < height and 0 < x+dx < width: neighbours.append([y+dy, x+dx]) return neighbours diff --git a/squirrelbattle/mapgeneration/randomwalk.py b/squirrelbattle/mapgeneration/randomwalk.py index 7cac8ff..913599f 100644 --- a/squirrelbattle/mapgeneration/randomwalk.py +++ b/squirrelbattle/mapgeneration/randomwalk.py @@ -106,7 +106,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.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"]: result.tiles[y][x] = Tile.FLOOR elif c > 0: