Advertisement
Guest User

stickydig.lua

a guest
Apr 7th, 2020
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.68 KB | None | 0 0
  1. -- Take and parse arguments
  2. local arg1, arg2, arg3, arg4 = ...
  3. local width = tonumber(arg1)
  4. local height = tonumber(arg2)
  5. local depth = tonumber(arg3)
  6. local superSecretFix = tonumber(arg4)
  7.  
  8. -- Validate arguments
  9. if not (depth % 3 == 0) then
  10.   print("- Depth should be a multiple of 3 for maximum efficiency.")
  11.   return
  12. end
  13.  
  14. -- Declare values used for mining
  15. local distanceMovedAlongZ = 0
  16. local distanceMovedAlongY = 0
  17. local distanceMovedAlongX = 0
  18.  
  19. -- Plan fuel necessary for each layer
  20. local fuelNeededForEachLayer = width * height + (height % 2 == 0 and height or width) + 3
  21.  
  22. -- Begin mining loop
  23. for z = 1, depth - 1, 3 do
  24.   -- Check if we have enough fuel
  25.   if turtle.getFuelLevel() < fuelNeededForEachLayer + distanceMovedAlongZ then
  26.     -- We don't have enough fuel
  27.     print("- Not enough fuel, trying to refuel using all fuel in own inventory...")
  28.  
  29.     for i = 1, 16 do
  30.       turtle.select(i)
  31.       turtle.refuel()
  32.     end
  33.  
  34.     -- Fix seleection back to first slot
  35.     turtle.select(1)
  36.  
  37.     -- Did we get anything?
  38.     if turtle.getFuelLevel() < fuelNeededForEachLayer + distanceMovedAlongZ then
  39.       -- We didn't
  40.       print("- There wasn't enough fuel in the inventory to get through this layer, coming back up...")
  41.  
  42.       for i = 1, distanceMovedAlongZ do
  43.         turtle.up()
  44.       end
  45.  
  46.       print("- Quitting because of no fuel :(")
  47.  
  48.       return
  49.     end
  50.   end
  51.  
  52.   -- Check if we have enough inventory space
  53.   local freeSlots = 0
  54.   for i = 1, 16 do
  55.     if turtle.getItemSpace(i) == 64 then
  56.       freeSlots = freeSlots + 1
  57.     end
  58.   end
  59.  
  60.   if freeSlots < 2 then
  61.     print("- There aren't enough free slots, coming back up to empty inventory...")
  62.  
  63.     -- Move back up and dump everything to the chest
  64.     for i = 1, distanceMovedAlongZ do
  65.       turtle.up()
  66.     end
  67.  
  68.     turtle.turnRight()
  69.     turtle.turnRight()
  70.    
  71.     for i = 1, 16 do
  72.       turtle.select(i)
  73.  
  74.       if turtle.getItemCount(i) > 0 then
  75.         if not turtle.drop() then
  76.           print("- Chest seems to be full, quitting.")
  77.  
  78.           turtle.turnRight()
  79.           turtle.turnRight()
  80.  
  81.           return
  82.         end
  83.       end
  84.     end
  85.  
  86.     turtle.turnRight()
  87.     turtle.turnRight()
  88.  
  89.     turtle.select(1)
  90.  
  91.     -- Move back down to continue with next layer
  92.     for i = 1, distanceMovedAlongZ do
  93.       turtle.down()
  94.     end
  95.   else
  96.     print("- There are " .. freeSlots .. " item slots available still.")
  97.   end
  98.  
  99.   -- Get into position
  100.   turtle.digDown()
  101.   turtle.down()
  102.   distanceMovedAlongZ = distanceMovedAlongZ + 1
  103.   turtle.digDown()
  104.   turtle.down()
  105.   distanceMovedAlongZ = distanceMovedAlongZ + 1
  106.   turtle.digDown()
  107.  
  108.   -- Loop vertically
  109.   for h = 1, height, 1 do
  110.     -- At the start of each row, check if we are at the further end from the chest or not
  111.     if h % 2 == 0 then
  112.       -- We are horizontally far away from the initial position, so we turn left to come back towards the chest
  113.       turtle.turnLeft()
  114.     else
  115.       -- We aim away from the initial position
  116.       turtle.turnRight()
  117.     end
  118.  
  119.     -- Loop along the width
  120.     for w = 1, width - 1, 1 do
  121.       -- Do a dig cycle
  122.       while turtle.inspect() do
  123.         turtle.dig()
  124.       end
  125.       turtle.forward()
  126.  
  127.       -- If we are going away from the chest we increase X distance, otherwise decrease it
  128.       if h % 2 == 0 then
  129.         distanceMovedAlongX = distanceMovedAlongX - 1
  130.       else
  131.         distanceMovedAlongX = distanceMovedAlongX + 1
  132.       end
  133.  
  134.       turtle.digUp()
  135.       turtle.digDown()
  136.     end
  137.  
  138.     -- We have reached the end of the cube to mine,
  139.     -- we can turn and move towards the new row
  140.     if h % 2 == 0 then
  141.       turtle.turnRight()
  142.     else
  143.       turtle.turnLeft()
  144.     end
  145.  
  146.     -- Check if this is the last row or not
  147.     if not (h == height) then
  148.       -- If there's gonna be a new row we do a dig cycle to start it
  149.       while turtle.inspect() do
  150.         turtle.dig()
  151.       end
  152.       turtle.forward()
  153.       distanceMovedAlongY = distanceMovedAlongY + 1
  154.       turtle.digUp()
  155.       turtle.digDown()
  156.     end
  157.   end
  158.  
  159.   -- At the end of a layer, we come back to the start of the current layer
  160.  
  161.   -- Cancel all Y movement
  162.   for i = 1, distanceMovedAlongY, 1 do
  163.     turtle.back()
  164.   end
  165.  
  166.   -- Cancel all X movement
  167.   turtle.turnLeft()
  168.   for i = 1, distanceMovedAlongX, 1 do
  169.     turtle.forward()
  170.   end
  171.   turtle.turnRight()
  172.  
  173.   -- Move down one last block to get back to start of the cycle
  174.   turtle.down()
  175.   distanceMovedAlongZ = distanceMovedAlongZ + 1
  176.  
  177.   -- Reset distances traveled to be used in the next layer
  178.   distanceMovedAlongX = 0
  179.   distanceMovedAlongY = 0
  180. end
  181.  
  182. -- Come back up
  183. for i = 1, distanceMovedAlongZ - 1 do
  184.   turtle.up()
  185. end
  186.  
  187. print("Done!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement