+++ /dev/null
-#!/usr/bin/env python
-
-import sys
-import os
-import xml.etree.ElementTree as ET
-
-TILE_SIZE = 16
-ACTOR_OFFSET_X = 0
-ACTOR_OFFSET_Y = 0
-
-state_ptr = "st_map_null"
-tile_bank0 = "bank8000"
-tile_bank1 = "bank8800"
-tile_bank2 = "bank8C00"
-tile_bank3 = "bank9000"
-actor_table_ptr = "map_actor_table_null"
-map_name = "noname"
-NAME_LEN = 8
-
-TILE_FLAGS = [0] * 256
-MAP_W = 16
-MAP_H = 16
-
-# set by layer properties
-# flags_x,y
-BG_FLAGS = [0] * MAP_W * MAP_H
-
-if len(sys.argv) < 3:
- print("Usage: tmx2map.py <source> <map name>")
- sys.exit(-1)
-
-src = sys.argv[1]
-name = sys.argv[2]
-
-def compress(data, convert):
- compressed = []
-
- run_length = 0
- last_byte = 0
-
- for i, byte in enumerate(data):
- if (last_byte != byte and run_length > 0) or run_length >= 255:
- compressed.append(run_length)
- compressed.append(convert(last_byte))
- run_length = 0
-
- last_byte = byte
- run_length += 1
-
- if run_length > 0:
- compressed.append(run_length)
- compressed.append(convert(last_byte))
-
- return compressed
-
-
-def convert_val(byte):
- val = int(byte)
- if val > 0:
- val -= 1
- # calculate the real tile offset from a 16x16 tileset
- # to a 8x8 tileset
- return (val * 2 + int(val / 8) * 16) & 0xFF
-
-def convert_none(byte):
- return int(byte)
-
-def print_bg_data(data, name, layer):
- split = data.split(",")
- print_data(split, name, layer, convert_val)
-
-def print_data(data, name, layer, convert):
- data = compress(data, convert)
- print(name + "_" + layer + ":")
- for i, byte in enumerate(data):
- end = ', '
-
- if i % 8 == 0:
- print("\n.db ", end = '');
-
- val = byte
-
- if (i+1) % 8 == 0:
- end = ''
- if i == len(data) - 1:
- end = '\n'
- print(hex(val), end=end)
-
- print(".db 0x00 ; termiante data")
-
-def print_bg_flags(data, name, layer):
- split = data.split(",")
- flags = []
- for i, byte in enumerate(split):
- flags.append(TILE_FLAGS[int(byte)-1] | BG_FLAGS[i])
- print_data(flags, name, 'tile_flags', convert_none)
-
-def print_header(name):
- print(name + "_header:")
- print(".db 0, 0, 0, 0 ; flags")
- print(".str \"" + map_name.upper().ljust(NAME_LEN)[:NAME_LEN] + "\"")
- print("dw " + name + "_bg")
- print("dw " + name + "_tile_flags")
- print("dw " + state_ptr)
- print("dw " + actor_table_ptr)
- print("dw " + tile_bank0)
- print("dw " + tile_bank1)
- print("dw " + tile_bank2)
- print("dw " + tile_bank3)
- print("")
-
-def get_flag(tile):
- for child in tile:
- if child.tag == 'properties':
- for sub_child in child:
- if sub_child.tag == 'property':
- if sub_child.attrib['name'] == 'flags':
- TILE_FLAGS[int(tile.attrib['id'])] |= int(sub_child.attrib['value'])
-
-# get all flags and write them to TILE_FLAGS
-def get_flags(tileset):
- # do we have to load a tileset?
- tilesetfile = os.path.join(os.path.dirname(src),tileset.attrib['source'])
- tileset = ET.parse(tilesetfile).getroot()
-
-
- for child in tileset:
- if child.tag == 'tile':
- get_flag(child)
-
-def get_map_props(root):
- global state_ptr
- global actor_table_ptr
- global tile_bank0
- global tile_bank1
- global tile_bank2
- global tile_bank3
- global map_name
-
- for child in root:
- if child.tag == "properties":
- for prop in child:
- name = prop.attrib['name']
- value = prop.attrib['value']
- if name == 'state_ptr':
- state_ptr = value
- elif name == 'actor_table_ptr':
- actor_table_ptr = value
- elif name == 'tile_bank0':
- tile_bank0 = value
- elif name == 'tile_bank1':
- tile_bank1 = value
- elif name == 'tile_bank2':
- tile_bank2 = value
- elif name == 'tile_bank3':
- tile_bank3 = value
- elif name == 'map_name':
- map_name = value
-
- return
-
-def read_bg_props(data):
- for child in data:
- name = child.attrib['name']
- if name.startswith('flags_left'):
- for i in range(0, MAP_H):
- BG_FLAGS[(MAP_W)*i] = int(child.attrib['value'])
- elif name.startswith('flags_up'):
- for i in range(0, MAP_W):
- BG_FLAGS[i] = int(child.attrib['value'])
- elif name.startswith('flags_down'):
- for i in range(0, MAP_W):
- BG_FLAGS[(MAP_H) * (MAP_W - 1) + i] = int(child.attrib['value'])
- elif name.startswith('flags_right'):
- for i in range(0, MAP_H):
- BG_FLAGS[(MAP_W - 1) + (MAP_W) * i] = int(child.attrib['value'])
- elif name.startswith('flags_'):
- name = name.replace('flags_', '')
- coords = name.split(',')
- tile_index = int(coords[1]) * MAP_W + int(coords[0])
- BG_FLAGS[tile_index] = int(child.attrib['value'])
-
-def convert(src, name):
- tree = ET.parse(src)
- root = tree.getroot()
-
- get_map_props(root)
- print_header(name)
-
- for child in root:
- if child.tag == "layer":
- for data in child:
- if data.tag == "properties":
- read_bg_props(data)
- else:
- print_bg_data(data.text, name, child.attrib['name'])
- print_bg_flags(data.text, name, child.attrib['name'])
- elif child.tag == "tileset":
- get_flags(child)
-
-convert(src, name)