lazypanda07

customTunnel

Nov 6th, 2020 (edited)
2,082
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.95 KB | None | 0 0
  1. args = {...}
  2. totalPath = 0
  3.  
  4. -- put excluded block in 15 and 16 slots
  5. -- turtle will return after fill 14 slots
  6.  
  7. -- exclude block in last slot(16)
  8. function excludeItemForward()
  9.     turtle.select(16)
  10.  
  11.     return turtle.compare()
  12. end
  13.  
  14. -- exclude block in last slot(16)
  15. function excludeItemTop()
  16.     turtle.select(16)
  17.  
  18.     return turtle.compareUp()
  19. end
  20.  
  21. function dropExcludeItem()
  22.     turtle.select(15)
  23.  
  24.     turtle.drop(1)
  25. end
  26.  
  27. function overflowForward()
  28.     for i=1,14 do
  29.         turtle.select(i)
  30.  
  31.         if turtle.getItemSpace(i) ~= 0 then
  32.             if turtle.compare() or turtle.getItemCount(i) == 0 then
  33.                 return false
  34.             end
  35.         end
  36.     end
  37.  
  38.     return true
  39. end
  40.  
  41. function overflowTop()
  42.     for i=1,14 do
  43.         turtle.select(i)
  44.  
  45.         if turtle.getItemSpace(i) ~= 0 then
  46.             if turtle.compareUp() or turtle.getItemCount(i) == 0 then
  47.                 return false
  48.             end
  49.         end
  50.     end
  51.  
  52.     return true
  53. end
  54.  
  55. function digForward()
  56.     local isDropItem = excludeItemForward()
  57.  
  58.     if overflowForward() then
  59.         return false
  60.     end
  61.  
  62.     turtle.select(1)
  63.  
  64.     if turtle.detect() then
  65.         if isDropItem then
  66.             turtle.select(15)
  67.         end
  68.         turtle.dig()    
  69.     end
  70.  
  71.     if isDropItem then
  72.         dropExcludeItem()
  73.     end
  74.  
  75.     while turtle.forward() == false do
  76.         if turtle.detect() then
  77.             turtle.dig()    
  78.         end
  79.     end
  80.  
  81.     return true
  82. end
  83.  
  84. function digTop()
  85.     local isDropItem = excludeItemTop()
  86.  
  87.     if overflowTop() then
  88.         return false
  89.     end
  90.  
  91.     turtle.select(1)
  92.  
  93.     if turtle.detectUp() then
  94.         if isDropItem then
  95.             turtle.select(15)
  96.         end
  97.         turtle.digUp()    
  98.     end
  99.  
  100.     if isDropItem then
  101.         dropExcludeItem()
  102.     end
  103.  
  104.     while turtle.up() == false do
  105.         if turtle.detectUp() then
  106.             turtle.digUp()    
  107.         end
  108.     end
  109.  
  110.     return true
  111. end
  112.  
  113. function turnAround()
  114.     turtle.turnRight()
  115.     turtle.turnRight()
  116. end
  117.  
  118. function returnBackStart()
  119.     turnAround()
  120.  
  121.     for i=1,totalPath do
  122.         while turtle.forward() == false do
  123.  
  124.         end
  125.     end
  126.  
  127.     for i=1,14 do
  128.         turtle.select(i)
  129.  
  130.         turtle.drop()
  131.     end
  132.  
  133.     turnAround()
  134. end
  135.  
  136. function returnBackTop(currentHeight)
  137.     for i=1,currentHeight - 1 do
  138.         turtle.down()
  139.     end
  140.  
  141.     returnBackStart()
  142. end
  143.  
  144. function returnBack(widthFromCenter, currentHeight, side)
  145.     turnAround()
  146.  
  147.     for i=1,widthFromCenter do
  148.         while turtle.forward() == false do
  149.        
  150.         end
  151.     end
  152.  
  153.     if side == "left" then
  154.         turtle.turnRight()        
  155.     elseif side == "right" then
  156.         turtle.turnLeft()
  157.     end
  158.  
  159.     for i=1,currentHeight - 1 do
  160.         while turtle.down() == false do
  161.  
  162.         end
  163.     end
  164.  
  165.     for i=1,totalPath do
  166.         while turtle.forward() == false do
  167.        
  168.         end
  169.     end
  170.  
  171.     for i=1,14 do
  172.         turtle.select(i)
  173.  
  174.         turtle.drop()
  175.     end
  176.  
  177.     turnAround()
  178.  
  179. end
  180.  
  181. if #args ~= 2 then
  182.     print("Must be 2 arguments")
  183.     return
  184. end
  185.  
  186. local width = tonumber(args[1])
  187. local height = tonumber(args[2])
  188. local isEnd = false
  189.  
  190. width = math.floor(width / 2)
  191.  
  192. while not isEnd do
  193.     if digForward() then
  194.         totalPath = totalPath + 1
  195.     else
  196.         returnBackStart()
  197.  
  198.         isEnd = true
  199.  
  200.         break
  201.  
  202.         -- return back
  203.     end
  204.  
  205.     if isEnd then
  206.         break
  207.     end
  208.  
  209.     for i=1,height do
  210.         turtle.turnRight()
  211.  
  212.         for j=1,width do
  213.             if not digForward() then
  214.                 returnBack(j - 1, i, "right")
  215.  
  216.                 isEnd = true
  217.  
  218.                 break
  219.  
  220.                 -- return back
  221.             end
  222.         end
  223.  
  224.         if isEnd then
  225.             break
  226.         end
  227.  
  228.         turnAround()
  229.  
  230.         for j=1,width do
  231.             while turtle.forward() == false do
  232.                 digForward()
  233.  
  234.                 turtle.attack()
  235.  
  236.                 break
  237.             end
  238.         end
  239.  
  240.         for j=1,width do
  241.             if not digForward() then
  242.                 returnBack(j - 1, i, "left")
  243.  
  244.                 isEnd = true
  245.  
  246.                 break
  247.  
  248.                 -- return back
  249.             end
  250.            
  251.         end
  252.  
  253.         if isEnd then
  254.             break
  255.         end
  256.  
  257.         turnAround()
  258.  
  259.         for j=1,width do
  260.             while turtle.forward() == false do
  261.                 digForward()
  262.  
  263.                 turtle.attack()
  264.  
  265.                 break
  266.             end
  267.         end
  268.  
  269.         turtle.turnLeft()
  270.  
  271.         if i ~= height then
  272.             if not digTop() then
  273.                 returnBackTop(i)
  274.  
  275.                 isEnd = true
  276.  
  277.                 break
  278.  
  279.                 -- return back
  280.             end
  281.            
  282.         else
  283.             for j=1,height - 1 do
  284.                 while turtle.down() == false do
  285.  
  286.                 end
  287.             end
  288.         end
  289.  
  290.     end
  291.  
  292. end
  293.  
Add Comment
Please, Sign In to add comment