?login_element?

Subversion Repositories NedoOS

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. -- Export binary file
  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 writeIntLSB ( file, number )
  9.  file:write ( num_to_char( number )) -- x>>0
  10. end
  11.  
  12. function main ()
  13. -- 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
  14.  
  15.   local w = mappy.getValue(mappy.MAPWIDTH)
  16.   local h = mappy.getValue(mappy.MAPHEIGHT)
  17.  
  18.   if (w == 0) then
  19.    mappy.msgBox ("Export binary file", "You need to load or create a map first", mappy.MMB_OK, mappy.MMB_ICONINFO)
  20.   else
  21.  
  22.    local isok,asname = mappy.fileRequester (".", "Map files (*.map)", "*.map", mappy.MMB_SAVE)
  23.    if isok == mappy.MMB_OK then
  24.  
  25.     if (not (string.sub (string.lower (asname), -4) == ".map")) then
  26.      asname = asname .. ".map"
  27.     end
  28.  
  29.     --local isok,adjust = mappy.doDialogue ("Export binary file", "Adjust exported values by:", "0", mappy.MMB_DIALOGUE1)
  30.     if isok == mappy.MMB_OK then
  31.  
  32.      adjust = tonumber (adjust)
  33. -- open file as binary
  34.      outas = io.open (asname, "wb")
  35.      --writeIntLSB (outas, w)
  36.      --writeIntLSB (outas, h)
  37.      local y = h-1
  38.      while y >= 0 do
  39.       local x = w-1
  40.       while x >= 0 do
  41.        local mapval = mappy.getBlockValue (mappy.getBlock (x, y), mappy.BLKBG)
  42.        --mapval = mapval + adjust
  43.        if mapval < 0 then
  44.         mapval = 0
  45.        end
  46.        writeIntLSB (outas, mapval)
  47.        x = x - 1
  48.       end
  49.       y = y - 1
  50.      end
  51.      outas:close ()
  52.  
  53.     end
  54.    end
  55.   end
  56.  end
  57. --end
  58.  
  59. test, errormsg = pcall( main )
  60. if not test then
  61.     mappy.msgBox("Error ...", errormsg, mappy.MMB_OK, mappy.MMB_ICONEXCLAMATION)
  62. end
  63.