Pandana

BetterTunnel

Jan 26th, 2021 (edited)
932
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. width = 3
  2. heigth = 2
  3. sleep_time = 0.5
  4. torch_slot = 16
  5. distance_moved = 0
  6. y_offset = 0
  7. torch_distance = 6
  8. blocks_mined = 0
  9. torch_id = "minecraft:torch"
  10. -- All orientation are relative to the initial placement of the turtle.
  11. -- Forward is considered as north.
  12. -- north = 1
  13. -- east = 2
  14. -- south = 3
  15. -- west = 4
  16. orientation = 1
  17.  
  18. function BlockMined ()
  19.     blocks_mined = blocks_mined + 1
  20. end
  21.  
  22. function Dig ()
  23.     while turtle.detect() do
  24.         turtle.dig()
  25.         BlockMined()
  26.         os.sleep(sleep_time)
  27.     end
  28. end
  29.  
  30. function DigUp ()
  31.     while turtle.detectUp() do
  32.         turtle.digUp()
  33.         BlockMined()
  34.         os.sleep(sleep_time)
  35.     end
  36. end
  37.  
  38. function DigDown ()
  39.     while turtle.detectDown() do
  40.         turtle.digDown()
  41.         BlockMined()
  42.         os.sleep(sleep_time)
  43.     end
  44. end
  45.  
  46. function MoveX (distance)
  47.     for i = 1, distance, 1 do
  48.         turtle.forward()
  49.         if orientation == 1 then
  50.             distance_moved = distance_moved + 1
  51.         elseif orientation == 3 then
  52.             distance_moved = distance_moved - 1
  53.         end
  54.     end
  55. end
  56.  
  57. function Move ()
  58.     MoveX(1)
  59. end
  60.  
  61. -- This function does the same as Move, but clears the way if it is obstructed.
  62. function MoveAndCheck ()
  63.     if turtle.detect() then
  64.         Dig()
  65.     end
  66.     Move()
  67. end
  68.  
  69. -- This function does do the same as MoveX, but clears the way if it is obstructed.
  70. function MoveAndCheckX (distance)
  71.     for i = 1, distance, 1 do
  72.         MoveAndCheck()
  73.     end
  74. end
  75.  
  76. function Left ()
  77.     turtle.turnLeft()
  78.     if orientation == 1 then
  79.         orientation = 4
  80.     else
  81.         orientation = orientation - 1
  82.     end
  83. end
  84.  
  85. function Right ()
  86.     turtle.turnRight()
  87.     if orientation == 4 then
  88.         orientation = 1
  89.     else
  90.         orientation = orientation + 1
  91.     end
  92. end
  93.  
  94. function Reverse ()
  95.     Right()
  96.     Right()
  97. end
  98.  
  99. function UpX (distance)
  100.     for i = 1, distance, 1 do
  101.         turtle.up()
  102.         y_offset = y_offset + 1
  103.     end
  104. end
  105.  
  106. function Up ()
  107.     UpX(1)
  108. end
  109.  
  110. function UpAndCheck ()
  111.     if turtle.detectUp() then
  112.         DigUp()
  113.     end
  114.     Up()
  115. end
  116.  
  117. function UpAndCheckX (distance)
  118.     for i = 1, distance, 1 do
  119.         UpAndCheck()
  120.     end
  121. end
  122.  
  123. function DownX (distance)
  124.     for i = 1, distance, 1 do
  125.         turtle.down()
  126.         y_offset = y_offset - 1
  127.     end
  128. end
  129.  
  130. function Down ()
  131.     DownX(1)
  132. end
  133.  
  134. function DownAndCheck ()
  135.     if turtle.detectDown() then
  136.         DigDown()
  137.     end
  138.     Down()
  139. end
  140.  
  141. function DownAndCheckX (distance)
  142.     for i = 1, distance, 1 do
  143.         DownAndCheck()
  144.     end
  145. end
  146.  
  147. function PlaceTorch ()
  148.     Reverse()
  149.     local prev_slot = turtle.getSelectedSlot()
  150.     turtle.select(torch_slot)
  151.     turtle.place()
  152.     turtle.select(prev_slot)
  153.     Reverse()
  154. end
  155.  
  156. function UnloadInventory ()
  157.     for i = 1, 15, 1 do
  158.         turtle.select(i)
  159.         if not turtle.drop() then
  160.             print("Chest invntory full.\n")
  161.             while not turtle.drop() do
  162.                 os.sleep(sleep_time * 10)
  163.             end
  164.         end
  165.     end
  166. end
  167.  
  168. function BackTrack ()
  169.     if orientation == 1 then
  170.         -- EAST (x)
  171.         Right()
  172.         if not turtle.detect() then
  173.             Move()
  174.             Right()
  175.             MoveAndCheck()
  176.         else
  177.             -- WEST (x)
  178.             Reverse()
  179.             if not turtle.detect() then
  180.                 Move()
  181.                 Left()
  182.                 MoveAndCheck()
  183.                 Left()
  184.                 -- This is in case there's a torch in the way.
  185.                 if turtle.detect() then
  186.                     UpAndCheck()
  187.                     MoveAndCheckX(2)
  188.                     DownAndCheck()
  189.                 end
  190.                 MoveAndCheckX(2)
  191.                 Right()
  192.             else
  193.                 -- SOUTH (x-1)
  194.                 Left()
  195.                 if not turtle.detect() then
  196.                     Move()
  197.                     Left()
  198.                     MoveAndCheck()
  199.                     Right()
  200.                 else
  201.                     UpAndCheck()
  202.                     MoveAndCheck()
  203.                     Left()
  204.                     MoveAndCheck()
  205.                     Right()
  206.                 end
  207.             end
  208.         end
  209.         MoveAndCheckX(distance_moved)
  210.     end
  211. end
  212.  
  213. function DigYLayer ()
  214.     DigUp()
  215.     Up()
  216.     Left()
  217.     Dig()
  218.     Reverse()
  219.     Dig()
  220.     Left()
  221. end
  222.  
  223. function DigBottomLayer ()
  224.     Dig()
  225.     Move()
  226.     Left()
  227.     Dig()
  228.     Reverse()
  229.     Dig()
  230.     Left()
  231. end
  232.  
  233. function DigLayer ()
  234.     DigBottomLayer()
  235.     for i = 1, heigth - 1, 1 do
  236.         DigYLayer()
  237.     end
  238.     DownX(y_offset)
  239. end
  240.  
  241. function DigTunnel (distance)
  242.     local distance_since_last_torch = 0
  243.     for i = 1, distance, 1 do
  244.         DigLayer()
  245.         if distance_since_last_torch == torch_distance then
  246.             PlaceTorch()
  247.         end
  248.     end
  249.     BackTrack()
  250. end
  251.  
  252. function Startup ()
  253.     if arg[1] then
  254.         distance = arg[1]
  255.     else
  256.         distance = 75
  257.     end
  258.    
  259.     turtle.select(torch_slot)
  260.     local _, data = turtle.getItemDetail()
  261.     if data then
  262.         if data.name == torch_id then
  263.             local torches_needed = math.floor(distance / torch_distance)
  264.             if data.count < math.floor(distance / torch_distance) then
  265.                 print(string.format("Please insert &d more torches and hit enter.\n",torches_needed - data.count))
  266.                 io.read()
  267.             end
  268.         end
  269.     end
  270.     DigTunnel(distance)
  271. end
  272.  
  273. Startup()
RAW Paste Data