?login_element?

Subversion Repositories NedoOS

Rev

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

  1. -- Read textfile
  2. -- Reads a texfile of ASCII digits (0 to 9) into current layer
  3. -- columns must be smaller than map width
  4.  
  5. function main ()
  6.  
  7.   local w = mappy.getValue(mappy.MAPWIDTH)
  8.   local h = mappy.getValue(mappy.MAPHEIGHT)
  9.  
  10.   if (w == 0) then
  11.    mappy.msgBox ("Read textfile", "You need to load or create a map first", mappy.MMB_OK, mappy.MMB_ICONINFO)
  12.   else
  13.  
  14.    local isok,asname = mappy.fileRequester (".", "Textfile (*.txt)", "*.txt", mappy.MMB_OPEN)
  15.    if isok == mappy.MMB_OK then
  16.  
  17.     mappy.copyLayer(mappy.getValue(mappy.CURLAYER),mappy.MPY_UNDO)
  18.  
  19.     infile = io.open (asname, "r")
  20.     local y = 0
  21.     while y < h do
  22.      local x = 0
  23.      while x < w do
  24.       local blk = tonumber (infile:read (1))
  25.       if (blk ~= nil) then
  26.        mappy.setBlock (x, y, blk)
  27.        x = x + 1
  28.       else
  29.        x = w
  30.       end
  31.      end
  32.      y = y + 1
  33.     end
  34.     infile:close ()
  35.  
  36.     mappy.updateScreen()
  37.  
  38.    end
  39.   end
  40. end
  41.  
  42. test, errormsg = pcall( main )
  43. if not test then
  44.     mappy.msgBox("Error ...", errormsg, mappy.MMB_OK, mappy.MMB_ICONEXCLAMATION)
  45. end
  46.