Advertisement
lucifersamfr

chargeTurtleWithSolidFuel

Jan 1st, 2015
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local fuel_types = {"charcoal", "coal"}
  2. local container_types = {"mcp_mobius_betterbarrel", "container_chest"}
  3. local side="bottom"
  4. local reverse ="up"
  5. local packetSize=16
  6. local p=peripheral.wrap(side)
  7.  
  8. function isFull()
  9.   if turtle.getFuelLevel() > turtle.getFuelLimit()-100 then
  10.     print("Turtle charged!")
  11.     return true
  12.   else
  13.     return false
  14.   end
  15. end
  16.  
  17. function validWithList(type,list)
  18.   for i=1,#list do
  19.     if list[i]==type then
  20.       return true
  21.     end
  22.   end
  23.   return false
  24. end
  25.  
  26. function isFuel(slot)
  27.   return validWithList(slot.name, fuel_types)
  28. end
  29.  
  30. function isFuelSource(type)
  31.   if validWithList(type, container_types) then
  32.     --print("valid fuel source")
  33.     return true
  34.   else
  35.     print("Fuel source is  not supported!")
  36.     return false
  37.   end
  38. end
  39.  
  40. function eatFuel(cont)
  41.   turtle.select(1)
  42.   local stacks = cont.getAllStacks()
  43.   for i, slot in pairs(stacks) do
  44.     if not isFull() and isFuel(slot) then
  45.       cont.pushItemIntoSlot(reverse,i,packetSize,1)
  46.       turtle.refuel()
  47.     end
  48.   end
  49. end
  50.  
  51. function getFuel()
  52.   local charge = turtle.getFuelLevel()
  53.   local max= turtle.getFuelLimit()
  54.   local percent = charge * 100 / max
  55.   print("charge level : "..percent.." ( "..charge.."/ "..max..")")
  56.   return charge
  57. end
  58.  
  59. if isFuelSource(peripheral.getType(side),container_types) then
  60.   print("CHARGING FROM")
  61.   print("name : "..p.getInventoryName())
  62.   print("size : "..p.getInventorySize())
  63.   local lastState = turtle.getFuelLevel()
  64.   local step = 0.5
  65.   while not isFull() do
  66.     eatFuel(p)
  67.     local charge = getFuel()
  68.     if not ( charge > lastState ) then
  69.       if step<8 then
  70.         step = step + 0.5
  71.         print("Fuel source empty, waiting step="..step)
  72.       else
  73.         print("Fuel source still empty, Stop charging.")
  74.         break
  75.       end
  76.     else
  77.       step=0.5  
  78.     end
  79.     lastState = charge
  80.     sleep(step)
  81.   end
  82. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement