Advertisement
Guest User

charge

a guest
Jan 1st, 2015
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.70 KB | None | 0 0
  1.  
  2. local fuel_types = {"charcoal", "coal"}
  3. local container_types = {"mcp_mobius_betterbarrel", "container_chest"}
  4. local side="bottom"
  5. local reverse ="up"
  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.   if validWithList(slot.name, fuel_types) then
  28.     print("fuel found.")
  29.     return true
  30.   else
  31.     return false
  32.   end
  33. end
  34.  
  35. function isFuelSource(type)
  36.   if validWithList(type, container_types) then
  37.     print("Sitting on a fuel source.")
  38.     return true
  39.   else
  40.     print("No fuel source down there.")
  41.     return false
  42.   end
  43. end
  44.  
  45. function eatFuel(cont)
  46.   turtle.select(1)
  47.   local stacks = cont.getAllStacks()
  48.   for i, slot in pairs(stacks) do
  49.     if not isFull() and isFuel(slot) then
  50.       cont.pushItemIntoSlot(reverse,i,16,1)
  51.       turtle.refuel()
  52.     end
  53.   end
  54. end
  55.  
  56. function getFuel()
  57.   local charge = turtle.getFuelLevel()
  58.   local max= turtle.getFuelLimit()
  59.   local percent = charge * 100 / max
  60.   print("charge level : "..percent.." ( "..charge.."/ "..max..")")
  61.   return charge
  62. end
  63.  
  64. if isFuelSource(peripheral.getType(side),container_types) then
  65.   print("CHARGING FROM")
  66.   print("name : "..p.getInventoryName())
  67.   print("size : "..p.getInventorySize())
  68.   local lastState = turtle.getFuelLevel()
  69.   while not isFull() do
  70.     eatFuel(p)
  71.     if not ( getFuel() > lastState ) then
  72.       print("Fuel source empty, Stop charging.")
  73.       break
  74.     end
  75.     sleep(1)
  76.   end
  77. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement