init.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. coal_alloy_furnace = coal_alloy_furnace or {}
  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. end
  166. -- make sure timer restarts automatically
  167. result = true
  168. else
  169. if not fuellist[1]:is_empty() then
  170. fuel_state = "0%"
  171. end
  172. machines.swap_node(pos, "coal_alloy_furnace:inactive")
  173. -- stop timer on the inactive furnace
  174. local timer = minetest.get_node_timer(pos)
  175. timer:stop()
  176. end
  177. local infotext = "Fuel-Fired Alloy Furnace " .. active .. "\n" ..
  178. "Item: " .. item_state .. "\n" .. "Fuel Burn: " .. fuel_state
  179. --
  180. -- Set meta values
  181. --
  182. meta:set_float("fuel_totaltime", fuel_totaltime)
  183. meta:set_float("fuel_time", fuel_time)
  184. meta:set_float("src_time", src_time)
  185. meta:set_string("formspec", formspec)
  186. meta:set_string("infotext", infotext)
  187. return result
  188. end
  189. coal_alloy_furnace.on_blast = function(pos)
  190. local drops = {}
  191. default.get_inventory_drops(pos, "src", drops)
  192. default.get_inventory_drops(pos, "fuel", drops)
  193. default.get_inventory_drops(pos, "dst", drops)
  194. drops[#drops+1] = "coal_alloy_furnace:inactive"
  195. minetest.remove_node(pos)
  196. return drops
  197. end
  198. coal_alloy_furnace.on_construct = function(pos)
  199. local meta = minetest.get_meta(pos)
  200. meta:set_string("formspec", coal_alloy_furnace.get_inactive_formspec())
  201. local inv = meta:get_inventory()
  202. inv:set_size('src', 2)
  203. inv:set_size('fuel', 1)
  204. inv:set_size('dst', 4)
  205. end
  206. coal_alloy_furnace.on_metadata_inventory_move = function(pos)
  207. local timer = minetest.get_node_timer(pos)
  208. timer:start(1.0)
  209. end
  210. coal_alloy_furnace.on_metadata_inventory_put = function(pos)
  211. -- Start timer function, it will sort out whether furnace can burn or not.
  212. local timer = minetest.get_node_timer(pos)
  213. timer:start(1.0)
  214. end
  215. coal_alloy_furnace.burn_feet = function(pos, player)
  216. if not heatdamage.is_immune(player:get_player_name()) then
  217. player:set_hp(player:get_hp() - 1)
  218. end
  219. end
  220. minetest.register_node("coal_alloy_furnace:inactive", {
  221. description = "Fuel-Fired Alloy Furnace\n\nThis combines materials by alloying.\nBurns coal or kalite for fuel.",
  222. tiles = {
  223. "coal_alloy_furnace_top.png", "coal_alloy_furnace_bottom.png",
  224. "coal_alloy_furnace_side.png", "coal_alloy_furnace_side.png",
  225. "coal_alloy_furnace_side.png", "coal_alloy_furnace_front.png"
  226. },
  227. groups = utility.dig_groups("machine", {
  228. tubedevice = 1, tubedevice_receiver = 1,
  229. immovable = 1,
  230. }),
  231. paramtype2 = "facedir",
  232. on_rotate = function(...) return screwdriver.rotate_simple(...) end,
  233. is_ground_content = false,
  234. sounds = default.node_sound_stone_defaults(),
  235. can_dig = function(...)
  236. return coal_alloy_furnace.can_dig(...) end,
  237. on_timer = function(...)
  238. return coal_alloy_furnace.on_timer(...) end,
  239. on_construct = function(...)
  240. return coal_alloy_furnace.on_construct(...) end,
  241. on_metadata_inventory_move = function(...)
  242. return coal_alloy_furnace.on_metadata_inventory_move(...) end,
  243. on_metadata_inventory_put = function(...)
  244. return coal_alloy_furnace.on_metadata_inventory_put(...) end,
  245. on_blast = function(...)
  246. return coal_alloy_furnace.on_blast(...) end,
  247. allow_metadata_inventory_put = function(...)
  248. return coal_alloy_furnace.allow_metadata_inventory_put(...) end,
  249. allow_metadata_inventory_move = function(...)
  250. return coal_alloy_furnace.allow_metadata_inventory_move(...) end,
  251. allow_metadata_inventory_take = function(...)
  252. return coal_alloy_furnace.allow_metadata_inventory_take(...) end,
  253. })
  254. minetest.register_node("coal_alloy_furnace:active", {
  255. description = "Fuel-Fired Alloy Furnace",
  256. tiles = {
  257. "coal_alloy_furnace_top.png", "coal_alloy_furnace_bottom.png",
  258. "coal_alloy_furnace_side.png", "coal_alloy_furnace_side.png",
  259. "coal_alloy_furnace_side.png", "coal_alloy_furnace_front_active.png",
  260. },
  261. light_source = 8,
  262. drop = "coal_alloy_furnace:inactive",
  263. groups = utility.dig_groups("machine", {
  264. not_in_creative_inventory=1,
  265. melt_around = 4,
  266. tubedevice = 1, tubedevice_receiver = 1,
  267. immovable = 1,
  268. }),
  269. paramtype2 = "facedir",
  270. on_rotate = function(...) return screwdriver.rotate_simple(...) end,
  271. is_ground_content = false,
  272. sounds = default.node_sound_stone_defaults(),
  273. on_timer = function(...)
  274. return coal_alloy_furnace.on_timer(...) end,
  275. can_dig = function(...)
  276. return coal_alloy_furnace.can_dig(...) end,
  277. on_blast = function(...)
  278. return coal_alloy_furnace.on_blast(...) end,
  279. allow_metadata_inventory_put = function(...)
  280. return coal_alloy_furnace.allow_metadata_inventory_put(...) end,
  281. allow_metadata_inventory_move = function(...)
  282. return coal_alloy_furnace.allow_metadata_inventory_move(...) end,
  283. allow_metadata_inventory_take = function(...)
  284. return coal_alloy_furnace.allow_metadata_inventory_take(...) end,
  285. on_player_walk_over = function(...)
  286. return coal_alloy_furnace.burn_feet(...) end,
  287. })
  288. minetest.register_craft({
  289. output = 'coal_alloy_furnace:inactive',
  290. recipe = {
  291. {'default:brick', 'default:brick', 'default:brick'},
  292. {'default:brick', 'group:torch_craftitem', 'default:brick'},
  293. {'default:brick', 'default:brick', 'default:brick'},
  294. }
  295. })