Advertisement
Xenogami

fire effect

Feb 15th, 2015
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.95 KB | None | 0 0
  1. --[[
  2. fire effect
  3. code by GopherAtl, 2013
  4.  
  5. No rights reserved, DWYW.
  6. --]]
  7.  
  8. function findDev (dType)
  9.   local d
  10.   for _,d in pairs(peripheral.getNames()) do
  11.     if (peripheral.getType(d) == dType) then
  12.           return peripheral.wrap(d)
  13.     end
  14.   end
  15.   return nil, dType..": not found"
  16. end
  17.  
  18. m=findDev("monitor")
  19.  
  20. local w,h=m.getSize()
  21.  
  22. local dithers={
  23.  " ",
  24.  ".",
  25.  "-",
  26.  "+",
  27.  "%",
  28.  "*",
  29.  "#",
  30.  "@",
  31. }
  32.  
  33.  
  34. local ranges= {
  35.   { colors.black, colors.gray, },
  36.   { colors.gray, colors.red, },
  37.   { colors.red, colors.orange, },
  38.   { colors.orange, colors.yellow, },
  39. }
  40.  
  41. local coolWeight=75
  42.  
  43. if not m.isColor() then
  44.   ranges={
  45.     { colors.black,colors.white },
  46.   }
  47.   coolWeight=25
  48. end
  49.  
  50. local numDithers=(#dithers*2-1)
  51. local max_bright=#ranges*numDithers
  52.  
  53.  
  54. function genShaded(brightness)
  55.   --break into color group
  56.   local bgColor=colors.black
  57.   local fgColor=colors.black
  58.   local char=" "
  59.  
  60.   if brightness>=max_bright then
  61.     bgColor=ranges[#ranges][2]
  62.     fgColor=ranges[#ranges][1]
  63.   else
  64.     local dither=brightness%numDithers    
  65.     local range=(brightness-dither)/numDithers
  66.     local inv=false
  67.     bgColor=ranges[range+1][1]
  68.     fgColor=ranges[range+1][2]
  69.     if dither>=#dithers then
  70.       dither=2*#dithers-dither-1
  71.       local t=bgColor
  72.       bgColor=fgColor
  73.       fgColor=t      
  74.     end
  75.     char=dithers[dither+1]
  76.   end
  77.  
  78.   --[[m.setBackgroundColor(bgColor)
  79.   m.setTextColor(fgColor)
  80.   m.write(char)--]]
  81.   return bgColor,fgColor,char
  82. end
  83.  
  84. local precalc_shade={}
  85.  
  86. for i=0,max_bright do
  87.   precalc_shade[i]={genShaded(i)}
  88. end
  89.  
  90. dither=true
  91.  
  92. function drawShaded(bright)
  93.   bright=math.floor(bright+.5)
  94.   if bright>max_bright then
  95.     bright=max_bright
  96.   elseif bright<0 then
  97.     bright=0
  98.   end
  99.   local shading=precalc_shade[bright]
  100.   if shading==nil then
  101.     error("brightness "..bright.." invalid!")
  102.   end
  103.   m.setBackgroundColor(shading[1])
  104.   m.setTextColor(shading[2])
  105.   m.write(dither and shading[3] or " ")
  106. end
  107.  
  108. local grid, gridB={},{}
  109.  
  110. for y=1,h+1 do
  111.   grid[y]={}
  112.   gridB[y]={}
  113.   for x=1,w do
  114.     grid[y][x]=0
  115.   end
  116. end
  117.  
  118. local coolingGrid={}
  119.  
  120. for y=1,h*4 do
  121.   coolingGrid[y]={}
  122.   for x=1,w*2 do
  123.     coolingGrid[y][x]=0
  124.   end
  125. end
  126.  
  127. function smooth(grid)
  128.   for y=0,(h*4-1) do
  129.     local up=(y-1)%(h*4)+1
  130.     local down=(y+1)%(h*4)+1
  131.     for x=0,(w*2-1) do
  132.       local left=(x-1)%(w*2)+1
  133.       local right=(x+1)%(w*2)+1
  134.       grid[y+1][x+1]=(grid[y+1][left]+grid[y+1][right]+grid[up][x+1]+grid[down][x+1])/4
  135.     end
  136.   end
  137. end
  138.  
  139. for i=1,1.6*w*h do
  140.   coolingGrid[math.random(1,h*4)][math.random(1,w*2)]=coolWeight
  141. end
  142.  
  143. for i=1,5 do
  144.   smooth(coolingGrid)
  145. end
  146.  
  147. coolingOffsetY=0
  148. coolingOffsetX=0
  149.  
  150.  
  151. function smoothFlame(grid)
  152.   for y=1,h do
  153.     local up=math.max(0,y-1)+1
  154.     local down=math.min((h-1),(y+1))+1
  155.     for x=0,(w-1) do
  156.       local left=(x-1)%w+1
  157.       local right=(x+1)%w+1
  158.       grid[y][x+1]=(grid[y+1][left]+grid[y+1][right]+grid[up][x+1]+grid[down][x+1])/4-coolingGrid[math.floor((y-1+coolingOffsetY)%(h*4))+1][math.floor((x+coolingOffsetX)%(w*2))+1]
  159.     end
  160.   end
  161. end
  162.  
  163. function draw()
  164.   for y=1,h do
  165.     for x=1,w do
  166.       m.setCursorPos(x,y)
  167.       drawShaded(grid[y][x]/2)
  168.     end
  169.   end
  170. end
  171.  
  172.  
  173.  
  174.  
  175. for x=1,w do
  176.   grid[h+1][x]=max_bright*2
  177. end
  178.  
  179. for i=1,w do
  180.   local x=math.random(1,w)
  181.   grid[h+1][x]=max_bright*3
  182. end
  183.  
  184. for i=1,h+1 do
  185.   smoothFlame(grid)
  186. end
  187.  
  188.  
  189. local tick=os.startTimer(0)
  190. while true do
  191.   local e={os.pullEvent()}
  192.   if e[1]=="timer" then
  193.     draw()
  194.     smoothFlame(grid)
  195.     for i=1,1 do
  196.       grid[h+1][math.random(1,w)]=max_bright*3
  197.     end
  198.     coolingOffsetY=(coolingOffsetY+1)%(h*4)
  199.     coolingOffsetX=(coolingOffsetX+math.random(-2,3)/2)%(w*2)
  200.     tick=os.startTimer(0)
  201.   elseif e[1]=="char" then
  202.     local ch=e[2]:lower()
  203.     if ch=="d" then
  204.       dither=not dither
  205.     elseif ch=="q" then
  206.       break
  207.     end
  208.   elseif e[1]=="monitor_touch" then
  209.     dither=not dither
  210.   end
  211. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement