init.lua 8.8 KB

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