Advertisement
se7enek

Kominek

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