init.lua 12 KB

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