Skip to content

Commit 3f00019

Browse files
committed
Go faster with numpy
1 parent 1cb26b3 commit 3f00019

File tree

1 file changed

+22
-26
lines changed

1 file changed

+22
-26
lines changed

patch_gen.py

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,43 +16,36 @@
1616
import twmap
1717

1818
def diff_layers(layer1, layer2):
19-
diffs = []
2019
if layer1.width() != layer2.width():
2120
print('Error: Layers of different width are not supported')
2221
sys.exit(1)
2322
if layer1.height() != layer2.height():
2423
print('Error: Layers of different height are not supported')
2524
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):
4441
"""
4542
Given a array of tile diffs
4643
this generates python patches
4744
"""
4845
patches = []
4946
layer_slug = re.sub(r'[^a-zA-Z_]', '', name)
5047
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):
5649
patches.append(f"{layer_slug}[{y}][{x}] = [{tile}, {flags}]")
5750
# print(diff
5851
patches.append(f"in_map.{layer}.tiles = {layer_slug}")
@@ -91,11 +84,14 @@ def gen_py_patch(base_map_file, diff_map_file):
9184
# TODO: support quads etc
9285
print(f"Warning: skipping {layer.kind()}")
9386
continue
94-
diffs = diff_layers(
87+
print(f"diffing layer")
88+
coordinates, values = diff_layers(
9589
base_map.groups[group_index].layers[layer_index],
9690
diff_map.groups[group_index].layers[layer_index])
91+
print("generating patches")
9792
patches += gen_py_layer_diff(
98-
diffs,
93+
coordinates,
94+
values,
9995
f"groups[{group_index}].layers[{layer_index}]",
10096
f"{group.name}_{layer.name}")
10197

0 commit comments

Comments
 (0)