Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Enums for direction
- UP = 0
- DOWN = 1
- FORWARD = 2
- --Ignored block IDs
- IGNORE_BLOCKS = {
- "STONE_ID", "GRAVEL_ID", "DIRT_ID",
- "MARBLE_ID"
- }
- -- Helper function to compare
- function compareDirection(direction)
- if direction == UP then
- return turtle.compareUp()
- elseif direction == DOWN then
- return turtle.compareDown()
- else
- return turtle.compare()
- end
- end
- -- Helper function to inspect
- function inspectDirection(direction)
- if direction == UP then
- return turtle.inspectUp()
- elseif direction == DOWN then
- return turtle.inspectDown()
- else
- return turtle.inspect()
- end
- end
- -- Helper function to detect
- function detectDirection(direction)
- if direction == UP then
- return turtle.detectUp()
- elseif direction == DOWN then
- return turtle.detectDown()
- else
- return turtle.detect()
- end
- end
- --Helper function to dig
- function digDirection(direction)
- if direciton == UP then
- return turtle.digUp()
- elseif direction == DOWN then
- return turtle.digDown()
- else
- return turtle.dig()
- end
- end
- --Helper function to move
- function moveDirection(direction)
- if direction == UP then
- return turtle.up()
- elseif direction == DOWN then
- return turtle.down()
- else
- return turtle.forward()
- end
- end
- --Helper function to undo move
- function undoMoveDirection(direction)
- if direction == UP then
- return turtle.down()
- elseif direction == DOWN then
- return turtle.up()
- else
- return turtle.back()
- end
- end
- -- Helper function to ensure I have enough fuel
- function checkSufficentFuel(requiredReturnFuel)
- -- We do +1 here because moving takes a fuel away and adds a fuel to required Return fuel
- return turtle.getFuelLevel() > requiredReturnFuel + 1
- end
- --Checks the inventory to see if there is room to mine the block in direction
- function checkInventorySpaceAvailable(direction)
- local curSlot = turtle.getSelectSlot()
- local hasRoom = false
- for i = 1, 16 do
- turtle.select(i)
- local sameBlock = turtleWrappers.compareDirection(direction)
- if sameBlock then
- if turtle.getItemCount() < 64 then
- hasRoom = true
- break
- end
- end
- end
- turtle.select(curSlot);
- return hasRoom
- end
- --Helper function to dig in given direction; does not move
- function advancedDig(direction)
- -- TODO: handle gravel fall in here somehow
- --Check if i need to dig
- local needToDig = false
- needToDig = turtleWrappers.compareDirection(direction)
- if not needToDig then
- return true
- end
- --If I need to dig - make sure I can dig
- local hasRoom = checkInventorySpaceAvailable(direction)
- if hasRoom then
- return turtleWrappers.digDirection(direction)
- else
- return false
- end
- end
- --Checks to see if we should dig in direction for ores
- function checkForOreInDirection(direction)
- --Note: We may want to ignore air block when checking because we could run into problems with caves
- --Its not really a problem just a waste of fuel
- --If we do ignore air spaces, it will be beneficial to remove the `advancedDig(UP)` from digBranch as the top row will never be checked for ores
- if not turtleWrappers.detectDirection(direction) then
- return false
- end
- local data = turtleWrappers.inspectDirection(direction)
- for k, v in pairs(IGNORE_BLOCKS) do
- if data.name == v then
- return false
- end
- end
- return true
- end
- -- Recursive function to dig out ores that Have been found
- function digOreInDirection(direction, requiredReturnFuel)
- --Make sure I have enough fuel to get home
- if not checkSufficentFuel(requiredReturnFuel) then
- return
- end
- --Ok, I "know" there is ore in direction (could be an empty space) lets dig
- local success = advancedDig(direction)
- if not success then
- return
- end
- turtleWrappers.moveDirection(direction)
- digOresAllAround(requiredReturnFuel + 1)
- turtleWrappers.undoMoveDirection(direction)
- end
- --Helper function to check around and dig out ores
- function digOresAllAround(requiredReturnFuel)
- --Check left
- turtle.turnLeft()
- if checkForOreInDirection(FORWARD) then
- digOreInDirection(FORWARD, requiredReturnFuel)
- end
- turtle.turnRight()
- -- check right
- turtle.turnRight()
- if checkForOreInDirection(FORWARD) then
- digOreInDirection(FORWARD, requiredReturnFuel)
- end
- turtle.turnLeft()
- -- check up
- if checkForOreInDirection(UP) then
- digOreInDirection(UP, requiredReturnFuel)
- end
- -- check down
- if checkForOreInDirection(DOWN) then
- digOreInDirection(DOWN, requiredReturnFuel)
- end
- -- check forward
- if checkForOreInDirection(FORWARD) then
- digOreInDirection(FORWARD, requiredReturnFuel)
- end
- end
- function digOresTopRow(requiredReturnFuel)
- turtleWrappers.moveDirection(UP)
- digOresAllAround(requiredReturnFuel + 1)
- turtleWrappers.undoMoveDirection(UP)
- end
- --This assumes the branch is supposed to be dug in the forward direction with turtle sitting at bottom of tunnel
- function digBranch(branchLength, requiredReturnFuel)
- --Make sure I have enough fuel to get home
- if not checkSufficentFuel(requiredReturnFuel) then
- return
- end
- --Base Case
- if branchLength <= 0 then
- return
- end
- --Make sure I'm capable of digging
- local success = true
- success = success and advancedDig(FORWARD)
- success = success and advancedDig(UP)
- --Cool I dig - lets go forward
- if success then
- turtleWrappers.moveDirection(FORWARD)
- digBranch(branchLength - 1, requiredReturnFuel + 1)
- --I have completely dug forward - lets move backwards and check for ores
- digOresAllAround(requiredReturnFuel + 1)
- digOresTopRow(requiredReturnFuel + 1)
- --Lets unwind
- undoMoveDirection(FORWARD)
- end
- end
- --======================================
- -- Above here completely digs out a branch with inventory check, ore check and fuel check
- --======================================
- function digStripMine(width, height, length, branchSpacing, branchLength, requiredReturnFuel)
- --home fuel check
- if not checkSufficentFuel(requiredReturnFuel) then
- return
- end
- -- Base case
- if length <= 0 then
- return
- end
- --Do I need to dig some branches?
- if lenght % branchSpacing == 0 then
- --Left branch
- turtle.turnLeft()
- digBranch(branchLength, requiredReturnFuel)
- turtle.turnRight()
- --Right branch
- turtle.turnRight()
- digBranch(branchLength, requiredReturnFuel)
- turtle.turnLeft()
- end
- --Dig out mine shaft
- local success = advancedDig(FORWARD)
- if not success then
- return
- end
- turtleWrappers.moveDirection(FORWARD)
- digTunnelSliceLeft(width, height, requiredReturnFuel + 1)
- digStripMine(width, height, length - 1, branchSpacing, branchLength, requiredReturnFuel + 1)
- turtleWrappers.undoMoveDirection(FORWARD)
- end
- function digTunnelSliceLeft(width, height, requiredReturnFuel)
- digTunnelColumn(height, requiredReturnFuel)
- if not checkSufficentFuel(requiredReturnFuel) then
- return
- end
- turtle.turnLeft()
- local success = advancedDig(FORWARD)
- if not success then
- return
- end
- turtleWrappers.moveDirection(FORWARD)
- digSideSlice(width - 1, height, requiredReturnFuel + 1)
- turtleWrappers.undoMoveDirection(FORWARD)
- turtleWrappers.turnRight()
- end
- function digTunnelSliceRight(width, height, requiredReturnFuel)
- digTunnelColumn(height, requiredReturnFuel)
- if not checkSufficentFuel(requiredReturnFuel) then
- return
- end
- turtle.turnRight()
- local success = advancedDig(FORWARD)
- if not success then
- return
- end
- turtleWrappers.moveDirection(FORWARD)
- digSideSlice(width - 1, height, requiredReturnFuel + 1)
- turtleWrappers.undoMoveDirection(FORWARD)
- turtleWrappers.turnLeft()
- end
- function digSideSlice(width, height, requiredReturnFuel)
- digTunnelColumn(height, requiredReturnFuel)
- if not checkSufficentFuel(requiredReturnFuel) then
- return
- end
- if width <= 0 then
- return
- end
- local success = advancedDig(FORWARD)
- if not success then
- return
- end
- turtleWrappers.moveDirection(FORWARD)
- digSideSlice(width - 1, height, requiredReturnFuel + 1)
- turtleWrappers.undoMoveDirection(FORWARD)
- end
- function digTunnelColumn(height, requiredReturnFuel)
- if not checkSufficentFuel(requiredReturnFuel) then
- return
- end
- -- <= 1 here because the first block is already dug out
- if height <= 1 then
- return
- end
- local success = advancedDig(UP)
- if not success then
- return
- end
- turtleWrappers.moveDirection(UP)
- digTunnelColumn(height - 1, requiredReturnFuel + 1 )
- turtleWrappers.undoMoveDirection(UP)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement