Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Start timer
- local startTime = os.clock()
- -- Take and parse arguments
- local arg1, arg2, arg3, arg4 = ...
- -- Validate arguments
- if arg1 == nil or arg2 == nil then
- print("- Not enough arguments provided. Width, height are required.")
- return
- end
- if arg3 == nil then
- arg3 = "255"
- end
- local width = tonumber(arg1)
- local height = tonumber(arg2)
- local depth = tonumber(arg3)
- local layersToSkip = tonumber(arg4)
- if not (depth % 3 == 0) then
- print("- Depth should be a multiple of 3 for maximum efficiency.")
- return
- end
- -- Plan fuel necessary for each layer
- local fuelNeededForEachLayer = width * height + (height % 2 == 0 and height or width) + 3
- -- Check if enough fuel has been provided
- print("- This mine seems to require " .. math.floor((fuelNeededForEachLayer / 80) * 100) / 100 .. " coal items, checking if enough have been provided...")
- for i = 1, 16 do
- turtle.select(i)
- turtle.refuel()
- end
- -- Reset selected slot
- turtle.select(1)
- if turtle.getFuelLevel() < fuelNeededForEachLayer then
- print("- Not enough fuel provided, quitting.")
- return
- end
- -- Declare values used for mining
- local distanceMovedAlongZ = 0
- local distanceMovedAlongY = 0
- local distanceMovedAlongX = 0
- -- Check if a skip has been requested
- if not (layersToSkip == nil) then
- print("- A skip has been requested.")
- -- For each layer to skip we go down 3 times and store this movement
- for i = 1, layersToSkip do
- for temp = 1, 3 do
- while turtle.inspectDown() do
- turtle.digDown()
- end
- while true do
- if turtle.down() then
- break
- end
- end
- distanceMovedAlongZ = distanceMovedAlongZ + 1
- depth = depth - 1
- end
- print("Skipped a layer.")
- end
- else
- print("- No layers to skip.")
- end
- -- Begin mining loop
- for z = 1, depth - 1, 3 do
- -- Check if we have enough fuel
- if turtle.getFuelLevel() < fuelNeededForEachLayer + distanceMovedAlongZ then
- -- We don't have enough fuel
- print("- Not enough fuel, trying to refuel using all fuel in own inventory...")
- for i = 1, 16 do
- turtle.select(i)
- turtle.refuel()
- end
- -- Reset selection back to first slot
- turtle.select(1)
- -- Did we get anything?
- if turtle.getFuelLevel() < fuelNeededForEachLayer + distanceMovedAlongZ then
- -- We didn't
- print("- There wasn't enough fuel in the inventory to get through this layer, coming back up...")
- for i = 1, distanceMovedAlongZ do
- while turtle.inspectUp() do
- turtle.digUp()
- end
- while true do
- if turtle.up() then
- break
- end
- end
- end
- print("- Quitting because of no fuel :(")
- return
- end
- end
- -- Check if we have enough inventory space
- local freeSlots = 0
- for i = 1, 16 do
- if turtle.getItemSpace(i) == 64 then
- freeSlots = freeSlots + 1
- end
- end
- if freeSlots < 2 then
- print("- There aren't enough free slots, coming back up to empty inventory...")
- -- Move back up and dump everything to the chest
- for i = 1, distanceMovedAlongZ do
- while turtle.inspectUp() do
- turtle.digUp()
- end
- while true do
- if turtle.up() then
- break
- end
- end
- end
- -- Turn towards chest
- turtle.turnRight()
- turtle.turnRight()
- -- Cycle through every slot in the turtle's inventory
- for i = 1, 16 do
- turtle.select(i)
- -- Try to drop it only if it has items
- if turtle.getItemCount(i) > 0 then
- -- Check if drop onto chest was actually successful
- if not turtle.drop() then
- -- If it wasn't, chest was full
- print("- Chest seems to be full, quitting.")
- -- Turn around to face start positio again before quitting
- turtle.turnRight()
- turtle.turnRight()
- return
- end
- end
- end
- -- Turn towards mine again
- turtle.turnRight()
- turtle.turnRight()
- -- Reset slot selection to first
- turtle.select(1)
- -- Move back down to continue with next layer
- for i = 1, distanceMovedAlongZ do
- while true do
- if turtle.down() then
- break
- end
- end
- end
- end
- -- Get into position
- while turtle.inspectDown() do
- if not turtle.digDown() then
- print("- Reached bedrock? Coming back up...")
- for i = 1, distanceMovedAlongZ do
- while turtle.inspectUp() do
- turtle.digUp()
- end
- while true do
- if turtle.up() then
- break
- end
- end
- end
- local endTime = os.clock()
- print("- Stopped due to bedrock, done!")
- print("- Took " .. (endTime - startTime) .. " seconds.")
- return
- end
- end
- while true do
- if turtle.down() then
- break
- end
- end
- distanceMovedAlongZ = distanceMovedAlongZ + 1
- while turtle.inspectDown() do
- if not turtle.digDown() then
- print("- Reached bedrock? Coming back up...")
- for i = 1, distanceMovedAlongZ do
- while turtle.inspectUp() do
- turtle.digUp()
- end
- while true do
- if turtle.up() then
- break
- end
- end
- end
- local endTime = os.clock()
- print("- Stopped due to bedrock, done!")
- print("- Took " .. (endTime - startTime) .. " seconds.")
- return
- end
- end
- while true do
- if turtle.down() then
- break
- end
- end
- distanceMovedAlongZ = distanceMovedAlongZ + 1
- if turtle.inspectDown() then
- turtle.digDown()
- end
- -- Loop vertically
- for h = 1, height, 1 do
- -- At the start of each row, check if we are at the further end from the chest or not
- if h % 2 == 0 then
- -- We are horizontally far away from the initial position, so we turn left to come back towards the chest
- turtle.turnLeft()
- else
- -- We aim away from the initial position
- turtle.turnRight()
- end
- -- Loop along the width
- for w = 1, width - 1, 1 do
- -- Do a dig cycle
- while turtle.inspect() do
- turtle.dig()
- end
- while true do
- if turtle.forward() then
- break
- end
- end
- -- If we are going away from the chest we increase X distance, otherwise decrease it
- if h % 2 == 0 then
- distanceMovedAlongX = distanceMovedAlongX - 1
- else
- distanceMovedAlongX = distanceMovedAlongX + 1
- end
- while turtle.inspectUp() do
- turtle.digUp()
- end
- if turtle.inspectDown() then
- turtle.digDown()
- end
- -- After a dig cycle we check if the inventory is full
- local isInventoryFull = true
- for i = 1, 16 do
- if turtle.getItemSpace(i) == 64 then
- isInventoryFull = false
- end
- end
- -- If inventory is full, dump any cobblestone or stone stacks (stone includes diorite, andesite and so on)
- if isInventoryFull then
- print("- Inventory full while mining, dumping some stone...")
- for i = 1, 16 do
- local itemInfo = turtle.getItemDetail(i)
- if itemInfo.name == "minecraft:cobblestone" or itemInfo.name == "minecraft:stone" then
- turtle.select(i)
- turtle.dropDown(itemInfo.count)
- end
- end
- end
- end
- -- We have reached the end of the cube to mine,
- -- we can turn and move towards the new row
- if h % 2 == 0 then
- turtle.turnRight()
- else
- turtle.turnLeft()
- end
- -- Check if this is the last row or not
- if not (h == height) then
- -- If there's gonna be a new row we do a dig cycle to start it
- while turtle.inspect() do
- turtle.dig()
- end
- while true do
- if turtle.forward() then
- break
- end
- end
- distanceMovedAlongY = distanceMovedAlongY + 1
- while turtle.inspectUp() do
- turtle.digUp()
- end
- -- No need to fix gravel bug downwards, and can't loop anyway because bedrock would break everything
- if turtle.inspectDown() then
- turtle.digDown()
- end
- end
- end
- -- At the end of a layer, we come back to the start
- -- Cancel all Y movement
- for i = 1, distanceMovedAlongY, 1 do
- while true do
- if turtle.back() then
- break
- end
- end
- end
- -- Cancel all X movement
- if distanceMovedAlongX > 0 then
- turtle.turnLeft()
- for i = 1, distanceMovedAlongX, 1 do
- while true do
- if turtle.forward() then
- break
- end
- end
- end
- turtle.turnRight()
- end
- -- Move down one last block to get back to start of the cycle
- if not turtle.down() then
- print("- Reached bedrock? Coming back up...")
- for i = 1, distanceMovedAlongZ do
- while turtle.inspectUp() do
- turtle.digUp()
- end
- while true do
- if turtle.up() then
- break
- end
- end
- end
- local endTime = os.clock()
- print("- Stopped due to bedrock, done!")
- print("- Took " .. (endTime - startTime) .. " seconds.")
- return
- end
- distanceMovedAlongZ = distanceMovedAlongZ + 1
- -- Reset distances traveled to be used in the next layer
- distanceMovedAlongX = 0
- distanceMovedAlongY = 0
- end
- -- Come back up
- for i = 1, distanceMovedAlongZ do
- while true do
- if turtle.up() then
- break
- end
- end
- end
- local endTime = os.clock()
- print("- Done! Took " .. (endTime - startTime) .. " seconds.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement