| 1 |
import json |
| 2 |
|
| 3 |
def apply_tileconn(f, d): |
| 4 |
def merge_nodes(a, b): |
| 5 |
for bwire in b.wires: |
| 6 |
bwire.tile.wire_to_node[bwire.index] = a |
| 7 |
a.wires.append(bwire) |
| 8 |
b.wires = [] |
| 9 |
|
| 10 |
tj = json.load(f) |
| 11 |
# Restructure to tiletype -> coord offset -> type -> wire_pairs |
| 12 |
ttn = {} |
| 13 |
for entry in tj: |
| 14 |
tile0, tile1 = entry["tile_types"] |
| 15 |
dx, dy = entry["grid_deltas"] |
| 16 |
if tile0 not in ttn: |
| 17 |
ttn[tile0] = {} |
| 18 |
if (dx, dy) not in ttn[tile0]: |
| 19 |
ttn[tile0][dx, dy] = {} |
| 20 |
ttn[tile0][dx, dy][tile1] = entry["wire_pairs"] |
| 21 |
for tile in d.tiles: |
| 22 |
tt = tile.tile_type() |
| 23 |
if tt not in ttn: |
| 24 |
continue |
| 25 |
# Search the neighborhood around a tile |
| 26 |
for dxy, nd in sorted(ttn[tt].items()): |
| 27 |
nx = tile.x + dxy[0] |
| 28 |
ny = tile.y + dxy[1] |
| 29 |
if (nx, ny) not in d.tiles_by_xy: |
| 30 |
continue |
| 31 |
ntile = d.tiles_by_xy[nx, ny] |
| 32 |
ntt = ntile.tile_type() |
| 33 |
if ntt not in nd: |
| 34 |
continue |
| 35 |
# Found a pair with connections |
| 36 |
wc = nd[ntt] |
| 37 |
for wirea, wireb in wc: |
| 38 |
merge_nodes(tile.wire(wirea).node(), ntile.wire(wireb).node()) |