RogueLife/assets-cg/converters.py

59 lines
1.6 KiB
Python
Raw Normal View History

2021-06-02 09:38:59 +02:00
import fxconv
import re
def convert(input, output, params, target):
recognized = True
if params["custom-type"] == "level":
o = convert_level(input, params)
else:
recognized = False
if recognized:
fxconv.elf(o, output, "_" + params["name"], **target)
return 0
return 1
def convert_level(input, params):
RE_CATEGORY = re.compile(r'^\[([^\]]+)\]$', re.MULTILINE)
with open(input, "r") as fp:
ct = [x.strip() for x in RE_CATEGORY.split(fp.read())]
ct = [x for x in ct if x != ""]
assert(len(ct) % 2 == 0)
ct = { ct[i]: ct[i+1] for i in range(0, len(ct), 2) }
assert(ct.keys() == { "level", "base", "decor" })
# Read tileset
def tileset(desc):
desc = [line.strip().split() for line in desc.split("\n")]
assert(len(line) <= 16 for line in desc)
tiles = dict()
for y, line in enumerate(desc):
for x, chara in enumerate(line):
tiles[chara] = 16 * y + x
return tiles
base = tileset(ct["base"])
decor = tileset(ct["decor"])
level = [line.strip().split() for line in ct["level"].split("\n")]
height = len(level)
width = max(len(line) for line in level)
tiles = bytearray(2 * width * height)
for y, line in enumerate(level):
for x, tile in enumerate(line):
index = 2 * (width * y + x)
tiles[index] = base[tile[0]]
tiles[index+1] = decor[tile[1]]
o = bytes()
o += fxconv.u16(width) + fxconv.u16(height)
o += bytes(tiles)
return o