Advertisement
Guest User

quarry

a guest
May 25th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.49 KB | None | 0 0
  1. print("enter the size (NxN):")
  2.  
  3. n = tonumber(read())
  4.  
  5. per_layer = n*n
  6.  
  7. x = 0
  8. y = 0
  9. z = 0
  10.  
  11. depth = 0
  12.  
  13. dir = 0
  14.  
  15. function updatePos()
  16.     if dir == 0 then
  17.         y = y + 1
  18.     elseif dir == 1 then
  19.         x = x + 1
  20.     elseif dir == 2 then
  21.         y = y - 1
  22.     elseif dir == 3 then
  23.         x = x - 1
  24.     end
  25. end
  26.  
  27. function dig()
  28.     turtle.digUp()
  29.     turtle.digDown()
  30. end
  31.  
  32. function refuel()
  33.     local fuel = turtle.getFuelLevel()
  34.  
  35.     if fuel == 0 then
  36.         turtle.refuel(1)
  37.     end
  38. end
  39.  
  40. function forward()
  41.     refuel()
  42.  
  43.     turtle.dig()
  44.  
  45.     while turtle.attack() do
  46.         turtle.attack()
  47.     end
  48.  
  49.     turtle.forward()
  50.  
  51.     updatePos()
  52. end
  53.  
  54. function right()
  55.     turtle.turnRight()
  56.  
  57.     if dir == 3 then
  58.         dir = 0
  59.     else
  60.         dir = dir + 1
  61.     end
  62. end
  63.  
  64. function left()
  65.     turtle.turnLeft()
  66.  
  67.     if dir == 0 then
  68.         dir = 3
  69.     else
  70.         dir = dir - 1
  71.     end
  72. end
  73.  
  74. function down()
  75.     local success, data = turtle.inspectDown()
  76.  
  77.     if success then
  78.         if data.name == "minecraft:bedrock" then
  79.             return false
  80.         end
  81.     end
  82.  
  83.     refuel()
  84.  
  85.     turtle.digDown()
  86.  
  87.     while turtle.attackDown() do
  88.         turtle.attackDown()
  89.     end
  90.  
  91.     turtle.down()
  92.  
  93.     z = z - 1
  94.  
  95.     return true
  96. end
  97.  
  98. function up()
  99.     refuel()
  100.  
  101.     turtle.digUp()
  102.  
  103.     while turtle.attackDown() do
  104.         turtle.attackDown()
  105.     end
  106.  
  107.     turtle.up()
  108.  
  109.     z = z + 1
  110. end
  111.  
  112. function deposit()
  113.     for i=2,16 do
  114.         turtle.select(i)
  115.         turtle.drop()
  116.     end
  117. end
  118.  
  119. function getFuel()
  120.     turtle.select(1)
  121.  
  122.     count = turtle.getItemCount()
  123.  
  124.     if count ~= 64 then
  125.         turtle.suck(64 - count)
  126.     end
  127. end
  128.  
  129. function returnToChest()
  130.     if dir == 0 then
  131.         left()
  132.     elseif dir == 1 then
  133.         right()
  134.         right()
  135.     elseif dir == 2 then
  136.         right()
  137.     end
  138.  
  139.     for i=1,x do
  140.         forward()
  141.     end
  142.  
  143.     left()
  144.  
  145.     for i=1,y do
  146.         forward()
  147.     end
  148.  
  149.     right()
  150.     right()
  151.  
  152.     for i=z,-1 do
  153.         up()
  154.     end
  155.  
  156.     right()
  157.     right()
  158.  
  159.     deposit()
  160.  
  161.     right()
  162.  
  163.     getFuel()
  164.  
  165.     right()
  166. end
  167.  
  168. function returnToDepth()
  169.     for i=1,depth do
  170.         down()
  171.     end
  172. end
  173.  
  174. function shouldReturn()
  175.     if (turtle.getItemCount(1)-1) * 80 < per_layer + x + y + z + 1 then
  176.         return true
  177.     end
  178.  
  179.     stacks_needed = math.floor(per_layer / 64)
  180.  
  181.     if turtle.getItemCount(16 - stacks_needed) ~= 0 then
  182.         return true
  183.     end
  184.  
  185.     return false
  186. end
  187.  
  188. while true do
  189.     for x=1,n do
  190.         for y=1,n do
  191.             dig()
  192.  
  193.             if y ~= n then
  194.                 forward()
  195.             end
  196.         end
  197.  
  198.         if x ~= n then
  199.             if x % 2 == 1 then
  200.                 right()
  201.                 forward()
  202.                 right()
  203.             else
  204.                 left()
  205.                 forward()
  206.                 left()
  207.             end
  208.         end
  209.     end
  210.  
  211.     if shouldReturn() then
  212.         returnToChest()
  213.         returnToDepth()
  214.     else
  215.         if dir == 0 then
  216.             left()
  217.         elseif dir == 1 then
  218.             right()
  219.             right()
  220.         elseif dir == 2 then
  221.             right()
  222.         end
  223.  
  224.         for i=1,x do
  225.             forward()
  226.         end
  227.  
  228.         left()
  229.  
  230.         for i=1,y do
  231.             forward()
  232.         end
  233.  
  234.         right()
  235.         right()
  236.     end
  237.  
  238.     if not down() then
  239.         returnToChest()
  240.         break
  241.     end
  242.  
  243.     depth = depth + 1
  244. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement