init.lua 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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.5, 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 (creative and creative.is_enabled_for
  103. and creative.is_enabled_for(player_name)) then
  104. -- Wear tool
  105. local wdef = itemstack:get_definition()
  106. itemstack:add_wear(1000)
  107. -- Tool break sound
  108. if itemstack:get_count() == 0 and wdef.sound and wdef.sound.breaks then
  109. minetest.sound_play(wdef.sound.breaks,
  110. {pos = sound_pos, gain = 0.5}, true)
  111. end
  112. return itemstack
  113. end
  114. end
  115. })
  116. minetest.register_craft({
  117. output = "fire:flint_and_steel",
  118. recipe = {
  119. {"default:flint", "default:steel_ingot"}
  120. }
  121. })
  122. -- Override coalblock to enable permanent flame above
  123. -- Coalblock is non-flammable to avoid unwanted basic_flame nodes
  124. minetest.override_item("default:coalblock", {
  125. after_destruct = function(pos)
  126. pos.y = pos.y + 1
  127. if minetest.get_node(pos).name == "fire:permanent_flame" then
  128. minetest.remove_node(pos)
  129. end
  130. end,
  131. on_ignite = function(pos)
  132. local flame_pos = {x = pos.x, y = pos.y + 1, z = pos.z}
  133. if minetest.get_node(flame_pos).name == "air" then
  134. minetest.set_node(flame_pos, {name = "fire:permanent_flame"})
  135. end
  136. end
  137. })
  138. --
  139. -- Sound
  140. --
  141. -- Enable if no setting present
  142. local flame_sound = minetest.settings:get_bool("flame_sound", true)
  143. if flame_sound then
  144. local handles = {}
  145. local timer = 0
  146. -- Parameters
  147. local radius = 8 -- Flame node search radius around player
  148. local cycle = 3 -- Cycle time for sound updates
  149. -- Update sound for player
  150. function fire.update_player_sound(player)
  151. local player_name = player:get_player_name()
  152. -- Search for flame nodes in radius around player
  153. local ppos = player:get_pos()
  154. local areamin = vector.subtract(ppos, radius)
  155. local areamax = vector.add(ppos, radius)
  156. local fpos, num = minetest.find_nodes_in_area(
  157. areamin,
  158. areamax,
  159. {"fire:basic_flame", "fire:permanent_flame"}
  160. )
  161. -- Total number of flames in radius
  162. local flames = (num["fire:basic_flame"] or 0) +
  163. (num["fire:permanent_flame"] or 0)
  164. -- Stop previous sound
  165. if handles[player_name] then
  166. minetest.sound_stop(handles[player_name])
  167. handles[player_name] = nil
  168. end
  169. -- If flames
  170. if flames > 0 then
  171. -- Find centre of flame positions
  172. local fposmid = fpos[1]
  173. -- If more than 1 flame
  174. if #fpos > 1 then
  175. local fposmin = areamax
  176. local fposmax = areamin
  177. for i = 1, #fpos do
  178. local fposi = fpos[i]
  179. if fposi.x > fposmax.x then
  180. fposmax.x = fposi.x
  181. end
  182. if fposi.y > fposmax.y then
  183. fposmax.y = fposi.y
  184. end
  185. if fposi.z > fposmax.z then
  186. fposmax.z = fposi.z
  187. end
  188. if fposi.x < fposmin.x then
  189. fposmin.x = fposi.x
  190. end
  191. if fposi.y < fposmin.y then
  192. fposmin.y = fposi.y
  193. end
  194. if fposi.z < fposmin.z then
  195. fposmin.z = fposi.z
  196. end
  197. end
  198. fposmid = vector.divide(vector.add(fposmin, fposmax), 2)
  199. end
  200. -- Play sound
  201. local handle = minetest.sound_play("fire_fire", {
  202. pos = fposmid,
  203. to_player = player_name,
  204. gain = math.min(0.06 * (1 + flames * 0.125), 0.18),
  205. max_hear_distance = 32,
  206. loop = true -- In case of lag
  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() end
  237. --
  238. -- ABMs
  239. --
  240. if fire_enabled then
  241. -- Ignite neighboring nodes, add basic flames
  242. minetest.register_abm({
  243. label = "Ignite flame",
  244. nodenames = {"group:flammable"},
  245. neighbors = {"group:igniter"},
  246. interval = 7,
  247. chance = 12,
  248. catch_up = false,
  249. action = function(pos)
  250. local p = minetest.find_node_near(pos, 1, {"air"})
  251. if p then
  252. minetest.set_node(p, {name = "fire:basic_flame"})
  253. end
  254. end
  255. })
  256. -- Remove flammable nodes around basic flame
  257. minetest.register_abm({
  258. label = "Remove flammable nodes",
  259. nodenames = {"fire:basic_flame"},
  260. neighbors = "group:flammable",
  261. interval = 5,
  262. chance = 18,
  263. catch_up = false,
  264. action = function(pos)
  265. local p = minetest.find_node_near(pos, 1, {"group:flammable"})
  266. if not p then
  267. return
  268. end
  269. local flammable_node = minetest.get_node(p)
  270. local def = minetest.registered_nodes[flammable_node.name]
  271. if def.on_burn then
  272. def.on_burn(p)
  273. else
  274. minetest.remove_node(p)
  275. minetest.check_for_falling(p)
  276. end
  277. end
  278. })
  279. end