Advertisement
Guest User

Single attempt tunneller v3

a guest
Feb 28th, 2015
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.30 KB | None | 0 0
  1. local slots=16
  2. local allFullUp=false
  3.  
  4. -- MoveUp, digging if necessary, but not removing the item in slot 2 (torches)
  5. function MoveUp()
  6.   if turtle.detectUp() then
  7.     turtle.select(2)
  8.     if not turtle.compareUp() then
  9.       turtle.digUp()
  10.     end
  11.   end
  12.   turtle.up()
  13. end
  14.  
  15. -- MoveForward, digging if necessary, but not removing the item in slot 2 (torches)
  16. function MoveForward()
  17.   while turtle.detect() do
  18.     turtle.select(2)
  19.     if not turtle.compare() then
  20.       turtle.dig()
  21.     else
  22.       break
  23.     end
  24.   end
  25.   return turtle.forward()
  26. end
  27.  
  28. function DetectAllFull()
  29.   allFullUp=true
  30.   for n=1, slots, 1 do
  31.     if turtle.getItemCount(n) == 0 then
  32.       allFullUp=false
  33.     end
  34.   end
  35.   return allFullUp
  36. end
  37.  
  38. function GoneFarEnough()
  39.   local Result=true
  40.   if turtle.detectDown() then
  41.     Result = DetectAllFull()
  42.   end
  43.   if Result then
  44.     print("Return to base")
  45.   end
  46.   return Result
  47. end
  48.  
  49. function OnStopBlock()
  50.   turtle.select(1)
  51.   return turtle.compareDown()
  52. end
  53.  
  54. -- We should stop if we've moved out into the air or we're on top of the stop block (in slot 1)
  55. function ShouldStop()
  56.   return not turtle.detectDown() or OnStopBlock()
  57. end
  58.  
  59. function ReturnBecauseAllFullUp()
  60.   print("So much stuff!")
  61.   while not turtle.detectDown() and turtle.forward() do
  62.   end
  63.   turtle.down()
  64.   print("Nearly home")
  65.   while not OnStopBlock() and turtle.forward() do
  66.   end  
  67. end
  68.  
  69. function ReturnBecauseEndOfRun()
  70.   print("My work here is done")
  71.   while not DetectAllFull() and not OnStopBlock() do
  72.     MoveForward()
  73.   end
  74.   if allFullUp then
  75.     turtle.up()
  76.     ReturnBecauseAllFullUp()
  77.   end
  78. end
  79.  
  80. while not ShouldStop() and turtle.forward() do
  81.   print("Hi Ho, hi ho...")
  82. end
  83. if turtle.detectDown() then
  84.   print("It's up to work I go")
  85.   MoveUp()
  86.   while not turtle.detect() do
  87.     turtle.forward()
  88.   end
  89.   while not GoneFarEnough() do
  90.     MoveForward()
  91.   end
  92.   turtle.turnRight()
  93.   turtle.turnRight()
  94.   if allFullUp then
  95.     ReturnBecauseAllFullUp()
  96.   else
  97.     turtle.digDown() -- Just to be sure
  98.     turtle.down()
  99.     ReturnBecauseEndOfRun()
  100.   end
  101. else
  102.   print("Weird, this one's already done!")
  103.   turtle.turnRight()
  104.   turtle.turnRight()
  105.   ReturnBecauseEndOfRun()
  106. end
  107. -- reset to normal starting position
  108. turtle.turnRight()
  109. turtle.turnRight()
  110. turtle.forward()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement