init.lua 11 KB

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