furnace.lua 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. --
  2. -- Formspecs
  3. --
  4. local function active_formspec(fuel_percent, item_percent)
  5. local formspec =
  6. "size[8,8.5]"..
  7. default.gui_bg..
  8. default.gui_bg_img..
  9. default.gui_slots..
  10. "list[current_name;src;2.75,0.5;1,1;]"..
  11. "list[current_name;fuel;2.75,2.5;1,1;]"..
  12. "image[2.75,1.5;1,1;default_furnace_fire_bg.png^[lowpart:"..
  13. (100-fuel_percent)..":default_furnace_fire_fg.png]"..
  14. "image[3.75,1.5;1,1;gui_furnace_arrow_bg.png^[lowpart:"..
  15. (item_percent)..":gui_furnace_arrow_fg.png^[transformR270]"..
  16. "list[current_name;dst;4.75,0.96;2,2;]"..
  17. "list[current_player;main;0,4.25;8,1;]"..
  18. "list[current_player;main;0,5.5;8,3;8]"..
  19. "listring[current_name;dst]"..
  20. "listring[current_player;main]"..
  21. "listring[current_name;src]"..
  22. "listring[current_player;main]"..
  23. "listring[current_name;fuel]"..
  24. "listring[current_player;main]"..
  25. default.get_hotbar_bg(0, 4.25)
  26. return formspec
  27. end
  28. local inactive_formspec =
  29. "size[8,8.5]"..
  30. default.gui_bg..
  31. default.gui_bg_img..
  32. default.gui_slots..
  33. "list[current_name;src;2.75,0.5;1,1;]"..
  34. "list[current_name;fuel;2.75,2.5;1,1;]"..
  35. "image[2.75,1.5;1,1;default_furnace_fire_bg.png]"..
  36. "image[3.75,1.5;1,1;gui_furnace_arrow_bg.png^[transformR270]"..
  37. "list[current_name;dst;4.75,0.96;2,2;]"..
  38. "list[current_player;main;0,4.25;8,1;]"..
  39. "list[current_player;main;0,5.5;8,3;8]"..
  40. "listring[current_name;dst]"..
  41. "listring[current_player;main]"..
  42. "listring[current_name;src]"..
  43. "listring[current_player;main]"..
  44. "listring[current_name;fuel]"..
  45. "listring[current_player;main]"..
  46. default.get_hotbar_bg(0, 4.25)
  47. --
  48. -- Node callback functions that are the same for active and inactive furnace
  49. --
  50. local function can_dig(pos, player)
  51. local meta = minetest.get_meta(pos);
  52. local inv = meta:get_inventory()
  53. return inv:is_empty("fuel") and inv:is_empty("dst") and inv:is_empty("src")
  54. end
  55. local function allow_metadata_inventory_put(pos, listname, index, stack, player)
  56. if minetest.is_protected(pos, player:get_player_name()) then
  57. return 0
  58. end
  59. local meta = minetest.get_meta(pos)
  60. local inv = meta:get_inventory()
  61. if listname == "fuel" then
  62. if minetest.get_craft_result({method="fuel", width=1, items={stack}}).time ~= 0 then
  63. if inv:is_empty("src") then
  64. meta:set_string("infotext", "Furnace is empty")
  65. end
  66. return stack:get_count()
  67. else
  68. return 0
  69. end
  70. elseif listname == "src" then
  71. return stack:get_count()
  72. elseif listname == "dst" then
  73. return 0
  74. end
  75. end
  76. local function allow_metadata_inventory_move(pos, from_list, from_index, to_list, to_index, count, player)
  77. local meta = minetest.get_meta(pos)
  78. local inv = meta:get_inventory()
  79. local stack = inv:get_stack(from_list, from_index)
  80. return allow_metadata_inventory_put(pos, to_list, to_index, stack, player)
  81. end
  82. local function allow_metadata_inventory_take(pos, listname, index, stack, player)
  83. if minetest.is_protected(pos, player:get_player_name()) then
  84. return 0
  85. end
  86. return stack:get_count()
  87. end
  88. local function swap_node(pos, name)
  89. local node = minetest.get_node(pos)
  90. if node.name == name then
  91. return
  92. end
  93. node.name = name
  94. minetest.swap_node(pos, node)
  95. end
  96. local function furnace_node_timer(pos, elapsed)
  97. --
  98. -- Inizialize metadata
  99. --
  100. local meta = minetest.get_meta(pos)
  101. local fuel_time = meta:get_float("fuel_time") or 0
  102. local src_time = meta:get_float("src_time") or 0
  103. local fuel_totaltime = meta:get_float("fuel_totaltime") or 0
  104. local inv = meta:get_inventory()
  105. local srclist, fuellist
  106. local cookable, cooked
  107. local fuel
  108. local update = true
  109. while update do
  110. update = false
  111. srclist = inv:get_list("src")
  112. fuellist = inv:get_list("fuel")
  113. --
  114. -- Cooking
  115. --
  116. -- Check if we have cookable content
  117. local aftercooked
  118. cooked, aftercooked = minetest.get_craft_result({method = "cooking", width = 1, items = srclist})
  119. cookable = cooked.time ~= 0
  120. -- Check if we have enough fuel to burn
  121. if fuel_time < fuel_totaltime then
  122. -- The furnace is currently active and has enough fuel
  123. fuel_time = fuel_time + elapsed
  124. -- If there is a cookable item then check if it is ready yet
  125. if cookable then
  126. src_time = src_time + elapsed
  127. if src_time >= cooked.time then
  128. -- Place result in dst list if possible
  129. if inv:room_for_item("dst", cooked.item) then
  130. inv:add_item("dst", cooked.item)
  131. inv:set_stack("src", 1, aftercooked.items[1])
  132. src_time = src_time - cooked.time
  133. update = true
  134. end
  135. end
  136. end
  137. else
  138. -- Furnace ran out of fuel
  139. if cookable then
  140. -- We need to get new fuel
  141. local afterfuel
  142. fuel, afterfuel = minetest.get_craft_result({method = "fuel", width = 1, items = fuellist})
  143. if fuel.time == 0 then
  144. -- No valid fuel in fuel list
  145. fuel_totaltime = 0
  146. src_time = 0
  147. else
  148. -- Take fuel from fuel list
  149. inv:set_stack("fuel", 1, afterfuel.items[1])
  150. update = true
  151. fuel_totaltime = fuel.time + (fuel_time - fuel_totaltime)
  152. src_time = src_time + elapsed
  153. end
  154. else
  155. -- We don't need to get new fuel since there is no cookable item
  156. fuel_totaltime = 0
  157. src_time = 0
  158. end
  159. fuel_time = 0
  160. end
  161. elapsed = 0
  162. end
  163. if fuel and fuel_totaltime > fuel.time then
  164. fuel_totaltime = fuel.time
  165. end
  166. if srclist[1]:is_empty() then
  167. src_time = 0
  168. end
  169. --
  170. -- Update formspec, infotext and node
  171. --
  172. local formspec = inactive_formspec
  173. local item_state
  174. local item_percent = 0
  175. if cookable then
  176. item_percent = math.floor(src_time / cooked.time * 100)
  177. if item_percent > 100 then
  178. item_state = "100% (output full)"
  179. else
  180. item_state = item_percent .. "%"
  181. end
  182. else
  183. if srclist[1]:is_empty() then
  184. item_state = "Empty"
  185. else
  186. item_state = "Not cookable"
  187. end
  188. end
  189. local fuel_state = "Empty"
  190. local active = "inactive "
  191. local result = false
  192. if fuel_totaltime ~= 0 then
  193. active = "active "
  194. local fuel_percent = math.floor(fuel_time / fuel_totaltime * 100)
  195. fuel_state = fuel_percent .. "%"
  196. formspec = active_formspec(fuel_percent, item_percent)
  197. swap_node(pos, "default:furnace_active")
  198. -- make sure timer restarts automatically
  199. result = true
  200. else
  201. if not fuellist[1]:is_empty() then
  202. fuel_state = "0%"
  203. end
  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 .. "; 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", 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. })