Advertisement
neilpopham

Export PICO-8 map to TMX format

Apr 21st, 2020
599
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.80 KB | None | 0 0
  1.  -- the width and height of the map
  2.  local width,height=0,0
  3.  
  4.  -- create the filename from the current date and time
  5.  local filename=""
  6.  for i=90,95 do
  7.   filename=filename..sub("0"..stat(i),-2)
  8.  end
  9.  
  10.  -- ascertain the width and height of our map
  11.  for y=0,127 do
  12.   for x=0,63 do
  13.    local t=mget(x,y)
  14.    if t>0 then
  15.     if x>width then width=x+1 end
  16.     if y>height then height=y+1 end
  17.    end
  18.   end
  19.  end
  20.  
  21.  -- start building an xml string to write to file
  22.  local tmx='<?xml version="1.0" encoding="UTF-8"?>\n'
  23.  tmx=tmx..'<map version="1.2" tiledversion="1.3.1" orientation="orthogonal" renderorder="right-down" compressionlevel="0" width="'..width..'" height="'..height..'" tilewidth="8" tileheight="8" infinite="0" nextlayerid="2" nextobjectid="1">\n'
  24.  tmx=tmx..' <editorsettings>\n'
  25.  tmx=tmx..'  <export target="'..filename..'.lua" format="lua"/>\n'
  26.  tmx=tmx..' </editorsettings>\n'
  27.  tmx=tmx..' <tileset firstgid="1" name="'..filename..'" tilewidth="8" tileheight="8" tilecount="256" columns="16">\n'
  28.  tmx=tmx..'  <image source="'..filename..'.png" trans="000000" width="128" height="128"/>\n'
  29.  tmx=tmx..' </tileset>\n'
  30.  tmx=tmx..' <layer id="1" name="pico8" width="'..width..'" height="'..height..'">\n'
  31.  tmx=tmx..'  <data encoding="csv">\n'
  32.  
  33.  -- add the map data to the string in csv format
  34.  local comma=","
  35.  for y=0,height-1 do
  36.   for x=0,width-1 do
  37.    if y==height-1 and x==width-1 then comma="" end
  38.    tmx=tmx..mget(x,y)..comma
  39.   end
  40.   tmx=tmx..'\n'
  41.  end
  42.  
  43.  -- complete the xml string
  44.  tmx=tmx..'  </data>\n'
  45.  tmx=tmx..' </layer>\n'
  46.  tmx=tmx..'</map>\n'
  47.  
  48.  -- dump the xml to a p8l file in the current directory (remove the .p8l extension manually)
  49.  printh(tmx,filename..".tmx",true)
  50.  
  51.  -- dump the xml to the clipboard (paste into a new file and save as .tmx)
  52.  printh(tmx,"@clip",true)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement