furnace.lua 9.8 KB

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