2
0

init.lua 8.0 KB

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