|
| 1 | +import twmap |
| 2 | +import numpy as np |
| 3 | +from src.config.app_state import AppState |
| 4 | +from pathlib import Path |
| 5 | +from src.dialogs.dialog_check_map import CheckMapDialog |
| 6 | + |
| 7 | + |
| 8 | +class MapGenerator: |
| 9 | + def __init__(self, map_name): |
| 10 | + self._map = twmap.Map.empty("DDNet06") |
| 11 | + |
| 12 | + solid_map = np.loadtxt("data/debroijn_torus.txt", dtype=np.uint8) |
| 13 | + solid_map = np.pad(solid_map, 1, mode='constant') # add gap to borders |
| 14 | + height, width = solid_map.shape |
| 15 | + |
| 16 | + # add layers |
| 17 | + physics_group = self._map.groups.new_physics() |
| 18 | + physics_layer = physics_group.layers.new_game(width, height) |
| 19 | + |
| 20 | + # add image |
| 21 | + assert AppState.imagePath() |
| 22 | + path = Path(AppState.imagePath()).absolute() |
| 23 | + self._map.images.new_from_file(str(path)) |
| 24 | + |
| 25 | + # set physic tiles |
| 26 | + zeros = np.zeros(solid_map.shape, dtype=np.uint8) |
| 27 | + solid_map_stacked = np.stack([solid_map, zeros], axis=2) |
| 28 | + physics_layer.tiles = solid_map_stacked |
| 29 | + |
| 30 | + # add tile layer |
| 31 | + tile_layer = physics_group.layers.new_tiles(width, height) |
| 32 | + tile_layer.name = "test" |
| 33 | + tile_layer.image = 0 |
| 34 | + tile_layer_map = np.zeros(solid_map_stacked.shape, dtype=np.uint8) |
| 35 | + |
| 36 | + # TODO twmap doesn't have automappers in the python version yet, so I have to do it manually |
| 37 | + for y in range(1, height - 1): |
| 38 | + for x in range(1, width - 1): |
| 39 | + tile, status = CheckMapDialog.getMapTile(solid_map, x, y) |
| 40 | + if tile: |
| 41 | + tile_layer_map[y, x, 0] = tile.tile_id |
| 42 | + if status: |
| 43 | + tile_layer_map[y, x, 1] = MapGenerator._encodeBits(status.h_flip, status.v_flip, status.rot) |
| 44 | + tile_layer.tiles = tile_layer_map |
| 45 | + |
| 46 | + # save map |
| 47 | + self._map.save(map_name) |
| 48 | + |
| 49 | + def __del__(self): |
| 50 | + # TODO delete map |
| 51 | + pass |
| 52 | + |
| 53 | + @staticmethod |
| 54 | + def _encodeBits(h_flip: bool, v_flip: bool, rot: bool) -> int: |
| 55 | + ret: int = 0 |
| 56 | + ret += (rot << 3) |
| 57 | + ret += (h_flip << 1) |
| 58 | + ret += v_flip |
| 59 | + return ret |
0 commit comments