?login_element?

Subversion Repositories NedoOS

Rev

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

  1. -- Random Distribution Script
  2. -- Robert Edwards 2003
  3.  
  4. -- This script is in the public domain and can be used for any purpose
  5. -- I just hope its useful as an example of binary IO in lua for mappy
  6.  
  7. shifttab = {}
  8. shifttab[0] = 1
  9. shifttab[1] = 256
  10. shifttab[2] = 65536
  11. shifttab[3] = 16777216
  12.  
  13. function ShowError(message)
  14.     mappy.msgBox("Error ...", message, mappy.MMB_OK, mappy.MMB_ICONEXCLAMATION)
  15. end
  16.  
  17. function ShowMessage(message)
  18.     mappy.msgBox("Message ...", message, mappy.MMB_OK, mappy.MMB_ICONNONE)
  19. end
  20.  
  21. function ReadInt( file )
  22.     acc = 0
  23.    
  24.     for i = 0,3 do
  25.         a = string.byte( file:read(1) )
  26.         acc = acc + (a * shifttab[i])
  27.     end
  28.    
  29.     return acc
  30. end
  31.  
  32. function ReadShort( file )
  33.     acc = 0
  34.  
  35.     for i = 0,1 do
  36.         a = string.byte( file:read(1) )
  37.         acc = acc + (a * shifttab[i])
  38.     end
  39.  
  40.     return acc
  41. end
  42.  
  43. function ReadChar( file )
  44.     a = string.byte( file:read(1) )
  45.     return a
  46. end
  47.  
  48.  
  49. function main()
  50.  
  51.     if mappy.msgBox("Random Distribution Plugin", "This will create a semi-random map based upon an input 8-bit TGA bitmap, high index = high density of current block", mappy.MMB_OKCANCEL, mappy.MMB_ICONQUESTION ) == mappy.MMB_OK then
  52.         isok, srcfile = mappy.fileRequester(".","Targa Bitmaps(*.tga)","*.TGA",mappy.MMB_OPEN )
  53.         if isok then
  54.             file = io.open( srcfile, "r+b" )
  55.             idsize=ReadChar(file)
  56.  
  57.             cmaptype = ReadChar(file)
  58.             if cmaptype ~= 0 and cmaptype ~= 1 then
  59.                 error("Incorrect type of targa file")
  60.             end
  61.  
  62.             type = ReadChar(file)
  63.             if type ~= 3 and type ~= 1 then
  64.                 error("Incorrect type of targa file")
  65.             end
  66.            
  67.             -- skip the colormap info and origin info
  68.             file:read(4+5)
  69.  
  70.  
  71.             xsize = ReadShort(file)
  72.             if mappy.getValue(mappy.MAPWIDTH) ~= xsize then
  73.                 error("Bitmap is wrong width")
  74.             end
  75.            
  76.            
  77.             ysize = ReadShort(file)
  78.             if mappy.getValue(mappy.MAPHEIGHT) ~= ysize then
  79.                 error("Bitmap is wrong height")
  80.             end
  81.  
  82.             bpp = ReadChar(file)
  83.             if bpp ~= 8 then
  84.                 error("Incorrect color depth")
  85.             end
  86.  
  87.             --ignore the image descriptor byte
  88.             file:read(1)
  89.  
  90.             -- ignore the file identification
  91.             file:read(idsize)
  92.            
  93.             if cmaptype == 1 then
  94.                 -- skip colour palette
  95.                 file:read(768)
  96.                 end
  97.  
  98.             if type == 1 then
  99.                 y = ysize-1
  100.             else
  101.                 y = 0
  102.             end
  103.  
  104.             mappy.copyLayer(mappy.getValue(mappy.CURLAYER),mappy.MPY_UNDO)
  105.  
  106.             cblock = mappy.getValue(mappy.CURBLOCK)
  107.             for i=0,(ysize-1) do
  108.                 for x=0,(xsize-1) do
  109.                                 io.write ("x=",x," y=",y,"\n")
  110.                     rnd = ReadChar(file) / 256
  111.                     if math.random() < rnd then
  112.                         mappy.setBlock(x,y,cblock)
  113.                     end
  114.                 end
  115.                 if type == 1 then
  116.                     y = y - 1
  117.                 else
  118.                     y = y + 1
  119.                 end
  120.             end
  121.            
  122.             file:close()
  123.  
  124.             mappy.updateScreen()
  125.  
  126.         end
  127.     end
  128.  
  129. end
  130.  
  131. test, errormsg = pcall( main )
  132. if not test then
  133.     ShowError(errormsg)
  134. end
  135.