init.lua 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. -- fire/init.lua
  2. -- Global namespace for functions
  3. fire = {}
  4. -- Load support for MT game translation.
  5. local S = minetest.get_translator("fire")
  6. -- 'Enable fire' setting
  7. local fire_enabled = minetest.settings:get_bool("enable_fire")
  8. if fire_enabled == nil then
  9. -- enable_fire setting not specified, check for disable_fire
  10. local fire_disabled = minetest.settings:get_bool("disable_fire")
  11. if fire_disabled == nil then
  12. -- Neither setting specified, check whether singleplayer
  13. fire_enabled = minetest.is_singleplayer()
  14. else
  15. fire_enabled = not fire_disabled
  16. end
  17. end
  18. --
  19. -- Items
  20. --
  21. -- Flood flame function
  22. local function flood_flame(pos, _, newnode)
  23. -- Play flame extinguish sound if liquid is not an 'igniter'
  24. if minetest.get_item_group(newnode.name, "igniter") == 0 then
  25. minetest.sound_play("fire_extinguish_flame",
  26. {pos = pos, max_hear_distance = 16, gain = 0.15}, true)
  27. end
  28. -- Remove the flame
  29. return false
  30. end
  31. -- Flame nodes
  32. local fire_node = {
  33. drawtype = "firelike",
  34. tiles = {{
  35. name = "fire_basic_flame_animated.png",
  36. animation = {
  37. type = "vertical_frames",
  38. aspect_w = 16,
  39. aspect_h = 16,
  40. length = 1
  41. }}
  42. },
  43. inventory_image = "fire_basic_flame.png",
  44. paramtype = "light",
  45. light_source = 13,
  46. walkable = false,
  47. buildable_to = true,
  48. sunlight_propagates = true,
  49. floodable = true,
  50. damage_per_second = 4,
  51. groups = {igniter = 2, dig_immediate = 3, fire = 1},
  52. drop = "",
  53. on_flood = flood_flame
  54. }
  55. -- Basic flame node
  56. local flame_fire_node = table.copy(fire_node)
  57. flame_fire_node.description = S("Fire")
  58. flame_fire_node.groups.not_in_creative_inventory = 1
  59. flame_fire_node.on_timer = function(pos)
  60. if not minetest.find_node_near(pos, 1, {"group:flammable"}) then
  61. minetest.remove_node(pos)
  62. return
  63. end
  64. -- Restart timer
  65. return true
  66. end
  67. flame_fire_node.on_construct = function(pos)
  68. minetest.get_node_timer(pos):start(math.random(30, 60))
  69. end
  70. minetest.register_node("fire:basic_flame", flame_fire_node)
  71. -- Permanent flame node
  72. local permanent_fire_node = table.copy(fire_node)
  73. permanent_fire_node.description = S("Permanent Fire")
  74. minetest.register_node("fire:permanent_flame", permanent_fire_node)
  75. -- Flint and Steel
  76. minetest.register_tool("fire:flint_and_steel", {
  77. description = S("Flint and Steel"),
  78. inventory_image = "fire_flint_steel.png",
  79. sound = {breaks = "default_tool_breaks"},
  80. on_use = function(itemstack, user, pointed_thing)
  81. local sound_pos = pointed_thing.above or user:get_pos()
  82. minetest.sound_play("fire_flint_and_steel",
  83. {pos = sound_pos, gain = 0.2, max_hear_distance = 8}, true)
  84. local player_name = user:get_player_name()
  85. if pointed_thing.type == "node" then
  86. local node_under = minetest.get_node(pointed_thing.under).name
  87. local nodedef = minetest.registered_nodes[node_under]
  88. if not nodedef then
  89. return
  90. end
  91. if minetest.is_protected(pointed_thing.under, player_name) then
  92. minetest.chat_send_player(player_name, "This area is protected")
  93. return
  94. end
  95. if nodedef.on_ignite then
  96. nodedef.on_ignite(pointed_thing.under, user)
  97. elseif minetest.get_item_group(node_under, "flammable") >= 1
  98. and minetest.get_node(pointed_thing.above).name == "air" then
  99. minetest.set_node(pointed_thing.above, {name = "fire:basic_flame"})
  100. end
  101. end
  102. if not minetest.is_creative_enabled(player_name) then
  103. -- Wear tool
  104. local wdef = itemstack:get_definition()
  105. itemstack:add_wear(1000)
  106. -- Tool break sound
  107. if itemstack:get_count() == 0 and wdef.sound and wdef.sound.breaks then
  108. minetest.sound_play(wdef.sound.breaks,
  109. {pos = sound_pos, gain = 0.5}, true)
  110. end
  111. return itemstack
  112. end
  113. end
  114. })
  115. minetest.register_craft({
  116. output = "fire:flint_and_steel",
  117. recipe = {
  118. {"default:flint", "default:steel_ingot"}
  119. }
  120. })
  121. -- Override coalblock to enable permanent flame above
  122. -- Coalblock is non-flammable to avoid unwanted basic_flame nodes
  123. minetest.override_item("default:coalblock", {
  124. after_destruct = function(pos)
  125. pos.y = pos.y + 1
  126. if minetest.get_node(pos).name == "fire:permanent_flame" then
  127. minetest.remove_node(pos)
  128. end
  129. end,
  130. on_ignite = function(pos)
  131. local flame_pos = {x = pos.x, y = pos.y + 1, z = pos.z}
  132. if minetest.get_node(flame_pos).name == "air" then
  133. minetest.set_node(flame_pos, {name = "fire:permanent_flame"})
  134. end
  135. end
  136. })
  137. --
  138. -- Sound
  139. --
  140. -- Enable if no setting present
  141. local flame_sound = minetest.settings:get_bool("flame_sound", true)
  142. if flame_sound then
  143. local handles = {}
  144. local timer = 0
  145. -- Parameters
  146. local radius = 8 -- Flame node search radius around player
  147. local cycle = 3 -- Cycle time for sound updates
  148. -- Update sound for player
  149. function fire.update_player_sound(player)
  150. local player_name = player:get_player_name()
  151. -- Search for flame nodes in radius around player
  152. local ppos = player:get_pos()
  153. local areamin = vector.subtract(ppos, radius)
  154. local areamax = vector.add(ppos, radius)
  155. local fpos, num = minetest.find_nodes_in_area(
  156. areamin,
  157. areamax,
  158. {"fire:basic_flame", "fire:permanent_flame"}
  159. )
  160. -- Total number of flames in radius
  161. local flames = (num["fire:basic_flame"] or 0) +
  162. (num["fire:permanent_flame"] or 0)
  163. -- Stop previous sound
  164. if handles[player_name] then
  165. minetest.sound_stop(handles[player_name])
  166. handles[player_name] = nil
  167. end
  168. -- If flames
  169. if flames > 0 then
  170. -- Find centre of flame positions
  171. local fposmid = fpos[1]
  172. -- If more than 1 flame
  173. if #fpos > 1 then
  174. local fposmin = areamax
  175. local fposmax = areamin
  176. for i = 1, #fpos do
  177. local fposi = fpos[i]
  178. if fposi.x > fposmax.x then
  179. fposmax.x = fposi.x
  180. end
  181. if fposi.y > fposmax.y then
  182. fposmax.y = fposi.y
  183. end
  184. if fposi.z > fposmax.z then
  185. fposmax.z = fposi.z
  186. end
  187. if fposi.x < fposmin.x then
  188. fposmin.x = fposi.x
  189. end
  190. if fposi.y < fposmin.y then
  191. fposmin.y = fposi.y
  192. end
  193. if fposi.z < fposmin.z then
  194. fposmin.z = fposi.z
  195. end
  196. end
  197. fposmid = vector.divide(vector.add(fposmin, fposmax), 2)
  198. end
  199. -- Play sound
  200. local handle = minetest.sound_play("fire_fire", {
  201. pos = fposmid,
  202. to_player = player_name,
  203. gain = math.min(0.06 * (1 + flames * 0.125), 0.18),
  204. max_hear_distance = 32,
  205. loop = true -- In case of lag
  206. })
  207. -- Store sound handle for this player
  208. if handle then
  209. handles[player_name] = handle
  210. end
  211. end
  212. end
  213. -- Cycle for updating players sounds
  214. minetest.register_globalstep(function(dtime)
  215. timer = timer + dtime
  216. if timer < cycle then
  217. return
  218. end
  219. timer = 0
  220. local players = minetest.get_connected_players()
  221. for n = 1, #players do
  222. fire.update_player_sound(players[n])
  223. end
  224. end)
  225. -- Stop sound and clear handle on player leave
  226. minetest.register_on_leaveplayer(function(player)
  227. local player_name = player:get_player_name()
  228. if handles[player_name] then
  229. minetest.sound_stop(handles[player_name])
  230. handles[player_name] = nil
  231. end
  232. end)
  233. end
  234. -- Deprecated function kept temporarily to avoid crashes if mod fire nodes call it
  235. function fire.update_sounds_around() end
  236. --
  237. -- ABMs
  238. --
  239. if fire_enabled then
  240. -- Ignite neighboring nodes, add basic flames
  241. minetest.register_abm({
  242. label = "Ignite flame",
  243. nodenames = {"group:flammable"},
  244. neighbors = {"group:igniter"},
  245. interval = 7,
  246. chance = 12,
  247. catch_up = false,
  248. action = function(pos)
  249. local p = minetest.find_node_near(pos, 1, {"air"})
  250. if p then
  251. minetest.set_node(p, {name = "fire:basic_flame"})
  252. end
  253. end
  254. })
  255. -- Remove flammable nodes around basic flame
  256. minetest.register_abm({
  257. label = "Remove flammable nodes",
  258. nodenames = {"fire:basic_flame"},
  259. neighbors = "group:flammable",
  260. interval = 5,
  261. chance = 18,
  262. catch_up = false,
  263. action = function(pos)
  264. local p = minetest.find_node_near(pos, 1, {"group:flammable"})
  265. if not p then
  266. return
  267. end
  268. local flammable_node = minetest.get_node(p)
  269. local def = minetest.registered_nodes[flammable_node.name]
  270. if def.on_burn then
  271. def.on_burn(p)
  272. else
  273. minetest.remove_node(p)
  274. minetest.check_for_falling(p)
  275. end
  276. end
  277. })
  278. end