Skip to content

Commit 30433cc

Browse files
committed
add MapGenerator for ddnet checker, use twmap-py for it!
1 parent c9e54c8 commit 30433cc

File tree

4 files changed

+70
-7
lines changed

4 files changed

+70
-7
lines changed

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ pyqt6==6.7.0
44
flake8==7.0.0
55
pyinstaller
66
pyyaml
7-
coverage
7+
coverage
8+
twmap

src/backend/map_generator.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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

src/dialogs/dialog_check_map.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def __init__(self, parent, title, cancel, *args, **kwargs):
2323
self.layout.setVerticalSpacing(0)
2424

2525
# load torus
26-
solid_map = np.loadtxt("data/debroijn_torus.txt", dtype=np.int8)
26+
solid_map = np.loadtxt("data/debroijn_torus.txt", dtype=np.uint8)
2727
height, width = solid_map.shape
2828

2929
# add stretch in front and after map for central alignment
@@ -45,7 +45,7 @@ def __init__(self, parent, title, cancel, *args, **kwargs):
4545
grid_y = map_y + 1
4646
grid_x = map_x + 1
4747

48-
tile, status = CheckMapDialog._getMapTile(solid_map, map_x, map_y)
48+
tile, status = CheckMapDialog.getMapTile(solid_map, map_x, map_y)
4949
if not tile:
5050
tile = BaseTile(-1) # Empty tile
5151
if solid_map[map_y, map_x] == 0:
@@ -98,7 +98,7 @@ def _getMapNeighborhood(solid_map: np.ndarray, x: int, y: int) -> List[int]:
9898
return neighbors
9999

100100
@staticmethod
101-
def _getMapTile(solid_map: np.ndarray, x, y) -> Tuple[Optional[BaseTile], Optional[TileStatus]]:
101+
def getMapTile(solid_map: np.ndarray, x, y) -> Tuple[Optional[BaseTile], Optional[TileStatus]]:
102102
if solid_map[y, x] == 0:
103103
return None, None # TODO this isn't true
104104
neighbors = CheckMapDialog._getMapNeighborhood(solid_map, x, y)

src/widgets/widget_mapper_generator.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from src.config.app_state import AppState, ApplicationStatusEnum
99
from src.dialogs.dialog_check_map import CheckMapDialog
1010
from src.config.config_manager import ConfigManager
11+
from src.backend.map_generator import MapGenerator
1112

1213

1314
class MapperGeneratorWidget(QWidget):
@@ -137,13 +138,15 @@ def startDDNetCheck(self):
137138
# add tmp mapping rule
138139
# automap map with debroijn torus
139140
# open map with ddnet
140-
map_name = ""
141+
map_name = "tmp_map.map"
142+
MapGenerator(map_name)
143+
141144
client_path = ConfigManager.instance().config()["client_path"]
142145
if not client_path:
143146
self.ddnet_push_button.setDisabled(True)
144147
return
145-
cmd = [client_path, map_name]
146-
subprocess.Popen(cmd, start_new_session=True)
148+
#cmd = [client_path, map_name]
149+
#subprocess.Popen(cmd, start_new_session=True)
147150

148151
def rulesLoaded(self):
149152
rules = AppState.ruleManager().getRules()

0 commit comments

Comments
 (0)