Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Check that some fuel exists (consumes one if it does exist; there is no other way to verify)
- function CheckInitialFuel()
- for i = 1, 16 do
- turtle.select(i)
- if turtle.refuel(1) then -- If fuel was successfully used, swap the stack to the first slot (faster refueling next time, probably unnecessary)
- --turtle.transferTo(i, 1)
- return true
- end
- end
- return false
- end
- function DigThenMove(dir)
- local movedSuccessfully = false
- while movedSuccessfully == false do
- if dir == "forward" then
- while turtle.detect() do
- turtle.dig()
- end
- elseif dir == "up" then
- while turtle.detectUp() do
- turtle.digUp()
- end
- elseif dir == "down" then
- while turtle.detectDown() do
- turtle.digDown()
- end
- end
- if dir == "forward" then
- movedSuccessfully = turtle.forward()
- elseif dir == "up" then
- movedSuccessfully = turtle.up()
- else
- movedSuccessfully = turtle.down()
- end
- end
- end
- -- Mines all blocks directly in front of the turtle (one square), then moves one square forward (blocks like gravel or sand can fall and result in a block being directly in front of the turtle multiple times)
- function RefuelIfNeeded()
- if turtle.getFuelLevel() <= 5 then -- If the turtle is empty or nearly empty on fuel, search for a fuel source and refuel (prioritize left to right, top to bottom)
- for i = 1, 16 do
- turtle.select(i)
- if turtle.refuel(1) then -- If fuel was successfully used, swap the stack to the first slot (faster refueling next time, probably unnecessary)
- return
- end
- end
- WaitForFuel()
- end
- end
- function WaitForFuel()
- print("Please add fuel.")
- while true do
- for i = 1, 16 do
- turtle.select(i)
- if turtle.refuel(1) then
- return
- end
- end
- sleep(5)
- end
- end
- function TurnTurtle(reverseDirection)
- -- The reverse direction flag is used for turning back to the initial facing direction (i.e. forward) after a row of clearing
- if (reverseDirection) then
- if rightOrLeft == "right" then
- turtle.turnLeft()
- else
- turtle.turnRight()
- end
- else
- if (rightOrLeft == "right") then
- turtle.turnRight()
- else
- turtle.turnLeft()
- end
- end
- end
- function MoveVertically()
- if upOrDown == "up" then
- DigThenMove("up")
- --turtle.up()
- else -- direction == "down"
- DigThenMove("down")
- --turtle.down()
- end
- end
- -- Return the turtle to whence he came
- function ReturnToStart()
- -- Return to the starting plane (height)
- if length%2 == 0 then
- for i = 1,height-1 do
- if initialVertical == "up" then
- turtle.down()
- else
- turtle.up()
- end
- end
- end
- -- Return to the side of the cuboid the turtle started on (left or right edge)
- if (height*length)%2 ~= 0 then
- if initialHorizontal == "right" then
- turtle.turnLeft()
- else
- turtle.turnRight()
- end
- for i = 1,width-1 do
- turtle.forward()
- end
- -- Adjust facing to be the same as when the turtle started
- if initialHorizontal == "right" then
- turtle.turnRight()
- else
- turtle.turnLeft()
- end
- end
- -- Return to the side of the cuboid the turtle started (rear edge). Turtle should now be at the starting coordinates
- for i =1,length-1 do
- turtle.back()
- end
- end
- function DumpItems()
- turtle.turnLeft()
- if turtle.detect() then
- for i = 1,16 do
- turtle.select(i)
- turtle.drop()
- end
- end
- turtle.turnRight()
- --turtle.turnRight()
- -- Add code to check multiple sides for a chest (detect gets any block, though)
- -- Could attempt a refuel and if successful move fuel to slot 1 then dump the rest of the items (useful for dump and continue logic)
- end
- -- Consider adding default values for fields left blank
- function PerformExcavation()
- for k = 1,length do
- DigThenMove("forward")
- for j = 1,height do
- if width > 1 then -- Only need to do turning if the width is greater than 1
- TurnTurtle(false)
- end
- for i = 1,width-1 do
- if RefuelIfNeeded() == false then
- return false
- end
- DigThenMove("forward")
- end
- if width > 1 then
- TurnTurtle(true)
- end
- if (rightOrLeft == "right") then
- rightOrLeft = "left"
- else
- rightOrLeft = "right"
- end
- if j < height then
- MoveVertically()
- end
- end
- -- Change vertical direction the turtle will move to clear the next 2D piece (width x height)
- if upOrDown == "up" then
- upOrDown = "down"
- else
- upOrDown = "up"
- end
- end
- return true
- end
- -- Gather mining parameters from input (Should it go from current square down or up? Should it go from current square left or right? How wide / tall / long? That is, determine which corner to start at and how big of a cuboid to mine.)
- print("Clear left or right?")
- print("[VALID: left | right]: ")
- while true do
- rightOrLeft = string.lower(read())
- if (rightOrLeft == "right" or rightOrLeft == "left") then
- break
- else
- print("Bad entry. Clear left or right?")
- print("[VALID: left | right]: ")
- end
- end
- print("Clear up or down?")
- print("[VALID: up | down]: ")
- while true do
- upOrDown = string.lower(read())
- if (upOrDown == "up" or upOrDown == "down") then
- break
- else
- print("Bad entry. Clear up or down?")
- print("[VALID: up | down]: ")
- end
- end
- print("Enter width (left/right)")
- print("[VALID: Any integer]: ")
- while true do
- if pcall(function () width = tonumber(read()) end) then
- break
- else
- print("Bad entry. Enter width (left/right)?")
- print("[VALID: Any integer]: ")
- end
- end
- print("Enter height (above/below)")
- print("[VALID: Any integer]: ")
- while true do
- if pcall(function () height = tonumber(read()) end) then
- break;
- else
- write ("Bad entry. Enter height (above/below)?")
- print("[VALID: Any integer]: ")
- end
- end
- print("Enter length (forward)")
- print("[VALID: Any integer]: ")
- while true do
- if pcall(function () length = tonumber(read()) end) then
- break;
- else
- write ("Bad entry. Enter length (forward)?")
- print("[VALID: Any integer]: ")
- end
- end
- initialVertical = upOrDown
- initialHorizontal = leftOrRight
- RefuelIfNeeded()
- if PerformExcavation() then
- ReturnToStart()
- --DumpItems()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement