furnace.lua 7.7 KB

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