Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local fuelSlot = 16
- function checkResources()
- --Check fuel
- local selectedSlot = turtle.getSelectedSlot()
- local fuelThreshold = 2
- if(turtle.getFuelLevel() < fuelThreshold) then
- print("Turtle out of fuel!")
- if(turtle.getItemCount(fuelSlot)>0) then
- turtle.select(fuelSlot) --switch to fuel slot
- if(turtle.refuel(1))then --refuel one coal
- print("Turtle Refueled")
- end
- turtle.select(selectedSlot) --switch back to previous slot
- end
- end
- end
- function turnAround()
- turtle.turnRight()
- turtle.turnRight()
- end
- function checksurroundings()
- --Checking surroundings return values
- -- -1 - unreliable
- -- 0 - no surrounding blocks
- -- 1 - block only on left
- -- 2 - block only on right
- -- 4 - block only on front
- -- 3 - blocks on left, right
- -- 5 - blocks on front, left
- -- 6 - blocks on front, right
- -- 7 - blocks on front, left, and right
- local surroundings = 0
- if(turtle.turnLeft()) then--turn left to face left
- if(turtle.detect()) then --detect
- surroundings = surroundings + 1 -- 1 - block on left
- end
- else --unable to turn left
- print "Unreliable left detect"
- return -1
- end
- if(turtle.turnRight()) then --Turn right to face forward
- if(turtle.detect()) then
- surroundings = surroundings + 4 -- 2 - block on front
- end
- else --unable to turn right returns unreliable
- print "Unreliable front detect"
- return -1
- end
- if(turtle.turnRight()) then --Turn right to face right
- if(turtle.detect()) then
- surroundings = surroundings + 2 -- 4 - block on right
- end
- turtle.turnLeft() --return to face forwards
- else --unable to turn right returns unreliable
- print "Unreliable right detect"
- return -1
- end
- return surroundings
- end
- function rotateToPosition(position)
- local timeout = 4 --how many times to turn before returning failure
- local timer = 0 --incremental timer
- local success = false
- while(not success) do
- if(timer<timeout) then
- local surroundings = checksurroundings()
- if(surroundings == position)then
- success = true
- else
- turtle.turnRight()
- end
- end
- return false
- end
- return success
- end
- function getPerimeterPath()
- local perimeterPath = {}
- --Forward
- --Left turn
- --Right turn
- --turnaround
- local lastSurroundings = 0
- local ogX,ogY,ogZ = gps.locate()
- local retToStart = false
- local startSurroundings = checksurroundings()
- while(not retToStart) do
- checkResources()
- local surroundings = checksurroundings() --get current surroundings
- --print("new: " .. tostring(surroundings)) --for diagnostics
- --print("last: " .. tostring(lastSurroundings)) --for diagnostics
- if(surroundings == 0)then --Nothing around the turtle
- turtle.turnLeft()
- turtle.forward()
- table.insert(perimeterPath, "left")
- table.insert(perimeterPath, "forward")
- print("move: turn left and forward")
- elseif(surroundings == 1) then --Follow a left wall
- turtle.forward()
- table.insert(perimeterPath, "forward")
- print("move: forward")
- elseif(surroundings == 2) then --Only block on right (probably previously followed wall)
- turtle.turnLeft()
- turtle.forward()
- table.insert(perimeterPath, "left")
- table.insert(perimeterPath, "forward")
- print("move: turn left and forward")
- elseif(surroundings == 3)then --Follow a left wall
- turtle.forward()
- table.insert(perimeterPath, "forward")
- print("move: forwards")
- elseif(surroundings == 4) then --Only block in front
- turtle.turnLeft()
- turtle.forward()
- table.insert(perimeterPath, "left")
- table.insert(perimeterPath, "forward")
- print("move: turn left and forwards")
- elseif(surroundings == 5) then --Normal corner
- turtle.turnRight()
- turtle.forward()
- table.insert(perimeterPath, "right")
- table.insert(perimeterPath, "forward")
- print("move: turn right and forwards")
- elseif(surroundings == 6) then --Block ahead and to right, able to move left
- turtle.turnLeft()
- turtle.forward()
- table.insert(perimeterPath, "left")
- table.insert(perimeterPath, "forward")
- print("move: turn left and forwards")
- elseif(surroundings == 7) then --Cornered in, turn around!
- turnAround()
- turtle.forward()
- table.insert(perimeterPath, "uturn")
- table.insert(perimeterPath, "forward")
- print("move: turn around and forwards")
- end
- local newX,newY,newZ = gps.locate()
- --print("gps: " .. tostring(newX) .. "," .. tostring(newY) .. "," .. tostring(newZ))
- if(newX == ogX and newY == ogY and newZ == ogZ) then
- retToStart = true
- end
- --print("----------------------")
- lastSurroundings = surroundings
- end
- if(rotateToPosition(startSurroundings)) then
- print("Returned to base!")
- else
- print("unable to find starting base position")
- end
- return perimeterPath
- end
- function doAction(action)
- if(action == "forward") then
- print("move: forward")
- turtle.forward()
- elseif(action == "left") then
- print("move: turn left")
- turtle.turnLeft()
- elseif(action == "right")then
- print("move: turn right")
- turtle.turnRight()
- elseif(action =="uturn")then
- print("move: turn around")
- turnAround()
- else
- print("Action not recognised")
- end
- end
- local perimeterPath = getPerimeterPath()
- for i, pathPiece in ipairs(perimeterPath) do
- doAction(pathPiece)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement