furnace.lua 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. --
  2. -- Formspecs
  3. --
  4. function default.get_furnace_active_formspec(fuel_percent, item_percent)
  5. return "size[8,8.5]"..
  6. "list[context;src;2.75,0.5;1,1;]"..
  7. "list[context;fuel;2.75,2.5;1,1;]"..
  8. "image[2.75,1.5;1,1;default_furnace_fire_bg.png^[lowpart:"..
  9. (100-fuel_percent)..":default_furnace_fire_fg.png]"..
  10. "image[3.75,1.5;1,1;gui_furnace_arrow_bg.png^[lowpart:"..
  11. (item_percent)..":gui_furnace_arrow_fg.png^[transformR270]"..
  12. "list[context;dst;4.75,0.96;2,2;]"..
  13. "list[current_player;main;0,4.25;8,1;]"..
  14. "list[current_player;main;0,5.5;8,3;8]"..
  15. "listring[context;dst]"..
  16. "listring[current_player;main]"..
  17. "listring[context;src]"..
  18. "listring[current_player;main]"..
  19. "listring[context;fuel]"..
  20. "listring[current_player;main]"..
  21. default.get_hotbar_bg(0, 4.25)
  22. end
  23. function default.get_furnace_inactive_formspec()
  24. return "size[8,8.5]"..
  25. "list[context;src;2.75,0.5;1,1;]"..
  26. "list[context;fuel;2.75,2.5;1,1;]"..
  27. "image[2.75,1.5;1,1;default_furnace_fire_bg.png]"..
  28. "image[3.75,1.5;1,1;gui_furnace_arrow_bg.png^[transformR270]"..
  29. "list[context;dst;4.75,0.96;2,2;]"..
  30. "list[current_player;main;0,4.25;8,1;]"..
  31. "list[current_player;main;0,5.5;8,3;8]"..
  32. "listring[context;dst]"..
  33. "listring[current_player;main]"..
  34. "listring[context;src]"..
  35. "listring[current_player;main]"..
  36. "listring[context;fuel]"..
  37. "listring[current_player;main]"..
  38. default.get_hotbar_bg(0, 4.25)
  39. end
  40. --
  41. -- Node callback functions that are the same for active and inactive furnace
  42. --
  43. local function can_dig(pos, player)
  44. local meta = minetest.get_meta(pos);
  45. local inv = meta:get_inventory()
  46. return inv:is_empty("fuel") and inv:is_empty("dst") and inv:is_empty("src")
  47. end
  48. local function allow_metadata_inventory_put(pos, listname, index, stack, player)
  49. if minetest.is_protected(pos, player:get_player_name()) then
  50. return 0
  51. end
  52. local meta = minetest.get_meta(pos)
  53. local inv = meta:get_inventory()
  54. if listname == "fuel" then
  55. if minetest.get_craft_result({method="fuel", width=1, items={stack}}).time ~= 0 then
  56. if inv:is_empty("src") then
  57. meta:set_string("infotext", "Furnace is empty")
  58. end
  59. return stack:get_count()
  60. else
  61. return 0
  62. end
  63. elseif listname == "src" then
  64. return stack:get_count()
  65. elseif listname == "dst" then
  66. return 0
  67. end
  68. end
  69. local function allow_metadata_inventory_move(pos, from_list, from_index, to_list, to_index, count, player)
  70. local meta = minetest.get_meta(pos)
  71. local inv = meta:get_inventory()
  72. local stack = inv:get_stack(from_list, from_index)
  73. return allow_metadata_inventory_put(pos, to_list, to_index, stack, player)
  74. end
  75. local function allow_metadata_inventory_take(pos, listname, index, stack, player)
  76. if minetest.is_protected(pos, player:get_player_name()) then
  77. return 0
  78. end
  79. return stack:get_count()
  80. end
  81. local function swap_node(pos, name)
  82. local node = minetest.get_node(pos)
  83. if node.name == name then
  84. return
  85. end
  86. node.name = name
  87. minetest.swap_node(pos, node)
  88. end
  89. local function furnace_node_timer(pos, elapsed)
  90. --
  91. -- Inizialize metadata
  92. --
  93. local meta = minetest.get_meta(pos)
  94. local fuel_time = meta:get_float("fuel_time") or 0
  95. local src_time = meta:get_float("src_time") or 0
  96. local fuel_totaltime = meta:get_float("fuel_totaltime") or 0
  97. local inv = meta:get_inventory()
  98. local srclist, fuellist
  99. local cookable, cooked
  100. local fuel
  101. local update = true
  102. while elapsed > 0 and update do
  103. update = false
  104. srclist = inv:get_list("src")
  105. fuellist = inv:get_list("fuel")
  106. --
  107. -- Cooking
  108. --
  109. -- Check if we have cookable content
  110. local aftercooked
  111. cooked, aftercooked = minetest.get_craft_result({method = "cooking", width = 1, items = srclist})
  112. cookable = cooked.time ~= 0
  113. local el = math.min(elapsed, fuel_totaltime - fuel_time)
  114. if cookable then -- fuel lasts long enough, adjust el to cooking duration
  115. el = math.min(el, cooked.time - src_time)
  116. end
  117. -- Check if we have enough fuel to burn
  118. if fuel_time < fuel_totaltime then
  119. -- The furnace is currently active and has enough fuel
  120. fuel_time = fuel_time + el
  121. -- If there is a cookable item then check if it is ready yet
  122. if cookable then
  123. src_time = src_time + el
  124. if src_time >= cooked.time then
  125. -- Place result in dst list if possible
  126. if inv:room_for_item("dst", cooked.item) then
  127. inv:add_item("dst", cooked.item)
  128. inv:set_stack("src", 1, aftercooked.items[1])
  129. src_time = src_time - cooked.time
  130. update = true
  131. end
  132. else
  133. -- Item could not be cooked: probably missing fuel
  134. update = true
  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_totaltime - fuel_time)
  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 = elapsed - el
  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 .. "\n(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. })