init.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. cobble_furnace = cobble_furnace or {}
  2. cobble_furnace.modpath = minetest.get_modpath("cobble_furnace")
  3. local FURNACE_SPEED = 3.0
  4. -- Get active formspec.
  5. cobble_furnace.get_active_formspec = function(fuel_percent, item_percent)
  6. local formspec =
  7. "size[8,8.5]"..
  8. default.formspec.get_form_colors() ..
  9. default.formspec.get_form_image() ..
  10. default.formspec.get_slot_colors() ..
  11. "label[2.75,0;Fuel & Input]" ..
  12. "list[context;src;2.75,0.5;1,1;]"..
  13. "list[context;fuel;2.75,2.5;1,1;]"..
  14. "image[2.75,1.5;1,1;default_furnace_fire_bg.png^[lowpart:"..
  15. (100-fuel_percent)..":default_furnace_fire_fg.png]"..
  16. "image[3.75,1.5;1,1;gui_furnace_arrow_bg.png^[lowpart:"..
  17. (item_percent)..":gui_furnace_arrow_fg.png^[transformR270]"..
  18. "label[4.75,0.46;Destination]" ..
  19. "list[context;dst;4.75,0.96;2,2;]"..
  20. "list[current_player;main;0,4.25;8,1;]"..
  21. "list[current_player;main;0,5.5;8,3;8]"..
  22. "listring[context;dst]"..
  23. "listring[current_player;main]"..
  24. "listring[context;src]"..
  25. "listring[current_player;main]"..
  26. "listring[context;fuel]"..
  27. "listring[current_player;main]"..
  28. default.get_hotbar_bg(0, 4.25)
  29. return formspec
  30. end
  31. cobble_furnace.get_inactive_formspec = function()
  32. return cobble_furnace.get_active_formspec(100, 0)
  33. end
  34. cobble_furnace.can_dig = function(pos, player)
  35. local meta = minetest.get_meta(pos);
  36. local inv = meta:get_inventory()
  37. return inv:is_empty("fuel") and inv:is_empty("dst") and inv:is_empty("src")
  38. end
  39. cobble_furnace.allow_metadata_inventory_put = function(pos, listname, index, stack, player)
  40. if minetest.test_protection(pos, player:get_player_name()) then
  41. return 0
  42. end
  43. local meta = minetest.get_meta(pos)
  44. local inv = meta:get_inventory()
  45. if listname == "fuel" then
  46. if minetest.get_craft_result({method="fuel", width=1, items={stack}}).time ~= 0 then
  47. if inv:is_empty("src") then
  48. meta:set_string("infotext", "Coal Furnace is Empty.")
  49. end
  50. return stack:get_count()
  51. else
  52. return 0
  53. end
  54. elseif listname == "src" then
  55. return stack:get_count()
  56. elseif listname == "dst" then
  57. return 0
  58. end
  59. end
  60. cobble_furnace.allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
  61. local meta = minetest.get_meta(pos)
  62. local inv = meta:get_inventory()
  63. local stack = inv:get_stack(from_list, from_index)
  64. return cobble_furnace.allow_metadata_inventory_put(pos, to_list, to_index, stack, player)
  65. end
  66. cobble_furnace.allow_metadata_inventory_take = function(pos, listname, index, stack, player)
  67. if minetest.test_protection(pos, player:get_player_name()) then
  68. return 0
  69. end
  70. return stack:get_count()
  71. end
  72. cobble_furnace.on_timer = function(pos, elapsed)
  73. --
  74. -- Inizialize metadata
  75. --
  76. local meta = minetest.get_meta(pos)
  77. local fuel_time = meta:get_float("fuel_time") or 0
  78. local src_time = meta:get_float("src_time") or 0
  79. local fuel_totaltime = meta:get_float("fuel_totaltime") or 0
  80. local inv = meta:get_inventory()
  81. local srclist = inv:get_list("src")
  82. local fuellist = inv:get_list("fuel")
  83. --
  84. -- Cooking
  85. --
  86. -- Check if we have cookable content
  87. local cooked, aftercooked = minetest.get_craft_result({method = "cooking", width = 1, items = srclist})
  88. local cookable = true
  89. if cooked.time == 0 then
  90. cookable = false
  91. else
  92. cooked.time = cooked.time * FURNACE_SPEED
  93. end
  94. -- Check if we have enough fuel to burn
  95. if fuel_time < fuel_totaltime then
  96. -- The furnace is currently active and has enough fuel
  97. fuel_time = fuel_time + 1
  98. -- If there is a cookable item then check if it is ready yet
  99. if cookable then
  100. src_time = src_time + 1
  101. if src_time >= cooked.time then
  102. -- Place result in dst list if possible
  103. if inv:room_for_item("dst", cooked.item) then
  104. inv:add_item("dst", cooked.item)
  105. inv:set_stack("src", 1, aftercooked.items[1])
  106. src_time = 0
  107. end
  108. end
  109. end
  110. else
  111. -- Furnace ran out of fuel
  112. if cookable then
  113. -- We need to get new fuel
  114. local fuel, afterfuel = minetest.get_craft_result({method = "fuel", width = 1, items = fuellist})
  115. if fuel.time == 0 then
  116. -- No valid fuel in fuel list
  117. fuel_totaltime = 0
  118. fuel_time = 0
  119. src_time = 0
  120. else
  121. -- Take fuel from fuel list
  122. inv:set_stack("fuel", 1, afterfuel.items[1])
  123. fuel_totaltime = fuel.time * FURNACE_SPEED
  124. fuel_time = 0
  125. end
  126. else
  127. -- We don't need to get new fuel since there is no cookable item
  128. fuel_totaltime = 0
  129. fuel_time = 0
  130. src_time = 0
  131. end
  132. end
  133. --
  134. -- Update formspec, infotext and node
  135. --
  136. local formspec = cobble_furnace.get_inactive_formspec()
  137. local item_state
  138. local item_percent = 0
  139. if cookable then
  140. item_percent = math.floor(src_time / cooked.time * 100)
  141. if item_percent > 100 then
  142. item_state = "100% (Output Full)"
  143. else
  144. item_state = item_percent .. "%"
  145. end
  146. else
  147. if srclist[1]:is_empty() then
  148. item_state = "Empty"
  149. else
  150. item_state = "Not Cookable"
  151. end
  152. end
  153. local fuel_state = "Empty"
  154. local active = "Inactive "
  155. local result = false
  156. if fuel_time <= fuel_totaltime and fuel_totaltime ~= 0 then
  157. active = "Active "
  158. local fuel_percent = math.floor(fuel_time / fuel_totaltime * 100)
  159. fuel_state = fuel_percent .. "%"
  160. formspec = cobble_furnace.get_active_formspec(fuel_percent, item_percent)
  161. local nnn = minetest.get_node(pos).name
  162. if machines.swap_node(pos, "cobble_furnace:active") then
  163. torchmelt.start_melting(pos)
  164. notify.notify_adjacent(pos)
  165. end
  166. -- make sure timer restarts automatically
  167. result = true
  168. else
  169. if not fuellist[1]:is_empty() then
  170. fuel_state = "0%"
  171. end
  172. machines.swap_node(pos, "cobble_furnace:inactive")
  173. -- stop timer on the inactive furnace
  174. local timer = minetest.get_node_timer(pos)
  175. timer:stop()
  176. end
  177. local infotext = "Coal Furnace " .. active .. "\n" ..
  178. "Item: " .. item_state .. "\n" .. "Fuel Burn: " .. fuel_state
  179. --
  180. -- Set meta values
  181. --
  182. meta:set_float("fuel_totaltime", fuel_totaltime)
  183. meta:set_float("fuel_time", fuel_time)
  184. meta:set_float("src_time", src_time)
  185. meta:set_string("formspec", formspec)
  186. meta:set_string("infotext", infotext)
  187. return result
  188. end
  189. cobble_furnace.on_blast = function(pos)
  190. local drops = {}
  191. default.get_inventory_drops(pos, "src", drops)
  192. default.get_inventory_drops(pos, "fuel", drops)
  193. default.get_inventory_drops(pos, "dst", drops)
  194. drops[#drops+1] = "cobble_furnace:inactive"
  195. minetest.remove_node(pos)
  196. return drops
  197. end
  198. cobble_furnace.on_construct = function(pos)
  199. local meta = minetest.get_meta(pos)
  200. meta:set_string("infotext", "Coal Furnace")
  201. meta:set_string("formspec", cobble_furnace.get_inactive_formspec())
  202. local inv = meta:get_inventory()
  203. inv:set_size('src', 1)
  204. inv:set_size('fuel', 1)
  205. inv:set_size('dst', 4)
  206. end
  207. cobble_furnace.on_metadata_inventory_move = function(pos)
  208. local timer = minetest.get_node_timer(pos)
  209. timer:start(1.0)
  210. end
  211. cobble_furnace.on_metadata_inventory_put = function(pos)
  212. -- Start timer function, it will sort out whether furnace can burn or not.
  213. local timer = minetest.get_node_timer(pos)
  214. timer:start(1.0)
  215. end
  216. cobble_furnace.burn_feet = function(pos, player)
  217. if not heatdamage.is_immune(player:get_player_name()) then
  218. player:set_hp(player:get_hp() - 1)
  219. end
  220. end
  221. if not cobble_furnace.run_once then
  222. minetest.register_node("cobble_furnace:inactive", {
  223. description = "Fuel-Fired Furnace\n\nCooks or smelts things, but slowly and inefficiently.\nBurns coal and other flammable things.",
  224. tiles = {
  225. "default_furnace_top.png", "default_furnace_bottom.png",
  226. "default_furnace_side.png", "default_furnace_side.png",
  227. "default_furnace_side.png", "default_furnace_front.png"
  228. },
  229. groups = utility.dig_groups("cobble", {
  230. tubedevice = 1, tubedevice_receiver = 1,
  231. immovable = 1,
  232. }),
  233. paramtype2 = "facedir",
  234. on_rotate = function(...) return screwdriver.rotate_simple(...) end,
  235. is_ground_content = false,
  236. sounds = default.node_sound_stone_defaults(),
  237. can_dig = function(...)
  238. return cobble_furnace.can_dig(...) end,
  239. on_timer = function(...)
  240. return cobble_furnace.on_timer(...) end,
  241. on_construct = function(...)
  242. return cobble_furnace.on_construct(...) end,
  243. on_metadata_inventory_move = function(...)
  244. return cobble_furnace.on_metadata_inventory_move(...) end,
  245. on_metadata_inventory_put = function(...)
  246. return cobble_furnace.on_metadata_inventory_put(...) end,
  247. on_blast = function(...)
  248. return cobble_furnace.on_blast(...) end,
  249. allow_metadata_inventory_put = function(...)
  250. return cobble_furnace.allow_metadata_inventory_put(...) end,
  251. allow_metadata_inventory_move = function(...)
  252. return cobble_furnace.allow_metadata_inventory_move(...) end,
  253. allow_metadata_inventory_take = function(...)
  254. return cobble_furnace.allow_metadata_inventory_take(...) end,
  255. })
  256. minetest.register_node("cobble_furnace:active", {
  257. tiles = {
  258. "default_furnace_top.png", "default_furnace_bottom.png",
  259. "default_furnace_side.png", "default_furnace_side.png",
  260. "default_furnace_side.png",
  261. {
  262. image = "default_furnace_front_active.png",
  263. backface_culling = false,
  264. animation = {
  265. type = "vertical_frames",
  266. aspect_w = 16,
  267. aspect_h = 16,
  268. length = 1.5
  269. },
  270. }
  271. },
  272. light_source = 8,
  273. drop = "cobble_furnace:inactive",
  274. groups = utility.dig_groups("cobble", {
  275. not_in_creative_inventory=1,
  276. melt_around = 4,
  277. tubedevice = 1, tubedevice_receiver = 1,
  278. immovable = 1,
  279. }),
  280. paramtype2 = "facedir",
  281. on_rotate = function(...) return screwdriver.rotate_simple(...) end,
  282. is_ground_content = false,
  283. sounds = default.node_sound_stone_defaults(),
  284. on_timer = function(...)
  285. return cobble_furnace.on_timer(...) end,
  286. can_dig = function(...)
  287. return cobble_furnace.can_dig(...) end,
  288. on_blast = function(...)
  289. return cobble_furnace.on_blast(...) end,
  290. allow_metadata_inventory_put = function(...)
  291. return cobble_furnace.allow_metadata_inventory_put(...) end,
  292. allow_metadata_inventory_move = function(...)
  293. return cobble_furnace.allow_metadata_inventory_move(...) end,
  294. allow_metadata_inventory_take = function(...)
  295. return cobble_furnace.allow_metadata_inventory_take(...) end,
  296. on_player_walk_over = function(...)
  297. return cobble_furnace.burn_feet(...) end,
  298. })
  299. -- Recipe modified by MustTest.
  300. minetest.register_craft({
  301. output = 'cobble_furnace:inactive',
  302. recipe = {
  303. {'default:cobble', 'default:cobble', 'default:cobble'},
  304. {'default:cobble', 'group:torch_craftitem', 'default:cobble'},
  305. {'default:cobble', 'default:cobble', 'default:cobble'},
  306. }
  307. })
  308. -- Compatibility.
  309. minetest.register_alias("default:furnace", "cobble_furnace:inactive")
  310. minetest.register_alias("default:furnace_active", "cobble_furnace:active")
  311. local c = "cobble_furnace:core"
  312. local f = cobble_furnace.modpath .. "/init.lua"
  313. reload.register_file(c, f, false)
  314. cobble_furnace.run_once = true
  315. end