init.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. if not minetest.global_exists("coal_alloy_furnace") then coal_alloy_furnace = {} end
  2. coal_alloy_furnace.modpath = minetest.get_modpath("coal_alloy_furnace")
  3. local FURNACE_SPEED = 3.0
  4. -- Localize for performance.
  5. local math_floor = math.floor
  6. -- Get active formspec.
  7. coal_alloy_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[1.75,0;Fuel & Input]" ..
  14. "list[context;src;1.75,0.5;2,1;]"..
  15. "list[context;fuel;2.26,2.5;1,1;]"..
  16. utility.progress_image(2.26, 1.5, "default_furnace_fire_bg.png", "default_furnace_fire_fg.png", fuel_percent) ..
  17. utility.progress_image(3.75, 1.5, "gui_furnace_arrow_bg.png", "gui_furnace_arrow_fg.png", item_percent, "^[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. coal_alloy_furnace.get_inactive_formspec = function()
  32. return coal_alloy_furnace.get_active_formspec(0, 0)
  33. end
  34. coal_alloy_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. coal_alloy_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="coalfuel", width=1, items={stack}}).time ~= 0 then
  47. if inv:is_empty("src") then
  48. meta:set_string("infotext", "Fuel-Fired Alloy 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. coal_alloy_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 coal_alloy_furnace.allow_metadata_inventory_put(pos, to_list, to_index, stack, player)
  65. end
  66. coal_alloy_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. coal_alloy_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 = "alloying", 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. inv:set_stack("src", 2, aftercooked.items[2])
  107. src_time = 0
  108. end
  109. end
  110. end
  111. else
  112. -- Furnace ran out of fuel
  113. if cookable then
  114. -- We need to get new fuel
  115. local fuel, afterfuel = minetest.get_craft_result({method = "coalfuel", width = 1, items = fuellist})
  116. if fuel.time == 0 then
  117. -- No valid fuel in fuel list
  118. fuel_totaltime = 0
  119. fuel_time = 0
  120. src_time = 0
  121. else
  122. -- Take fuel from fuel list
  123. inv:set_stack("fuel", 1, afterfuel.items[1])
  124. fuel_totaltime = fuel.time * FURNACE_SPEED
  125. fuel_time = 0
  126. end
  127. else
  128. -- We don't need to get new fuel since there is no cookable item
  129. fuel_totaltime = 0
  130. fuel_time = 0
  131. src_time = 0
  132. end
  133. end
  134. --
  135. -- Update formspec, infotext and node
  136. --
  137. local formspec = coal_alloy_furnace.get_inactive_formspec()
  138. local item_state
  139. local item_percent = 0
  140. if cookable then
  141. item_percent = math_floor(src_time / cooked.time * 100)
  142. if item_percent > 100 then
  143. item_state = "100% (Output Full)"
  144. else
  145. item_state = item_percent .. "%"
  146. end
  147. else
  148. if srclist[1]:is_empty() then
  149. item_state = "Empty"
  150. else
  151. item_state = "Not Alloyable"
  152. end
  153. end
  154. local fuel_state = "Empty"
  155. local active = "Inactive "
  156. local result = false
  157. if fuel_time <= fuel_totaltime and fuel_totaltime ~= 0 then
  158. active = "Active "
  159. local fuel_percent = 100 - math_floor(fuel_time / fuel_totaltime * 100)
  160. fuel_state = fuel_percent .. "%"
  161. formspec = coal_alloy_furnace.get_active_formspec(fuel_percent, item_percent)
  162. if machines.swap_node(pos, "coal_alloy_furnace:active") then
  163. torchmelt.start_melting(pos)
  164. notify.notify_adjacent(pos)
  165. ambiance.spawn_sound_beacon("ambiance:furnace_active", pos)
  166. end
  167. -- make sure timer restarts automatically
  168. result = true
  169. else
  170. if not fuellist[1]:is_empty() then
  171. fuel_state = "0%"
  172. end
  173. machines.swap_node(pos, "coal_alloy_furnace:inactive")
  174. -- stop timer on the inactive furnace
  175. local timer = minetest.get_node_timer(pos)
  176. timer:stop()
  177. end
  178. local infotext = "Fuel-Fired Alloy Furnace " .. active .. "\n" ..
  179. "Item: " .. item_state .. "\n" .. "Fuel Burn: " .. fuel_state
  180. --
  181. -- Set meta values
  182. --
  183. meta:set_float("fuel_totaltime", fuel_totaltime)
  184. meta:set_float("fuel_time", fuel_time)
  185. meta:set_float("src_time", src_time)
  186. meta:set_string("formspec", formspec)
  187. meta:set_string("infotext", infotext)
  188. return result
  189. end
  190. coal_alloy_furnace.on_blast = function(pos)
  191. local drops = {}
  192. default.get_inventory_drops(pos, "src", drops)
  193. default.get_inventory_drops(pos, "fuel", drops)
  194. default.get_inventory_drops(pos, "dst", drops)
  195. drops[#drops+1] = "coal_alloy_furnace:inactive"
  196. minetest.remove_node(pos)
  197. return drops
  198. end
  199. coal_alloy_furnace.on_construct = function(pos)
  200. local meta = minetest.get_meta(pos)
  201. meta:set_string("formspec", coal_alloy_furnace.get_inactive_formspec())
  202. local inv = meta:get_inventory()
  203. inv:set_size('src', 2)
  204. inv:set_size('fuel', 1)
  205. inv:set_size('dst', 4)
  206. end
  207. coal_alloy_furnace.on_metadata_inventory_move = function(pos)
  208. local timer = minetest.get_node_timer(pos)
  209. timer:start(1.0)
  210. end
  211. coal_alloy_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. coal_alloy_furnace.burn_feet = function(pos, player)
  217. if not heatdamage.is_immune(player:get_player_name()) then
  218. utility.damage_player(player, "heat", 1*500)
  219. end
  220. end
  221. minetest.register_node("coal_alloy_furnace:inactive", {
  222. description = "Fuel-Fired Alloy Furnace\n\nThis combines materials by alloying.\nBurns coal or kalite for fuel.",
  223. tiles = {
  224. "coal_alloy_furnace_top.png", "coal_alloy_furnace_bottom.png",
  225. "coal_alloy_furnace_side.png", "coal_alloy_furnace_side.png",
  226. "coal_alloy_furnace_side.png", "coal_alloy_furnace_front.png"
  227. },
  228. groups = utility.dig_groups("machine", {
  229. tubedevice = 1, tubedevice_receiver = 1,
  230. immovable = 1,
  231. }),
  232. paramtype2 = "facedir",
  233. on_rotate = function(...) return screwdriver.rotate_simple(...) end,
  234. is_ground_content = false,
  235. sounds = default.node_sound_stone_defaults(),
  236. can_dig = function(...)
  237. return coal_alloy_furnace.can_dig(...) end,
  238. on_timer = function(...)
  239. return coal_alloy_furnace.on_timer(...) end,
  240. on_construct = function(...)
  241. return coal_alloy_furnace.on_construct(...) end,
  242. on_metadata_inventory_move = function(...)
  243. return coal_alloy_furnace.on_metadata_inventory_move(...) end,
  244. on_metadata_inventory_put = function(...)
  245. return coal_alloy_furnace.on_metadata_inventory_put(...) end,
  246. on_blast = function(...)
  247. return coal_alloy_furnace.on_blast(...) end,
  248. allow_metadata_inventory_put = function(...)
  249. return coal_alloy_furnace.allow_metadata_inventory_put(...) end,
  250. allow_metadata_inventory_move = function(...)
  251. return coal_alloy_furnace.allow_metadata_inventory_move(...) end,
  252. allow_metadata_inventory_take = function(...)
  253. return coal_alloy_furnace.allow_metadata_inventory_take(...) end,
  254. })
  255. minetest.register_node("coal_alloy_furnace:active", {
  256. description = "Fuel-Fired Alloy Furnace",
  257. tiles = {
  258. "coal_alloy_furnace_top.png", "coal_alloy_furnace_bottom.png",
  259. "coal_alloy_furnace_side.png", "coal_alloy_furnace_side.png",
  260. "coal_alloy_furnace_side.png", "coal_alloy_furnace_front_active.png",
  261. },
  262. light_source = 8,
  263. drop = "coal_alloy_furnace:inactive",
  264. groups = utility.dig_groups("machine", {
  265. not_in_creative_inventory=1,
  266. melt_around = 4,
  267. tubedevice = 1, tubedevice_receiver = 1,
  268. immovable = 1,
  269. }),
  270. paramtype2 = "facedir",
  271. on_rotate = function(...) return screwdriver.rotate_simple(...) end,
  272. is_ground_content = false,
  273. sounds = default.node_sound_stone_defaults(),
  274. on_timer = function(...)
  275. return coal_alloy_furnace.on_timer(...) end,
  276. can_dig = function(...)
  277. return coal_alloy_furnace.can_dig(...) end,
  278. on_blast = function(...)
  279. return coal_alloy_furnace.on_blast(...) end,
  280. allow_metadata_inventory_put = function(...)
  281. return coal_alloy_furnace.allow_metadata_inventory_put(...) end,
  282. allow_metadata_inventory_move = function(...)
  283. return coal_alloy_furnace.allow_metadata_inventory_move(...) end,
  284. allow_metadata_inventory_take = function(...)
  285. return coal_alloy_furnace.allow_metadata_inventory_take(...) end,
  286. on_player_walk_over = function(...)
  287. return coal_alloy_furnace.burn_feet(...) end,
  288. })
  289. minetest.register_craft({
  290. output = 'coal_alloy_furnace:inactive',
  291. recipe = {
  292. {'default:brick', 'default:brick', 'default:brick'},
  293. {'default:brick', 'group:torch_craftitem', 'default:brick'},
  294. {'default:brick', 'default:brick', 'default:brick'},
  295. }
  296. })