DeliciousJaffa

XP Program

Mar 30th, 2013
765
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.80 KB | None | 0 0
  1. -- XP Turtle program by DeliciousJaffa
  2.  
  3. m = peripheral.wrap("right")
  4. m.setAutoCollect(true)
  5. local currLevel = 0
  6. local booksEnchanted = 0
  7. local waittext = " - Wait"
  8. local tstop
  9. local running = true
  10. local sx, sy
  11. local ix, xy
  12.  
  13. function checkInv ()
  14.     for s=2,16 do
  15.     if turtle.getItemCount(s) > 0 then
  16.       turtle.select(s)
  17.       if turtle.compareTo(1) == true then
  18.         turtle.transferTo(2)
  19.       else
  20.         turtle.dropDown()
  21.       end
  22.     end
  23.   end
  24.   if turtle.getItemCount(1) > 1 then
  25.     turtle.select(1)
  26.     turtle.transferTo(2, turtle.getItemCount(1) - 1)
  27.   end
  28.   turtle.select(2)
  29.   if turtle.getItemCount(2) > 1 then
  30.     turtle.dropDown(turtle.getItemCount(2) - 1)
  31.   end
  32. end
  33.  
  34. function enchantBook()
  35.   if turtle.getItemCount(2) < 1 then
  36.     turtle.suckDown()
  37.   end
  38.   checkInv()
  39.   turtle.select(2)
  40.   if turtle.compareTo(1) == true then
  41.     if m.enchant(30) then
  42.       booksEnchanted = booksEnchanted + 1
  43.       turtle.dropDown()
  44.     end
  45.   else
  46.     term.setCursorPos(1,sy)
  47.     term.clearLine()
  48.     term.setCursorPos(1,sy)
  49.     write("Out of books.")
  50.   end
  51. end
  52.  
  53. function updatestats()
  54.   currLevel = m.getLevels()
  55.   term.setCursorPos(1,ty)
  56.   term.clearLine()
  57.   term.setCursorPos(1,ty)
  58.   write("Level: " ..currLevel.. " | Books Completed: " ..booksEnchanted)
  59. end
  60.  
  61. function keywait ()
  62.   while not tstop do
  63.     local event,key = os.pullEvent()
  64.       if (event == "char") and (key == "s") then
  65.         running = not running
  66.         write(waittext)
  67.         term.setCursorPos(1,sy)
  68.         term.clearLine()
  69.         term.setCursorPos(1,sy)
  70.         if running then
  71.           m.setAutoCollect(true)
  72.           write("Running")
  73.         else
  74.           m.setAutoCollect(false)
  75.           write("Not running")
  76.         end
  77.       elseif (event == "char") and (key == "q") then
  78.         tstop = true
  79.       end
  80.   end
  81. end
  82.  
  83. function stats ()
  84.   tx,ty = term.getCursorPos()
  85.   while not tstop do
  86.     updatestats()
  87.     sleep(1)
  88.   end
  89.   write(waittext)
  90. end
  91.  
  92. --print(m.getLevels())
  93. --enchantBook()
  94.  
  95. function main ()
  96.   while not tstop do
  97.     currLevel = m.getLevels()
  98.     if running then
  99.       if currLevel >=30 then
  100.         enchantBook()
  101.       else
  102.         term.setCursorPos(1,sy)
  103.         term.clearLine()
  104.         term.setCursorPos(1,sy)
  105.         write("Waiting for level 30")
  106.         sleep(3)
  107.       end
  108.     else
  109.       term.setCursorPos(1,sy)
  110.       term.clearLine()
  111.       term.setCursorPos(1,sy)
  112.       write("Not running")
  113.       sleep(3)
  114.     end
  115.   end
  116. end
  117.  
  118. term.clear()
  119. term.setCursorPos(1,1)
  120. turtle.select(1)
  121. sx,sy = term.getCursorPos()
  122. print("Starting up")
  123. print("Press S to stop collecting, Q to quit")
  124. parallel.waitForAll(stats,keywait,main)
  125. local mx,my = term.getCursorPos()
  126. term.setCursorPos((mx - string.len(waittext)),my)
  127. updatestats()
  128. print(" \\e/")
Advertisement
Add Comment
Please, Sign In to add comment