Advertisement
Guest User

startup

a guest
Jan 20th, 2015
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.14 KB | None | 0 0
  1. --mana
  2. local target = 12
  3. --comparator
  4. local compSide = "front"
  5. ---cakeChest
  6. local chestSide = "back"
  7. local reverse = "south"
  8. --turtle
  9. local buffer = 16
  10. local cakeSlot = 1
  11.  
  12. local chest = peripheral.wrap(chestSide)
  13.  
  14. function lowCake()
  15.   return turtle.getItemCount(cakeSlot) < (buffer/2)
  16. end
  17.  
  18. function round(n)
  19.   return n % 1 >= 0.5 and math.ceil(n) or math.floor(n)
  20. end  
  21.  
  22. function refillCake()
  23.   if lowCake() then
  24.     local as = chest.getAllStacks()
  25.     for i=1,#as do
  26.       if as[i].name == "cake" and lowCake() then
  27.         chest.pushItem(reverse,i,(buffer-turtle.getItemCount(cakeSlot)),cakeSlot)
  28.       end
  29.     end
  30.   end  
  31. end
  32.  
  33. function getLevel()
  34.   local lvl = rs.getAnalogInput(compSide)
  35.   print("Mana level is "..round(lvl*100/15).."%")
  36.   return lvl
  37. end
  38.  
  39. refillCake()
  40. while true do
  41.   while getLevel() < target do
  42.     print("RECHARGING!")
  43.     if not turtle.detectUp() then
  44.       turtle.select(cakeSlot)
  45.       if turtle.placeUp() then
  46.         refillCake()
  47.       else
  48.         print("Error placing cake!")
  49.       end
  50.     else
  51.       print("Previous cake still present!")
  52.     end
  53.     sleep(8)
  54.   end
  55.   sleep(8)
  56. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement