Login

Subversion Repositories NedoOS

Rev

Rev 496 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

-- Export binary file
-- Thanks to Jerzy Kut for the num_to_char function

function num_to_char ( number )
 return ( string.char ( math.mod ( math.mod ( number, 256 ) + 256, 256 ) ) )
end

function writeIntLSB ( file, number )
 file:write ( num_to_char( number )) -- x>>0
end

function main ()
-- if mappy.msgBox ("Export binary file", "This example script will export the current layer as a binary file (CDXMap format) (anims are replaced with block 0)\nThis is the same as the default .map format when you save a .map file\n\nRun the script (you will be prompted for a filename to save as)?", mappy.MMB_OKCANCEL, mappy.MMB_ICONQUESTION) == mappy.MMB_OK then

  local w = mappy.getValue(mappy.MAPWIDTH)
  local h = mappy.getValue(mappy.MAPHEIGHT)

  if (w == 0) then
   mappy.msgBox ("Export binary file", "You need to load or create a map first", mappy.MMB_OK, mappy.MMB_ICONINFO)
  else

   local isok,asname = mappy.fileRequester (".", "Map files (*.map)", "*.map", mappy.MMB_SAVE)
   if isok == mappy.MMB_OK then

    if (not (string.sub (string.lower (asname), -4) == ".map")) then
     asname = asname .. ".map"
    end

    --local isok,adjust = mappy.doDialogue ("Export binary file", "Adjust exported values by:", "0", mappy.MMB_DIALOGUE1)
    if isok == mappy.MMB_OK then
     L0 = 0
     L1 = 1

     adjust = tonumber (adjust)
-- open file as binary
     outas = io.open (asname, "wb")
     --writeIntLSB (outas, w)
     --writeIntLSB (outas, h)
     local y = h-1
     while y >= 0 do
      local x = w-1
      while x >= 0 do
       local mapval = mappy.getBlockValue (mappy.getBlock (x, y,L0), mappy.BLKBG)
       --mapval = mapval + adjust
       if mapval < 0 then
        mapval = 0
       end
       writeIntLSB (outas, mapval)
       x = x - 1
      end
      y = y - 1
     end
     outas:close ()

     asname = string.sub (asname, 0,string.len(asname)-4) .. ".enm"
-- open file as binary
     outas = io.open (asname, "wb")
     --writeIntLSB (outas, w)
     --writeIntLSB (outas, h)
     y = h-1
     while y >= 0 do
      local x = w-1
      while x >= 0 do
       local mapval = mappy.getBlockValue (mappy.getBlock (x, y,L1), mappy.BLKBG)
       --mapval = mapval + adjust
       if mapval < 0 then
        mapval = 0
       end
       --writeIntLSB (outas, mapval)
       if mapval == 0 then
       else
         writeIntLSB (outas, mapval)
         writeIntLSB (outas, x)
         writeIntLSB (outas, y)
       end
       x = x - 1
      end
      y = y - 1
     end
     outas:close ()

    end
   end
  end
 end
--end

test, errormsg = pcall( main )
if not test then
    mappy.msgBox("Error ...", errormsg, mappy.MMB_OK, mappy.MMB_ICONEXCLAMATION)
end