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