Guest User

urgot.lua

a guest
Apr 9th, 2020
455
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Start timer
  2. local startTime = os.clock()
  3.  
  4. -- Take and parse arguments
  5. local arg1, arg2, arg3, arg4 = ...
  6.  
  7. -- Validate arguments
  8. if arg1 == nil or arg2 == nil then
  9.   print("- Not enough arguments provided. Width, height are required.")
  10.   return
  11. end
  12.  
  13. if arg3 == nil then
  14.   arg3 = "255"
  15. end
  16.  
  17. local width = tonumber(arg1)
  18. local height = tonumber(arg2)
  19. local depth = tonumber(arg3)
  20. local layersToSkip = tonumber(arg4)
  21.  
  22. if not (depth % 3 == 0) then
  23.   print("- Depth should be a multiple of 3 for maximum efficiency.")
  24.   return
  25. end
  26.  
  27. -- Plan fuel necessary for each layer
  28. local fuelNeededForEachLayer = width * height + (height % 2 == 0 and height or width) + 3
  29.  
  30. -- Check if enough fuel has been provided
  31. print("- This mine seems to require " ..  math.floor((fuelNeededForEachLayer / 80) * 100) / 100 .. " coal items, checking if enough have been provided...")
  32.  
  33. for i = 1, 16 do
  34.   turtle.select(i)
  35.   turtle.refuel()
  36. end
  37.  
  38. -- Reset selected slot
  39. turtle.select(1)
  40.  
  41. if turtle.getFuelLevel() < fuelNeededForEachLayer then
  42.   print("- Not enough fuel provided, quitting.")
  43.   return
  44. end
  45.  
  46. -- Declare values used for mining
  47. local distanceMovedAlongZ = 0
  48. local distanceMovedAlongY = 0
  49. local distanceMovedAlongX = 0
  50.  
  51. -- Check if a skip has been requested
  52. if not (layersToSkip == nil) then
  53.   print("- A skip has been requested.")
  54.   -- For each layer to skip we go down 3 times and store this movement
  55.   for i = 1, layersToSkip do
  56.     for temp = 1, 3 do
  57.       while turtle.inspectDown() do
  58.         turtle.digDown()
  59.       end
  60.       while true do
  61.         if turtle.down() then
  62.           break
  63.         end
  64.       end
  65.       distanceMovedAlongZ = distanceMovedAlongZ + 1
  66.       depth = depth - 1
  67.     end
  68.     print("Skipped a layer.")
  69.   end
  70. else
  71.   print("- No layers to skip.")
  72. end
  73.  
  74. -- Begin mining loop
  75. for z = 1, depth - 1, 3 do
  76.   -- Check if we have enough fuel
  77.   if turtle.getFuelLevel() < fuelNeededForEachLayer + distanceMovedAlongZ then
  78.     -- We don't have enough fuel
  79.     print("- Not enough fuel, trying to refuel using all fuel in own inventory...")
  80.  
  81.     for i = 1, 16 do
  82.       turtle.select(i)
  83.       turtle.refuel()
  84.     end
  85.  
  86.     -- Reset selection back to first slot
  87.     turtle.select(1)
  88.  
  89.     -- Did we get anything?
  90.     if turtle.getFuelLevel() < fuelNeededForEachLayer + distanceMovedAlongZ then
  91.       -- We didn't
  92.       print("- There wasn't enough fuel in the inventory to get through this layer, coming back up...")
  93.  
  94.       for i = 1, distanceMovedAlongZ do
  95.         while turtle.inspectUp() do
  96.           turtle.digUp()
  97.         end
  98.         while true do
  99.           if turtle.up() then
  100.             break
  101.           end
  102.         end
  103.       end
  104.  
  105.       print("- Quitting because of no fuel :(")
  106.  
  107.       return
  108.     end
  109.   end
  110.  
  111.   -- Check if we have enough inventory space
  112.   local freeSlots = 0
  113.   for i = 1, 16 do
  114.     if turtle.getItemSpace(i) == 64 then
  115.       freeSlots = freeSlots + 1
  116.     end
  117.   end
  118.  
  119.   if freeSlots < 2 then
  120.     print("- There aren't enough free slots, coming back up to empty inventory...")
  121.  
  122.     -- Move back up and dump everything to the chest
  123.     for i = 1, distanceMovedAlongZ do
  124.       while turtle.inspectUp() do
  125.         turtle.digUp()
  126.       end
  127.       while true do
  128.         if turtle.up() then
  129.           break
  130.         end
  131.       end
  132.     end
  133.  
  134.     -- Turn towards chest
  135.     turtle.turnRight()
  136.     turtle.turnRight()
  137.    
  138.     -- Cycle through every slot in the turtle's inventory
  139.     for i = 1, 16 do
  140.       turtle.select(i)
  141.  
  142.       -- Try to drop it only if it has items
  143.       if turtle.getItemCount(i) > 0 then
  144.         -- Check if drop onto chest was actually successful
  145.         if not turtle.drop() then
  146.           -- If it wasn't, chest was full
  147.           print("- Chest seems to be full, quitting.")
  148.  
  149.           -- Turn around to face start positio again before quitting
  150.           turtle.turnRight()
  151.           turtle.turnRight()
  152.  
  153.           return
  154.         end
  155.       end
  156.     end
  157.  
  158.     -- Turn towards mine again
  159.     turtle.turnRight()
  160.     turtle.turnRight()
  161.  
  162.     -- Reset slot selection to first
  163.     turtle.select(1)
  164.  
  165.     -- Move back down to continue with next layer
  166.     for i = 1, distanceMovedAlongZ do
  167.       while true do
  168.         if turtle.down() then
  169.           break
  170.         end
  171.       end
  172.     end
  173.   end
  174.  
  175.   -- Get into position
  176.   while turtle.inspectDown() do
  177.     if not turtle.digDown() then
  178.       print("- Reached bedrock? Coming back up...")
  179.  
  180.       for i = 1, distanceMovedAlongZ do
  181.         while turtle.inspectUp() do
  182.           turtle.digUp()
  183.         end
  184.         while true do
  185.           if turtle.up() then
  186.             break
  187.           end
  188.         end
  189.       end
  190.  
  191.       local endTime = os.clock()
  192.  
  193.       print("- Stopped due to bedrock, done!")
  194.       print("- Took " .. (endTime - startTime) .. " seconds.")
  195.       return
  196.     end
  197.   end
  198.  
  199.   while true do
  200.     if turtle.down() then
  201.       break
  202.     end
  203.   end
  204.   distanceMovedAlongZ = distanceMovedAlongZ + 1
  205.  
  206.   while turtle.inspectDown() do
  207.     if not turtle.digDown() then
  208.       print("- Reached bedrock? Coming back up...")
  209.  
  210.       for i = 1, distanceMovedAlongZ do
  211.         while turtle.inspectUp() do
  212.           turtle.digUp()
  213.         end
  214.         while true do
  215.           if turtle.up() then
  216.             break
  217.           end
  218.         end
  219.       end
  220.  
  221.       local endTime = os.clock()
  222.  
  223.       print("- Stopped due to bedrock, done!")
  224.       print("- Took " .. (endTime - startTime) .. " seconds.")
  225.       return
  226.     end
  227.   end
  228.  
  229.   while true do
  230.     if turtle.down() then
  231.       break
  232.     end
  233.   end
  234.   distanceMovedAlongZ = distanceMovedAlongZ + 1
  235.  
  236.   if turtle.inspectDown() then
  237.     turtle.digDown()
  238.   end
  239.  
  240.   -- Loop vertically
  241.   for h = 1, height, 1 do
  242.     -- At the start of each row, check if we are at the further end from the chest or not
  243.     if h % 2 == 0 then
  244.       -- We are horizontally far away from the initial position, so we turn left to come back towards the chest
  245.       turtle.turnLeft()
  246.     else
  247.       -- We aim away from the initial position
  248.       turtle.turnRight()
  249.     end
  250.  
  251.     -- Loop along the width
  252.     for w = 1, width - 1, 1 do
  253.       -- Do a dig cycle
  254.       while turtle.inspect() do
  255.         turtle.dig()
  256.       end
  257.       while true do
  258.         if turtle.forward() then
  259.           break
  260.         end
  261.       end
  262.  
  263.       -- If we are going away from the chest we increase X distance, otherwise decrease it
  264.       if h % 2 == 0 then
  265.         distanceMovedAlongX = distanceMovedAlongX - 1
  266.       else
  267.         distanceMovedAlongX = distanceMovedAlongX + 1
  268.       end
  269.  
  270.       while turtle.inspectUp() do
  271.         turtle.digUp()
  272.       end
  273.  
  274.       if turtle.inspectDown() then
  275.         turtle.digDown()
  276.       end
  277.  
  278.       -- After a dig cycle we check if the inventory is full
  279.       local isInventoryFull = true
  280.       for i = 1, 16 do
  281.         if turtle.getItemSpace(i) == 64 then
  282.           isInventoryFull = false
  283.         end
  284.       end
  285.  
  286.       -- If inventory is full, dump any cobblestone or stone stacks (stone includes diorite, andesite and so on)
  287.       if isInventoryFull then
  288.         print("- Inventory full while mining, dumping some stone...")
  289.         for i = 1, 16 do
  290.           local itemInfo = turtle.getItemDetail(i)
  291.  
  292.           if itemInfo.name == "minecraft:cobblestone" or itemInfo.name == "minecraft:stone" then
  293.             turtle.select(i)
  294.             turtle.dropDown(itemInfo.count)
  295.           end
  296.         end
  297.       end
  298.     end
  299.  
  300.     -- We have reached the end of the cube to mine,
  301.     -- we can turn and move towards the new row
  302.     if h % 2 == 0 then
  303.       turtle.turnRight()
  304.     else
  305.       turtle.turnLeft()
  306.     end
  307.  
  308.     -- Check if this is the last row or not
  309.     if not (h == height) then
  310.       -- If there's gonna be a new row we do a dig cycle to start it
  311.       while turtle.inspect() do
  312.         turtle.dig()
  313.       end
  314.  
  315.       while true do
  316.         if turtle.forward() then
  317.           break
  318.         end
  319.       end
  320.       distanceMovedAlongY = distanceMovedAlongY + 1
  321.  
  322.       while turtle.inspectUp() do
  323.         turtle.digUp()
  324.       end
  325.  
  326.       -- No need to fix gravel bug downwards, and can't loop anyway because bedrock would break everything
  327.       if turtle.inspectDown() then
  328.         turtle.digDown()
  329.       end
  330.     end
  331.   end
  332.  
  333.   -- At the end of a layer, we come back to the start
  334.  
  335.   -- Cancel all Y movement
  336.   for i = 1, distanceMovedAlongY, 1 do
  337.     while true do
  338.       if turtle.back() then
  339.         break
  340.       end
  341.     end
  342.   end
  343.  
  344.   -- Cancel all X movement
  345.   if distanceMovedAlongX > 0 then
  346.     turtle.turnLeft()
  347.     for i = 1, distanceMovedAlongX, 1 do
  348.       while true do
  349.         if turtle.forward() then
  350.           break
  351.         end
  352.       end
  353.     end
  354.     turtle.turnRight()
  355.   end
  356.  
  357.   -- Move down one last block to get back to start of the cycle
  358.   if not turtle.down() then
  359.     print("- Reached bedrock? Coming back up...")
  360.  
  361.     for i = 1, distanceMovedAlongZ do
  362.       while turtle.inspectUp() do
  363.         turtle.digUp()
  364.       end
  365.       while true do
  366.         if turtle.up() then
  367.           break
  368.         end
  369.       end
  370.     end
  371.  
  372.     local endTime = os.clock()
  373.  
  374.     print("- Stopped due to bedrock, done!")
  375.     print("- Took " .. (endTime - startTime) .. " seconds.")
  376.     return
  377.   end
  378.  
  379.   distanceMovedAlongZ = distanceMovedAlongZ + 1
  380.  
  381.   -- Reset distances traveled to be used in the next layer
  382.   distanceMovedAlongX = 0
  383.   distanceMovedAlongY = 0
  384. end
  385.  
  386. -- Come back up
  387. for i = 1, distanceMovedAlongZ do
  388.   while true do
  389.     if turtle.up() then
  390.       break
  391.     end
  392.   end
  393. end
  394.  
  395. local endTime = os.clock()
  396.  
  397. print("- Done! Took " .. (endTime - startTime) .. " seconds.")
RAW Paste Data