import base64 import struct from typing import Dict class Tile: def __init__(self, type_id: int, flags: int = 0, variant: int = 0): self.type_id = type_id self.flags = flags self.variant = variant def __repr__(self): return f"Tile {self.type_id}, {self.flags}, {self.variant}" def __eq__(self, other): return ( self.type_id == other.type_id and self.flags == other.flags and self.variant == other.variant ) class MapChunk: def __init__(self, x: int, y: int, chunk_size: int): self.x = x self.y = y self.chunk_size = chunk_size self.tiles = [[Tile(0) for _ in range(chunk_size)] for _ in range(chunk_size)] def __repr__(self): return f"MapChunk ({self.x}, {self.y}) - Size {self.chunk_size}" def set_tile(self, x: int, y: int, tile: Tile): self.tiles[y][x] = tile def get_tile(self, x: int, y: int) -> Tile: return self.tiles[y][x] def deserialize_map_chunk( node: Dict[str, any], tile_map: Dict[int, str], chunk_size: int = 16 ) -> MapChunk: ind = tuple(map(int, node["ind"].split(","))) chunk = MapChunk(ind[0], ind[1], chunk_size) tile_bytes = base64.b64decode(node["tiles"]) version = node.get("version", 1) idx = 0 for y in range(chunk_size): for x in range(chunk_size): if version < 6: tile_id = struct.unpack_from(" Dict[str, any]: root = { "ind": f"{chunk.x},{chunk.y}", "version": version, "tiles": encode_tiles(chunk), } return root def encode_tiles(chunk: MapChunk) -> str: tile_bytes = bytearray() for y in range(chunk.chunk_size): for x in range(chunk.chunk_size): tile = chunk.get_tile(x, y) tile_bytes.extend(struct.pack("