init.lua 8.7 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_alias("firestone:lighter", "fire:flint_and_steel")
  68. minetest.register_tool("fire:flint_and_steel", {
  69. description = "Lighter",
  70. inventory_image = "fire_flint_steel.png",
  71. sound = {breaks = "default_tool_breaks"},
  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. minetest.override_item("default:coalblock", {
  118. after_destruct = function(pos, oldnode)
  119. pos.y = pos.y + 1
  120. if minetest.get_node(pos).name == "fire:permanent_flame" then
  121. minetest.remove_node(pos)
  122. end
  123. end,
  124. on_ignite = function(pos, igniter)
  125. local flame_pos = {x = pos.x, y = pos.y + 1, z = pos.z}
  126. if minetest.get_node(flame_pos).name == "air" then
  127. minetest.set_node(flame_pos, {name = "fire:permanent_flame"})
  128. end
  129. end,
  130. })
  131. --
  132. -- Sound
  133. --
  134. local flame_sound = minetest.settings:get_bool("flame_sound")
  135. if flame_sound == nil then
  136. -- Enable if no setting present
  137. flame_sound = true
  138. end
  139. if flame_sound then
  140. local handles = {}
  141. local timer = 0
  142. -- Parameters
  143. local radius = 8 -- Flame node search radius around player
  144. local cycle = 3 -- Cycle time for sound updates
  145. -- Update sound for player
  146. function fire.update_player_sound(player)
  147. local player_name = player:get_player_name()
  148. -- Search for flame nodes in radius around player
  149. local ppos = player:getpos()
  150. local areamin = vector.subtract(ppos, radius)
  151. local areamax = vector.add(ppos, radius)
  152. local fpos, num = minetest.find_nodes_in_area(
  153. areamin,
  154. areamax,
  155. {"fire:basic_flame", "fire:permanent_flame"}
  156. )
  157. -- Total number of flames in radius
  158. local flames = (num["fire:basic_flame"] or 0) +
  159. (num["fire:permanent_flame"] or 0)
  160. -- Stop previous sound
  161. if handles[player_name] then
  162. minetest.sound_stop(handles[player_name])
  163. handles[player_name] = nil
  164. end
  165. -- If flames
  166. if flames > 0 then
  167. -- Find centre of flame positions
  168. local fposmid = fpos[1]
  169. -- If more than 1 flame
  170. if #fpos > 1 then
  171. local fposmin = areamax
  172. local fposmax = areamin
  173. for i = 1, #fpos do
  174. local fposi = fpos[i]
  175. if fposi.x > fposmax.x then
  176. fposmax.x = fposi.x
  177. end
  178. if fposi.y > fposmax.y then
  179. fposmax.y = fposi.y
  180. end
  181. if fposi.z > fposmax.z then
  182. fposmax.z = fposi.z
  183. end
  184. if fposi.x < fposmin.x then
  185. fposmin.x = fposi.x
  186. end
  187. if fposi.y < fposmin.y then
  188. fposmin.y = fposi.y
  189. end
  190. if fposi.z < fposmin.z then
  191. fposmin.z = fposi.z
  192. end
  193. end
  194. fposmid = vector.divide(vector.add(fposmin, fposmax), 2)
  195. end
  196. -- Play sound
  197. local handle = minetest.sound_play(
  198. "fire_fire",
  199. {
  200. pos = fposmid,
  201. to_player = player_name,
  202. gain = math.min(0.06 * (1 + flames * 0.125), 0.18),
  203. max_hear_distance = 32,
  204. loop = true, -- In case of lag
  205. }
  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(pos)
  236. end
  237. --
  238. -- ABMs
  239. --
  240. -- Extinguish all flames quickly with water, snow, ice
  241. minetest.register_abm({
  242. label = "Extinguish flame",
  243. nodenames = {"fire:basic_flame", "fire:permanent_flame"},
  244. neighbors = {"group:puts_out_fire"},
  245. interval = 3,
  246. chance = 1,
  247. catch_up = false,
  248. action = function(pos, node, active_object_count, active_object_count_wider)
  249. minetest.remove_node(pos)
  250. minetest.sound_play("fire_extinguish_flame",
  251. {pos = pos, max_hear_distance = 16, gain = 0.15})
  252. end,
  253. })
  254. -- Enable the following ABMs according to 'enable fire' setting
  255. local fire_enabled = minetest.settings:get_bool("enable_fire")
  256. if fire_enabled == nil then
  257. -- enable_fire setting not specified, check for disable_fire
  258. local fire_disabled = minetest.settings:get_bool("disable_fire")
  259. if fire_disabled == nil then
  260. -- Neither setting specified, check whether singleplayer
  261. fire_enabled = minetest.is_singleplayer()
  262. else
  263. fire_enabled = not fire_disabled
  264. end
  265. end
  266. if not fire_enabled then
  267. -- Remove basic flames only if fire disabled
  268. minetest.register_abm({
  269. label = "Remove disabled fire",
  270. nodenames = {"fire:basic_flame"},
  271. interval = 7,
  272. chance = 1,
  273. catch_up = false,
  274. action = minetest.remove_node,
  275. })
  276. else -- Fire enabled
  277. -- Ignite neighboring nodes, add basic flames
  278. minetest.register_abm({
  279. label = "Ignite flame",
  280. nodenames = {"group:flammable"},
  281. neighbors = {"group:igniter"},
  282. interval = 1,
  283. chance = 5,
  284. catch_up = false,
  285. action = function(pos, node, active_object_count, active_object_count_wider)
  286. -- If there is water or stuff like that around node, don't ignite
  287. if minetest.find_node_near(pos, 1, {"group:puts_out_fire"}) then
  288. return
  289. end
  290. local p = minetest.find_node_near(pos, 1, {"air"})
  291. if p then
  292. minetest.set_node(p, {name = "fire:basic_flame"})
  293. end
  294. end,
  295. })
  296. -- Remove flammable nodes around basic flame
  297. minetest.register_abm({
  298. label = "Remove flammable nodes",
  299. nodenames = {"fire:basic_flame"},
  300. neighbors = "group:flammable",
  301. interval = 5,
  302. chance = 18,
  303. catch_up = false,
  304. action = function(pos, node, active_object_count, active_object_count_wider)
  305. local p = minetest.find_node_near(pos, 1, {"group:flammable"})
  306. if p then
  307. local flammable_node = minetest.get_node(p)
  308. local def = minetest.registered_nodes[flammable_node.name]
  309. if def.on_burn then
  310. def.on_burn(p)
  311. else
  312. minetest.remove_node(p)
  313. minetest.check_for_falling(p)
  314. end
  315. end
  316. end,
  317. })
  318. end