furnace.lua 9.0 KB

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