Advertisement
postrach

Turtle quarry new

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