Skip to content

Commit

Permalink
Go faster with numpy
Browse files Browse the repository at this point in the history
  • Loading branch information
ChillerDragon committed Apr 17, 2024
1 parent 1cb26b3 commit 3f00019
Showing 1 changed file with 22 additions and 26 deletions.
48 changes: 22 additions & 26 deletions patch_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,43 +16,36 @@
import twmap

def diff_layers(layer1, layer2):
diffs = []
if layer1.width() != layer2.width():
print('Error: Layers of different width are not supported')
sys.exit(1)
if layer1.height() != layer2.height():
print('Error: Layers of different height are not supported')
sys.exit(1)
for (y, x, flags), tile in numpy.ndenumerate(layer1.tiles):
if flags != 0:
# TODO: support flag diffs
continue
cmp_tile = layer2.tiles[y][x]
# print('##')
# print(f"x={x} y={y} tile={tile} cmp={cmp_tile[0]}")
if tile == cmp_tile[0]:
# skip matches
continue
diffs.append({
'x': x,
'y': y,
'flags': cmp_tile[1],
'tile': cmp_tile[0]})
return diffs

def gen_py_layer_diff(diffs, layer, name):

a = layer1.tiles
b = layer2.tiles

axes = numpy.where(a != b)[:2]
axes = numpy.transpose(axes)
axes = numpy.unique(axes, axis=0)

axes_ = (axes[:,0], axes[:,1])

coordinates = axes
values = b[axes_]

return coordinates, values

def gen_py_layer_diff(coordinates, values, layer, name):
"""
Given a array of tile diffs
this generates python patches
"""
patches = []
layer_slug = re.sub(r'[^a-zA-Z_]', '', name)
patches.append(f"{layer_slug} = in_map.{layer}.tiles")
for diff in diffs:
x = diff['x']
y = diff['y']
tile = diff['tile']
flags = diff['flags']
for y, x, tile, flags in numpy.concatenate((coordinates, values), axis=1):
patches.append(f"{layer_slug}[{y}][{x}] = [{tile}, {flags}]")
# print(diff
patches.append(f"in_map.{layer}.tiles = {layer_slug}")
Expand Down Expand Up @@ -91,11 +84,14 @@ def gen_py_patch(base_map_file, diff_map_file):
# TODO: support quads etc
print(f"Warning: skipping {layer.kind()}")
continue
diffs = diff_layers(
print(f"diffing layer")
coordinates, values = diff_layers(
base_map.groups[group_index].layers[layer_index],
diff_map.groups[group_index].layers[layer_index])
print("generating patches")
patches += gen_py_layer_diff(
diffs,
coordinates,
values,
f"groups[{group_index}].layers[{layer_index}]",
f"{group.name}_{layer.name}")

Expand Down

0 comments on commit 3f00019

Please sign in to comment.