2
0

furnace.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. while elapsed > 0 and update do
  121. update = false
  122. srclist = inv:get_list("src")
  123. fuellist = inv:get_list("fuel")
  124. --
  125. -- Cooking
  126. --
  127. -- Check if we have cookable content
  128. local aftercooked
  129. cooked, aftercooked = minetest.get_craft_result({method = "cooking", width = 1, items = srclist})
  130. cookable = cooked.time ~= 0
  131. local el = math.min(elapsed, fuel_totaltime - fuel_time)
  132. if cookable then -- fuel lasts long enough, adjust el to cooking duration
  133. el = math.min(el, cooked.time - src_time)
  134. end
  135. -- Check if we have enough fuel to burn
  136. if fuel_time < fuel_totaltime then
  137. -- The furnace is currently active and has enough fuel
  138. fuel_time = fuel_time + el
  139. -- If there is a cookable item then check if it is ready yet
  140. if cookable then
  141. src_time = src_time + el
  142. if src_time >= cooked.time then
  143. -- Place result in dst list if possible
  144. if inv:room_for_item("dst", cooked.item) then
  145. inv:add_item("dst", cooked.item)
  146. inv:set_stack("src", 1, aftercooked.items[1])
  147. src_time = src_time - cooked.time
  148. update = true
  149. else
  150. dst_full = true
  151. end
  152. -- Play cooling sound
  153. minetest.sound_play("default_cool_lava",
  154. {pos = pos, max_hear_distance = 16, gain = 0.07}, true)
  155. else
  156. -- Item could not be cooked: probably missing fuel
  157. update = true
  158. end
  159. end
  160. else
  161. -- Furnace ran out of fuel
  162. if cookable then
  163. -- We need to get new fuel
  164. local afterfuel
  165. fuel, afterfuel = minetest.get_craft_result({method = "fuel", width = 1, items = fuellist})
  166. if fuel.time == 0 then
  167. -- No valid fuel in fuel list
  168. fuel_totaltime = 0
  169. src_time = 0
  170. else
  171. -- prevent blocking of fuel inventory (for automatization mods)
  172. local is_fuel = minetest.get_craft_result({method = "fuel", width = 1, items = {afterfuel.items[1]:to_string()}})
  173. if is_fuel.time == 0 then
  174. table.insert(fuel.replacements, afterfuel.items[1])
  175. inv:set_stack("fuel", 1, "")
  176. else
  177. -- Take fuel from fuel list
  178. inv:set_stack("fuel", 1, afterfuel.items[1])
  179. end
  180. -- Put replacements in dst list or drop them on the furnace.
  181. local replacements = fuel.replacements
  182. if replacements[1] then
  183. local leftover = inv:add_item("dst", replacements[1])
  184. if not leftover:is_empty() then
  185. local above = vector.new(pos.x, pos.y + 1, pos.z)
  186. local drop_pos = minetest.find_node_near(above, 1, {"air"}) or above
  187. minetest.item_drop(replacements[1], nil, drop_pos)
  188. end
  189. end
  190. update = true
  191. fuel_totaltime = fuel.time + (fuel_totaltime - fuel_time)
  192. end
  193. else
  194. -- We don't need to get new fuel since there is no cookable item
  195. fuel_totaltime = 0
  196. src_time = 0
  197. end
  198. fuel_time = 0
  199. end
  200. elapsed = elapsed - el
  201. end
  202. if fuel and fuel_totaltime > fuel.time then
  203. fuel_totaltime = fuel.time
  204. end
  205. if srclist and srclist[1]:is_empty() then
  206. src_time = 0
  207. end
  208. --
  209. -- Update formspec, infotext and node
  210. --
  211. local formspec
  212. local item_state
  213. local item_percent = 0
  214. if cookable then
  215. item_percent = math.floor(src_time / cooked.time * 100)
  216. if dst_full then
  217. item_state = S("100% (output full)")
  218. else
  219. item_state = S("@1%", item_percent)
  220. end
  221. else
  222. if srclist and not srclist[1]:is_empty() then
  223. item_state = S("Not cookable")
  224. else
  225. item_state = S("Empty")
  226. end
  227. end
  228. local fuel_state = S("Empty")
  229. local active = false
  230. local result = false
  231. if fuel_totaltime ~= 0 then
  232. active = true
  233. local fuel_percent = 100 - math.floor(fuel_time / fuel_totaltime * 100)
  234. fuel_state = S("@1%", fuel_percent)
  235. formspec = default.get_furnace_active_formspec(fuel_percent, item_percent)
  236. swap_node(pos, "default:furnace_active")
  237. -- make sure timer restarts automatically
  238. result = true
  239. -- Play sound every 5 seconds while the furnace is active
  240. if timer_elapsed == 0 or (timer_elapsed + 1) % 5 == 0 then
  241. local sound_id = minetest.sound_play("default_furnace_active",
  242. {pos = pos, max_hear_distance = 16, gain = 0.25})
  243. local hash = minetest.hash_node_position(pos)
  244. furnace_fire_sounds[hash] = furnace_fire_sounds[hash] or {}
  245. table.insert(furnace_fire_sounds[hash], sound_id)
  246. -- Only remember the 3 last sound handles
  247. if #furnace_fire_sounds[hash] > 3 then
  248. table.remove(furnace_fire_sounds[hash], 1)
  249. end
  250. -- Remove the sound ID automatically from table after 11 seconds
  251. minetest.after(11, function()
  252. if not furnace_fire_sounds[hash] then
  253. return
  254. end
  255. for f=#furnace_fire_sounds[hash], 1, -1 do
  256. if furnace_fire_sounds[hash][f] == sound_id then
  257. table.remove(furnace_fire_sounds[hash], f)
  258. end
  259. end
  260. if #furnace_fire_sounds[hash] == 0 then
  261. furnace_fire_sounds[hash] = nil
  262. end
  263. end)
  264. end
  265. else
  266. if fuellist and not fuellist[1]:is_empty() then
  267. fuel_state = S("@1%", 0)
  268. end
  269. formspec = default.get_furnace_inactive_formspec()
  270. swap_node(pos, "default:furnace")
  271. -- stop timer on the inactive furnace
  272. minetest.get_node_timer(pos):stop()
  273. meta:set_int("timer_elapsed", 0)
  274. stop_furnace_sound(pos)
  275. end
  276. local infotext
  277. if active then
  278. infotext = S("Furnace active")
  279. else
  280. infotext = S("Furnace inactive")
  281. end
  282. infotext = infotext .. "\n" .. S("(Item: @1; Fuel: @2)", item_state, fuel_state)
  283. --
  284. -- Set meta values
  285. --
  286. meta:set_float("fuel_totaltime", fuel_totaltime)
  287. meta:set_float("fuel_time", fuel_time)
  288. meta:set_float("src_time", src_time)
  289. meta:set_string("formspec", formspec)
  290. meta:set_string("infotext", infotext)
  291. return result
  292. end
  293. --
  294. -- Node definitions
  295. --
  296. minetest.register_node("default:furnace", {
  297. description = S("Furnace"),
  298. tiles = {
  299. "default_furnace_top.png", "default_furnace_bottom.png",
  300. "default_furnace_side.png", "default_furnace_side.png",
  301. "default_furnace_side.png", "default_furnace_front.png"
  302. },
  303. paramtype2 = "facedir",
  304. groups = {cracky=2},
  305. legacy_facedir_simple = true,
  306. is_ground_content = false,
  307. sounds = default.node_sound_stone_defaults(),
  308. can_dig = can_dig,
  309. on_timer = furnace_node_timer,
  310. on_construct = function(pos)
  311. local meta = minetest.get_meta(pos)
  312. local inv = meta:get_inventory()
  313. inv:set_size('src', 1)
  314. inv:set_size('fuel', 1)
  315. inv:set_size('dst', 4)
  316. furnace_node_timer(pos, 0)
  317. end,
  318. on_metadata_inventory_move = function(pos)
  319. minetest.get_node_timer(pos):start(1.0)
  320. end,
  321. on_metadata_inventory_put = function(pos)
  322. -- start timer function, it will sort out whether furnace can burn or not.
  323. minetest.get_node_timer(pos):start(1.0)
  324. end,
  325. on_metadata_inventory_take = function(pos)
  326. -- check whether the furnace is empty or not.
  327. minetest.get_node_timer(pos):start(1.0)
  328. end,
  329. on_blast = function(pos)
  330. local drops = {}
  331. default.get_inventory_drops(pos, "src", drops)
  332. default.get_inventory_drops(pos, "fuel", drops)
  333. default.get_inventory_drops(pos, "dst", drops)
  334. drops[#drops+1] = "default:furnace"
  335. minetest.remove_node(pos)
  336. return drops
  337. end,
  338. allow_metadata_inventory_put = allow_metadata_inventory_put,
  339. allow_metadata_inventory_move = allow_metadata_inventory_move,
  340. allow_metadata_inventory_take = allow_metadata_inventory_take,
  341. })
  342. minetest.register_node("default:furnace_active", {
  343. description = S("Furnace"),
  344. tiles = {
  345. "default_furnace_top.png", "default_furnace_bottom.png",
  346. "default_furnace_side.png", "default_furnace_side.png",
  347. "default_furnace_side.png",
  348. {
  349. image = "default_furnace_front_active.png",
  350. backface_culling = false,
  351. animation = {
  352. type = "vertical_frames",
  353. aspect_w = 16,
  354. aspect_h = 16,
  355. length = 1.5
  356. },
  357. }
  358. },
  359. paramtype2 = "facedir",
  360. light_source = 8,
  361. drop = "default:furnace",
  362. groups = {cracky=2, not_in_creative_inventory=1},
  363. legacy_facedir_simple = true,
  364. is_ground_content = false,
  365. sounds = default.node_sound_stone_defaults(),
  366. on_timer = furnace_node_timer,
  367. on_destruct = function(pos)
  368. stop_furnace_sound(pos)
  369. end,
  370. can_dig = can_dig,
  371. allow_metadata_inventory_put = allow_metadata_inventory_put,
  372. allow_metadata_inventory_move = allow_metadata_inventory_move,
  373. allow_metadata_inventory_take = allow_metadata_inventory_take,
  374. })
  375. minetest.register_craft({
  376. output = "default:furnace",
  377. recipe = {
  378. {"group:stone", "group:stone", "group:stone"},
  379. {"group:stone", "", "group:stone"},
  380. {"group:stone", "group:stone", "group:stone"},
  381. }
  382. })