furnace.lua 9.4 KB

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