|
16 | 16 | import twmap
|
17 | 17 |
|
18 | 18 | def diff_layers(layer1, layer2):
|
19 |
| - diffs = [] |
20 | 19 | if layer1.width() != layer2.width():
|
21 | 20 | print('Error: Layers of different width are not supported')
|
22 | 21 | sys.exit(1)
|
23 | 22 | if layer1.height() != layer2.height():
|
24 | 23 | print('Error: Layers of different height are not supported')
|
25 | 24 | sys.exit(1)
|
26 |
| - for (y, x, flags), tile in numpy.ndenumerate(layer1.tiles): |
27 |
| - if flags != 0: |
28 |
| - # TODO: support flag diffs |
29 |
| - continue |
30 |
| - cmp_tile = layer2.tiles[y][x] |
31 |
| - # print('##') |
32 |
| - # print(f"x={x} y={y} tile={tile} cmp={cmp_tile[0]}") |
33 |
| - if tile == cmp_tile[0]: |
34 |
| - # skip matches |
35 |
| - continue |
36 |
| - diffs.append({ |
37 |
| - 'x': x, |
38 |
| - 'y': y, |
39 |
| - 'flags': cmp_tile[1], |
40 |
| - 'tile': cmp_tile[0]}) |
41 |
| - return diffs |
42 |
| - |
43 |
| -def gen_py_layer_diff(diffs, layer, name): |
| 25 | + |
| 26 | + a = layer1.tiles |
| 27 | + b = layer2.tiles |
| 28 | + |
| 29 | + axes = numpy.where(a != b)[:2] |
| 30 | + axes = numpy.transpose(axes) |
| 31 | + axes = numpy.unique(axes, axis=0) |
| 32 | + |
| 33 | + axes_ = (axes[:,0], axes[:,1]) |
| 34 | + |
| 35 | + coordinates = axes |
| 36 | + values = b[axes_] |
| 37 | + |
| 38 | + return coordinates, values |
| 39 | + |
| 40 | +def gen_py_layer_diff(coordinates, values, layer, name): |
44 | 41 | """
|
45 | 42 | Given a array of tile diffs
|
46 | 43 | this generates python patches
|
47 | 44 | """
|
48 | 45 | patches = []
|
49 | 46 | layer_slug = re.sub(r'[^a-zA-Z_]', '', name)
|
50 | 47 | patches.append(f"{layer_slug} = in_map.{layer}.tiles")
|
51 |
| - for diff in diffs: |
52 |
| - x = diff['x'] |
53 |
| - y = diff['y'] |
54 |
| - tile = diff['tile'] |
55 |
| - flags = diff['flags'] |
| 48 | + for y, x, tile, flags in numpy.concatenate((coordinates, values), axis=1): |
56 | 49 | patches.append(f"{layer_slug}[{y}][{x}] = [{tile}, {flags}]")
|
57 | 50 | # print(diff
|
58 | 51 | patches.append(f"in_map.{layer}.tiles = {layer_slug}")
|
@@ -91,11 +84,14 @@ def gen_py_patch(base_map_file, diff_map_file):
|
91 | 84 | # TODO: support quads etc
|
92 | 85 | print(f"Warning: skipping {layer.kind()}")
|
93 | 86 | continue
|
94 |
| - diffs = diff_layers( |
| 87 | + print(f"diffing layer") |
| 88 | + coordinates, values = diff_layers( |
95 | 89 | base_map.groups[group_index].layers[layer_index],
|
96 | 90 | diff_map.groups[group_index].layers[layer_index])
|
| 91 | + print("generating patches") |
97 | 92 | patches += gen_py_layer_diff(
|
98 |
| - diffs, |
| 93 | + coordinates, |
| 94 | + values, |
99 | 95 | f"groups[{group_index}].layers[{layer_index}]",
|
100 | 96 | f"{group.name}_{layer.name}")
|
101 | 97 |
|
|
0 commit comments