furnace.lua 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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 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. -- Check if we have enough fuel to burn
  120. if fuel_time < fuel_totaltime then
  121. -- The furnace is currently active and has enough fuel
  122. fuel_time = fuel_time + elapsed
  123. -- If there is a cookable item then check if it is ready yet
  124. if cookable then
  125. src_time = src_time + elapsed
  126. if src_time >= cooked.time then
  127. -- Place result in dst list if possible
  128. if inv:room_for_item("dst", cooked.item) then
  129. inv:add_item("dst", cooked.item)
  130. inv:set_stack("src", 1, aftercooked.items[1])
  131. src_time = src_time - cooked.time
  132. update = true
  133. end
  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_time - fuel_totaltime)
  151. src_time = src_time + elapsed
  152. end
  153. else
  154. -- We don't need to get new fuel since there is no cookable item
  155. fuel_totaltime = 0
  156. src_time = 0
  157. end
  158. fuel_time = 0
  159. end
  160. elapsed = 0
  161. end
  162. if fuel and fuel_totaltime > fuel.time then
  163. fuel_totaltime = fuel.time
  164. end
  165. if srclist[1]:is_empty() then
  166. src_time = 0
  167. end
  168. --
  169. -- Update formspec, infotext and node
  170. --
  171. local formspec
  172. local item_state
  173. local item_percent = 0
  174. if cookable then
  175. item_percent = math.floor(src_time / cooked.time * 100)
  176. if item_percent > 100 then
  177. item_state = "100% (output full)"
  178. else
  179. item_state = item_percent .. "%"
  180. end
  181. else
  182. if srclist[1]:is_empty() then
  183. item_state = "Empty"
  184. else
  185. item_state = "Not cookable"
  186. end
  187. end
  188. local fuel_state = "Empty"
  189. local active = "inactive "
  190. local result = false
  191. if fuel_totaltime ~= 0 then
  192. active = "active "
  193. local fuel_percent = math.floor(fuel_time / fuel_totaltime * 100)
  194. fuel_state = fuel_percent .. "%"
  195. formspec = default.get_furnace_active_formspec(fuel_percent, item_percent)
  196. swap_node(pos, "default:furnace_active")
  197. -- make sure timer restarts automatically
  198. result = true
  199. else
  200. if not fuellist[1]:is_empty() then
  201. fuel_state = "0%"
  202. end
  203. formspec = default.get_furnace_inactive_formspec()
  204. swap_node(pos, "default:furnace")
  205. -- stop timer on the inactive furnace
  206. minetest.get_node_timer(pos):stop()
  207. end
  208. local infotext = "Furnace " .. active .. "(Item: " .. item_state ..
  209. "; Fuel: " .. fuel_state .. ")"
  210. --
  211. -- Set meta values
  212. --
  213. meta:set_float("fuel_totaltime", fuel_totaltime)
  214. meta:set_float("fuel_time", fuel_time)
  215. meta:set_float("src_time", src_time)
  216. meta:set_string("formspec", formspec)
  217. meta:set_string("infotext", infotext)
  218. return result
  219. end
  220. --
  221. -- Node definitions
  222. --
  223. minetest.register_node("default:furnace", {
  224. description = "Furnace",
  225. tiles = {
  226. "default_furnace_top.png", "default_furnace_bottom.png",
  227. "default_furnace_side.png", "default_furnace_side.png",
  228. "default_furnace_side.png", "default_furnace_front.png"
  229. },
  230. paramtype2 = "facedir",
  231. groups = {cracky=2},
  232. legacy_facedir_simple = true,
  233. is_ground_content = false,
  234. sounds = default.node_sound_stone_defaults(),
  235. can_dig = can_dig,
  236. on_timer = furnace_node_timer,
  237. on_construct = function(pos)
  238. local meta = minetest.get_meta(pos)
  239. meta:set_string("formspec", default.get_furnace_inactive_formspec())
  240. local inv = meta:get_inventory()
  241. inv:set_size('src', 1)
  242. inv:set_size('fuel', 1)
  243. inv:set_size('dst', 4)
  244. end,
  245. on_metadata_inventory_move = function(pos)
  246. minetest.get_node_timer(pos):start(1.0)
  247. end,
  248. on_metadata_inventory_put = function(pos)
  249. -- start timer function, it will sort out whether furnace can burn or not.
  250. minetest.get_node_timer(pos):start(1.0)
  251. end,
  252. on_blast = function(pos)
  253. local drops = {}
  254. default.get_inventory_drops(pos, "src", drops)
  255. default.get_inventory_drops(pos, "fuel", drops)
  256. default.get_inventory_drops(pos, "dst", drops)
  257. drops[#drops+1] = "default:furnace"
  258. minetest.remove_node(pos)
  259. return drops
  260. end,
  261. allow_metadata_inventory_put = allow_metadata_inventory_put,
  262. allow_metadata_inventory_move = allow_metadata_inventory_move,
  263. allow_metadata_inventory_take = allow_metadata_inventory_take,
  264. })
  265. minetest.register_node("default:furnace_active", {
  266. description = "Furnace",
  267. tiles = {
  268. "default_furnace_top.png", "default_furnace_bottom.png",
  269. "default_furnace_side.png", "default_furnace_side.png",
  270. "default_furnace_side.png",
  271. {
  272. image = "default_furnace_front_active.png",
  273. backface_culling = false,
  274. animation = {
  275. type = "vertical_frames",
  276. aspect_w = 16,
  277. aspect_h = 16,
  278. length = 1.5
  279. },
  280. }
  281. },
  282. paramtype2 = "facedir",
  283. light_source = 8,
  284. drop = "default:furnace",
  285. groups = {cracky=2, not_in_creative_inventory=1},
  286. legacy_facedir_simple = true,
  287. is_ground_content = false,
  288. sounds = default.node_sound_stone_defaults(),
  289. on_timer = furnace_node_timer,
  290. can_dig = can_dig,
  291. allow_metadata_inventory_put = allow_metadata_inventory_put,
  292. allow_metadata_inventory_move = allow_metadata_inventory_move,
  293. allow_metadata_inventory_take = allow_metadata_inventory_take,
  294. })