?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download

  1. -- Export MAR files
  2. -- Thanks to Jerzy Kut for the num_to_char function
  3.  
  4. function num_to_char ( number )
  5.  return ( string.char ( math.mod ( math.mod ( number, 256 ) + 256, 256 ) ) )
  6. end
  7.  
  8. function writeShortLSB ( file, number )
  9.  file:write ( num_to_char( number )) -- x>>0
  10.  file:write ( num_to_char( number / 256 )) -- x>>8
  11. end
  12.  
  13. function main ()
  14.  if mappy.msgBox ("Export MAR files", "This will export all layers as consecutive .MAR files.\nEnter the base filename WITHOUT the number or .MAR extension (??.MAR is added)\n\nRun the script (you will be prompted for a filename to save as)?", mappy.MMB_OKCANCEL, mappy.MMB_ICONQUESTION) == mappy.MMB_OK then
  15.  
  16.   local w = mappy.getValue(mappy.MAPWIDTH)
  17.   local h = mappy.getValue(mappy.MAPHEIGHT)
  18.  
  19.   if (w == 0) then
  20.    mappy.msgBox ("Export MAR files", "You need to load or create a map first", mappy.MMB_OK, mappy.MMB_ICONINFO)
  21.   else
  22.  
  23.    local isok,asname = mappy.fileRequester (".", "MAR layer files (*.mar)", "*.MAR", mappy.MMB_SAVE)
  24.    if isok == mappy.MMB_OK then
  25.  
  26.     local isok,adjust = mappy.doDialogue ("Export MAR files", "Start file number:", "0", mappy.MMB_DIALOGUE1)
  27.     if isok == mappy.MMB_OK then
  28.  
  29.      adjust = tonumber (adjust)
  30. -- open file as binary
  31.      local l = 0
  32.      while l < mappy.getValue(mappy.NUMLAYERS) do
  33.      outas = io.open (asname..string.format("%02d.MAR", l+adjust), "wb")
  34.      local y = 0
  35.      while y < h do
  36.       local x = 0
  37.       while x < w do
  38.        writeShortLSB (outas, mappy.getBlock (x, y, l))
  39.        x = x + 1
  40.       end
  41.       y = y + 1
  42.      end
  43.      outas:close ()
  44.      l = l + 1
  45.      end
  46.  
  47.     end
  48.    end
  49.   end
  50.  end
  51. end
  52.  
  53. test, errormsg = pcall( main )
  54. if not test then
  55.     mappy.msgBox("Error ...", errormsg, mappy.MMB_OK, mappy.MMB_ICONEXCLAMATION)
  56. end
  57.