From ca4c67f80550ec23464142414cafa7109feaeb85 Mon Sep 17 00:00:00 2001 From: Stefan Harmuth Date: Sun, 25 Dec 2022 05:19:57 +0100 Subject: [PATCH] some bug fixing around 3d grids --- tools/coordinate.py | 2 ++ tools/grid.py | 33 ++++++++++++++++++++++++--------- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/tools/coordinate.py b/tools/coordinate.py index a7c63e7..a27d7bb 100644 --- a/tools/coordinate.py +++ b/tools/coordinate.py @@ -177,6 +177,8 @@ class Coordinate: return self.__class__(self.x - other.x, self.y - other.y, self.z - other.z) def __eq__(self, other): + if not isinstance(other, Coordinate): + return False return self.x == other.x and self.y == other.y and self.z == other.z def __gt__(self, other): diff --git a/tools/grid.py b/tools/grid.py index 46a4d1e..3994b02 100644 --- a/tools/grid.py +++ b/tools/grid.py @@ -185,11 +185,13 @@ class Grid: else: return self.minX <= pos.x <= self.maxX and self.minY <= pos.y <= self.maxY - def getActiveCells(self, x: int = None, y: int = None) -> List[Coordinate]: + def getActiveCells(self, x: int = None, y: int = None, z: int = None) -> List[Coordinate]: if x: return [c for c in self.__grid.keys() if c.x == x] elif y: return [c for c in self.__grid.keys() if c.y == y] + elif z: + return [c for c in self.__grid.keys() if c.z == z] else: return list(self.__grid.keys()) @@ -293,8 +295,10 @@ class Grid: raise NotImplementedError(mode) def shift(self, shift_x: int = 0, shift_y: int = 0, shift_z: int = 0): - self.minX, self.minY, self.minZ = self.minX + shift_x, self.minY + shift_y, self.minZ + shift_z - self.maxX, self.maxY, self.minZ = self.maxX + shift_x, self.maxY + shift_y, self.minZ + shift_z + self.minX, self.minY = self.minX + shift_x, self.minY + shift_y + self.maxX, self.maxY = self.maxX + shift_x, self.maxY + shift_y + if self.mode3D: + self.minZ, self.maxZ = self.minZ + shift_z, self.maxZ + shift_z coords = self.__grid self.__grid = {} for c, v in coords.items(): @@ -308,7 +312,10 @@ class Grid: # self.shift() to (0, 0, 0) being top, left, front if recalc: self.recalcBoundaries() - self.shift(0 - self.minX, 0 - self.minY, 0 - self.minZ) + if self.mode3D: + self.shift(0 - self.minX, 0 - self.minY, 0 - self.minZ) + else: + self.shift(0 - self.minX, 0 - self.minY) def getPath_BFS(self, pos_from: Coordinate, pos_to: Coordinate, includeDiagonal: bool, walls: List[Any] = None, stop_at_first: Any = None) -> Union[None, List[Coordinate]]: @@ -433,6 +440,9 @@ class Grid: print("X", end="") else: value = self.get(pos) + if isinstance(value, list): + value = len(value) + if isinstance(value, Enum): value = value.value @@ -456,18 +466,23 @@ class Grid: ) @classmethod - def from_str(cls, grid_string: str, default: Any = False, true_char: str = '#', true_value: Any = True, translate: dict = None) -> 'Grid': + def from_str(cls, grid_string: str, default: Any = False, true_char: str = '#', true_value: Any = True, translate: dict = None, mode3d: bool = False) -> 'Grid': if translate is None: translate = {} - if true_char is not None and True not in translate.values(): + if true_char is not None and True not in translate.values() and true_char not in translate: translate[true_char] = true_value if true_value is not None else True ret = cls(default=default) for y, line in enumerate(grid_string.split("/")): for x, c in enumerate(line): - if c in translate: - ret.set(Coordinate(x, y), translate[c]) + if mode3d: + coord = Coordinate(x, y, 0) else: - ret.set(Coordinate(x, y), c) + coord = Coordinate(x, y) + + if c in translate: + ret.set(coord, translate[c]) + else: + ret.set(coord, c) return ret