import json, math, sys, collections import numpy as np import factorion_rs from corpus.convert import _SIZES, designs, rasterise, to_world from factorion import items def frame(bp): xs, ys = [], [] for e in bp["entities"]: w, h = _SIZES[e["name"]][:2] if e.get("direction", 0) % 8 in (4,) and w != h: # east/west (16-step) swaps the footprint w, h = h, w xs.append(math.floor(e["position"]["x"] - w / 2 + 0.01)); ys.append(math.floor(e["position"]["y"] - h / 2 + 0.01)) return min(xs), min(ys) def region_flows(rec): bp = next(designs(rec["string"])); bp["unbuildable"] = rec.get("unbuildable") or [] r = rasterise(bp) if r is None: return None mx, my = frame(bp) w = to_world(r) flows = {} for label, dem, sup, fed, drained in factorion_rs.py_lane_items(np.ascontiguousarray(w.transpose(1, 2, 0))): kind, rest = label.split("@"); xy, _, lane = rest.partition(":") x, y = map(int, xy.split(",")) if lane: flows[(x, y, lane)] = ({items[i].name for i in dem}, {items[i].name for i in sup}, fed, drained) truth = {} for x, y, line, its in rec.get("lanes") or []: truth[(x - mx, y - my, line)] = {s.replace("-", "_") for s in its.split(",")} return r, w, flows, truth if __name__ == "__main__": regions = json.load(open(sys.argv[1])) for mapping in ({1: "L", 2: "R"}, {1: "R", 2: "L"}): c = collections.Counter() for rec in regions: out = region_flows(rec) if out is None: continue r, w, flows, truth = out for (x, y, line), true in truth.items(): f = flows.get((x, y, mapping[line])) if f is None: c["true lane has no engine node (masked/cut)"] += 1; continue dem, sup, *_ = f guess = sup or dem c["lanes compared"] += 1 if not guess: c["inferred nothing"] += 1 elif true <= guess and len(guess) == 1: c["inferred exactly right"] += 1 elif true & guess: c["inferred a superset/overlap"] += 1 else: c["inferred wrong item"] += 1 print(mapping, dict(c))