init.lua 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. -- minetest/fire/init.lua
  2. minetest.register_node("fire:basic_flame", {
  3. description = "Fire",
  4. drawtype = "firelike",
  5. tiles = {{
  6. name="fire_basic_flame_animated.png",
  7. animation={type="vertical_frames", aspect_w=16, aspect_h=16, length=1},
  8. }},
  9. inventory_image = "fire_basic_flame.png",
  10. light_source = 14,
  11. groups = {igniter=2,dig_immediate=3,hot=3},
  12. drop = '',
  13. walkable = false,
  14. buildable_to = true,
  15. damage_per_second = 4,
  16. after_place_node = function(pos, placer)
  17. fire.on_flame_add_at(pos)
  18. end,
  19. after_dig_node = function(pos, oldnode, oldmetadata, digger)
  20. fire.on_flame_remove_at(pos)
  21. end,
  22. })
  23. fire = {}
  24. fire.D = 6
  25. -- key: position hash of low corner of area
  26. -- value: {handle=sound handle, name=sound name}
  27. fire.sounds = {}
  28. function fire.get_area_p0p1(pos)
  29. local p0 = {
  30. x=math.floor(pos.x/fire.D)*fire.D,
  31. y=math.floor(pos.y/fire.D)*fire.D,
  32. z=math.floor(pos.z/fire.D)*fire.D,
  33. }
  34. local p1 = {
  35. x=p0.x+fire.D-1,
  36. y=p0.y+fire.D-1,
  37. z=p0.z+fire.D-1
  38. }
  39. return p0, p1
  40. end
  41. function fire.update_sounds_around(pos)
  42. local p0, p1 = fire.get_area_p0p1(pos)
  43. local cp = {x=(p0.x+p1.x)/2, y=(p0.y+p1.y)/2, z=(p0.z+p1.z)/2}
  44. local flames_p = minetest.find_nodes_in_area(p0, p1, {"fire:basic_flame"})
  45. --print("number of flames at "..minetest.pos_to_string(p0).."/"
  46. -- ..minetest.pos_to_string(p1)..": "..#flames_p)
  47. local should_have_sound = (#flames_p > 0)
  48. local wanted_sound = nil
  49. if #flames_p >= 9 then
  50. wanted_sound = {name="fire_large", gain=1.5}
  51. elseif #flames_p > 0 then
  52. wanted_sound = {name="fire_small", gain=1.5}
  53. end
  54. local p0_hash = minetest.hash_node_position(p0)
  55. local sound = fire.sounds[p0_hash]
  56. if not sound then
  57. if should_have_sound then
  58. fire.sounds[p0_hash] = {
  59. handle = minetest.sound_play(wanted_sound, {pos=cp, loop=true}),
  60. name = wanted_sound.name,
  61. }
  62. end
  63. else
  64. if not wanted_sound then
  65. minetest.sound_stop(sound.handle)
  66. fire.sounds[p0_hash] = nil
  67. elseif sound.name ~= wanted_sound.name then
  68. minetest.sound_stop(sound.handle)
  69. fire.sounds[p0_hash] = {
  70. handle = minetest.sound_play(wanted_sound, {pos=cp, loop=true}),
  71. name = wanted_sound.name,
  72. }
  73. end
  74. end
  75. end
  76. function fire.on_flame_add_at(pos)
  77. --print("flame added at "..minetest.pos_to_string(pos))
  78. fire.update_sounds_around(pos)
  79. end
  80. function fire.on_flame_remove_at(pos)
  81. --print("flame removed at "..minetest.pos_to_string(pos))
  82. fire.update_sounds_around(pos)
  83. end
  84. function fire.find_pos_for_flame_around(pos)
  85. return minetest.find_node_near(pos, 1, {"air"})
  86. end
  87. function fire.flame_should_extinguish(pos)
  88. if minetest.setting_getbool("disable_fire") then return true end
  89. --return minetest.find_node_near(pos, 1, {"group:puts_out_fire"})
  90. local p0 = {x=pos.x-2, y=pos.y, z=pos.z-2}
  91. local p1 = {x=pos.x+2, y=pos.y, z=pos.z+2}
  92. local ps = minetest.find_nodes_in_area(p0, p1, {"group:puts_out_fire"})
  93. return (#ps ~= 0)
  94. end
  95. -- Ignite neighboring nodes
  96. minetest.register_abm({
  97. nodenames = {"group:flammable"},
  98. neighbors = {"group:igniter"},
  99. interval = 1,
  100. chance = 2,
  101. action = function(p0, node, _, _)
  102. -- If there is water or stuff like that around flame, don't ignite
  103. if fire.flame_should_extinguish(p0) then
  104. return
  105. end
  106. local p = fire.find_pos_for_flame_around(p0)
  107. if p then
  108. minetest.set_node(p, {name="fire:basic_flame"})
  109. fire.on_flame_add_at(p)
  110. end
  111. end,
  112. })
  113. -- Rarely ignite things from far
  114. minetest.register_abm({
  115. nodenames = {"group:igniter"},
  116. neighbors = {"air"},
  117. interval = 2,
  118. chance = 10,
  119. action = function(p0, node, _, _)
  120. local reg = minetest.registered_nodes[node.name]
  121. if not reg or not reg.groups.igniter or reg.groups.igniter < 2 then
  122. return
  123. end
  124. local d = reg.groups.igniter
  125. local p = minetest.find_node_near(p0, d, {"group:flammable"})
  126. if p then
  127. -- If there is water or stuff like that around flame, don't ignite
  128. if fire.flame_should_extinguish(p) then
  129. return
  130. end
  131. local p2 = fire.find_pos_for_flame_around(p)
  132. if p2 then
  133. minetest.set_node(p2, {name="fire:basic_flame"})
  134. fire.on_flame_add_at(p2)
  135. end
  136. end
  137. end,
  138. })
  139. -- Remove flammable nodes and flame
  140. minetest.register_abm({
  141. nodenames = {"fire:basic_flame"},
  142. interval = 1,
  143. chance = 2,
  144. action = function(p0, node, _, _)
  145. -- If there is water or stuff like that around flame, remove flame
  146. if fire.flame_should_extinguish(p0) then
  147. minetest.remove_node(p0)
  148. fire.on_flame_remove_at(p0)
  149. return
  150. end
  151. -- Make the following things rarer
  152. if math.random(1,3) == 1 then
  153. return
  154. end
  155. -- If there are no flammable nodes around flame, remove flame
  156. if not minetest.find_node_near(p0, 1, {"group:flammable"}) then
  157. minetest.remove_node(p0)
  158. fire.on_flame_remove_at(p0)
  159. return
  160. end
  161. if math.random(1,4) == 1 then
  162. -- remove a flammable node around flame
  163. local p = minetest.find_node_near(p0, 1, {"group:flammable"})
  164. if p then
  165. -- If there is water or stuff like that around flame, don't remove
  166. if fire.flame_should_extinguish(p0) then
  167. return
  168. end
  169. minetest.remove_node(p)
  170. minetest.check_for_falling(p)
  171. end
  172. else
  173. -- remove flame
  174. minetest.remove_node(p0)
  175. fire.on_flame_remove_at(p0)
  176. end
  177. end,
  178. })