Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ---@diagnostic disable: unused-function
- --[[----------------------------------------
- Turtle Mining Script
- Author: Evan Robertson
- Created: 2023-05-25
- Last updated: 2023-05-25
- ToDo:
- - List of shovel/pickaxe blocks
- - Create a looping sort of system so the turtle isnt
- just mining in a straight line forever
- - Add a "place chest" at end of loop system and
- empty any non-fuel source items into chest
- - After each loop, turn right, dig three blocks (2 high),
- and then start new loop.
- - Switch out the "while" loop for a doLoop function
- that creates the loop system
- - Create the place chest/empty contents function
- - Check for bedrock, if found, move up
- ------------------------------------------]]
- --[[----------------------------------------
- Variables
- ------------------------------------------]]
- local inventory = {}
- local success = true
- local isBlock = false
- local blockType = ""
- local fuelLevel = 0
- local steps = 0
- local pickaxeBlocks = ""
- local shovelBlocks = ""
- local ableToMove = true
- local posY = 0
- local buildingMaterial = {"minecraft:cobblestone"}
- local noBreakList = {"minecraft:torch","tconstruct:stone_torch"} -- list of blocks not to break
- local posHome = 0 -- The starting position of the turtle
- local stepsToDestination = 0
- local miningPattern = ""
- local margin = 10 --should have about 10 fuel when we return home
- local torchPerSteps = 10 --The number of steps taken before placing a torch
- local stepsTillToch = 10
- local facing = "forward" -- the direction the turtle started
- local noFuelList = {"minecraft:torch"} -- list of items to exclude from using as fuel
- local globalDebug = false -- when true, waits for user input
- --[[----------------------------------------
- Functions
- ------------------------------------------]]
- --[[
- setDirection
- uses the current facing direction and returns the
- new direction after a turn
- ]]
- local function setDirection(turnDirection)
- if turnDirection == "right" then
- if facing == "forward" then
- facing = "right"
- end
- if facing == "right" then
- facing = "backwards"
- end
- if facing == "backwards" then
- facing = "left"
- end
- if facing == "left" then
- facing = "forward"
- end
- end -- end if right
- if turnDirection == "left" then
- if facing == "forward" then
- facing = "left"
- end
- if facing == "right" then
- facing = "forwards"
- end
- if facing == "backwards" then
- facing = "right"
- end
- if facing == "left" then
- facing = "backwards"
- end
- end -- end if left
- end -- end function setDirection
- --[[ Check all inventory slots for a fuel source and refuel --]]
- local function refuelTurtle()
- local refuelSuccess
- local slotInfo
- --[[ Default to false ]]
- refuelSuccess = false
- for i = 1, 16 do
- turtle.select(i)
- slotInfo = turtle.getItemDetail()
- if type(slotInfo) == "table" then
- for key, value in pairs(noFuelList) do
- if slotInfo.name ~= value then
- refuelSuccess = turtle.refuel()
- if refuelSuccess then
- break
- end
- end
- end
- end
- end --[[ end for loop --]]
- if refuelSuccess then
- print("Refuel success, Fuel level: ", turtle.getFuelLevel())
- else
- print("Refuel failed")
- end
- return refuelSuccess
- end --[[ end function refuleTurtle --]]
- --[[
- findBuildingMaterial
- Checks all inventory slots for applicable building blocks
- returns if block was found and the slot its in
- ]]
- local function findBuildingMaterial()
- local functionSuccess = false
- local success = true
- local slotNum = 0
- local slotInfo = {}
- local startIndex = 0
- local endIndex = 0
- local errReason = ""
- for i = 1, 16 do
- -- select each slot
- success = turtle.select(i)
- -- if select was good
- if success then
- slotInfo = turtle.getItemDetail()
- -- make sure it returns a table, else nothing there
- if type(slotInfo) == "table" then
- -- check the item's name for acceptable building materials like cobblestone
- for key, value in pairs(buildingMaterial) do
- if slotInfo.name == value then
- print("Applicable building material found")
- slotNum = i
- functionSuccess = true
- break
- end -- end compare to list of materials
- end -- end for loop
- end -- end if slot not empty
- end -- end if select succeeds
- end -- end for loop
- return functionSuccess, slotNum
- end -- end function findBuildingMaterial
- --[[
- faceLeft
- Makes the turtle face left
- ]]
- local function faceLeft()
- if facing == "foward" then
- turtle.turnLeft()
- setDirection("left")
- elseif facing == "right" then
- turtle.turnLeft()
- setDirection("left")
- turtle.turnLeft()
- setDirection("left")
- elseif facing == "backwards" then
- turtle.turnRight()
- setDirection("right")
- end
- end -- end function faceLeft
- --[[
- faceRight
- Makes the turtle face right
- ]]
- local function faceRight()
- if facing == "backwards" then
- turtle.turnLeft()
- setDirection("left")
- elseif facing == "left" then
- turtle.turnLeft()
- setDirection("left")
- turtle.turnLeft()
- setDirection("left")
- elseif facing == "forwards" then
- turtle.turnRight()
- setDirection("right")
- end
- end -- end function faceRight
- --[[
- faceBackwards
- Makes the turtle face backwards
- ]]
- local function faceBackwards()
- if facing == "foward" then
- turtle.turnLeft()
- setDirection("left")
- turtle.turnLeft()
- setDirection("left")
- elseif facing == "right" then
- turtle.turnRight()
- setDirection("right")
- turtle.turnLeft()
- setDirection("left")
- elseif facing == "left" then
- turtle.turnLeft()
- setDirection("left")
- end
- end -- end function faceBackwards
- --[[
- faceForwards
- Makes the turtle face forwards
- ]]
- local function faceForwards()
- if facing == "backwards" then
- turtle.turnLeft()
- setDirection("left")
- turtle.turnLeft()
- setDirection("left")
- elseif facing == "left" then
- turtle.turnRight()
- setDirection("right")
- turtle.turnLeft()
- setDirection("left")
- elseif facing == "right" then
- turtle.turnLeft()
- setDirection("left")
- end
- end -- end function faceForwards
- --[[
- placeTorch
- checks for and places a torch
- ]]
- local function placeTorch()
- local slotInfo = {}
- for i = 1, 16 do
- -- select each slot
- turtle.select(i)
- slotInfo = turtle.getItemDetail()
- if type(slotInfo) == "table" then
- if slotInfo.name == "minecraft:torch" then
- print("Torch found, placing above")
- turtle.select(i)
- turtle.placeUp()
- end -- end if torch found in item slot
- end -- end if type is table
- end -- end for loop
- end -- end function placeTorch
- --[[
- buildForward
- Accepts an inventory slot (1-16) and places the block in that slot forward
- returns true if placeDown was successful
- ]]
- local function buildForward(slotNum)
- local success = true
- local errReason = true
- local functionSuccess = true
- -- select the inventory slot of the parameter slotNum
- success = turtle.select(slotNum)
- if success then
- -- Attempt to place the block
- success, errReason = turtle.place()
- if not success then
- print("Place forward failed, reason: ", errReason)
- functionSuccess = false
- else
- print("Place forward success!")
- functionSuccess = true
- end -- end if placeDown failed
- else
- print("Could not select slot. Bad slot number? ", slotNum)
- functionSuccess = false
- end -- if success was good
- return functionSuccess
- end -- end function buildForward
- --[[
- buildDown
- Accepts an inventory slot (1-16) and places the block in that slot down
- returns true if placeDown was successful
- ]]
- local function buildDown(slotNum)
- local success = true
- local errReason = true
- local functionSuccess = true
- -- select the inventory slot of the parameter slotNum
- success = turtle.select(slotNum)
- if success then
- -- Attempt to place the block
- success, errReason = turtle.placeDown()
- if not success then
- print("Place down failed, reason: ", errReason)
- functionSuccess = false
- else
- print("Place down success!")
- functionSuccess = true
- end -- end if placeDown failed
- else
- print("Could not select slot. Bad slot number? ", slotNum)
- functionSuccess = false
- end -- if success was good
- return functionSuccess
- end -- end function buildDown
- --[[ inspect the block in front of the turtle --]]
- --[[ returns false when there is air in front --]]
- --[[ will be used in more depth later, --]]
- --[[ like comparing against a list of blocks --]]
- local function checkFrontFacingBlock()
- local inspectSuccess
- local block
- inspectSuccess, block = turtle.inspect()
- return inspectSuccess, block
- end --[[ end function checkFrontFacingBlock --]]
- local function checkTopFacingBlock()
- local inspectSuccess
- local block
- inspectSuccess, block = turtle.inspectUp()
- return inspectSuccess, block
- end --[[ end function checkFrontFacingBlock --]]
- local function checkBottomFacingBlock()
- local inspectSuccess
- local block
- inspectSuccess, block = turtle.inspectDown()
- return inspectSuccess, block
- end --[[ end function checkFrontFacingBlock --]]
- --[[ Functions to move, dig, refuel, etc ]]
- --[[
- digUntilEmpty
- Digs in front of the turtle until no block is left
- Ideal for clearing falling sand/gravel
- Retruns false when no block is detected
- ]]
- local function digUntilEmpty(direction)
- local blockDetected = true
- local block
- local blockFound
- local skip = false
- while blockDetected do
- if direction == "up" then
- blockDetected = turtle.detectUp()
- blockFound, block = turtle.inspectUp()
- for key, value in pairs(noBreakList) do
- if block.name == value then
- break
- end
- end
- turtle.digUp()
- elseif direction == "down" then
- blockDetected = turtle.detectDown()
- turtle.digDown()
- else
- blockDetected = turtle.detect()
- turtle.dig()
- end -- end if direction
- end -- end while loop
- return blockDetected
- end -- end function digUntilEmpty
- --[[
- moveForward
- Checks the block in front, if solid digs and moves forward
- if air, just moves forward
- returns true if dig and/or move succeeded
- ]]
- local function moveForward(positionY)
- local isBlock = false
- local block = ""
- local success = true
- local refuelSuccess = false
- local functionSuccess = true
- local slotNum = 0
- -- IF the Y position is 0, meaning on the floor, check for gaps
- if positionY == 0 then
- -- Start by checking block below. If non-solid, use material
- -- to fill it in to create a solid walking surface
- isBlock, block = checkBottomFacingBlock()
- -- if not solid
- if not isBlock then
- success, slotNum = findBuildingMaterial()
- if success then
- success = buildDown(slotNum)
- if not success then
- print("Could not build below turtle. Watch your step!")
- end -- end if build down failed
- else
- print("No applicable building material found. Watch your step!")
- end -- end if no building mats found
- end -- end if not solid block
- end -- end if position Y is 0
- digUntilEmpty("forward")
- print("Moving forward")
- success = turtle.forward()
- if not success then
- refuelSuccess = refuelTurtle()
- if not refuelSuccess then
- functionSuccess = false
- else
- print("Refuel successful, moving again")
- --[[ Will always be true since refuel was good ]]
- success = turtle.forward()
- if not success then
- print("Move failed, retrying once")
- success = turtle.forward()
- if not success then
- functionSuccess = false
- end
- end
- end -- end if refuel fails
- end -- end if move failed
- -- return success, true if dug and/or moved,
- -- false if any action and refueling failed
- if functionSuccess then
- -- clear block above
- digUntilEmpty("up")
- steps = steps +1
- -- decrement number of steps required till placing a torch
- stepsTillToch = stepsTillToch - 1
- print("steps till torch:", stepsTillToch)
- if stepsTillToch == 0 then
- print("placing torch!")
- placeTorch()
- -- reset the numberof steps still torch place
- stepsTillToch = torchPerSteps
- end
- end
- if globalDebug then
- print("")
- print("Hit enter to continue")
- read()
- end -- end if debug is on
- return functionSuccess
- end -- end function moveForward
- --[[
- moveUpward
- Checks the block above, if solid digs and moves up
- if air, just moves up
- returns true if dig and/or move succeeded
- ]]
- local function moveUpward()
- local isBlock = false
- local block = ""
- local success = true
- local refuelSuccess = false
- local functionSuccess = true
- digUntilEmpty("up")
- print("Moving upward")
- success = turtle.up()
- if not success then
- refuelSuccess = refuelTurtle()
- if not refuelSuccess then
- functionSuccess = false
- else
- print("Refuel successful, moving again")
- --[[ Will always be true since refuel was good ]]
- success = turtle.up()
- end -- end if refuel fails
- end -- end if move failed
- -- return success, true if dug and/or moved,
- -- false if any action and refueling failed
- return functionSuccess
- end -- end function moveUpward
- --[[
- moveDownward
- Checks the block below, if solid digs and moves down
- if air, just moves down
- returns true if dig and/or move succeeded
- ]]
- local function moveDownward()
- local isBlock = false
- local block = ""
- local success = true
- local refuelSuccess = false
- local functionSuccess = true
- -- Check if block is solid, and return what kind
- isBlock, block = checkBottomFacingBlock()
- -- if solid
- if isBlock then
- -- dig
- print("Solid block below, digging and moving")
- success = turtle.digDown('right')
- if not success then
- print("Dig failed, attempting refuel")
- refuelSuccess = refuelTurtle()
- if not refuelSuccess then
- functionSuccess = false
- else
- print("Refuel successful, digging again")
- --[[ Will always be true since refuel was good ]]
- success = turtle.digDown('right')
- end -- end if refuel fails
- end -- end if dig fails
- -- if dig succeeds, move up
- if success then
- print("Dig Successfull, moving Down")
- success = turtle.down()
- if not success then
- print("Move failed, attempting refuel")
- refuelSuccess = refuelTurtle()
- if not refuelSuccess then
- functionSuccess = false
- else
- print("Refuel successful, moving again")
- --[[ Will always be true since refuel was good ]]
- success = turtle.down()
- end -- end if refuel fails
- end -- end if move fails
- end -- end if dig succeeds
- -- else if block is not solid/air
- else
- print("Non-solid block, just moving")
- success = turtle.down()
- if not success then
- refuelSuccess = refuelTurtle()
- if not refuelSuccess then
- functionSuccess = false
- else
- print("Refuel successful, moving again")
- --[[ Will always be true since refuel was good ]]
- success = turtle.down()
- end -- end if refuel fails
- end -- end if move failed
- end -- end if block is solid
- -- return success, true if dug and/or moved,
- -- false if any action and refueling failed
- return functionSuccess
- end -- end function moveDownward
- --[[
- requiredFuel
- Accepts a destination like "home" and the current steps travelled
- returns the fuel level required to travel there
- Note - 1 fuel count equals 1 block, and each movement is tracked in steps
- ]]
- local function requiredFuel(destination, stepsMoved)
- local difference = stepsMoved - destination
- return difference
- end -- end function requiredFuel
- --[[
- goToDestination
- Accepts a destination and mining pattern
- Then executes the required steps to go to that destination
- returns true if it made it to it's destination
- ]]
- local function goToDestination(destination, turns, goingTowardsHome)
- -- if pattern is strip mining
- if miningPattern == "1" then
- -- Turn the turtle backwards
- print("Turning turtle around")
- if facing == "foward" then
- turtle.turnRight()
- setDirection("right")
- turtle.turnRight()
- setDirection("right")
- elseif facing == "left" then
- turtle.turnLeft()
- setDirection("left")
- elseif facing == "right" then
- turtle.turnRight()
- setDirection("right")
- end
- -- Make sure we're on the floor
- if posY == 1 then
- moveDownward()
- end
- -- Then exectue moveForward for the number of steps taken
- for i=1,steps do
- moveForward(posY)
- -- Each moveForward increments the steps by 1 so
- -- counteract that with -2
- steps = steps - 2
- end -- end for loop
- -- Then we handle however many turns were made
- turtle.turnRight()
- setDirection("right")
- for i=1,turns do
- print("Accounting for number of shafts made")
- moveForward(posY)
- turns = turns - 2
- end
- print("Turning turtle back around")
- turtle.turnRight()
- setDirection("right")
- print("Turtle is now facing: ", facing)
- end -- end if pattern 1
- -- if steps == 0 then we retraced all our steps and made it
- return steps == 0
- end -- end function goToDestination
- --[[
- checkDistanceToHome
- returns the distance required to make it home
- If distance is equal to fuel level, go home
- ]]
- local function checkDistanceToHome(turns, goingTowardsHome)
- local wentHome = false
- stepsToDestination = requiredFuel(posHome, steps)
- print("Steps to destination:", stepsToDestination)
- print("Fuel level:", turtle.getFuelLevel())
- if stepsToDestination > turtle.getFuelLevel() - margin then
- print("Steps > fuel, checking for fuel")
- -- First attempt to refuel
- success = refuelTurtle()
- -- if can't refuel, return home
- if not success then
- print("Returning home")
- success = goToDestination(posHome, turns, goingTowardsHome)
- if success then
- print("Returned home")
- wentHome = true
- end
- end
- end
- -- return whether or not the turtle went home
- return wentHome
- end -- end function checkDistanceToHome
- --[[
- getMiningPattern
- Requests input from the user
- returns the selected pattern
- ]]
- local function getMiningPattern()
- local awaitInput = true
- local input = ""
- local patterns = "1,"
- while awaitInput do
- print("Select the desired mining pattern")
- print("1. Strip Mining")
- print("Type help for more info")
- input = read()
- -- Check if the accepted answer is one of the available patterns
- if string.find("help", input) then
- print("Enter the selection for more info on it, or enter anything else to return.")
- input = read()
- if string.find("1", input) then
- print("Strip Mines:")
- print("A two block tall shaft that goes in the direction the turtle is facing")
- print("Hit enter to continue")
- read()
- print("A desired depth is input")
- print("When the turtle reaches the desired depth, turns right, moves a block over, and mines another shaft back.")
- print("Hit enter to continue")
- read()
- end -- end help for strip mine
- else
- if string.find(patterns, input) then
- awaitInput = false
- break
- else
- print("Not an option, try again")
- end -- end check for correct pattern
- end -- end print help
- end -- end while loop
- return input
- end -- end function getMiningPattern
- local function stripMinePattern()
- local input
- local lengthOfMine
- local inputGood = false
- local turns = 0
- -- When the turtle is going towards "home" track steps differently
- local goingTowardsHome = false
- while not inputGood do
- print("How many blocks forward should the turtle mine?")
- input = read()
- lengthOfMine = tonumber(input)
- if type(lengthOfMine) ~= "number" then
- print("Please enter a number")
- else
- inputGood = true
- end -- end if input not number
- end -- end while
- print("Beginning strip mine")
- while ableToMove do
- digUntilEmpty("up")
- success = moveForward(posY)
- if not success then
- print("Could not move forward")
- ableToMove = false
- break
- end -- end if move failed
- -- when going towards home, negate the moveForward
- -- step increment, and subtract one as well, as we're
- -- going towards "home"
- if goingTowardsHome then
- steps = steps - 2
- end -- end if going towards home
- success = checkDistanceToHome(turns, goingTowardsHome)
- if success then
- print("Went home! Exiting mining loop.")
- ableToMove = false
- break
- end
- -- If it reaches the desired depth, turn and make another shaft
- if steps >= lengthOfMine then
- turtle.turnRight()
- setDirection("right")
- success = moveForward(posY)
- digUntilEmpty("up")
- -- negate the step increment
- steps = steps - 1
- -- increment turns
- if success then
- turns = turns + 1
- end -- end if move was successful
- turtle.turnRight()
- setDirection("right")
- goingTowardsHome = true
- elseif steps == 0 then
- turtle.turnLeft()
- setDirection("left")
- success = moveForward(posY)
- digUntilEmpty("up")
- -- negate the step increment
- steps = steps - 1
- -- increment turns
- if success then
- turns = turns + 1
- end -- end if move was successful
- turtle.turnLeft()
- setDirection("left")
- goingTowardsHome = false
- end
- end --[[ end while turtle foward loop --]]
- end -- end function stripMinePattern
- --[[----------------------------------------
- Startup jobs
- ------------------------------------------]]
- print("Initializing Mining Toolkit V0.1")
- --[[ check for fuel --]]
- fuelLevel = turtle.getFuelLevel()
- if fuelLevel == 0 then
- success = refuelTurtle()
- end
- if success then
- fuelLevel = turtle.getFuelLevel()
- end
- if fuelLevel == 0 then
- print("Not enough fuel")
- ableToMove = false
- else
- -- Reset selected inventory to slot 1
- turtle.select(1)
- miningPattern = getMiningPattern()
- if miningPattern == "1" then
- stripMinePattern()
- end
- end
Add Comment
Please, Sign In to add comment