?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download | RSS feed

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