Guest User

Untitled

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