Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- depth = 0
- maxOperations = 1
- fuelThreshold = 30
- abortDueToFuel = false
- useChests = true
- descending = true
- ascending = true
- dropJunk = true
- function forceMove(direction)
- if direction == "up" then
- while not turtle.up() do
- turtle.digUp()
- end
- end
- if direction == "down" then
- while not turtle.down() do
- turtle.digDown()
- end
- end
- if direction == "forward" then
- while not turtle.forward() do
- turtle.dig()
- end
- end
- end
- function forcePlace(direction)
- if direction == "up" then
- while not turtle.placeUp() do
- turtle.digUp()
- end
- end
- if direction == "down" then
- while not turtle.placeDown() do
- turtle.digDown()
- end
- end
- if direction == "forward" then
- while not turtle.place() do
- turtle.dig()
- end
- end
- end
- function fuel()
- print("Attempting to refuel with something in inventory...")
- local fuelTable = {"minecraft:coal", "minecraft:lava_bucket"}
- local successfulRefuel = false
- for i = 1, 16, 1 do
- local itemData = turtle.getItemDetail(i)
- if itemData then
- for k, v in ipairs(fuelTable) do
- if itemData.name == v then
- turtle.select(i)
- turtle.refuel()
- turtle.select(1)
- successfulRefuel = true
- end
- end
- end
- end
- return successfulRefuel
- end
- function isValuable()
- local junkTable = {"minecraft:stone", "minecraft:dirt", "minecraft:gravel", "minecraft:sand", "minecraft:torch"}
- local valuable = true
- local isBlock, blockData = turtle.inspect()
- if isBlock == true then
- --print("There's something here. Block data:")
- --print(blockData.name)
- --print(blockData.metadata)
- for k, v in ipairs(junkTable) do
- if blockData.name == v then
- --print("This is junk.")
- valuable = false
- break
- end
- end
- else
- --print("There isn't anything here.")
- valuable = false
- end
- if valuable then
- --print("There's something valuable here!")
- return true
- else
- --print("Nothing interesting here. Moving on")
- return false
- end
- end
- function checkFreeSlot()
- for i = 1, 16, 1 do
- if turtle.getItemCount(i) <= 0 then
- return i
- end
- end
- return -1
- end
- function throwAwayJunk() --Finds junk in inventory and throws it away
- local junkTable = {"minecraft:stone", "minecraft:dirt", "minecraft:gravel", "minecraft:sand"}
- local successful = false
- for i = 1, 16, 1 do
- local itemData = turtle.getItemDetail(i)
- if itemName then
- for k, v in ipairs(fuelTable) do
- if itemData.name == v then
- print("Throwing away ", itemData.name)
- turtle.select(i)
- turtle.dropDown()
- turtle.select(1)
- successful = true
- end
- if successful then break end
- end
- end
- end
- return successful
- end
- function checkSides()
- for i = 0, 3, 1 do
- if isValuable() then
- turtle.dig()
- end
- turtle.turnRight()
- end
- end
- function isBottomEmpty()
- local isBlock, blockData = turtle.inspect()
- if isBlock then
- return true
- else
- return false
- end
- end
- function isBottomBedrock()
- local isBlock, blockData = turtle.inspectDown()
- if isBlock then
- if blockData.name == "minecraft:bedrock" then
- print("We've hit bedrock!")
- return true
- else
- return false
- end
- else
- return false
- end
- end
- function knightMove()
- print("Knight move")
- forceMove("forward")
- forceMove("forward")
- turtle.turnRight()
- forceMove("forward")
- turtle.turnLeft()
- end
- function findChestInInventory()
- local chestSlot = -1
- for i = 1, 16, 1 do
- local data = turtle.getItemDetail(i)
- if data then
- if data.name == "minecraft:chest" then
- chestSlot = i
- print("Chest found in slot: " , chestSlot)
- break
- end
- end
- end
- return chestSlot
- end
- function isChestInInventory()
- local foundChest = false
- for i = 1, 16, 1 do
- local data = turtle.getItemDetail(i)
- if data then
- if data.name == "minecraft:chest" then
- foundChest = true
- break
- end
- end
- end
- return foundChest
- end
- function returnToSurface()
- print("Let's go back up...")
- while depth ~= 0 do
- if turtle.up() then
- depth = depth - 1
- else
- print("Obstruction detected...")
- turtle.digUp()
- end
- end
- end
- function closeHole()
- local junkTable = {"minecraft:stone", "minecraft:dirt"}
- local successful = false
- for i = 1, 16, 1 do
- local itemData = turtle.getItemDetail(i)
- if itemData then
- for k, v in ipairs(junkTable) do
- if itemData.name == v then
- turtle.select(i)
- turtle.placeDown()
- turtle.select(1)
- successful = true
- end
- end
- end
- end
- return successful
- end
- function closeHoleAbove()
- local junkTable = {"minecraft:stone", "minecraft:dirt"}
- local successful = false
- for i = 1, 16, 1 do
- local itemData = turtle.getItemDetail(i)
- if itemData then
- for k, v in ipairs(junkTable) do
- if itemData.name == v then
- turtle.select(i)
- turtle.placeUp()
- turtle.select(1)
- successful = true
- end
- end
- end
- end
- return successful
- end
- function chestDump()
- local itemsDeposited = 0
- print("Chest dumping")
- turtle.select(findChestInInventory())
- forcePlace("down")
- for a = 1, 16 do
- turtle.select(a)
- local data = turtle.getItemDetail()
- if data then
- if data.name ~= "minecraft:chest" then
- turtle.dropDown()
- itemsDeposited = itemsDeposited + 1
- end
- end
- end
- print(itemsDeposited, " different items deposited.")
- if not isChestInInventory() then
- printError("That was the last chest! I won't use anymore for the rest of the operation")
- useChests = false
- end
- turtle.select(1)
- end
- ----------------Program Starts Here----------------
- print("Checking Fuel")
- print("Fuel level is ", turtle.getFuelLevel())
- if turtle.getFuelLevel() <= fuelThreshold then
- print("Fuel low (", turtle.getFuelLevel(), ")! Attempting refuel")
- if not fuel() then
- repeat
- printError("There is not enough fuel to run an operation. Please add more and press enter")
- print("Or, type a to abort")
- local decision = io.read()
- if decision == "a" then error("Aborted") end
- until fuel()
- else
- print("Refuel successful: ", turtle.getFuelLevel())
- end
- else
- print("Fuel level OK")
- end
- print("Checking for chests")
- if not isChestInInventory() then
- repeat
- printError("No chests found! Please add chests to inventory and press enter to continue.")
- print("Type c to continue without using chests, or a to abort")
- local decision = io.read()
- if decision == "c" then
- useChests = false
- break
- end
- if decision == "a" then
- error("Aborted")
- end
- until findChestInInventory() ~= -1
- else
- print("Chest found in ", findChestInInventory())
- end
- print("Please specify number of operations. It must be a positive number")
- repeat
- maxOperations = io.read()
- maxOperations = tonumber(maxOperations)
- if maxOperations == nil or maxOperations <= 0 then
- print("Invalid entry!")
- end
- until maxOperations ~= nil and maxOperations > 0
- print("Let's go!")
- for operations = 1, maxOperations do
- print("Current operation: ", operations)
- while descending do
- if depth == 2 then
- print("Closing Hole")
- if not closeHoleAbove() then
- print("I could not close the hole for some reason.")
- end
- end
- checkSides()
- if isBottomBedrock() then
- descending = false
- break
- end
- turtle.digDown()
- if turtle.getFuelLevel() <= fuelThreshold then
- print("Fuel low (", turtle.getFuelLevel(), ")! Attempting refuel")
- if not fuel() then
- abortDueToFuel = true
- returnToSurface()
- break
- else
- abortDueToFuel = false
- print("Refuel successful")
- end
- end
- if dropJunk then
- if checkFreeSlot() == -1 then throwAwayJunk() end
- end
- turtle.down()
- depth = depth + 1
- end
- if not abortDueToFuel then
- knightMove()
- while ascending do
- if turtle.getFuelLevel() <= fuelThreshold then
- print("Fuel low (", turtle.getFuelLevel(), ")! Attempting refuel")
- if not fuel() then
- abortDueToFuel = true
- print("No fuel to refuel with! Stopping after this ascent operation")
- else
- abortDueToFuel = false
- print("Refuel successful")
- end
- end
- checkSides()
- if depth <= 0 then
- ascending = false
- break
- end
- turtle.digUp()
- if turtle.up() then
- depth = depth - 1
- else
- print("Obstruction detected...")
- turtle.digUp()
- end
- end
- end
- if not closeHole() then
- print("Uh oh, I have nothing to close the hole with")
- end
- if operations >= maxOperations or abortDueToFuel == true then
- if useChests then
- forceMove("up")
- chestDump()
- break
- else
- break
- end
- else
- if useChests then
- forceMove("up")
- chestDump()
- knightMove()
- forceMove("down")
- else
- knightMove()
- end
- end
- descending = true
- ascending = true
- end
- print("Done! Remaining fuel: ", turtle.getFuelLevel())
- if abortDueToFuel then
- print("The operation may be incomplete due to lack of fuel.")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement