2021-07-29 18:33:22 +02:00
|
|
|
import fxconv
|
2021-07-30 02:01:05 +02:00
|
|
|
import json
|
2021-07-29 18:33:22 +02:00
|
|
|
|
|
|
|
def convert(input, output, params, target):
|
|
|
|
if params["custom-type"] == "map":
|
|
|
|
convert_map(input, output, params, target)
|
|
|
|
return 0
|
2021-08-06 23:42:49 +02:00
|
|
|
elif params["custom-type"] == "character":
|
|
|
|
convert_character(input, output, params, target)
|
|
|
|
return 0
|
2021-07-29 18:33:22 +02:00
|
|
|
else:
|
|
|
|
return 1
|
|
|
|
|
|
|
|
def convert_map(input, output, params, target):
|
2021-07-31 03:40:14 +02:00
|
|
|
TILE_AIR = 0
|
|
|
|
TILE_SOLID = 1
|
|
|
|
TILE_DOOR = 2
|
2021-08-06 23:42:49 +02:00
|
|
|
TILE_CHARACTER = 3
|
2021-07-29 18:33:22 +02:00
|
|
|
|
2021-08-12 03:07:08 +02:00
|
|
|
TILE_BRIDGE = -1 #only for bridge detection to avoid solid behind
|
|
|
|
|
2021-07-30 02:01:05 +02:00
|
|
|
with open(input, "r") as jsonData:
|
|
|
|
data = json.load(jsonData)
|
|
|
|
|
2021-08-15 03:10:05 +02:00
|
|
|
#find the tileset using the location of the input
|
2021-07-31 03:40:14 +02:00
|
|
|
tileset = open(input.replace(input.split("/")[-1], "tileset.json"), "r")
|
|
|
|
data_tileset = json.load(tileset)
|
|
|
|
tileset.close()
|
|
|
|
|
|
|
|
tile_value = {}
|
|
|
|
info_map = bytearray()
|
|
|
|
|
2021-08-15 03:10:05 +02:00
|
|
|
#create a dictionnary between tile-type
|
2021-07-31 03:40:14 +02:00
|
|
|
for i in data_tileset["tiles"]:
|
|
|
|
try:
|
|
|
|
id = i["id"]+1
|
|
|
|
type = i["type"]
|
2021-07-30 02:01:05 +02:00
|
|
|
|
2021-07-31 03:40:14 +02:00
|
|
|
if type == "air":
|
|
|
|
value = TILE_AIR
|
|
|
|
elif type == "solid":
|
|
|
|
value = TILE_SOLID
|
|
|
|
elif type == "door":
|
|
|
|
value = TILE_DOOR
|
2021-08-06 23:42:49 +02:00
|
|
|
elif type == "character":
|
|
|
|
value = TILE_CHARACTER
|
2021-08-12 03:07:08 +02:00
|
|
|
elif type == "bridge":
|
|
|
|
value = TILE_BRIDGE
|
2021-07-31 03:40:14 +02:00
|
|
|
else:
|
|
|
|
value = TILE_AIR
|
2021-07-30 02:01:05 +02:00
|
|
|
|
2021-07-31 03:40:14 +02:00
|
|
|
tile_value[id] = value
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
|
2021-08-15 03:10:05 +02:00
|
|
|
#Extract from the json the width, height and layers of the map
|
2021-08-17 22:19:19 +02:00
|
|
|
w, h = data["width"], data["height"]
|
2021-07-31 03:40:14 +02:00
|
|
|
nblayer = len(data["layers"])
|
|
|
|
|
|
|
|
o = fxconv.ObjectData()
|
|
|
|
o += fxconv.u32(w) + fxconv.u32(h) + fxconv.u32(nblayer)
|
|
|
|
o += fxconv.ref("img_tileset")
|
|
|
|
|
2021-08-15 03:10:05 +02:00
|
|
|
#To preserve performance, max 2 layers
|
2021-07-31 03:40:14 +02:00
|
|
|
if(nblayer <= 2):
|
2021-08-15 03:10:05 +02:00
|
|
|
#generate the array of tiles from the layer
|
2021-07-31 03:40:14 +02:00
|
|
|
for i in range(nblayer):
|
2021-07-30 02:01:05 +02:00
|
|
|
tiles = data["layers"][i]["data"]
|
|
|
|
|
|
|
|
byte_tiles = bytearray()
|
|
|
|
for j in tiles:
|
|
|
|
byte_tiles += bytearray(fxconv.u16(j))
|
|
|
|
o += fxconv.ref(byte_tiles)
|
2021-08-12 03:07:08 +02:00
|
|
|
|
|
|
|
#generation of the collision map (take the maximum of the layer except for bridges)
|
2021-08-15 03:10:05 +02:00
|
|
|
#bridges is always walkable
|
2021-08-12 03:07:08 +02:00
|
|
|
for x in range(w*h):
|
|
|
|
value1 = tile_value.get(data["layers"][0]["data"][x])
|
|
|
|
if(nblayer >= 2):
|
|
|
|
value2 = tile_value.get(data["layers"][1]["data"][x])
|
|
|
|
if value1 == None: value1 = TILE_AIR
|
|
|
|
if value2 == None: value2 = TILE_AIR
|
|
|
|
if value2 == TILE_BRIDGE: value1 = value2 = TILE_AIR
|
|
|
|
info_map += bytearray(fxconv.u16(max(value1, value2)))
|
|
|
|
else:
|
|
|
|
if value1 == TILE_BRIDGE: value1 = TILE_AIR
|
|
|
|
info_map += bytearray(fxconv.u16(value1))
|
2021-07-31 03:40:14 +02:00
|
|
|
o += fxconv.ref(info_map)
|
|
|
|
else:
|
|
|
|
raise fxconv.FxconvError(f"There is too much layer ! {nblayer} found for a max of 2")
|
2021-07-29 18:33:22 +02:00
|
|
|
|
2021-08-15 03:10:05 +02:00
|
|
|
#generate !
|
2021-08-06 23:42:49 +02:00
|
|
|
fxconv.elf(o, output, "_" + params["name"], **target)
|
|
|
|
|
|
|
|
def convert_character(input, output, params, target):
|
|
|
|
with open(input,"r") as dialog:
|
|
|
|
file = dialog.read().splitlines()
|
|
|
|
|
|
|
|
o = fxconv.ObjectData()
|
|
|
|
o += fxconv.u32((int)(file[0])) + fxconv.u32((int)(file[1]))
|
2021-08-15 03:10:05 +02:00
|
|
|
o += fxconv.ref(bytes(file[2], 'utf-8') + bytes(1)) #bytes(1) is necessary to end a char
|
2021-08-06 23:42:49 +02:00
|
|
|
o += fxconv.ref(bytes(file[3], 'utf-8') + bytes(1))
|
|
|
|
|
2021-07-29 18:33:22 +02:00
|
|
|
fxconv.elf(o, output, "_" + params["name"], **target)
|