?login_element?

Subversion Repositories NedoOS

Rev

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

  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.      L0 = 0
  32.      L1 = 1
  33.  
  34.      adjust = tonumber (adjust)
  35. -- open file as binary
  36.      outas = io.open (asname, "wb")
  37.      --writeIntLSB (outas, w)
  38.      --writeIntLSB (outas, h)
  39.      local y = h-1
  40.      while y >= 0 do
  41.       local x = w-1
  42.       while x >= 0 do
  43.        local mapval = mappy.getBlockValue (mappy.getBlock (x, y,L0), mappy.BLKBG)
  44.        --mapval = mapval + adjust
  45.        if mapval < 0 then
  46.         mapval = 0
  47.        end
  48.        writeIntLSB (outas, mapval)
  49.        x = x - 1
  50.       end
  51.       y = y - 1
  52.      end
  53.      outas:close ()
  54.  
  55.      asname = string.sub (asname, 0,string.len(asname)-4) .. ".enm"
  56. -- open file as binary
  57.      outas = io.open (asname, "wb")
  58.      --writeIntLSB (outas, w)
  59.      --writeIntLSB (outas, h)
  60.      y = h-1
  61.      while y >= 0 do
  62.       local x = w-1
  63.       while x >= 0 do
  64.        local mapval = mappy.getBlockValue (mappy.getBlock (x, y,L1), mappy.BLKBG)
  65.        --mapval = mapval + adjust
  66.        if mapval < 0 then
  67.         mapval = 0
  68.        end
  69.        --writeIntLSB (outas, mapval)
  70.        if mapval == 0 then
  71.        else
  72.          writeIntLSB (outas, mapval)
  73.          writeIntLSB (outas, x)
  74.          writeIntLSB (outas, y)
  75.        end
  76.        x = x - 1
  77.       end
  78.       y = y - 1
  79.      end
  80.      outas:close ()
  81.  
  82.     end
  83.    end
  84.   end
  85.  end
  86. --end
  87.  
  88. test, errormsg = pcall( main )
  89. if not test then
  90.     mappy.msgBox("Error ...", errormsg, mappy.MMB_OK, mappy.MMB_ICONEXCLAMATION)
  91. end
  92.