furnace.lua 10 KB

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