FeynmanTech

Advanced image plotting for Powder Toy

Mar 21st, 2013
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.19 KB | None | 0 0
  1. --To use the main function: Type in console 'plot("filename.bmp")' (It is necessary that you save the file as a 24-bit bitmap), and then type in the element to be drawn with. With some elements, it will use a property other than deco layer. It also always automatically does temp and life.
  2. --COAL/BCOL: tmp2 - Grayscale
  3. --GRAV: vx - Grayscale
  4. --BRAY: life - Grayscale
  5. --EMBR: ctype - Color, but not very high-quality for very dark colors
  6. --WIFI: tmp - Neither color nor grayscale, no idea what to call it.
  7. --Any powered material: life - Grayscale
  8. --WWLD: Ctype - not useful for drawing images, but can transfer patterns from Golly!
  9. --There is a secondary function too: elemplot("filename.bmp"). It creates an image with a limited pallete of elements, but it is just in monochrome.
  10. function plot(filename)
  11. elemtype = tpt.input("Element type","This is the element it will be drawn in")
  12. local f=io.open(filename,'rb')
  13. f:seek('set',18)
  14. local wval=string.byte(f:read(1))+string.byte(f:read(1))*256+string.byte(f:read(1))*65536+string.byte(f:read(1))*16777216
  15. local hval=string.byte(f:read(1))+string.byte(f:read(1))*256+string.byte(f:read(1))*65536+string.byte(f:read(1))*16777216
  16. f:seek('set',54)
  17. local r,g,b=0,0,0
  18. for j=0,hval-1 do
  19.  for i=0,wval-1 do
  20.   b,g,r=string.byte(f:read(1)),string.byte(f:read(1)),string.byte(f:read(1))
  21.   if i<612 and hval-j<380 and(not(r + g + b < 0)) then
  22.    tpt.create(i,hval-j,elemtype)
  23.     if elemtype ~= 'wwld' then
  24.      tpt.set_property('dcolour',r*65536+g*256+b+16777216*255,i,hval-j)
  25.     end
  26.    if elemtype ~= 'embr' and elemtype ~= 'pump' and elemtype ~= 'lcry' and elemtype ~= 'pcln' and elemtype ~= 'hswc' and elemtype ~= 'gpmp' and elemtype ~= 'swch' and elemtype ~= 'pbcn'  and elemtype ~= 'dlay' then
  27.    tpt.set_property('life',(r+g+b) * (1000/765),i,hval-j)
  28.    end
  29.    if elemtype ~= 'coal' then
  30.    tpt.set_property('temp',((r / 255) * (6150) - (g / 255) * (4270)-(b / 255)) + 273.15,i,hval-j)
  31.    end
  32.    if elemtype == 'grav' then
  33.    tpt.set_property('vx',(r + g + b) * (10 / 765),i,hval-j)
  34.    end
  35.    if elemtype == 'bray' then
  36.     tpt.set_property('tmp',1,i,hval-j)
  37.    elseif elemtype == 'embr' then
  38.     tpt.set_property('ctype',r*65536+g*256+b+16777216*255,i,hval-j)
  39.    elseif elemtype == 'coal' or elemtype == 'bcol' then
  40.     tpt.set_property('tmp2',(r+g+b) + 273.15,i,hval-j)
  41.      if elemtype ~= 'bcol' then
  42.      tpt.set_property('life',110,i,hval-j)
  43.      end
  44.     tpt.set_property('tmp',50,i,hval-j)
  45.    elseif elemtype == 'wifi' then
  46.     tpt.set_property('tmp',((r+g+b) * (99 / 765)) + 273.15,i,hval-j)
  47.    elseif elemtype == 'pump' or elemtype == 'lcry' or elemtype == 'pcln' or elemtype == 'hswc' or elemtype == 'gpmp' or elemtype == 'swch' or elemtype == 'pbcn'  or elemtype == 'dlay' then
  48.    tpt.set_property('life',(r + g + b) / 76.5,i,hval-j)
  49.    elseif elemtype == 'wwld' or elemtype == 'wire' then
  50.     if b > 250 and g < 200 then
  51.      tpt.set_property('ctype',1,i,hval-j)
  52.     elseif r > 150 and g > 150 then
  53.      tpt.set_property('ctype',2,i,hval-j)
  54.     elseif r < 100 and g < 100 and b < 100 then
  55.      tpt.set_property('type',0,i,hval-j)
  56.     end
  57.   end
  58.   end
  59.   end
  60.  end
  61.  for i=1,wval%4 do f:read(1) end
  62. end
  63. function elemplot(filename)
  64. local f=io.open(filename,'rb')
  65. f:seek('set',18)
  66. local width=string.byte(f:read(1))+string.byte(f:read(1))*256+string.byte(f:read(1))*65536+string.byte(f:read(1))*16777216
  67. local height=string.byte(f:read(1))+string.byte(f:read(1))*256+string.byte(f:read(1))*65536+string.byte(f:read(1))*16777216
  68. f:seek('set',55)
  69. local r,g,b=0,0,0
  70. for j=0,height-1 do
  71.  for i=0,width-1 do
  72.   b,g,r=string.byte(f:read(1)),string.byte(f:read(1)),string.byte(f:read(1))
  73.   if i<612 and height-j<380 then
  74.    tpt.create(i,height-j,elemtype)
  75.    if r + g + b < 10 then
  76.    tpt.set_property('type','bray',i,height-j)
  77.    tpt.set_property('life',50,i,height-j)
  78.    tpt.set_property('tmp',1,i,height-j)
  79.    elseif r + g + b < 80 then
  80.    tpt.set_property('type','grav',i,height-j)
  81.    elseif r + g + b < 120 then
  82.    tpt.set_property('type','coal',i,height-j)
  83.    tpt.set_property('life',110,i,height-j)
  84.    tpt.set_property('tmp',50,i,height-j)
  85.    elseif r + g + b < 160 then
  86.    tpt.set_property('type','bcol',i,height-j)
  87.    tpt.set_property('life',110,i,height-j)
  88.    elseif r + g + b < 200 then
  89.    tpt.set_property('type','glas',i,height-j)
  90.    elseif r + g + b < 250 then
  91.    tpt.set_property('type','lcry',i,height-j)
  92.    elseif r + g + b < 290 then
  93.    tpt.set_property('type','bgla',i,height-j)
  94.    elseif r + g + b < 380 then
  95.    tpt.set_property('type','brck',i,height-j)
  96.    elseif r + g + b < 430 then
  97.    tpt.set_property('type','lcry',i,height-j)
  98.    tpt.set_property('tmp2',6,i,height-j)
  99.    elseif r + g + b < 490 then
  100.    tpt.set_property('type','stne',i,height-j)
  101.    elseif r + g + b < 560 then
  102.    tpt.set_property('type','lcry',i,height-j)
  103.    tpt.set_property('tmp2',10,i,height-j)
  104.    elseif r + g + b < 580 then
  105.    tpt.set_property('type','cnct',i,height-j)
  106.    elseif r + g + b < 620 then
  107.    tpt.set_property('type','rbdm',i,height-j)
  108.    elseif r + g + b < 800 then
  109.    tpt.set_property('type','whol',i,height-j)
  110. end
  111.   end
  112.  end
  113.  for i=1,width%4 do f:read(1) end
  114. end
  115. end
  116.  
  117. --   elseif r + g + b < 10 then
  118. --   tpt.set_property('type','bray',i,height-j)
Advertisement
Add Comment
Please, Sign In to add comment