Advertisement
GenesisFan64

tiled_md

Dec 25th, 2016
386
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.11 KB | None | 0 0
  1. #======================================================================
  2. # Tiled+TGA to MD
  3. #======================================================================
  4.  
  5. import sys
  6. import xml.etree.ElementTree as ET
  7.  
  8. #======================================================================
  9. # -------------------------------------------------
  10. # Subs
  11. # -------------------------------------------------
  12.  
  13. def get_val(string_in):
  14.   got_this_str=""
  15.   for do_loop_for in string_in:
  16.     got_this_str = got_this_str + ("0"+((hex(ord(do_loop_for)))[2:]))[-2:]
  17.   return(got_this_str)
  18.  
  19. #======================================================================
  20. # -------------------------------------------------
  21. # Subs
  22. # -------------------------------------------------
  23.  
  24. def get_val(string_in):
  25.   got_this_str=""
  26.   for do_loop_for in string_in:
  27.     got_this_str = got_this_str + ("0"+((hex(ord(do_loop_for)))[2:]))[-2:]
  28.   return(got_this_str)
  29.  
  30. def write_line(in_offset):
  31.   input_file.seek(in_offset)
  32.  
  33.   a = int(get_val(input_file.read(1)),16) & 0x0F
  34.   b = int(get_val(input_file.read(1)),16) & 0x0F
  35.   c = int(get_val(input_file.read(1)),16) & 0x0F
  36.   d = int(get_val(input_file.read(1)),16) & 0x0F
  37.   e = int(get_val(input_file.read(1)),16) & 0x0F
  38.   f = int(get_val(input_file.read(1)),16) & 0x0F
  39.   g = int(get_val(input_file.read(1)),16) & 0x0F
  40.   h = int(get_val(input_file.read(1)),16) & 0x0F
  41.      
  42.   a = a << 4
  43.   a = a+b
  44.   c = c << 4
  45.   c = c+d
  46.   e = e << 4
  47.   e = e+f
  48.   g = g << 4
  49.   g = g+h
  50.      
  51.   out_art.write(chr(a))
  52.   out_art.write(chr(c))
  53.   out_art.write(chr(e))
  54.   out_art.write(chr(g))
  55.  
  56. def write_cell(cell_off):
  57.   rept = 8
  58.   while rept:
  59.     write_line(cell_off)
  60.     cell_off += x_size
  61.     rept -= 1
  62.    
  63. def seek_cell(x,y):
  64.   x = x<<3
  65.   y = y*(x_size*8)
  66.  
  67.   out_offset=x+y
  68.   return(out_offset)
  69.  
  70. def chks_make(lay):
  71.   d7 = 0
  72.   d5 = 0
  73.  
  74.   d4 = 0
  75.   d1 = 8
  76.   while d1:
  77.     input_file.seek(lay)
  78.     d2 = 8
  79.     while d2:
  80.       byte = int(get_val(input_file.read(1)),16)
  81.       if byte != 0:
  82.         d3 = byte + d4 + d5 + d7
  83.         d7 += d3
  84.       d4 += 1
  85.       d2 -= 1
  86.      
  87.     d4 = 0
  88.     d5 += 1
  89.     lay += x_size
  90.     d1 -= 1
  91.  
  92.   return(d7)
  93.  
  94. def chks_dupl(chksum):
  95.   sizeof_filter=2               #number of list entries
  96.  
  97.   vram1=0
  98.   vram2=0
  99.   vram3=0
  100.   vram4=0
  101.   a0=cells_list
  102.   d3=0  #page
  103.   d2=0  #lay
  104.   result=False
  105.   if len(a0) != 0:
  106.     d0 = len(a0)
  107.     d1 = 0
  108.     while d0:
  109.       d0 -= sizeof_filter
  110.       if a0[d1] == chksum:
  111.     result=True
  112.     d2 = a0[d1+1]
  113.     d0 = False
  114.       d1 += sizeof_filter
  115.  
  116.   return(result,d2,d3,vram1,vram2,vram3,vram4)
  117.  
  118. #======================================================================
  119. # -------------------------------------------------
  120. # Convert blocks
  121. # -------------------------------------------------
  122.  
  123. DUPL_CHECK  = True
  124. BLANK_CELL  = True
  125. DOUBLE_MODE = False
  126.  
  127. # ------------------------------------
  128.  
  129. cells_used = 0
  130. cells_list = list()
  131.  
  132. # ------------------------------------
  133.  
  134. d0 = "map16.tga"
  135. #d0 = sys.argv[2]
  136. input_file = open(d0,"rb")
  137. out_art    = open("lvl_art.bin","wb")
  138. out_pal    = open("lvl_pal.bin","wb")
  139. out_map    = open("lvl_blk.bin","wb")
  140.  
  141. input_file.seek(0x5)                    #$05, palsize
  142. size_pal = int(get_val(input_file.read(1)),16)
  143.  
  144. input_file.seek(0xC)                    #$0C, xsize,ysize (little endian)
  145. x_r = int(get_val(input_file.read(1)),16)
  146. x_l = (int(get_val(input_file.read(1)),16)<<8)
  147. x_size = x_l+x_r
  148. y_r = int(get_val(input_file.read(1)),16)
  149. y_l = (int(get_val(input_file.read(1)),16)<<8)
  150. y_size = (y_l+y_r)
  151.  
  152. a = x_size&7
  153. b = y_size&7
  154. c = "X SIZE IS MISALIGNED"
  155. d = "Y SIZE IS MISALIGNED"
  156. e = " "
  157. f = " "
  158. g = False
  159. if a != 0:
  160.   print hex(a)
  161.   e = c
  162.   g = True
  163. if b !=0:
  164.   f = d
  165.   g = True
  166.  
  167. if g == True:
  168.   print "WARNING:",e,f
  169.  
  170. # ----------------------
  171. # Write palette
  172. # ----------------------
  173.  
  174. input_file.seek(0x12)
  175. d0 = size_pal
  176. while d0:
  177.   b = int(get_val(input_file.read(1)),16)
  178.   g = int(get_val(input_file.read(1)),16)
  179.   r = int(get_val(input_file.read(1)),16)
  180.   r = r >> 5
  181.   r = r << 1
  182.   g = g >> 5
  183.   g = g << 1
  184.   b = b >> 5
  185.   b = b << 1
  186.   g = g << 4
  187.   gr = g+r
  188.   out_pal.write(chr(b))
  189.   out_pal.write(chr(gr))
  190.   d0 -= 1
  191.  
  192. # ----------------------
  193. # Make NULL block
  194. # ----------------------
  195.  
  196. if DOUBLE_MODE == True:
  197.   map_vram = 2
  198.   out_art.write(chr(0)*0x40)
  199.   d0 = 0x0000
  200.   d1 = 0x0001
  201.   out_map.write(
  202.     chr(int((d0&0xFF00)>>8)&0xFF)
  203.     +
  204.     chr(int(d0)&0xFF)
  205.     +
  206.     chr(int((d0&0xFF00)>>8)&0xFF)
  207.     +
  208.     chr(int(d0)&0xFF)
  209.     +
  210.     chr(int((d1&0xFF00)>>8)&0xFF)
  211.     +
  212.     chr(int(d1)&0xFF)
  213.     +
  214.     chr(int((d1&0xFF00)>>8)&0xFF)
  215.     +
  216.     chr(int(d1)&0xFF)
  217.     )
  218. else:
  219.   map_vram = 1
  220.   out_art.write(chr(0)*0x20)
  221.   d0 = 0x0000
  222.   d1 = 0x0000
  223.   out_map.write(
  224.     chr(int((d0&0xFF00)>>8)&0xFF)
  225.     +
  226.     chr(int(d0)&0xFF)
  227.     +
  228.     chr(int((d0&0xFF00)>>8)&0xFF)
  229.     +
  230.     chr(int(d0)&0xFF)
  231.     +
  232.     chr(int((d1&0xFF00)>>8)&0xFF)
  233.     +
  234.     chr(int(d1)&0xFF)
  235.     +
  236.     chr(int((d1&0xFF00)>>8)&0xFF)
  237.     +
  238.     chr(int(d1)&0xFF)
  239.     )
  240.  
  241. #======================================================================
  242. # -------------------------------------------------
  243. # Convert tga
  244. # -------------------------------------------------
  245.  
  246. cells_used = 0
  247. x_pos = 0
  248. y_pos = 0
  249. d1 = map_vram
  250. image_addr=input_file.tell()
  251.  
  252. y_pos=0
  253. cell_y_size=y_size/16
  254. while cell_y_size:
  255.   x_pos=0
  256.   cell_x_size=x_size/16
  257.   while cell_x_size:
  258.    
  259.     d2 = chks_make(image_addr+seek_cell(x_pos,y_pos))
  260.     d1 = 0
  261.     if d2 != 0:
  262.       if chks_dupl(d2)[0] == True:
  263.         d1=chks_dupl(d2)[1]
  264.       else:
  265.         write_cell(image_addr+seek_cell(x_pos,y_pos))
  266.         cells_used += 1
  267.         d1=map_vram
  268.         cells_list.append(d2)
  269.         cells_list.append(d1)
  270.         map_vram+=1
  271.     out_map.write(chr(int((d1&0xFF00)>>8)&0xFF)+chr(int(d1)&0xFF))
  272.    
  273.     d2 = chks_make(image_addr+seek_cell(x_pos,y_pos+1))
  274.     d1 = 0
  275.     if d2 != 0:
  276.       if chks_dupl(d2)[0] == True:
  277.         d1=chks_dupl(d2)[1]
  278.       else:
  279.         write_cell(image_addr+seek_cell(x_pos,y_pos+1))
  280.         cells_used += 1
  281.         d1=map_vram
  282.         cells_list.append(d2)
  283.         cells_list.append(d1)
  284.         map_vram+=1
  285.     out_map.write(chr(int((d1&0xFF00)>>8)&0xFF)+chr(int(d1)&0xFF))
  286.    
  287.     d2 = chks_make(image_addr+seek_cell(x_pos+1,y_pos))
  288.     d1 = 0
  289.     if d2 != 0:
  290.       if chks_dupl(d2)[0] == True:
  291.         d1=chks_dupl(d2)[1]
  292.       else:
  293.         write_cell(image_addr+seek_cell(x_pos+1,y_pos))
  294.         cells_used += 1
  295.         d1=map_vram
  296.         cells_list.append(d2)
  297.         cells_list.append(d1)
  298.         map_vram+=1
  299.     out_map.write(chr(int((d1&0xFF00)>>8)&0xFF)+chr(int(d1)&0xFF))
  300.      
  301.     d2 = chks_make(image_addr+seek_cell(x_pos+1,y_pos+1))
  302.     d1 = 0
  303.     if d2 != 0:
  304.       if chks_dupl(d2)[0] == True:
  305.         d1=chks_dupl(d2)[1]
  306.       else:
  307.         write_cell(image_addr+seek_cell(x_pos+1,y_pos+1))
  308.         cells_used += 1
  309.         d1=map_vram
  310.         cells_list.append(d2)
  311.         cells_list.append(d1)
  312.         map_vram+=1
  313.     out_map.write(chr(int((d1&0xFF00)>>8)&0xFF)+chr(int(d1)&0xFF))
  314.      
  315.     x_pos += 2
  316.     cell_x_size -= 1
  317.    
  318.   y_pos += 2
  319.   cell_y_size -= 1
  320. #d1 = 1
  321.  
  322. #y_pos=0
  323. #cell_y_size=1#(y_size/16)
  324. #while cell_y_size:
  325.   #x_pos=0
  326.   #cell_x_size=(x_size/16)
  327.   #while cell_x_size:
  328.     #d1 = 0
  329.     #d2 = chks_make(image_addr+seek_cell(x_pos,y_pos))
  330.     #if d2 != 0:
  331.       #if DUPL_CHECK == True:
  332.     #if chks_dupl(d2)[0] == True:
  333.       #d1=chks_dupl(d2)[1]
  334.     #else:
  335.       #write_cell(image_addr+seek_cell(x_pos,y_pos))
  336.       #cells_used += 1
  337.       #d1=vram
  338.       #cells_list.append(d2)
  339.       #cells_list.append(d1)
  340.       #vram+=1
  341.       #else:    
  342.     #write_cell(image_addr+seek_cell(x_pos,y_pos))
  343.     #cells_used += 1
  344.     #d1=vram
  345.     #cells_list.append(d2)
  346.     #cells_list.append(d1)
  347.     #vram+=1
  348.      
  349.     ##out_map.write(chr(int((d1&0xFF00)>>8)&0xFF)+chr(int(d1)&0xFF))
  350.     ##x_pos+=1
  351.     ##cell_x_size -= 1
  352.   #y_pos+=1
  353.   #cell_y_size -= 1
  354.  
  355. input_file.close()
  356. out_art.close()
  357. out_pal.close()
  358. out_map.close()
  359.  
  360. #======================================================================
  361. # -------------------------------------------------
  362. # Convert layout
  363. # -------------------------------------------------
  364.  
  365. d0 = "main.tmx"
  366. #d0 = sys.argv[1]
  367. input_file = open(d0,"rb")
  368.  
  369. tree = ET.parse(input_file)
  370. root = tree.getroot()
  371.  
  372. layer_list = list()
  373. data_list  = list()
  374.  
  375. a = True
  376. for layer in root.iter('layer'):
  377.     layer_list.append(layer.attrib.values()[1])
  378.     if a == True:
  379.         a = False
  380.         lay_xsize = layer.attrib.values()[0]
  381.         lay_ysize = layer.attrib.values()[2]
  382. for data in root.iter('data'):
  383.     data_list.append(data.text)
  384.  
  385. c = (3*2)
  386. d = 0
  387. while c:
  388.     a = data_list[d].replace("\n","").split(",")
  389.     b = layer_list[d]
  390.    
  391.     this_file = open(layer_list[d]+".bin","wb")
  392.     f = 0
  393.     g = data_list[d].split(",")
  394.     e = len(g)
  395.     while e:
  396.         this_file.write(chr(int(g[f])&0xFF))
  397.         f += 1
  398.         e -= 1
  399.        
  400.     this_file.close()
  401.    
  402.     d += 1
  403.     c -= 1
  404.  
  405. print "LEVEL SIZE:",lay_xsize,lay_ysize
  406.  
  407. #======================================================================
  408.  
  409. #print "File:",sys.argv[1],"| Map size:",hex(x_size/8),hex(y_size/8),"| Cells used:",hex(cells_used)
  410. input_file.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement