Advertisement
ozybrum

Xp Turtle Gather

Jul 21st, 2014
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.54 KB | None | 0 0
  1. --TODO :        make check for slot 1 being empty
  2. --                      create exit for when chest is full
  3.  
  4.  
  5. --Load the saved Turtle state from disk if available, if not set to 0
  6. function loadState()
  7.         if fs.exists("xpTurtleState") then
  8.                 local file = fs.open("xpTurtleState","r")
  9.                 data = file.readAll()
  10.                 file.close()
  11.                 data=tonumber(data)
  12.                 moveState=data
  13.         else
  14.                 print("debug -- no xpTurtleState found, setting movement state to 0\n")
  15.                 moveState = 0
  16.         end
  17. end
  18.  
  19. -- Save the current state to disk
  20. function saveState(a)
  21.         local file = fs.open("xpTurtleState","w")
  22.         file.write(a)
  23.         file.close()
  24. --      io.write(string.format("debug -- wrote %s to file\n",a))
  25.  
  26. end
  27.  
  28. -- Check where the turtle is right now, and it it isn't at block 0, move there
  29. function moveToStart()
  30.         loadState()
  31. --      io.write(string.format("debug -- Moving %s blocks to starting point\n", moveState))
  32.         if moveState~=nil then
  33.                 printStatus("moving to start", getLvl())
  34.                 moveForward(moveState)
  35.         end
  36. end
  37.  
  38. -- Move forward with checking for blockage and save the move state
  39. function moveForward(nDistance)
  40.         printStatus("moving to start", getLvl())
  41. --      print("moveForward nDistance is  ", nDistance)
  42.         while nDistance > 0 do
  43.                 if turtle.forward() then
  44.                         nDistance=nDistance-1
  45.                         moveState = moveState -1
  46.                         saveState(moveState)
  47.                 else
  48.                         sleep(0.5)
  49.                 end
  50.         end
  51. end
  52.  
  53. -- Move back with checking for blockage and save the move state
  54. function moveBack(nDistance)
  55.         while nDistance > 0 do
  56.                 printStatus("moving to enchant area", curLev)
  57. --              print("moveBack () nDistance is  ", nDistance)
  58.                 if turtle.back() then
  59.                         nDistance=nDistance-1
  60.                         moveState = moveState +1
  61.                         saveState(moveState)
  62.                 else
  63.                         sleep(0.5)
  64.                 end
  65.         end
  66. end
  67.  
  68.  
  69. -- check if there's enough fuel, if not refuel from slot 16. If no fuel available, exit.
  70. function checkFuel()
  71.         if turtle.getFuelLevel()<20 then
  72.                 if      turtle.getItemCount(16)>=1 then
  73.                         turtle.select(16)
  74.                         turtle.refuel(1)
  75.                 else
  76.                         exitMsg(1)                    
  77.                 end
  78.         end
  79.  
  80. end
  81.  
  82. -- get the current level of the turtle
  83. function getLvl()
  84.         curLev = m.getLevels()
  85.         return curLev
  86. end
  87.  
  88. -- Check if nLvl(default=30) levels have been reached, if not wait, if yes, return true.
  89. function checkLevel(nLvl)
  90.         getLvl()
  91.         if curLev<nLvl then
  92.                 printStatus("gathering XP", curLev)
  93.                 return false
  94.         else
  95.                 printStatus("moving to enchant area", getLvl())
  96.                 return true
  97.         end
  98.        
  99. end
  100.  
  101. -- Look for items to enchant in slots 2 through 14, if there are none, default to findBooks()
  102. function getItems()
  103.         for i=2,14 do
  104.                 if turtle.getItemCount(i)>0 then
  105.                         turtle.select(i)
  106.                         turtle.transferTo(1,1)
  107.                         turtle.select(1)
  108.                         return true
  109.                 end
  110.         end
  111. --              print("no items found, trying to find books")
  112.         if findBook() then
  113.         --              print("Books found")
  114.                 return true
  115.         else
  116.                 return false
  117.         end
  118.  
  119. end
  120.  
  121. -- Get books from inventory slot 15 and put 1 in slot 1, returning true, if not return false
  122. function findBook()
  123.         if turtle.getItemCount(15)>=1 then
  124.                 turtle.select(15)
  125.                 turtle.transferTo(1,1)
  126.                 turtle.select(1)
  127.                 return true
  128.         else
  129.                 return false
  130.         end
  131. end
  132.  
  133. -- enchant the book in slot one and drop it into the chest underneath the turtle
  134. function enchantBook(nLvl)
  135.         if m.enchant(nLvl) then
  136.                 turtle.dropDown()
  137.         else
  138.                 error("Unexpected error while trying to enchant")
  139.         end
  140.        
  141. end
  142.  
  143. -- End program with a message
  144.  
  145. function exitMsg(exitNr)
  146.         if exitNr == 1 then exitStr="Out of fuel. Place more fuel in slot 16 and restart the program"
  147.         elseif exitNr == 2 then exitStr="No more items or books"
  148. --      elseif exitNr == 3 then exitStr=""
  149. --      elseif exitNr == 4 then exitStr=""
  150. --      elseif exitNr == then exitStr=""
  151.        
  152.         end
  153.         term.clear()
  154.         term.setCursorPos(2,2)
  155.         sleep(0.5)
  156.         print(exitStr)
  157.         run.state="end"
  158. end
  159.  
  160.  
  161.  
  162. function printIntro()
  163.         term.clear()
  164.         print("---------------------------------------")
  165.         print("------- XPturtle 0.2 by Gili710 -------")
  166.         print("---------------------------------------")
  167.         print()
  168.         print("This turtle will automatically enchant items in its inventory")
  169.         print("Place any items in slots 2 through 14")
  170.         print("Place some books in slot 15.")
  171.         print("Place some fuel in slot 16")
  172.         print("Press any key to start")
  173.         print()
  174.  
  175. end
  176.  
  177. function printStatus(curTask, curLvl)
  178.         term.clear()
  179.         term.setCursorPos(1,1)
  180.         print("------- XPturtle 0.2 by Gili710 -------")
  181.         term.setCursorPos(2,4)
  182.         io.write(string.format("Current Task: %s", curTask))
  183.         term.setCursorPos(2,6)
  184.         io.write(string.format("Current Level: %s", curLvl))
  185. end
  186.  
  187.  
  188. --INIT
  189. m=peripheral.wrap("right")
  190. m.setAutoCollect(true)
  191. nLvl=30
  192. run = {}
  193. run.state="running"
  194. screenw, screenh = term.getSize()
  195.  
  196. -- Main Program
  197.  
  198. printIntro()
  199. os.pullEvent()
  200. checkFuel()
  201. if run.state=="running" then
  202.         moveToStart()
  203. end
  204.  
  205.  
  206.  
  207. -- start main loop
  208. while run.state=="running" do
  209.         while not checkLevel(nLvl) do
  210.                 sleep(1)
  211.  
  212.         end
  213.         checkFuel()
  214.         loadState()
  215.  
  216.         --      Extra safety here
  217.         while moveState ~= 5 do
  218.                 moveToStart()
  219.                 moveBack(5)
  220.         end
  221.         printStatus("enchanting", getLvl())
  222.         sleep(2)
  223.         if getItems() then
  224.                 enchantBook(nLvl)
  225.                 sleep(2)
  226.                 moveForward(5)
  227.         else
  228.                 exitMsg(2)
  229.         end
  230. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement