furnace.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. -- default/furnace.lua
  2. -- support for MT game translation.
  3. local S = default.get_translator
  4. -- List of sound handles for active furnace
  5. local furnace_fire_sounds = {}
  6. --
  7. -- Formspecs
  8. --
  9. function default.get_furnace_active_formspec(fuel_percent, item_percent)
  10. return "size[8,8.5]"..
  11. "list[context;src;2.75,0.5;1,1;]"..
  12. "list[context;fuel;2.75,2.5;1,1;]"..
  13. "image[2.75,1.5;1,1;default_furnace_fire_bg.png^[lowpart:"..
  14. (fuel_percent)..":default_furnace_fire_fg.png]"..
  15. "image[3.75,1.5;1,1;gui_furnace_arrow_bg.png^[lowpart:"..
  16. (item_percent)..":gui_furnace_arrow_fg.png^[transformR270]"..
  17. "list[context;dst;4.75,0.96;2,2;]"..
  18. "list[current_player;main;0,4.25;8,1;]"..
  19. "list[current_player;main;0,5.5;8,3;8]"..
  20. "listring[context;dst]"..
  21. "listring[current_player;main]"..
  22. "listring[context;src]"..
  23. "listring[current_player;main]"..
  24. "listring[context;fuel]"..
  25. "listring[current_player;main]"..
  26. default.get_hotbar_bg(0, 4.25)
  27. end
  28. function default.get_furnace_inactive_formspec()
  29. return "size[8,8.5]"..
  30. "list[context;src;2.75,0.5;1,1;]"..
  31. "list[context;fuel;2.75,2.5;1,1;]"..
  32. "image[2.75,1.5;1,1;default_furnace_fire_bg.png]"..
  33. "image[3.75,1.5;1,1;gui_furnace_arrow_bg.png^[transformR270]"..
  34. "list[context;dst;4.75,0.96;2,2;]"..
  35. "list[current_player;main;0,4.25;8,1;]"..
  36. "list[current_player;main;0,5.5;8,3;8]"..
  37. "listring[context;dst]"..
  38. "listring[current_player;main]"..
  39. "listring[context;src]"..
  40. "listring[current_player;main]"..
  41. "listring[context;fuel]"..
  42. "listring[current_player;main]"..
  43. default.get_hotbar_bg(0, 4.25)
  44. end
  45. --
  46. -- Node callback functions that are the same for active and inactive furnace
  47. --
  48. local function can_dig(pos, player)
  49. local meta = minetest.get_meta(pos);
  50. local inv = meta:get_inventory()
  51. return inv:is_empty("fuel") and inv:is_empty("dst") and inv:is_empty("src")
  52. end
  53. local function allow_metadata_inventory_put(pos, listname, index, stack, player)
  54. if minetest.is_protected(pos, player:get_player_name()) then
  55. return 0
  56. end
  57. local meta = minetest.get_meta(pos)
  58. local inv = meta:get_inventory()
  59. if listname == "fuel" then
  60. if minetest.get_craft_result({method="fuel", width=1, items={stack}}).time ~= 0 then
  61. if inv:is_empty("src") then
  62. meta:set_string("infotext", S("Furnace is empty"))
  63. end
  64. return stack:get_count()
  65. else
  66. return 0
  67. end
  68. elseif listname == "src" then
  69. return stack:get_count()
  70. elseif listname == "dst" then
  71. return 0
  72. end
  73. end
  74. local function allow_metadata_inventory_move(pos, from_list, from_index, to_list, to_index, count, player)
  75. local meta = minetest.get_meta(pos)
  76. local inv = meta:get_inventory()
  77. local stack = inv:get_stack(from_list, from_index)
  78. return allow_metadata_inventory_put(pos, to_list, to_index, stack, player)
  79. end
  80. local function allow_metadata_inventory_take(pos, listname, index, stack, player)
  81. if minetest.is_protected(pos, player:get_player_name()) then
  82. return 0
  83. end
  84. return stack:get_count()
  85. end
  86. local function stop_furnace_sound(pos, fadeout_step)
  87. local hash = minetest.hash_node_position(pos)
  88. local sound_ids = furnace_fire_sounds[hash]
  89. if sound_ids then
  90. for _, sound_id in ipairs(sound_ids) do
  91. minetest.sound_fade(sound_id, -1, 0)
  92. end
  93. furnace_fire_sounds[hash] = nil
  94. end
  95. end
  96. local function swap_node(pos, name)
  97. local node = minetest.get_node(pos)
  98. if node.name == name then
  99. return
  100. end
  101. node.name = name
  102. minetest.swap_node(pos, node)
  103. end
  104. local function furnace_node_timer(pos, elapsed)
  105. --
  106. -- Initialize metadata
  107. --
  108. local meta = minetest.get_meta(pos)
  109. local fuel_time = meta:get_float("fuel_time") or 0
  110. local src_time = meta:get_float("src_time") or 0
  111. local fuel_totaltime = meta:get_float("fuel_totaltime") or 0
  112. local inv = meta:get_inventory()
  113. local srclist, fuellist
  114. local dst_full = false
  115. local timer_elapsed = meta:get_int("timer_elapsed") or 0
  116. meta:set_int("timer_elapsed", timer_elapsed + 1)
  117. local cookable, cooked
  118. local fuel
  119. local update = true
  120. local items_smelt = 0
  121. while elapsed > 0 and update do
  122. update = false
  123. srclist = inv:get_list("src")
  124. fuellist = inv:get_list("fuel")
  125. --
  126. -- Cooking
  127. --
  128. -- Check if we have cookable content
  129. local aftercooked
  130. cooked, aftercooked = minetest.get_craft_result({method = "cooking", width = 1, items = srclist})
  131. cookable = cooked.time ~= 0
  132. local el = math.min(elapsed, fuel_totaltime - fuel_time)
  133. if cookable then -- fuel lasts long enough, adjust el to cooking duration
  134. el = math.min(el, cooked.time - src_time)
  135. end
  136. -- Check if we have enough fuel to burn
  137. if fuel_time < fuel_totaltime then
  138. -- The furnace is currently active and has enough fuel
  139. fuel_time = fuel_time + el
  140. -- If there is a cookable item then check if it is ready yet
  141. if cookable then
  142. src_time = src_time + el
  143. if src_time >= cooked.time then
  144. -- Place result in dst list if possible
  145. if inv:room_for_item("dst", cooked.item) then
  146. inv:add_item("dst", cooked.item)
  147. inv:set_stack("src", 1, aftercooked.items[1])
  148. src_time = src_time - cooked.time
  149. update = true
  150. else
  151. dst_full = true
  152. end
  153. items_smelt = items_smelt + 1
  154. else
  155. -- Item could not be cooked: probably missing fuel
  156. update = true
  157. end
  158. end
  159. else
  160. -- Furnace ran out of fuel
  161. if cookable then
  162. -- We need to get new fuel
  163. local afterfuel
  164. fuel, afterfuel = minetest.get_craft_result({method = "fuel", width = 1, items = fuellist})
  165. if fuel.time == 0 then
  166. -- No valid fuel in fuel list
  167. fuel_totaltime = 0
  168. src_time = 0
  169. else
  170. -- prevent blocking of fuel inventory (for automatization mods)
  171. local is_fuel = minetest.get_craft_result({method = "fuel", width = 1, items = {afterfuel.items[1]:to_string()}})
  172. if is_fuel.time == 0 then
  173. table.insert(fuel.replacements, afterfuel.items[1])
  174. inv:set_stack("fuel", 1, "")
  175. else
  176. -- Take fuel from fuel list
  177. inv:set_stack("fuel", 1, afterfuel.items[1])
  178. end
  179. -- Put replacements in dst list or drop them on the furnace.
  180. local replacements = fuel.replacements
  181. if replacements[1] then
  182. local leftover = inv:add_item("dst", replacements[1])
  183. if not leftover:is_empty() then
  184. local above = vector.new(pos.x, pos.y + 1, pos.z)
  185. local drop_pos = minetest.find_node_near(above, 1, {"air"}) or above
  186. minetest.item_drop(replacements[1], nil, drop_pos)
  187. end
  188. end
  189. update = true
  190. fuel_totaltime = fuel.time + (fuel_totaltime - fuel_time)
  191. end
  192. else
  193. -- We don't need to get new fuel since there is no cookable item
  194. fuel_totaltime = 0
  195. src_time = 0
  196. end
  197. fuel_time = 0
  198. end
  199. elapsed = elapsed - el
  200. end
  201. if items_smelt > 0 then
  202. -- Play cooling sound
  203. minetest.sound_play("default_cool_lava",
  204. { pos = pos, max_hear_distance = 16, gain = 0.07 * math.min(items_smelt, 7) }, true)
  205. end
  206. if fuel and fuel_totaltime > fuel.time then
  207. fuel_totaltime = fuel.time
  208. end
  209. if srclist and srclist[1]:is_empty() then
  210. src_time = 0
  211. end
  212. --
  213. -- Update formspec, infotext and node
  214. --
  215. local formspec
  216. local item_state
  217. local item_percent = 0
  218. if cookable then
  219. item_percent = math.floor(src_time / cooked.time * 100)
  220. if dst_full then
  221. item_state = S("100% (output full)")
  222. else
  223. item_state = S("@1%", item_percent)
  224. end
  225. else
  226. if srclist and not srclist[1]:is_empty() then
  227. item_state = S("Not cookable")
  228. else
  229. item_state = S("Empty")
  230. end
  231. end
  232. local fuel_state = S("Empty")
  233. local active = false
  234. local result = false
  235. if fuel_totaltime ~= 0 then
  236. active = true
  237. local fuel_percent = 100 - math.floor(fuel_time / fuel_totaltime * 100)
  238. fuel_state = S("@1%", fuel_percent)
  239. formspec = default.get_furnace_active_formspec(fuel_percent, item_percent)
  240. swap_node(pos, "default:furnace_active")
  241. -- make sure timer restarts automatically
  242. result = true
  243. -- Play sound every 5 seconds while the furnace is active
  244. if timer_elapsed == 0 or (timer_elapsed + 1) % 5 == 0 then
  245. local sound_id = minetest.sound_play("default_furnace_active",
  246. {pos = pos, max_hear_distance = 16, gain = 0.25})
  247. local hash = minetest.hash_node_position(pos)
  248. furnace_fire_sounds[hash] = furnace_fire_sounds[hash] or {}
  249. table.insert(furnace_fire_sounds[hash], sound_id)
  250. -- Only remember the 3 last sound handles
  251. if #furnace_fire_sounds[hash] > 3 then
  252. table.remove(furnace_fire_sounds[hash], 1)
  253. end
  254. -- Remove the sound ID automatically from table after 11 seconds
  255. minetest.after(11, function()
  256. if not furnace_fire_sounds[hash] then
  257. return
  258. end
  259. for f=#furnace_fire_sounds[hash], 1, -1 do
  260. if furnace_fire_sounds[hash][f] == sound_id then
  261. table.remove(furnace_fire_sounds[hash], f)
  262. end
  263. end
  264. if #furnace_fire_sounds[hash] == 0 then
  265. furnace_fire_sounds[hash] = nil
  266. end
  267. end)
  268. end
  269. else
  270. if fuellist and not fuellist[1]:is_empty() then
  271. fuel_state = S("@1%", 0)
  272. end
  273. formspec = default.get_furnace_inactive_formspec()
  274. swap_node(pos, "default:furnace")
  275. -- stop timer on the inactive furnace
  276. minetest.get_node_timer(pos):stop()
  277. meta:set_int("timer_elapsed", 0)
  278. stop_furnace_sound(pos)
  279. end
  280. local infotext
  281. if active then
  282. infotext = S("Furnace active")
  283. else
  284. infotext = S("Furnace inactive")
  285. end
  286. infotext = infotext .. "\n" .. S("(Item: @1; Fuel: @2)", item_state, fuel_state)
  287. --
  288. -- Set meta values
  289. --
  290. meta:set_float("fuel_totaltime", fuel_totaltime)
  291. meta:set_float("fuel_time", fuel_time)
  292. meta:set_float("src_time", src_time)
  293. meta:set_string("formspec", formspec)
  294. meta:set_string("infotext", infotext)
  295. return result
  296. end
  297. --
  298. -- Node definitions
  299. --
  300. local function apply_logger(def)
  301. default.set_inventory_action_loggers(def, "furnace")
  302. return def
  303. end
  304. minetest.register_node("default:furnace", apply_logger({
  305. description = S("Furnace"),
  306. tiles = {
  307. "default_furnace_top.png", "default_furnace_bottom.png",
  308. "default_furnace_side.png", "default_furnace_side.png",
  309. "default_furnace_side.png", "default_furnace_front.png"
  310. },
  311. paramtype2 = "facedir",
  312. groups = {cracky=2},
  313. legacy_facedir_simple = true,
  314. is_ground_content = false,
  315. sounds = default.node_sound_stone_defaults(),
  316. can_dig = can_dig,
  317. on_timer = furnace_node_timer,
  318. on_construct = function(pos)
  319. local meta = minetest.get_meta(pos)
  320. local inv = meta:get_inventory()
  321. inv:set_size('src', 1)
  322. inv:set_size('fuel', 1)
  323. inv:set_size('dst', 4)
  324. furnace_node_timer(pos, 0)
  325. end,
  326. on_metadata_inventory_move = function(pos)
  327. minetest.get_node_timer(pos):start(1.0)
  328. end,
  329. on_metadata_inventory_put = function(pos)
  330. -- start timer function, it will sort out whether furnace can burn or not.
  331. minetest.get_node_timer(pos):start(1.0)
  332. end,
  333. on_metadata_inventory_take = function(pos)
  334. -- check whether the furnace is empty or not.
  335. minetest.get_node_timer(pos):start(1.0)
  336. end,
  337. on_blast = function(pos)
  338. local drops = {}
  339. default.get_inventory_drops(pos, "src", drops)
  340. default.get_inventory_drops(pos, "fuel", drops)
  341. default.get_inventory_drops(pos, "dst", drops)
  342. drops[#drops+1] = "default:furnace"
  343. minetest.remove_node(pos)
  344. return drops
  345. end,
  346. allow_metadata_inventory_put = allow_metadata_inventory_put,
  347. allow_metadata_inventory_move = allow_metadata_inventory_move,
  348. allow_metadata_inventory_take = allow_metadata_inventory_take,
  349. }))
  350. minetest.register_node("default:furnace_active", apply_logger({
  351. description = S("Furnace"),
  352. tiles = {
  353. "default_furnace_top.png", "default_furnace_bottom.png",
  354. "default_furnace_side.png", "default_furnace_side.png",
  355. "default_furnace_side.png",
  356. {
  357. image = "default_furnace_front_active.png",
  358. backface_culling = false,
  359. animation = {
  360. type = "vertical_frames",
  361. aspect_w = 16,
  362. aspect_h = 16,
  363. length = 1.5
  364. },
  365. }
  366. },
  367. paramtype2 = "facedir",
  368. light_source = 8,
  369. drop = "default:furnace",
  370. groups = {cracky=2, not_in_creative_inventory=1},
  371. legacy_facedir_simple = true,
  372. is_ground_content = false,
  373. sounds = default.node_sound_stone_defaults(),
  374. on_timer = furnace_node_timer,
  375. on_destruct = function(pos)
  376. stop_furnace_sound(pos)
  377. end,
  378. can_dig = can_dig,
  379. allow_metadata_inventory_put = allow_metadata_inventory_put,
  380. allow_metadata_inventory_move = allow_metadata_inventory_move,
  381. allow_metadata_inventory_take = allow_metadata_inventory_take,
  382. }))
  383. minetest.register_craft({
  384. output = "default:furnace",
  385. recipe = {
  386. {"group:stone", "group:stone", "group:stone"},
  387. {"group:stone", "", "group:stone"},
  388. {"group:stone", "group:stone", "group:stone"},
  389. }
  390. })