Advertisement
GopherAtl

fire effect (cc lua)

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