Advertisement
Godleydemon

Mining Program 2

Jun 15th, 2013
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.  
  70.  
  71. -- get the current level of the turtle
  72. function getLvl()
  73.     curLev = m.getLevels()
  74.     return curLev
  75. end
  76.  
  77. -- Check if nLvl(default=30) levels have been reached, if not wait, if yes, return true.
  78. function checkLevel(nLvl)
  79.     getLvl()
  80.     if curLev<nLvl then
  81.         printStatus("gathering XP", curLev)
  82.         return false
  83.     else
  84.         printStatus("moving to enchant area", getLvl())
  85.         return true
  86.     end
  87.    
  88. end
  89.  
  90. -- Look for items to enchant in slots 2 through 14, if there are none, default to findBooks()
  91. function getItems()
  92.     for i=2,14 do
  93.         if turtle.getItemCount(i)>0 then
  94.             turtle.select(i)
  95.             turtle.transferTo(1,1)
  96.             turtle.select(1)
  97.             return true
  98.         end
  99.     end
  100. --      print("no items found, trying to find books")
  101.     if findBook() then
  102.     --      print("Books found")
  103.         return true
  104.     else
  105.         return false
  106.     end
  107.  
  108. end
  109.  
  110. -- Get books from inventory slot 15 and put 1 in slot 1, returning true, if not return false
  111. function findBook()
  112.     if turtle.getItemCount(15)>=1 then
  113.         turtle.select(15)
  114.         turtle.transferTo(1,1)
  115.         turtle.select(1)
  116.         return true
  117.     else
  118.         return false
  119.     end
  120. end
  121.  
  122. -- enchant the book in slot one and drop it into the chest underneath the turtle
  123. function enchantBook(nLvl)
  124.     if m.enchant(nLvl) then
  125.         turtle.dropDown()
  126.     else
  127.         error("Unexpected error while trying to enchant")
  128.     end
  129.    
  130. end
  131.  
  132. -- End program with a message
  133.  
  134. function exitMsg(exitNr)
  135.     if exitNr == 1 then exitStr="Out of fuel. Place more fuel in slot 16 and restart the program"
  136.     elseif exitNr == 2 then exitStr="No more items or books"
  137. --  elseif exitNr == 3 then exitStr=""
  138. --  elseif exitNr == 4 then exitStr=""
  139. --  elseif exitNr == then exitStr=""
  140.    
  141.     end
  142.     term.clear()
  143.     term.setCursorPos(2,2)
  144.     sleep(0.5)
  145.     print(exitStr)
  146.     run.state="end"
  147. end
  148.  
  149.  
  150.  
  151. function printIntro()
  152.     term.clear()
  153.     print("---------------------------------------")
  154.     print("------- XPturtle 0.2 by Gili710 -------")
  155.     print("---------------------------------------")
  156.     print()
  157.     print("This turtle will automatically enchant items in its inventory")
  158.     print("Place any items in slots 2 through 14")
  159.     print("Place some books in slot 15.")
  160.     print("Place some fuel in slot 16")
  161.     print("Press any key to start")
  162.     print()
  163.  
  164. end
  165.  
  166. function printStatus(curTask, curLvl)
  167.     term.clear()
  168.     term.setCursorPos(1,1)
  169.     print("------- XPturtle 0.2 by Gili710 -------")
  170.     term.setCursorPos(2,4)
  171.     io.write(string.format("Current Task: %s", curTask))
  172.     term.setCursorPos(2,6)
  173.     io.write(string.format("Current Level: %s", curLvl))
  174. end
  175.  
  176.  
  177. --INIT
  178. m=peripheral.wrap("right")
  179. m.setAutoCollect(true)
  180. nLvl=30
  181. run = {}
  182. run.state="running"
  183. screenw, screenh = term.getSize()
  184.  
  185. -- Main Program
  186.  
  187. printIntro()
  188. os.pullEvent()
  189. if run.state=="running" then
  190.     moveToStart()
  191. end
  192.  
  193.  
  194.  
  195. -- start main loop
  196. while run.state=="running" do
  197.     while not checkLevel(nLvl) do
  198.         sleep(1)
  199.  
  200.     end
  201.    
  202.     loadState()
  203.  
  204.     --  Extra safety here
  205.     while moveState ~= 5 do
  206.         moveToStart()
  207.         moveBack(5)
  208.     end
  209.     printStatus("enchanting", getLvl())
  210.     sleep(2)
  211.     if getItems() then
  212.         enchantBook(nLvl)
  213.         sleep(2)
  214.         moveForward(5)
  215.     else
  216.         exitMsg(2)
  217.     end
  218. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement