furnace.lua 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. default.get_hotbar_bg(0, 4.25)
  20. return formspec
  21. end
  22. local inactive_formspec =
  23. "size[8,8.5]"..
  24. default.gui_bg..
  25. default.gui_bg_img..
  26. default.gui_slots..
  27. "list[current_name;src;2.75,0.5;1,1;]"..
  28. "list[current_name;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[current_name;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. default.get_hotbar_bg(0, 4.25)
  35. --
  36. -- Node callback functions that are the same for active and inactive furnace
  37. --
  38. local function can_dig(pos, player)
  39. local meta = minetest.get_meta(pos);
  40. local inv = meta:get_inventory()
  41. return inv:is_empty("fuel") and inv:is_empty("dst") and inv:is_empty("src")
  42. end
  43. local function allow_metadata_inventory_put(pos, listname, index, stack, player)
  44. if minetest.is_protected(pos, player:get_player_name()) then
  45. return 0
  46. end
  47. local meta = minetest.get_meta(pos)
  48. local inv = meta:get_inventory()
  49. if listname == "fuel" then
  50. if minetest.get_craft_result({method="fuel", width=1, items={stack}}).time ~= 0 then
  51. if inv:is_empty("src") then
  52. meta:set_string("infotext", "Furnace is empty")
  53. end
  54. return stack:get_count()
  55. else
  56. return 0
  57. end
  58. elseif listname == "src" then
  59. return stack:get_count()
  60. elseif listname == "dst" then
  61. return 0
  62. end
  63. end
  64. local function allow_metadata_inventory_move(pos, from_list, from_index, to_list, to_index, count, player)
  65. local meta = minetest.get_meta(pos)
  66. local inv = meta:get_inventory()
  67. local stack = inv:get_stack(from_list, from_index)
  68. return allow_metadata_inventory_put(pos, to_list, to_index, stack, player)
  69. end
  70. local function allow_metadata_inventory_take(pos, listname, index, stack, player)
  71. if minetest.is_protected(pos, player:get_player_name()) then
  72. return 0
  73. end
  74. return stack:get_count()
  75. end
  76. --
  77. -- Node definitions
  78. --
  79. minetest.register_node("default:furnace", {
  80. description = "Furnace",
  81. tiles = {
  82. "default_furnace_top.png", "default_furnace_bottom.png",
  83. "default_furnace_side.png", "default_furnace_side.png",
  84. "default_furnace_side.png", "default_furnace_front.png"
  85. },
  86. paramtype2 = "facedir",
  87. groups = {cracky=2},
  88. legacy_facedir_simple = true,
  89. is_ground_content = false,
  90. sounds = default.node_sound_stone_defaults(),
  91. can_dig = can_dig,
  92. allow_metadata_inventory_put = allow_metadata_inventory_put,
  93. allow_metadata_inventory_move = allow_metadata_inventory_move,
  94. allow_metadata_inventory_take = allow_metadata_inventory_take,
  95. })
  96. minetest.register_node("default:furnace_active", {
  97. description = "Furnace",
  98. tiles = {
  99. "default_furnace_top.png", "default_furnace_bottom.png",
  100. "default_furnace_side.png", "default_furnace_side.png",
  101. "default_furnace_side.png",
  102. {
  103. image = "default_furnace_front_active.png",
  104. backface_culling = false,
  105. animation = {
  106. type = "vertical_frames",
  107. aspect_w = 16,
  108. aspect_h = 16,
  109. length = 1.5
  110. },
  111. }
  112. },
  113. paramtype2 = "facedir",
  114. light_source = 8,
  115. drop = "default:furnace",
  116. groups = {cracky=2, not_in_creative_inventory=1},
  117. legacy_facedir_simple = true,
  118. is_ground_content = false,
  119. sounds = default.node_sound_stone_defaults(),
  120. can_dig = can_dig,
  121. allow_metadata_inventory_put = allow_metadata_inventory_put,
  122. allow_metadata_inventory_move = allow_metadata_inventory_move,
  123. allow_metadata_inventory_take = allow_metadata_inventory_take,
  124. })
  125. --
  126. -- ABM
  127. --
  128. local function swap_node(pos, name)
  129. local node = minetest.get_node(pos)
  130. if node.name == name then
  131. return
  132. end
  133. node.name = name
  134. minetest.swap_node(pos, node)
  135. end
  136. minetest.register_abm({
  137. nodenames = {"default:furnace", "default:furnace_active"},
  138. interval = 1.0,
  139. chance = 1,
  140. action = function(pos, node, active_object_count, active_object_count_wider)
  141. --
  142. -- Inizialize metadata
  143. --
  144. local meta = minetest.get_meta(pos)
  145. local fuel_time = meta:get_float("fuel_time") or 0
  146. local src_time = meta:get_float("src_time") or 0
  147. local fuel_totaltime = meta:get_float("fuel_totaltime") or 0
  148. --
  149. -- Inizialize inventory
  150. --
  151. local inv = meta:get_inventory()
  152. for listname, size in pairs({
  153. src = 1,
  154. fuel = 1,
  155. dst = 4,
  156. }) do
  157. if inv:get_size(listname) ~= size then
  158. inv:set_size(listname, size)
  159. end
  160. end
  161. local srclist = inv:get_list("src")
  162. local fuellist = inv:get_list("fuel")
  163. local dstlist = inv:get_list("dst")
  164. --
  165. -- Cooking
  166. --
  167. -- Check if we have cookable content
  168. local cooked, aftercooked = minetest.get_craft_result({method = "cooking", width = 1, items = srclist})
  169. local cookable = true
  170. if cooked.time == 0 then
  171. cookable = false
  172. end
  173. -- Check if we have enough fuel to burn
  174. if fuel_time < fuel_totaltime then
  175. -- The furnace is currently active and has enough fuel
  176. fuel_time = fuel_time + 1
  177. -- If there is a cookable item then check if it is ready yet
  178. if cookable then
  179. src_time = src_time + 1
  180. if src_time >= cooked.time then
  181. -- Place result in dst list if possible
  182. if inv:room_for_item("dst", cooked.item) then
  183. inv:add_item("dst", cooked.item)
  184. inv:set_stack("src", 1, aftercooked.items[1])
  185. src_time = 0
  186. end
  187. end
  188. end
  189. else
  190. -- Furnace ran out of fuel
  191. if cookable then
  192. -- We need to get new fuel
  193. local fuel, afterfuel = minetest.get_craft_result({method = "fuel", width = 1, items = fuellist})
  194. if fuel.time == 0 then
  195. -- No valid fuel in fuel list
  196. fuel_totaltime = 0
  197. fuel_time = 0
  198. src_time = 0
  199. else
  200. -- Take fuel from fuel list
  201. inv:set_stack("fuel", 1, afterfuel.items[1])
  202. fuel_totaltime = fuel.time
  203. fuel_time = 0
  204. end
  205. else
  206. -- We don't need to get new fuel since there is no cookable item
  207. fuel_totaltime = 0
  208. fuel_time = 0
  209. src_time = 0
  210. end
  211. end
  212. --
  213. -- Update formspec, infotext and node
  214. --
  215. local formspec = inactive_formspec
  216. local item_state = ""
  217. local item_percent = 0
  218. if cookable then
  219. item_percent = math.floor(src_time / cooked.time * 100)
  220. item_state = item_percent .. "%"
  221. else
  222. if srclist[1]:is_empty() then
  223. item_state = "Empty"
  224. else
  225. item_state = "Not cookable"
  226. end
  227. end
  228. local fuel_state = "Empty"
  229. local active = "inactive "
  230. if fuel_time <= fuel_totaltime and fuel_totaltime ~= 0 then
  231. active = "active "
  232. local fuel_percent = math.floor(fuel_time / fuel_totaltime * 100)
  233. fuel_state = fuel_percent .. "%"
  234. formspec = active_formspec(fuel_percent, item_percent)
  235. swap_node(pos, "default:furnace_active")
  236. else
  237. if not fuellist[1]:is_empty() then
  238. fuel_state = "0%"
  239. end
  240. swap_node(pos, "default:furnace")
  241. end
  242. local infotext = "Furnace " .. active .. "(Item: " .. item_state .. "; Fuel: " .. fuel_state .. ")"
  243. --
  244. -- Set meta values
  245. --
  246. meta:set_float("fuel_totaltime", fuel_totaltime)
  247. meta:set_float("fuel_time", fuel_time)
  248. meta:set_float("src_time", src_time)
  249. meta:set_string("formspec", formspec)
  250. meta:set_string("infotext", infotext)
  251. end,
  252. })