furnace.lua 10 KB

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