burner.lua 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. bitumen.burners = {}
  2. local function grab_fuel(inv)
  3. local list = inv:get_list("fuel")
  4. for i,st in ipairs(list) do
  5. print(st:get_name())
  6. local fuel, remains
  7. fuel, remains = minetest.get_craft_result({
  8. method = "fuel",
  9. width = 1,
  10. items = {
  11. ItemStack(st:get_name())
  12. },
  13. })
  14. if fuel.time > 0 then
  15. -- Take fuel from fuel list
  16. st:take_item()
  17. inv:set_stack("fuel", i, st)
  18. return fuel.time
  19. end
  20. end
  21. return 0 -- no fuel found
  22. end
  23. bitumen.get_melter_active_formspec = function(fuel_percent, item_percent)
  24. fuel_percent = fuel_percent or 0
  25. item_percent = item_percent or 0
  26. return "size[8,8.5]"..
  27. default.gui_bg..
  28. default.gui_bg_img..
  29. default.gui_slots..
  30. "list[context;fuel;.75,.5;2,4;]"..
  31. "image[2.75,1.5;1,1;default_furnace_fire_bg.png^[lowpart:"..
  32. (100-fuel_percent)..":default_furnace_fire_fg.png]"..
  33. "image[3.75,1.5;1,1;gui_furnace_arrow_bg.png^[lowpart:"..
  34. (item_percent)..":gui_furnace_arrow_fg.png^[transformR270]"..
  35. "list[current_player;main;0,4.25;8,1;]"..
  36. "list[current_player;main;0,5.5;8,3;8]"..
  37. default.get_hotbar_bg(0, 4.25)
  38. end
  39. bitumen.register_burner = function(nodes, callbacks, burnfactor)
  40. local bf = burnfactor or 1.0
  41. local default_callbacks = {
  42. grab_fuel = grab_fuel, -- needs to return the fuel time
  43. start_cook = function() end, -- needs to return the cook time
  44. finish_cook = function() end,
  45. abort_cook = function() end,
  46. get_formspec_on = bitumen.get_melter_active_formspec,
  47. turn_on = function() end,
  48. turn_off = function() end,
  49. }
  50. for k,v in pairs(callbacks) do
  51. default_callbacks[k] = v
  52. end
  53. local gf = default_callbacks.grab_fuel
  54. default_callbacks.grab_fuel = function(inv)
  55. return gf(inv) * bf
  56. end
  57. for _,n in ipairs(nodes) do
  58. print("setting burner: "..n)
  59. bitumen.burners[n] = default_callbacks
  60. end
  61. end
  62. bitumen.burner_on_timer = function(pos, elapsed)
  63. local posnode = minetest.get_node(pos)
  64. local fns = bitumen.burners[posnode.name]
  65. if fns == nil then
  66. return false
  67. end
  68. local meta = minetest.get_meta(pos)
  69. local fuel_time = meta:get_float("fuel_time") or 0
  70. local fuel_burned = meta:get_float("fuel_burned") or 0
  71. local cook_time = meta:get_float("cook_time") or 0
  72. local cook_burned = meta:get_float("cook_burned") or 0
  73. local inv = meta:get_inventory()
  74. local burned = elapsed
  75. local turn_off = false
  76. -- print("\n\naf timer")
  77. --/ print("fuel_burned: " .. fuel_burned)
  78. -- print("fuel_time: " .. fuel_time)
  79. if fuel_time > 0 and fuel_burned + elapsed < fuel_time then
  80. -- still good on fuel
  81. fuel_burned = fuel_burned + elapsed
  82. meta:set_float("fuel_burned", fuel_burned + elapsed)
  83. else
  84. local t = fns.grab_fuel(inv)
  85. if t <= 0 then -- out of fuel
  86. --print("out of fuel")
  87. meta:set_float("fuel_time", 0)
  88. meta:set_float("fuel_burned", 0)
  89. burned = fuel_time - fuel_burned
  90. turn_off = true
  91. else
  92. -- check if the machine is turning on
  93. if fuel_time == 0 then
  94. fns.turn_on(pos, meta, inv)
  95. end
  96. -- roll into the next period
  97. fuel_burned = elapsed - (fuel_time - fuel_burned)
  98. fuel_time = t
  99. meta:set_float("fuel_time", fuel_time)
  100. meta:set_float("fuel_burned", fuel_burned)
  101. end
  102. end
  103. --print("cooktime " .. cook_time)
  104. --print("cookburned " .. cook_burned)
  105. if cook_time == 0 then -- nothing cooking atm
  106. --print("fueltime " .. fuel_time)
  107. --print("turnoff " .. dump(turn_off))
  108. if fuel_time ~= 0 and turn_off == false then -- should we start to cook?
  109. cook_time = fns.start_cook(pos, meta, inv)
  110. meta:set_float("cook_time", cook_time)
  111. meta:set_float("cook_burned", 0)
  112. else
  113. -- no fuel
  114. end
  115. else -- continue cooking
  116. cook_burned = cook_burned + burned
  117. if cook_burned >= cook_time then -- cooking finished
  118. fns.finish_cook(pos, met, inv)
  119. local new_cook_time = fns.start_cook(pos, meta, inv)
  120. meta:set_float("cook_time", new_cook_time)
  121. cook_burned = cook_burned - cook_time
  122. end
  123. meta:set_float("cook_burned", cook_burned)
  124. end
  125. if turn_off then
  126. fns.turn_off(pos)
  127. return false
  128. end
  129. fuel_pct = math.floor((fuel_burned * 100) / fuel_time)
  130. meta:set_string("formspec", fns.get_formspec_on(fuel_pct, 0))
  131. meta:set_string("infotext", "Fuel: " .. fuel_pct)
  132. return true
  133. end