from random import randint import fxconv import json import pathlib import csv def convert(input, output, params, target): if params["custom-type"] == "map": convert_map(input, output, params, target) return 0 else: return 1 def convert_map(input, output, params, target): data = json.load(open(input, "r")) #find the tileset in use. it's a relative path (like ../tileset.tsx) nameTileset = data["tilesets"][0]["source"].replace(".tsx","") print(nameTileset) #the name of the tileset without the .something nameTilesetFree = nameTileset.split("/")[-1] #count the number of "back" (cd ..) to locate the tileset on the computer nbRetour = nameTileset.count("..")+1 #create the tileset absolute path tilesetPath = "/".join(input.split("/")[:-nbRetour]) + "/" + nameTileset + ".json" tileset = open(tilesetPath, "r") data_tileset = json.load(tileset) tileset_size = data_tileset.get("columns") tileset.close() #find the ID of the first tile in the walkable tileset () indexWalkable = data["tilesets"][1]["firstgid"] print(indexWalkable) #Extract from the json the width, height w, h = data["width"], data["height"] #nbTileLayer is the number of "true" layers (without ObjectsLayer) nbTilelayer = ["data" in i for i in data["layers"]].count(True) - 1 print( nbTilelayer) #index of the various layers (may change from one map to another) layer_walkable = 0 layer_foreground = 0 layer_background = 0 #create the structure of the map structMap = fxconv.Structure() structMap += fxconv.u16(w) + fxconv.u16(h) + fxconv.u16(nbTilelayer) structMap += fxconv.u16(tileset_size) structMap += fxconv.ref(f"img_{nameTilesetFree}") #extraction of the data contained in the layer "Walkable" of the map for i in range(nbTilelayer+1): datavalid = data["layers"][i] if datavalid["name"]=="Walkable": layer_walkable = i print( "Walkable Tile Data in layer : ", layer_walkable) break elif i==nbTilelayer: printf( "ERROR : No Walkable layer data !!!" ) walk_data = bytes() layer = data["layers"][layer_walkable] for tile in layer["data"]: #print( tile ) if tile == 0: walk_data += fxconv.u8(tile) #if walkable_data = 0 then it is a blanck cell so nothing to change else : walk_data += fxconv.u8(tile-indexWalkable) #if !=0 than we need to shift the tile number by considering the first tileID (given by indexwalkable) structMap += fxconv.ptr(walk_data) #extraction of the data contained in the layer "Background" and "Foreground" of the map #import the Background layer of the map for i in range(nbTilelayer+1): datavalid = data["layers"][i] if datavalid["name"]=="Background": layer_background = i print( "Background Tile Data in layer : ", layer_background) break elif i==nbTilelayer: printf( "ERROR : No Background layer data !!!" ) layer_data = bytes() layer = data["layers"][layer_background] for tile in layer["data"]: layer_data += fxconv.u16(tile-1) structMap += fxconv.ptr(layer_data) #import the foreground layer of the map for i in range(nbTilelayer+1): datavalid = data["layers"][i] if datavalid["name"]=="Foreground": layer_foreground = i print( "Foreground Tile Data in layer : ", layer_foreground) break elif i==nbTilelayer: printf( "ERROR : No Foreground layer data !!!" ) layer_data = bytes() layer = data["layers"][layer_foreground] for tile in layer["data"]: layer_data += fxconv.u16(tile-1) structMap += fxconv.ptr(layer_data) #generate ! fxconv.elf(structMap, output, "_" + params["name"], **target)