furnace.lua 10 KB

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