burners.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. local function swap_node(pos, name)
  2. local node = minetest.get_node(pos)
  3. if node.name == name then
  4. return
  5. end
  6. node.name = name
  7. minetest.swap_node(pos, node)
  8. end
  9. local function get_af_active_formspec(fuel_percent, item_percent)
  10. return "size[8,8.5]"..
  11. default.gui_bg..
  12. default.gui_bg_img..
  13. default.gui_slots..
  14. -- "list[context;src;2.75,0.5;1,1;]"..
  15. "list[context;fuel;.75,.5;2,4;]"..
  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. -- "list[context;dst;4.75,0.96;2,2;]"..
  21. "list[current_player;main;0,4.25;8,1;]"..
  22. "list[current_player;main;0,5.5;8,3;8]"..
  23. -- "listring[context;dst]"..
  24. -- "listring[current_player;main]"..
  25. -- "listring[context;src]"..
  26. -- "listring[current_player;main]"..
  27. -- "listring[context;fuel]"..
  28. -- "listring[current_player;main]"..
  29. default.get_hotbar_bg(0, 4.25)
  30. end
  31. function get_af_inactive_formspec()
  32. return "size[8,8.5]"..
  33. default.gui_bg..
  34. default.gui_bg_img..
  35. default.gui_slots..
  36. -- "list[context;src;2.75,0.5;1,1;]"..
  37. "list[context;fuel;2.75,2.5;2,2;]"..
  38. "image[2.75,1.5;1,1;default_furnace_fire_bg.png]"..
  39. -- "image[3.75,1.5;1,1;gui_furnace_arrow_bg.png^[transformR270]"..
  40. -- "list[context;dst;4.75,0.96;2,2;]"..
  41. "list[current_player;main;0,4.25;8,1;]"..
  42. "list[current_player;main;0,5.5;8,3;8]"..
  43. "listring[context;dst]"..
  44. "listring[current_player;main]"..
  45. "listring[context;src]"..
  46. "listring[current_player;main]"..
  47. "listring[context;fuel]"..
  48. "listring[current_player;main]"..
  49. default.get_hotbar_bg(0, 4.25)
  50. end
  51. local function grab_fuel(inv)
  52. local list = inv:get_list("fuel")
  53. for i,st in ipairs(list) do
  54. if st:get_name() == "forge:coke" or st:get_name() == "geology:anthracite" then
  55. local fuel, remains
  56. fuel, remains = minetest.get_craft_result({
  57. method = "fuel",
  58. width = 1,
  59. items = {
  60. ItemStack(st:get_name())
  61. },
  62. })
  63. if fuel.time > 0 then
  64. -- Take fuel from fuel list
  65. st:take_item()
  66. inv:set_stack("fuel", i, st)
  67. return fuel.time
  68. end
  69. end
  70. end
  71. return 0 -- no fuel found
  72. end
  73. local function burner_on_timer(pos, elapsed)
  74. local meta = minetest.get_meta(pos)
  75. local fuel_time = meta:get_float("fuel_time") or 0
  76. local fuel_burned = meta:get_float("fuel_burned") or 0
  77. local cook_time_remaining = meta:get_float("cook_time_remaining") or 60
  78. local inv = meta:get_inventory()
  79. local burned = elapsed
  80. local turn_off = false
  81. print("\n\naf timer")
  82. print("fuel_burned: " .. fuel_burned)
  83. print("fuel_time: " .. fuel_time)
  84. if fuel_time > 0 and fuel_burned + elapsed < fuel_time then
  85. fuel_burned = fuel_burned + elapsed
  86. meta:set_float("fuel_burned", fuel_burned + elapsed)
  87. else
  88. local t = grab_fuel(inv)
  89. if t <= 0 then -- out of fuel
  90. --print("out of fuel")
  91. meta:set_float("fuel_time", 0)
  92. meta:set_float("fuel_burned", 0)
  93. burned = fuel_time - fuel_burned
  94. turn_off = true
  95. else
  96. -- roll into the next period
  97. fuel_burned = elapsed - (fuel_time - fuel_burned)
  98. fuel_time = t
  99. --print("fuel remaining: " .. (fuel_time - fuel_burned))
  100. meta:set_float("fuel_time", fuel_time)
  101. meta:set_float("fuel_burned", fuel_burned)
  102. end
  103. end
  104. if burned > 0 then
  105. local remain = cook_time_remaining - burned
  106. print("remain: ".. remain);
  107. if remain > 0 then
  108. meta:set_float("cook_time_remaining", remain)
  109. else
  110. print("finished")
  111. local ore_nodes = minetest.find_nodes_in_area(
  112. {x=pos.x - 5, y=pos.y + 0, z=pos.z - 5},
  113. {x=pos.x + 5, y=pos.y + 9, z=pos.z + 5},
  114. forge.meltable_ores
  115. )
  116. if #ore_nodes > 0 then
  117. local i = math.random(1, #ore_nodes)
  118. local p = ore_nodes[i]
  119. local n = minetest.get_node(p)
  120. if n ~= nil then
  121. minetest.set_node(p, {name=forge.random_melt_product(n.name)})
  122. end
  123. end
  124. meta:set_float("cook_time_remaining", 4)
  125. end
  126. end
  127. if turn_off then
  128. swap_node(pos, "forge:burner")
  129. return
  130. end
  131. fuel_pct = math.floor((fuel_burned * 100) / fuel_time)
  132. -- item_pct = math.floor((fuel_burned * 100) / fuel_time)
  133. meta:set_string("formspec", get_af_active_formspec(fuel_pct, 0))
  134. meta:set_string("infotext", "Fuel: " .. fuel_pct)
  135. --minetest.get_node_timer(pos):start(1.0)
  136. return true
  137. end
  138. minetest.register_node("forge:burner_on", {
  139. description = "Forge Burner (active)",
  140. tiles = {
  141. "default_steel_block.png", "default_steel_block.png",
  142. "default_steel_block.png", "default_steel_block.png",
  143. "default_steel_block.png",
  144. {
  145. image = "default_furnace_front_active.png",
  146. backface_culling = false,
  147. animation = {
  148. type = "vertical_frames",
  149. aspect_w = 16,
  150. aspect_h = 16,
  151. length = 1.5
  152. },
  153. }
  154. },
  155. paramtype2 = "facedir",
  156. groups = {cracky=1, refractory=1},
  157. legacy_facedir_simple = true,
  158. is_ground_content = false,
  159. sounds = default.node_sound_stone_defaults(),
  160. stack_max = 1,
  161. can_dig = can_dig,
  162. on_timer = burner_on_timer,
  163. on_construct = function(pos)
  164. local meta = minetest.get_meta(pos)
  165. meta:set_string("formspec", get_af_inactive_formspec())
  166. local inv = meta:get_inventory()
  167. inv:set_size('fuel', 4)
  168. minetest.get_node_timer(pos):start(1.0)
  169. end,
  170. on_metadata_inventory_move = function(pos)
  171. minetest.get_node_timer(pos):start(1.0)
  172. end,
  173. on_metadata_inventory_put = function(pos)
  174. -- start timer function, it will sort out whether furnace can burn or not.
  175. minetest.get_node_timer(pos):start(1.0)
  176. end,
  177. on_punch = function(pos)
  178. swap_node(pos, "forge:burner")
  179. end,
  180. -- on_blast = function(pos)
  181. -- local drops = {}
  182. -- default.get_inventory_drops(pos, "src", drops)
  183. -- default.get_inventory_drops(pos, "fuel", drops)
  184. -- default.get_inventory_drops(pos, "dst", drops)
  185. -- drops[#drops+1] = "machines:machine"
  186. -- minetest.remove_node(pos)
  187. -- return drops
  188. -- end,
  189. allow_metadata_inventory_put = allow_metadata_inventory_put,
  190. allow_metadata_inventory_move = allow_metadata_inventory_move,
  191. allow_metadata_inventory_take = allow_metadata_inventory_take,
  192. })
  193. minetest.register_node("forge:burner", {
  194. description = "Forge Burner",
  195. tiles = {
  196. "default_steel_block.png", "default_steel_block.png",
  197. "default_steel_block.png", "default_steel_block.png",
  198. "default_steel_block.png", "default_furnace_front.png"
  199. },
  200. paramtype2 = "facedir",
  201. groups = {cracky=1, refractory=1},
  202. legacy_facedir_simple = true,
  203. is_ground_content = false,
  204. sounds = default.node_sound_stone_defaults(),
  205. stack_max = 1,
  206. can_dig = can_dig,
  207. --on_timer = af_node_timer,
  208. on_construct = function(pos)
  209. local meta = minetest.get_meta(pos)
  210. meta:set_string("formspec", get_af_inactive_formspec())
  211. local inv = meta:get_inventory()
  212. inv:set_size('fuel', 4)
  213. end,
  214. on_metadata_inventory_move = function(pos)
  215. --minetest.get_node_timer(pos):start(1.0)
  216. end,
  217. on_metadata_inventory_put = function(pos)
  218. -- start timer function, it will sort out whether furnace can burn or not.
  219. --minetest.get_node_timer(pos):start(1.0)
  220. end,
  221. on_punch = function(pos, node, player)
  222. swap_node(pos, "forge:burner_on")
  223. minetest.get_node_timer(pos):start(1.0)
  224. end,
  225. -- on_blast = function(pos)
  226. -- local drops = {}
  227. -- default.get_inventory_drops(pos, "src", drops)
  228. -- default.get_inventory_drops(pos, "fuel", drops)
  229. -- default.get_inventory_drops(pos, "dst", drops)
  230. -- drops[#drops+1] = "machines:machine"
  231. -- minetest.remove_node(pos)
  232. -- return drops
  233. -- end,
  234. allow_metadata_inventory_put = allow_metadata_inventory_put,
  235. allow_metadata_inventory_move = allow_metadata_inventory_move,
  236. allow_metadata_inventory_take = allow_metadata_inventory_take,
  237. })
  238. minetest.register_craft({
  239. output = 'forge:burner',
  240. recipe = {
  241. {'forge:refractory_clay_brick', 'forge:refractory_clay_brick', 'forge:refractory_clay_brick'},
  242. {'forge:refractory_clay_brick', 'default:furnace', 'forge:refractory_clay_brick'},
  243. {'forge:refractory_clay_brick', 'forge:refractory_clay_brick', 'forge:refractory_clay_brick'},
  244. }
  245. })
  246. if minetest.global_exists("bitumen") and bitumen then
  247. minetest.register_node("forge:oil_burner_on", {
  248. description = "Forge Oil Burner (active)",
  249. tiles = {
  250. "default_steel_block.png", "default_steel_block.png",
  251. "default_steel_block.png", "default_steel_block.png",
  252. "default_steel_block.png",
  253. {
  254. image = "default_furnace_front_active.png",
  255. backface_culling = false,
  256. animation = {
  257. type = "vertical_frames",
  258. aspect_w = 16,
  259. aspect_h = 16,
  260. length = 1.5
  261. },
  262. }
  263. },
  264. paramtype2 = "facedir",
  265. groups = {cracky=1, refractory=1},
  266. legacy_facedir_simple = true,
  267. is_ground_content = false,
  268. sounds = default.node_sound_stone_defaults(),
  269. drops = "forge:oil_burner",
  270. on_punch = function(pos)
  271. swap_node(pos, "forge:oil_burner")
  272. end,
  273. })
  274. minetest.register_node("forge:oil_burner", {
  275. description = "Forge Oil Burner",
  276. tiles = {
  277. "default_steel_block.png", "default_steel_block.png",
  278. "default_steel_block.png", "default_steel_block.png",
  279. "default_steel_block.png", "default_furnace_front.png"
  280. },
  281. paramtype2 = "facedir",
  282. groups = {cracky=1, refractory=1},
  283. legacy_facedir_simple = true,
  284. is_ground_content = false,
  285. sounds = default.node_sound_stone_defaults(),
  286. on_construct = function(pos)
  287. end,
  288. on_punch = function(pos, node, player)
  289. swap_node(pos, "forge:oil_burner_on")
  290. end,
  291. })
  292. minetest.register_abm({
  293. nodenames = {"forge:oil_burner_on"},
  294. interval = 2,
  295. chance = 1,
  296. action = function(pos, node, active_object_count, active_object_count_wider)
  297. local npos = {x=pos.x, y=pos.y - 1, z=pos.z}
  298. local pnet = bitumen.pipes.get_net(npos)
  299. if not pnet or pnet.buffer <= 0 then
  300. print("oil burner: no oil in pipe")
  301. return -- no oil in the pipe
  302. end
  303. if pnet.fluid ~= "bitumen:heavy_oil" then
  304. print("barrel filler: bad_fluid ".. pnet.fluid)
  305. return -- incompatible fluids
  306. end
  307. local to_take = 1
  308. if pnet.buffer <= to_take then
  309. print("oil burner: not enough fuel")
  310. return
  311. end
  312. local taken, fluid = bitumen.pipes.take_fluid(npos, to_take)
  313. if fluid == "air" then
  314. print("oil burner: failed to take enough fuel")
  315. return
  316. end
  317. local ore_nodes = minetest.find_nodes_in_area(
  318. {x=pos.x - 1, y=pos.y + 1,z=pos.z - 1},
  319. {x=pos.x + 1, y=pos.y + 6, z=pos.z + 1},
  320. forge.meltable_ores
  321. )
  322. if #ore_nodes > 0 then
  323. local i = math.random(1, #ore_nodes)
  324. local p = ore_nodes[i]
  325. local n = minetest.get_node(p)
  326. if n ~= nil then
  327. minetest.set_node(p, {name=forge.random_melt_product(n.name)})
  328. end
  329. end
  330. end
  331. })
  332. minetest.register_craft({
  333. output = 'forge:oil_burner',
  334. type = "shapeless",
  335. recipe = {'forge:burner', 'bitumen:pipe'},
  336. })
  337. end