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. "image[2.26,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. coal_alloy_furnace.get_inactive_formspec = function()
  34. return coal_alloy_furnace.get_active_formspec(100, 0)
  35. end
  36. coal_alloy_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. coal_alloy_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="coalfuel", width=1, items={stack}}).time ~= 0 then
  49. if inv:is_empty("src") then
  50. meta:set_string("infotext", "Fuel-Fired Alloy 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. coal_alloy_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 coal_alloy_furnace.allow_metadata_inventory_put(pos, to_list, to_index, stack, player)
  67. end
  68. coal_alloy_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. coal_alloy_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 = "alloying", 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. inv:set_stack("src", 2, aftercooked.items[2])
  109. src_time = 0
  110. end
  111. end
  112. end
  113. else
  114. -- Furnace ran out of fuel
  115. if cookable then
  116. -- We need to get new fuel
  117. local fuel, afterfuel = minetest.get_craft_result({method = "coalfuel", width = 1, items = fuellist})
  118. if fuel.time == 0 then
  119. -- No valid fuel in fuel list
  120. fuel_totaltime = 0
  121. fuel_time = 0
  122. src_time = 0
  123. else
  124. -- Take fuel from fuel list
  125. inv:set_stack("fuel", 1, afterfuel.items[1])
  126. fuel_totaltime = fuel.time * FURNACE_SPEED
  127. fuel_time = 0
  128. end
  129. else
  130. -- We don't need to get new fuel since there is no cookable item
  131. fuel_totaltime = 0
  132. fuel_time = 0
  133. src_time = 0
  134. end
  135. end
  136. --
  137. -- Update formspec, infotext and node
  138. --
  139. local formspec = coal_alloy_furnace.get_inactive_formspec()
  140. local item_state
  141. local item_percent = 0
  142. if cookable then
  143. item_percent = math_floor(src_time / cooked.time * 100)
  144. if item_percent > 100 then
  145. item_state = "100% (Output Full)"
  146. else
  147. item_state = item_percent .. "%"
  148. end
  149. else
  150. if srclist[1]:is_empty() then
  151. item_state = "Empty"
  152. else
  153. item_state = "Not Alloyable"
  154. end
  155. end
  156. local fuel_state = "Empty"
  157. local active = "Inactive "
  158. local result = false
  159. if fuel_time <= fuel_totaltime and fuel_totaltime ~= 0 then
  160. active = "Active "
  161. local fuel_percent = math_floor(fuel_time / fuel_totaltime * 100)
  162. fuel_state = fuel_percent .. "%"
  163. formspec = coal_alloy_furnace.get_active_formspec(fuel_percent, item_percent)
  164. if machines.swap_node(pos, "coal_alloy_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, "coal_alloy_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 = "Fuel-Fired Alloy 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. coal_alloy_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] = "coal_alloy_furnace:inactive"
  197. minetest.remove_node(pos)
  198. return drops
  199. end
  200. coal_alloy_furnace.on_construct = function(pos)
  201. local meta = minetest.get_meta(pos)
  202. meta:set_string("formspec", coal_alloy_furnace.get_inactive_formspec())
  203. local inv = meta:get_inventory()
  204. inv:set_size('src', 2)
  205. inv:set_size('fuel', 1)
  206. inv:set_size('dst', 4)
  207. end
  208. coal_alloy_furnace.on_metadata_inventory_move = function(pos)
  209. local timer = minetest.get_node_timer(pos)
  210. timer:start(1.0)
  211. end
  212. coal_alloy_furnace.on_metadata_inventory_put = function(pos)
  213. -- Start timer function, it will sort out whether furnace can burn or not.
  214. local timer = minetest.get_node_timer(pos)
  215. timer:start(1.0)
  216. end
  217. coal_alloy_furnace.burn_feet = function(pos, player)
  218. if not heatdamage.is_immune(player:get_player_name()) then
  219. player:set_hp(player:get_hp() - 1)
  220. end
  221. end
  222. minetest.register_node("coal_alloy_furnace:inactive", {
  223. description = "Fuel-Fired Alloy Furnace\n\nThis combines materials by alloying.\nBurns coal or kalite for fuel.",
  224. tiles = {
  225. "coal_alloy_furnace_top.png", "coal_alloy_furnace_bottom.png",
  226. "coal_alloy_furnace_side.png", "coal_alloy_furnace_side.png",
  227. "coal_alloy_furnace_side.png", "coal_alloy_furnace_front.png"
  228. },
  229. groups = utility.dig_groups("machine", {
  230. tubedevice = 1, tubedevice_receiver = 1,
  231. immovable = 1,
  232. }),
  233. paramtype2 = "facedir",
  234. on_rotate = function(...) return screwdriver.rotate_simple(...) end,
  235. is_ground_content = false,
  236. sounds = default.node_sound_stone_defaults(),
  237. can_dig = function(...)
  238. return coal_alloy_furnace.can_dig(...) end,
  239. on_timer = function(...)
  240. return coal_alloy_furnace.on_timer(...) end,
  241. on_construct = function(...)
  242. return coal_alloy_furnace.on_construct(...) end,
  243. on_metadata_inventory_move = function(...)
  244. return coal_alloy_furnace.on_metadata_inventory_move(...) end,
  245. on_metadata_inventory_put = function(...)
  246. return coal_alloy_furnace.on_metadata_inventory_put(...) end,
  247. on_blast = function(...)
  248. return coal_alloy_furnace.on_blast(...) end,
  249. allow_metadata_inventory_put = function(...)
  250. return coal_alloy_furnace.allow_metadata_inventory_put(...) end,
  251. allow_metadata_inventory_move = function(...)
  252. return coal_alloy_furnace.allow_metadata_inventory_move(...) end,
  253. allow_metadata_inventory_take = function(...)
  254. return coal_alloy_furnace.allow_metadata_inventory_take(...) end,
  255. })
  256. minetest.register_node("coal_alloy_furnace:active", {
  257. description = "Fuel-Fired Alloy Furnace",
  258. tiles = {
  259. "coal_alloy_furnace_top.png", "coal_alloy_furnace_bottom.png",
  260. "coal_alloy_furnace_side.png", "coal_alloy_furnace_side.png",
  261. "coal_alloy_furnace_side.png", "coal_alloy_furnace_front_active.png",
  262. },
  263. light_source = 8,
  264. drop = "coal_alloy_furnace:inactive",
  265. groups = utility.dig_groups("machine", {
  266. not_in_creative_inventory=1,
  267. melt_around = 4,
  268. tubedevice = 1, tubedevice_receiver = 1,
  269. immovable = 1,
  270. }),
  271. paramtype2 = "facedir",
  272. on_rotate = function(...) return screwdriver.rotate_simple(...) end,
  273. is_ground_content = false,
  274. sounds = default.node_sound_stone_defaults(),
  275. on_timer = function(...)
  276. return coal_alloy_furnace.on_timer(...) end,
  277. can_dig = function(...)
  278. return coal_alloy_furnace.can_dig(...) end,
  279. on_blast = function(...)
  280. return coal_alloy_furnace.on_blast(...) end,
  281. allow_metadata_inventory_put = function(...)
  282. return coal_alloy_furnace.allow_metadata_inventory_put(...) end,
  283. allow_metadata_inventory_move = function(...)
  284. return coal_alloy_furnace.allow_metadata_inventory_move(...) end,
  285. allow_metadata_inventory_take = function(...)
  286. return coal_alloy_furnace.allow_metadata_inventory_take(...) end,
  287. on_player_walk_over = function(...)
  288. return coal_alloy_furnace.burn_feet(...) end,
  289. })
  290. minetest.register_craft({
  291. output = 'coal_alloy_furnace:inactive',
  292. recipe = {
  293. {'default:brick', 'default:brick', 'default:brick'},
  294. {'default:brick', 'group:torch_craftitem', 'default:brick'},
  295. {'default:brick', 'default:brick', 'default:brick'},
  296. }
  297. })