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()
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