init.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. cobble_furnace = cobble_furnace or {}
  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. end
  172. -- make sure timer restarts automatically
  173. result = true
  174. else
  175. if not fuellist[1]:is_empty() then
  176. fuel_state = "0%"
  177. end
  178. machines.swap_node(pos, "cobble_furnace:inactive")
  179. -- stop timer on the inactive furnace
  180. local timer = minetest.get_node_timer(pos)
  181. timer:stop()
  182. end
  183. local infotext = "Coal Furnace " .. active .. "\n" ..
  184. "Item: " .. item_state .. "\n" .. "Fuel Burn: " .. fuel_state
  185. --
  186. -- Set meta values
  187. --
  188. meta:set_float("fuel_totaltime", fuel_totaltime)
  189. meta:set_float("fuel_time", fuel_time)
  190. meta:set_float("src_time", src_time)
  191. meta:set_string("formspec", formspec)
  192. meta:set_string("infotext", infotext)
  193. return result
  194. end
  195. cobble_furnace.on_blast = function(pos)
  196. local drops = {}
  197. default.get_inventory_drops(pos, "src", drops)
  198. default.get_inventory_drops(pos, "fuel", drops)
  199. default.get_inventory_drops(pos, "dst", drops)
  200. drops[#drops+1] = "cobble_furnace:inactive"
  201. minetest.remove_node(pos)
  202. return drops
  203. end
  204. cobble_furnace.on_construct = function(pos)
  205. local meta = minetest.get_meta(pos)
  206. meta:set_string("infotext", "Coal Furnace")
  207. meta:set_string("formspec", cobble_furnace.get_inactive_formspec())
  208. local inv = meta:get_inventory()
  209. inv:set_size('src', 1)
  210. inv:set_size('fuel', 1)
  211. inv:set_size('dst', 4)
  212. end
  213. cobble_furnace.on_metadata_inventory_move = function(pos)
  214. local timer = minetest.get_node_timer(pos)
  215. timer:start(1.0)
  216. end
  217. cobble_furnace.on_metadata_inventory_put = function(pos)
  218. -- Start timer function, it will sort out whether furnace can burn or not.
  219. local timer = minetest.get_node_timer(pos)
  220. timer:start(1.0)
  221. end
  222. cobble_furnace.burn_feet = function(pos, player)
  223. if not heatdamage.is_immune(player:get_player_name()) then
  224. player:set_hp(player:get_hp() - 1)
  225. end
  226. end
  227. if not cobble_furnace.run_once then
  228. minetest.register_node("cobble_furnace:inactive", {
  229. description = "Fuel-Fired Furnace\n\nCooks or smelts things, but slowly and inefficiently.\nBurns coal and other flammable things.",
  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", "default_furnace_front.png"
  234. },
  235. groups = utility.dig_groups("cobble", {
  236. tubedevice = 1, tubedevice_receiver = 1,
  237. immovable = 1,
  238. }),
  239. paramtype2 = "facedir",
  240. on_rotate = function(...) return screwdriver.rotate_simple(...) end,
  241. is_ground_content = false,
  242. sounds = default.node_sound_stone_defaults(),
  243. can_dig = function(...)
  244. return cobble_furnace.can_dig(...) end,
  245. on_timer = function(...)
  246. return cobble_furnace.on_timer(...) end,
  247. on_construct = function(...)
  248. return cobble_furnace.on_construct(...) end,
  249. on_metadata_inventory_move = function(...)
  250. return cobble_furnace.on_metadata_inventory_move(...) end,
  251. on_metadata_inventory_put = function(...)
  252. return cobble_furnace.on_metadata_inventory_put(...) end,
  253. on_blast = function(...)
  254. return cobble_furnace.on_blast(...) end,
  255. allow_metadata_inventory_put = function(...)
  256. return cobble_furnace.allow_metadata_inventory_put(...) end,
  257. allow_metadata_inventory_move = function(...)
  258. return cobble_furnace.allow_metadata_inventory_move(...) end,
  259. allow_metadata_inventory_take = function(...)
  260. return cobble_furnace.allow_metadata_inventory_take(...) end,
  261. })
  262. minetest.register_node("cobble_furnace:active", {
  263. tiles = {
  264. "default_furnace_top.png", "default_furnace_bottom.png",
  265. "default_furnace_side.png", "default_furnace_side.png",
  266. "default_furnace_side.png",
  267. {
  268. image = "default_furnace_front_active.png",
  269. backface_culling = false,
  270. animation = {
  271. type = "vertical_frames",
  272. aspect_w = 16,
  273. aspect_h = 16,
  274. length = 1.5
  275. },
  276. }
  277. },
  278. light_source = 8,
  279. drop = "cobble_furnace:inactive",
  280. groups = utility.dig_groups("cobble", {
  281. not_in_creative_inventory=1,
  282. melt_around = 4,
  283. tubedevice = 1, tubedevice_receiver = 1,
  284. immovable = 1,
  285. }),
  286. paramtype2 = "facedir",
  287. on_rotate = function(...) return screwdriver.rotate_simple(...) end,
  288. is_ground_content = false,
  289. sounds = default.node_sound_stone_defaults(),
  290. on_timer = function(...)
  291. return cobble_furnace.on_timer(...) end,
  292. can_dig = function(...)
  293. return cobble_furnace.can_dig(...) end,
  294. on_blast = function(...)
  295. return cobble_furnace.on_blast(...) end,
  296. allow_metadata_inventory_put = function(...)
  297. return cobble_furnace.allow_metadata_inventory_put(...) end,
  298. allow_metadata_inventory_move = function(...)
  299. return cobble_furnace.allow_metadata_inventory_move(...) end,
  300. allow_metadata_inventory_take = function(...)
  301. return cobble_furnace.allow_metadata_inventory_take(...) end,
  302. on_player_walk_over = function(...)
  303. return cobble_furnace.burn_feet(...) end,
  304. })
  305. -- Recipe modified by MustTest.
  306. minetest.register_craft({
  307. output = 'cobble_furnace:inactive',
  308. recipe = {
  309. {'default:cobble', 'default:cobble', 'default:cobble'},
  310. {'default:cobble', 'group:torch_craftitem', 'default:cobble'},
  311. {'default:cobble', 'default:cobble', 'default:cobble'},
  312. }
  313. })
  314. -- Compatibility.
  315. minetest.register_alias("default:furnace", "cobble_furnace:inactive")
  316. minetest.register_alias("default:furnace_active", "cobble_furnace:active")
  317. local c = "cobble_furnace:core"
  318. local f = cobble_furnace.modpath .. "/init.lua"
  319. reload.register_file(c, f, false)
  320. cobble_furnace.run_once = true
  321. end