postrach

Turtle quarry new

Oct 31st, 2020
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --Edit these values for the dimensions to dig out----------------------
  2.  
  3. width = 55
  4. length = 55
  5. height = 4
  6.  
  7. --[[--------Setup----------------------
  8. 1. Dig out a 5x5 square to work in
  9.     (The turtle will not dig this area!)
  10. 2. Place the turtle facing out at the
  11.     center of any edge
  12. 3. Place a chest of coal/charcoal
  13.     to the left of the turtle
  14. 4. Place a chest of vanilla wood items
  15.     (logs, planks, sticks) or
  16.     torches to the right.
  17. 5. Place storage chests facing
  18.     the turtle to the left
  19.     and the right of the
  20.     center of the 5x5
  21.     You can place the chests as high
  22.     as you like for more storage
  23. ---Example---
  24. t = turtle
  25. c = chest
  26. x = empty spot
  27. O = center of the area to dig
  28.  
  29. x c t c x
  30. x x x x x
  31. c c O c c
  32. x x x x x
  33. x x x x x
  34. --]]
  35.  
  36. --[[ Enter Starting Position ]]
  37.  
  38. StartingRadius = 3
  39. StartingHeight = 1
  40.  
  41. --[[ Defaults ]]
  42. --StartingRadius = 3
  43. --StartingHeight = 1
  44. --------------------------------------------------------
  45.  
  46.  
  47.  
  48. dir = "z"
  49. facing = 1
  50. x = 0
  51. z = 2
  52. y = 1
  53.  
  54. xmax = math.floor(width/2)
  55. xmin = 0-math.floor(width/2)
  56. zmax = math.floor(length/2)
  57. zmin = 0-math.floor(length/2)
  58.  
  59. stop = 0
  60. returning = 0
  61.  
  62. function refuel()
  63.     if turtle.getFuelLevel() < ((width + length + height) * 2) then
  64.         turtle.select(16)
  65.         if turtle.refuel(1) then
  66.             if turtle.getItemCount(16) < 2 then
  67.                 print "Low on Fuel. Restocking..."
  68.                 home()
  69.                 restock()
  70.                 goBack()
  71.             end
  72.         else
  73.             print "Low on Fuel. Restocking..."
  74.             home()
  75.             restock()
  76.             goBack()
  77.         end
  78.         turtle.select(1)
  79.     end
  80. end
  81.  
  82. function forward()
  83.     local dig = 1
  84.     torch = false
  85.   if returning == 0 then
  86.     refuel()
  87.   end
  88.  
  89.   if (z % 7) == 0 and (x % 7) == 0 then
  90.     torch = true
  91.     turnLeft()
  92.     turnLeft()
  93.     turtle.dig()
  94.     turnLeft()
  95.     turnLeft()
  96.     if turtle.getItemCount(15) < 5 and returning == 0 then
  97.         home()
  98.         restock()
  99.         goBack()
  100.     end
  101.   end
  102.  
  103.   if dir == "x" then
  104.     if facing == 1 and x == xmax then
  105.         dig = 0
  106.     end
  107.     if facing == -1 and x == xmin then
  108.         dig = 0
  109.     end
  110.   end
  111.   if dir == "z" then
  112.     if facing == 1 and z == zmax then
  113.         dig = 0
  114.     end
  115.     if facing == -1 and z == zmin then
  116.         dig = 0
  117.     end
  118.   end
  119.   while not turtle.forward() and dig == 1 do
  120.     turtle.dig()
  121.     turtle.attack()
  122.   end
  123.   if torch == true and y == 1 then
  124.     turnLeft()
  125.     turnLeft()
  126.     turtle.select(15)
  127.     turtle.place()
  128.     turtle.select(1)
  129.     turnLeft()
  130.     turnLeft()
  131.     torch = false
  132.   end
  133.   if dir == "z" then
  134.     z = z + facing
  135.   elseif dir == "x" then
  136.     x = x + facing
  137.   end
  138.  
  139.   if y == 1 then
  140.     turtle.select(1)
  141.     turtle.placeDown()
  142.   end
  143.  
  144.   if y == height then
  145.     turtle.placeUp()
  146.   end
  147.  
  148.  
  149.  
  150.   if returning == 0 then
  151.       if turtle.getItemCount(14) > 0 then
  152.         print "Inventory Full. Dropping off Items..."
  153.         home()
  154.         restock()
  155.         goBack()
  156.       end
  157.   end
  158. end
  159.  
  160. function turnRight()
  161.     turtle.turnRight()
  162.     if dir == "z" then
  163.         dir = "x"
  164.     elseif dir == "x" then
  165.         dir = "z"
  166.         if facing == 1 then
  167.             facing = -1
  168.         elseif facing == -1 then
  169.             facing = 1
  170.         end
  171.     end
  172. end
  173.  
  174. function turnLeft()
  175.     turtle.turnLeft()
  176.     if dir == "z" then
  177.         dir = "x"
  178.         if facing == 1 then
  179.             facing = -1
  180.         elseif facing == -1 then
  181.             facing = 1
  182.         end
  183.     elseif dir == "x" then
  184.         dir = "z"
  185.     end
  186. end
  187.  
  188. function up()
  189.     while turtle.up() == false do
  190.         turtle.digUp()
  191.     end
  192.     y = y + 1
  193. end
  194.  
  195. function down()
  196.     while turtle.down() == false do
  197.         turtle.digDown()
  198.     end
  199.     y = y - 1
  200. end
  201.  
  202. function dig()
  203.     if y < height then
  204.         if inspectUp("minecraft:flowing_water") or inspectUp("minecraft:water") or inspectUp("minecraft:lava") or inspectUp("minecraft:flowing_lava") then
  205.             up()
  206.             if inspectUp("minecraft:flowing_water") or inspectUp("minecraft:water") or inspectUp("minecraft:lava") or inspectUp("minecraft:flowing_lava") then
  207.                 turtle.placeUp()
  208.             end
  209.             for i=1,4 do
  210.                 if inspect("minecraft:flowing_water") or inspect("minecraft:water") or inspect("minecraft:lava") or inspect("minecraft:flowing_lava") then
  211.                     turtle.place()
  212.                     turtle.dig()
  213.                 end
  214.                 turnRight()
  215.             end
  216.             down()
  217.         end
  218.         while turtle.digUp() do
  219.         end
  220.         if inspectUp("minecraft:gravel") or inspectUp("minecraft:sand") then
  221.             while turtle.digUp() do
  222.                 sleep(1)
  223.             end
  224.         end
  225.     end
  226. end
  227. function home()
  228.     oldx = x
  229.     oldz = z
  230.     oldy = y
  231.     olddir = dir
  232.     oldfacing = facing
  233.    
  234.     returning = 1
  235.    
  236.     while y > 1 do
  237.         down()
  238.     end
  239.    
  240.     if dir == "z" then
  241.         if facing == 1 then
  242.             if z > 3 then
  243.                 turnRight()
  244.                 turnRight()
  245.                 while z > 3 do
  246.                     forward()
  247.                 end
  248.                 turnLeft()
  249.                 while x < 0 do
  250.                     forward()
  251.                 end
  252.                 turnRight()
  253.                 forward()
  254.             elseif z < 3 then
  255.                 turnRight()
  256.                 forward()
  257.                 turnLeft()
  258.                 while z < 3 do
  259.                     forward()
  260.                 end
  261.                 turnRight()
  262.                 while x < 0 do
  263.                     forward()
  264.                 end
  265.                 turnRight()
  266.                 forward()
  267.             elseif z == 3 then
  268.                 turnRight()
  269.                 while x < 0 do
  270.                     forward()
  271.                 end
  272.                 turnRight()
  273.                 forward()
  274.             end
  275.         elseif facing == -1 then
  276.             if z < 3 then
  277.                 turnRight()
  278.                 turnRight()
  279.                 while z < 3 do
  280.                     forward()
  281.                 end
  282.                 turnLeft()
  283.                 while x > 0 do
  284.                     forward()
  285.                 end
  286.                 turnLeft()
  287.                 forward()
  288.             elseif z > 3 then
  289.                 turnRight()
  290.                 forward()
  291.                 turnLeft()
  292.                 while z > 3 do
  293.                     forward()
  294.                 end
  295.                 turnRight()
  296.                 while x > 0 do
  297.                     forward()
  298.                 end
  299.                 turnLeft()
  300.                 forward()
  301.             elseif z == 3 then
  302.                 turnLeft()
  303.                 while x < 0 do
  304.                     forward()
  305.                 end
  306.                 turnLeft()
  307.                 forward()
  308.             end
  309.         end
  310.     elseif dir == "x" then
  311.         if facing == 1 then
  312.             if x > 0 then
  313.                 turnRight()
  314.                 turnRight()
  315.                 while x > 0 do
  316.                     forward()
  317.                 end
  318.                 turnLeft()
  319.                 while z > 2 do
  320.                     forward()
  321.                 end
  322.             elseif x < 0 then
  323.                 turnRight()
  324.                 forward()
  325.                 turnLeft()
  326.                 while x < 0 do
  327.                     forward()
  328.                 end
  329.                 turnRight()
  330.                 while z > 2 do
  331.                     forward()
  332.                 end
  333.             elseif x == 0 then
  334.                 turnRight()
  335.                 while z > 2 do
  336.                     forward()
  337.                 end
  338.             end
  339.         elseif facing == -1 then
  340.             if x > 3 then
  341.                 turnRight()
  342.                 forward()
  343.                 turnLeft()
  344.                 while x > 3 do
  345.                     forward()
  346.                 end
  347.                 turnRight()
  348.                 while z < 3 do
  349.                     forward()
  350.                 end
  351.                 turnLeft()
  352.                 while x > 0 do
  353.                     forward()
  354.                 end
  355.                 turnLeft()
  356.                 forward()
  357.             elseif x < 3 then
  358.                 turnRight()
  359.                 turnRight()
  360.                 while x < 3 do
  361.                     forward()
  362.                 end
  363.                 turnLeft()
  364.                 while z < 3 do
  365.                     forward()
  366.                 end
  367.                 turnLeft()
  368.                 while x > 0 do
  369.                     forward()
  370.                 end
  371.                 turnLeft()
  372.                 forward()
  373.             end
  374.         end
  375.     end
  376.     turnLeft()
  377.     turnLeft()
  378.     returning = 0
  379. end
  380.  
  381. function dropoff()
  382.     for i=2,14 do
  383.         turtle.select(i)
  384.         if turtle.drop() == false and turtle.getItemCount() > 0 then
  385.             return false
  386.         end
  387.     end
  388.     return true
  389. end
  390.  
  391. function inspect(block)
  392.     p1, p2 = turtle.inspect()
  393.     if p1 == true then
  394.         for k,v in pairs(p2) do
  395.             if v == block then
  396.                 return true
  397.             else
  398.                 return false
  399.             end
  400.         end
  401.     else
  402.         return false
  403.     end
  404. end
  405.  
  406. function inspectUp(block)
  407.     p1, p2 = turtle.inspectUp()
  408.     if p1 == true then
  409.         for k,v in pairs(p2) do
  410.             if v == block then
  411.                 return true
  412.             else
  413.                 return false
  414.             end
  415.         end
  416.     else
  417.         return false
  418.     end
  419. end
  420.  
  421. function detail(item)
  422.     local data = turtle.getItemDetail()
  423.    
  424.     if data then
  425.         if data.name == item then
  426.             return true
  427.         else
  428.             return false
  429.         end
  430.     else
  431.         return false
  432.     end
  433. end
  434.  
  435. function stick()
  436.     turtle.select(1)
  437.     turtle.transferTo(5)
  438.     turnLeft()
  439.     turnLeft()
  440.     turtle.suck()
  441.     turtle.craft()
  442.     if detail("minecraft:coal") or detail("minecraft:charcoal") then
  443.         turtle.drop()
  444.         turtle.select(2)
  445.         turtle.transferTo(1)
  446.         turtle.select(1)
  447.     end
  448.     turnLeft()
  449.     turnLeft()
  450.     turtle.transferTo(15)
  451. end
  452.  
  453. function planks()
  454.     turtle.select(1)
  455.     turtle.transferTo(5,(math.floor(turtle.getItemCount()/2)))
  456.     turtle.craft()
  457.     if detail("minecraft:planks") then
  458.         turtle.drop()
  459.         turtle.select(5)
  460.         turtle.drop()
  461.         turtle.select(2)
  462.         turtle.transferTo(1)
  463.     end
  464.     stick()
  465. end
  466.  
  467. function log()
  468.     turtle.select(1)
  469.     turtle.craft()
  470.     if detail("minecraft:log") then
  471.         turtle.drop()
  472.         turtle.select(2)
  473.         turtle.transferTo(1)
  474.     end
  475.     planks()
  476. end
  477.  
  478. function restock()
  479.     local empty = 0
  480.     returning = 1
  481.     turnLeft()
  482.     turnLeft()
  483.     forward()
  484.     turnRight()
  485.     forward()
  486.     turnLeft()
  487.     while inspect("minecraft:chest") == true and empty == 0 do
  488.         if dropoff() == true then
  489.             empty = 1
  490.         else
  491.             up()
  492.         end
  493.     end
  494.    
  495.     if empty == 0 then
  496.         while y > 1 do
  497.             down()
  498.         end
  499.         turnLeft()
  500.         forward()
  501.         forward()
  502.         turnRight()
  503.         while inspect("minecraft:chest") == true and empty == 0 do
  504.             if dropoff() == true then
  505.                 empty = 1
  506.             else
  507.                 up()
  508.             end
  509.         end
  510.     end
  511.    
  512.     if empty == 1 then
  513.         while y > 1 do
  514.             down()
  515.         end
  516.         if x > 0 then
  517.             turnRight()
  518.             forward()
  519.             turnRight()
  520.         else
  521.             turnLeft()
  522.             forward()
  523.             turnLeft()
  524.         end
  525.         forward()
  526.     end
  527.    
  528.     if empty == 0 then
  529.         shell.exit()
  530.         print "WARNING! No more storage: Shutting down..."
  531.         print "Press any key to end."
  532.     end
  533.     if turtle.getItemCount(15) < 60 then
  534.         turnLeft()
  535.         turtle.select(16)
  536.         turtle.drop()
  537.         turnRight()
  538.         turtle.select(1)
  539.         turtle.drop()
  540.         turtle.select(15)
  541.         turnRight()
  542.         turtle.drop()
  543.         turtle.select(1)
  544.         while turtle.getItemCount(15) < 60 do
  545.             turtle.select(15)
  546.             turtle.drop()
  547.             turtle.select(1)
  548.             turtle.suck()
  549.             if detail("minecraft:torch") then
  550.                 turtle.transferTo(15)
  551.             elseif detail("minecraft:stick") then
  552.                 stick()
  553.             elseif detail("minecraft:planks") then
  554.                 planks()
  555.             elseif detail("minecraft:log") then
  556.                 log()
  557.             end
  558.         end
  559.         dropoff()
  560.         turnLeft()
  561.         turtle.select(1)
  562.         turtle.suck()
  563.     end
  564.     if turtle.getItemCount(16) < 60 then
  565.         turnLeft()
  566.         turtle.select(16)
  567.         while turtle.getItemCount() < 60 do
  568.             turtle.suck(5)
  569.         end
  570.         turnRight()
  571.         turtle.select(1)
  572.     end
  573.     returning = 0
  574. end
  575.  
  576. function goBack()
  577.     returning = 1
  578.     forward()
  579.     turtle.select(1)
  580.     turtle.suck()
  581.     if oldx > 0 then
  582.         turnRight()
  583.         while x < oldx do
  584.             forward()
  585.         end
  586.         turnLeft()
  587.     elseif oldx < 0 then
  588.         turnLeft()
  589.         while x > oldx do
  590.             forward()
  591.         end
  592.         turnRight()
  593.     end
  594.     if x > -3 and x < 3 and oldz < 3 then
  595.         turnRight()
  596.         while x < 3 do
  597.             forward()
  598.         end
  599.         turnLeft()
  600.     end
  601.     if oldz > 0 then
  602.         while z < oldz do
  603.             forward()
  604.         end
  605.     elseif oldz < 0 then
  606.         turnLeft()
  607.         turnLeft()
  608.         while z > oldz do
  609.             forward()
  610.         end
  611.         turnLeft()
  612.         turnLeft()
  613.     end
  614.     if x > oldx then
  615.         turnLeft()
  616.         while x > oldx do
  617.             forward()
  618.         end
  619.         turnRight()
  620.     end
  621.     while dir ~= olddir or facing ~= oldfacing do
  622.         turnRight()
  623.     end
  624.     while y < oldy do
  625.         up()
  626.     end
  627.     returning = 0
  628. end
  629.  
  630. levels = math.ceil(height/2)
  631.  
  632. goback = 0
  633. for i=1,levels do
  634.     while y < StartingHeight do
  635.         up()
  636.     end
  637.     while y < ((i*2)-1) do
  638.         up()
  639.     end
  640.     ring = 6
  641.     if StartingRadius > 3 then
  642.         if goback == 0 then
  643.             goback = 1
  644.             print "Starting Radius set higher than 3"
  645.             print "Moving to new start"
  646.             ring = StartingRadius * 2
  647.             while z < StartingRadius and z < zmax do
  648.                 forward()
  649.                 dig()
  650.             end
  651.             turnRight()
  652.             dig()
  653.             while x < StartingRadius and x < xmax do
  654.                 forward()
  655.                 dig()
  656.             end
  657.             turnRight()
  658.         end
  659.     else
  660.         dig()
  661.         forward()
  662.         dig()
  663.         turnLeft()
  664.         forward()
  665.         dig()
  666.         forward()
  667.         dig()
  668.         turnRight()
  669.         turnRight()
  670.         while x < 3 do
  671.           forward()
  672.           dig()
  673.         end
  674.         turnRight()
  675.     end
  676.    
  677.    
  678.  
  679.     while z < zmax or x > xmin or stop == 0 do
  680.       for i=1,(ring) do
  681.         if z > zmin then
  682.           forward()
  683.           dig()
  684.         end
  685.       end
  686.       turnRight()
  687.       for i=1,(ring) do
  688.         if x > xmin then
  689.           forward()
  690.           dig()
  691.         end
  692.       end
  693.       turnRight()
  694.       ring = ring + 1
  695.       for i=1,(ring) do
  696.         if z < zmax then
  697.           forward()
  698.           dig()
  699.         end
  700.       end
  701.       turnRight()
  702.       if x == xmin and z == zmax then
  703.         stop = 1
  704.       end
  705.       for i=1,(ring) do
  706.         if x < xmax and stop == 0 then
  707.           forward()
  708.           dig()
  709.         end
  710.       end
  711.       turnRight()
  712.       ring = ring + 1
  713.     end
  714.  
  715.     turnLeft()
  716.     turnLeft()
  717.     home()
  718.     restock()
  719.  
  720. end
  721. print ("Finished Clearing Area!")
  722. print ("Mined an area " .. length .. " by " .. width .. " by " .. height .. ".")
  723. print ("Mined a total area of " .. (length * width * height) .. " blocks!")
Add Comment
Please, Sign In to add comment