furnace.lua 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. --
  2. -- Formspecs
  3. --
  4. function default.get_furnace_active_formspec(fuel_percent, item_percent)
  5. return "size[8,8.5]"..
  6. default.gui_bg..
  7. default.gui_bg_img..
  8. default.gui_slots..
  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. (100-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. default.gui_bg..
  29. default.gui_bg_img..
  30. default.gui_slots..
  31. "list[context;src;2.75,0.5;1,1;]"..
  32. "list[context;fuel;2.75,2.5;1,1;]"..
  33. "image[2.75,1.5;1,1;default_furnace_fire_bg.png]"..
  34. "image[3.75,1.5;1,1;gui_furnace_arrow_bg.png^[transformR270]"..
  35. "list[context;dst;4.75,0.96;2,2;]"..
  36. "list[current_player;main;0,4.25;8,1;]"..
  37. "list[current_player;main;0,5.5;8,3;8]"..
  38. "listring[context;dst]"..
  39. "listring[current_player;main]"..
  40. "listring[context;src]"..
  41. "listring[current_player;main]"..
  42. "listring[context;fuel]"..
  43. "listring[current_player;main]"..
  44. default.get_hotbar_bg(0, 4.25)
  45. end
  46. --
  47. -- Node callback functions that are the same for active and inactive furnace
  48. --
  49. local function can_dig(pos, player)
  50. local meta = minetest.get_meta(pos);
  51. local inv = meta:get_inventory()
  52. return inv:is_empty("fuel") and inv:is_empty("dst") and inv:is_empty("src")
  53. end
  54. local function allow_metadata_inventory_put(pos, listname, index, stack, player)
  55. if minetest.is_protected(pos, player:get_player_name()) then
  56. return 0
  57. end
  58. local meta = minetest.get_meta(pos)
  59. local inv = meta:get_inventory()
  60. if listname == "fuel" then
  61. if minetest.get_craft_result({method="fuel", width=1, items={stack}}).time ~= 0 then
  62. if inv:is_empty("src") then
  63. meta:set_string("infotext", "Furnace is empty")
  64. end
  65. return stack:get_count()
  66. else
  67. return 0
  68. end
  69. elseif listname == "src" then
  70. return stack:get_count()
  71. elseif listname == "dst" then
  72. return 0
  73. end
  74. end
  75. local function allow_metadata_inventory_move(pos, from_list, from_index, to_list, to_index, count, player)
  76. local meta = minetest.get_meta(pos)
  77. local inv = meta:get_inventory()
  78. local stack = inv:get_stack(from_list, from_index)
  79. return allow_metadata_inventory_put(pos, to_list, to_index, stack, player)
  80. end
  81. local function allow_metadata_inventory_take(pos, listname, index, stack, player)
  82. if minetest.is_protected(pos, player:get_player_name()) then
  83. return 0
  84. end
  85. return stack:get_count()
  86. end
  87. local function swap_node(pos, name)
  88. local node = minetest.get_node(pos)
  89. if node.name == name then
  90. return
  91. end
  92. node.name = name
  93. minetest.swap_node(pos, node)
  94. end
  95. local function furnace_node_timer(pos, elapsed)
  96. --
  97. -- Inizialize metadata
  98. --
  99. local meta = minetest.get_meta(pos)
  100. local fuel_time = meta:get_float("fuel_time") or 0
  101. local src_time = meta:get_float("src_time") or 0
  102. local fuel_totaltime = meta:get_float("fuel_totaltime") or 0
  103. local inv = meta:get_inventory()
  104. local srclist, fuellist
  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. 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. update = true
  157. fuel_totaltime = fuel.time + (fuel_totaltime - fuel_time)
  158. end
  159. else
  160. -- We don't need to get new fuel since there is no cookable item
  161. fuel_totaltime = 0
  162. src_time = 0
  163. end
  164. fuel_time = 0
  165. end
  166. elapsed = elapsed - el
  167. end
  168. if fuel and fuel_totaltime > fuel.time then
  169. fuel_totaltime = fuel.time
  170. end
  171. if srclist[1]:is_empty() then
  172. src_time = 0
  173. end
  174. --
  175. -- Update formspec, infotext and node
  176. --
  177. local formspec
  178. local item_state
  179. local item_percent = 0
  180. if cookable then
  181. item_percent = math.floor(src_time / cooked.time * 100)
  182. if item_percent > 100 then
  183. item_state = "100% (output full)"
  184. else
  185. item_state = item_percent .. "%"
  186. end
  187. else
  188. if srclist[1]:is_empty() then
  189. item_state = "Empty"
  190. else
  191. item_state = "Not cookable"
  192. end
  193. end
  194. local fuel_state = "Empty"
  195. local active = "inactive"
  196. local result = false
  197. if fuel_totaltime ~= 0 then
  198. active = "active"
  199. local fuel_percent = math.floor(fuel_time / fuel_totaltime * 100)
  200. fuel_state = fuel_percent .. "%"
  201. formspec = default.get_furnace_active_formspec(fuel_percent, item_percent)
  202. swap_node(pos, "default:furnace_active")
  203. -- make sure timer restarts automatically
  204. result = true
  205. else
  206. if not fuellist[1]:is_empty() then
  207. fuel_state = "0%"
  208. end
  209. formspec = default.get_furnace_inactive_formspec()
  210. swap_node(pos, "default:furnace")
  211. -- stop timer on the inactive furnace
  212. minetest.get_node_timer(pos):stop()
  213. end
  214. local infotext = "Furnace " .. active .. "\n(Item: " .. item_state ..
  215. "; Fuel: " .. fuel_state .. ")"
  216. --
  217. -- Set meta values
  218. --
  219. meta:set_float("fuel_totaltime", fuel_totaltime)
  220. meta:set_float("fuel_time", fuel_time)
  221. meta:set_float("src_time", src_time)
  222. meta:set_string("formspec", formspec)
  223. meta:set_string("infotext", infotext)
  224. return result
  225. end
  226. --
  227. -- Node definitions
  228. --
  229. minetest.register_node("default:furnace", {
  230. description = "Furnace",
  231. tiles = {
  232. "default_furnace_top.png", "default_furnace_bottom.png",
  233. "default_furnace_side.png", "default_furnace_side.png",
  234. "default_furnace_side.png", "default_furnace_front.png"
  235. },
  236. paramtype2 = "facedir",
  237. groups = {cracky=2},
  238. legacy_facedir_simple = true,
  239. is_ground_content = false,
  240. sounds = default.node_sound_stone_defaults(),
  241. stack_max = 1,
  242. can_dig = can_dig,
  243. on_timer = furnace_node_timer,
  244. on_construct = function(pos)
  245. local meta = minetest.get_meta(pos)
  246. meta:set_string("formspec", default.get_furnace_inactive_formspec())
  247. local inv = meta:get_inventory()
  248. inv:set_size('src', 1)
  249. inv:set_size('fuel', 1)
  250. inv:set_size('dst', 4)
  251. end,
  252. on_metadata_inventory_move = function(pos)
  253. minetest.get_node_timer(pos):start(1.0)
  254. end,
  255. on_metadata_inventory_put = function(pos)
  256. -- start timer function, it will sort out whether furnace can burn or not.
  257. minetest.get_node_timer(pos):start(1.0)
  258. end,
  259. on_blast = function(pos)
  260. local drops = {}
  261. default.get_inventory_drops(pos, "src", drops)
  262. default.get_inventory_drops(pos, "fuel", drops)
  263. default.get_inventory_drops(pos, "dst", drops)
  264. drops[#drops+1] = "default:furnace"
  265. minetest.remove_node(pos)
  266. return drops
  267. end,
  268. allow_metadata_inventory_put = allow_metadata_inventory_put,
  269. allow_metadata_inventory_move = allow_metadata_inventory_move,
  270. allow_metadata_inventory_take = allow_metadata_inventory_take,
  271. })
  272. minetest.register_node("default:furnace_active", {
  273. description = "Furnace",
  274. tiles = {
  275. "default_furnace_top.png", "default_furnace_bottom.png",
  276. "default_furnace_side.png", "default_furnace_side.png",
  277. "default_furnace_side.png",
  278. {
  279. image = "default_furnace_front_active.png",
  280. backface_culling = false,
  281. animation = {
  282. type = "vertical_frames",
  283. aspect_w = 16,
  284. aspect_h = 16,
  285. length = 1.5
  286. },
  287. }
  288. },
  289. paramtype2 = "facedir",
  290. light_source = 8,
  291. drop = "default:furnace",
  292. groups = {cracky=2, not_in_creative_inventory=1},
  293. legacy_facedir_simple = true,
  294. is_ground_content = false,
  295. sounds = default.node_sound_stone_defaults(),
  296. on_timer = furnace_node_timer,
  297. can_dig = can_dig,
  298. allow_metadata_inventory_put = allow_metadata_inventory_put,
  299. allow_metadata_inventory_move = allow_metadata_inventory_move,
  300. allow_metadata_inventory_take = allow_metadata_inventory_take,
  301. })