smelter.lua 11 KB

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