some bug fixing around 3d grids

This commit is contained in:
Stefan Harmuth 2022-12-25 05:19:57 +01:00
parent 9963a21821
commit ca4c67f805
2 changed files with 26 additions and 9 deletions

View File

@ -177,6 +177,8 @@ class Coordinate:
return self.__class__(self.x - other.x, self.y - other.y, self.z - other.z) return self.__class__(self.x - other.x, self.y - other.y, self.z - other.z)
def __eq__(self, other): 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 return self.x == other.x and self.y == other.y and self.z == other.z
def __gt__(self, other): def __gt__(self, other):

View File

@ -185,11 +185,13 @@ class Grid:
else: else:
return self.minX <= pos.x <= self.maxX and self.minY <= pos.y <= self.maxY 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: if x:
return [c for c in self.__grid.keys() if c.x == x] return [c for c in self.__grid.keys() if c.x == x]
elif y: elif y:
return [c for c in self.__grid.keys() if c.y == 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: else:
return list(self.__grid.keys()) return list(self.__grid.keys())
@ -293,8 +295,10 @@ class Grid:
raise NotImplementedError(mode) raise NotImplementedError(mode)
def shift(self, shift_x: int = 0, shift_y: int = 0, shift_z: int = 0): 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.minX, self.minY = self.minX + shift_x, self.minY + shift_y
self.maxX, self.maxY, self.minZ = self.maxX + shift_x, self.maxY + shift_y, self.minZ + shift_z 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 coords = self.__grid
self.__grid = {} self.__grid = {}
for c, v in coords.items(): for c, v in coords.items():
@ -308,7 +312,10 @@ class Grid:
# self.shift() to (0, 0, 0) being top, left, front # self.shift() to (0, 0, 0) being top, left, front
if recalc: if recalc:
self.recalcBoundaries() self.recalcBoundaries()
if self.mode3D:
self.shift(0 - self.minX, 0 - self.minY, 0 - self.minZ) 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, 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]]: stop_at_first: Any = None) -> Union[None, List[Coordinate]]:
@ -433,6 +440,9 @@ class Grid:
print("X", end="") print("X", end="")
else: else:
value = self.get(pos) value = self.get(pos)
if isinstance(value, list):
value = len(value)
if isinstance(value, Enum): if isinstance(value, Enum):
value = value.value value = value.value
@ -456,18 +466,23 @@ class Grid:
) )
@classmethod @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: if translate is None:
translate = {} 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 translate[true_char] = true_value if true_value is not None else True
ret = cls(default=default) ret = cls(default=default)
for y, line in enumerate(grid_string.split("/")): for y, line in enumerate(grid_string.split("/")):
for x, c in enumerate(line): for x, c in enumerate(line):
if c in translate: if mode3d:
ret.set(Coordinate(x, y), translate[c]) coord = Coordinate(x, y, 0)
else: 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 return ret