init.lua 8.4 KB

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