#!python # -*- mode: python; Encoding: utf-8; coding: utf-8 -*- # Last updated: <2021/10/06 01:35:35 +0900> """ Make replace pattern dictionary. Windows10 x64 21H1 + Python 3.9.7 64bit """ import json import sys jsonfile = "road_replace_pattern.json" outfile = "rep_dic0.json" def get_black(m, mw, x, y, w, h): lst = [] for yi in range(h): for xi in range(w): lst.append(m[(y + yi) * mw + (x + xi)]) return lst with open(jsonfile, 'r') as f: data = json.load(f) map_w = int(data["width"]) map_h = int(data["height"]) tile_w = int(data["tilewidth"]) tile_h = int(data["tileheight"]) orig_map = None rep_map = None layers = data["layers"] for l in layers: if l["name"] == "originalpattern": orig_map = l["data"] elif l["name"] == "replacepattern": rep_map = l["data"] if orig_map is None: print("Error : Not found original pattern layer.") sys.exit(0) elif rep_map is None: print("Error : Not found replace pattern layer.") sys.exit(0) orig_map = list(map(lambda x: x-1, orig_map)) rep_map = list(map(lambda x: x-1, rep_map)) empty_blk0 = [0, 0, 0, 0, 0, 0, 0, 0, 0] empty_blk1 = [-1, -1, -1, -1, -1, -1, -1, -1, -1] lst = [] for y in range(0, map_h, 4): for x in range(0, map_w, 4): bo = get_black(orig_map, map_w, x, y, 3, 3) br = get_black(rep_map, map_w, x, y, 3, 3) if bo == empty_blk0 or br == empty_blk1: continue lst.append([bo, br]) # print(json.dumps(lst)) with open(outfile, 'w') as f: json.dump(lst, f) print("Output : %s" % outfile)