Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- License: Authors Right to Brag
- -- You may use this source without restrictions with the exception of boasting.
- -- Only Micha may brag about this.
- print ("follow v0.1")
- print ("Follows a path of whatever the turtle has got in his inventory")
- print ("(I recommend using train tracks)")
- print ("All Bragging Rights Reserved - Micha 2012")
- print ("")
- local state = "scan"
- function innerscan()
- local somethingDown = false
- local somethingForward = false
- local matchDown = false
- local matchForward = false
- if turtle.detectDown() then
- somethingDown = true
- for i = 1, 9, 1 do
- turtle.select(i)
- if turtle.compareDown() then
- matchDown = true
- end
- end
- end
- if turtle.detect() then
- somethingForward = true
- for i = 1, 9, 1 do
- turtle.select(i)
- if turtle.compare() then
- matchForward = true
- end
- end
- end
- return somethingDown, somethingForward, matchDown, matchForward
- end
- function upscan()
- local somethingUp = false
- local matchUp = false
- if turtle.detectUp() then
- somethingUp = true
- for i = 1, 9, 1 do
- turtle.select(i)
- if turtle.compareUp() then
- matchUp = true
- end
- end
- end
- return somethingUp, matchUp
- end
- function scan()
- local somethingDown, somethingForward, matchDown, matchForward = innerscan()
- if not somethingDown then
- state = "toGround"
- else
- if matchDown then
- state = "forwardAndScan"
- else
- if somethingForward then
- if matchForward then
- -- train track up?
- state = "riseAndScan"
- else
- turtle.turnLeft()
- end
- else
- turtle.turnLeft()
- end
- end
- end
- end
- function moveDown()
- local somethingDown, somethingForward, matchDown, matchForward = innerscan()
- if not somethingDown then
- turtle.down()
- return true
- else
- return false
- end
- end
- function moveUp()
- local somethingUp, matchUp = upscan()
- if not somethingUp then
- turtle.up()
- return true
- else
- return false
- end
- end
- function moveForward()
- local somethingDown, somethingForward, matchDown, matchForward = innerscan()
- if not somethingForward then
- turtle.forward()
- return true
- else
- return false
- end
- end
- function moveLeft()
- turtle.turnLeft()
- return moveForward()
- end
- function moveRight()
- turtle.turnRight()
- return moveForward()
- end
- function moveBackTrack()
- turtle.turnRight()
- turtle.turnRight()
- moveForward()
- turtle.turnLeft()
- turtle.turnLeft()
- end
- local lastSide="left"
- function run()
- if state == "toGround" then
- if not moveDown() then
- state = "scan"
- end
- elseif state == "backTrack" then
- state = "scanLeft-Right"
- elseif state == "scanLeft-Right" then
- if lastSide == "right" then
- lastSide="left"
- if moveLeft() then
- local somethingDown, somethingForward, matchDown, matchForward = innerscan()
- if matchDown then
- -- right path!
- state = "forwardAndScan"
- else
- -- wrong path, double back
- moveBackTrack()
- turtle.turnRight()
- end
- else
- turtle.turnRight()
- end
- else
- lastSide="right"
- if moveRight() then
- local somethingDown, somethingForward, matchDown, matchForward = innerscan()
- if matchDown then
- -- right path!
- state = "forwardAndScan"
- else
- moveBackTrack()
- turtle.turnLeft()
- end
- else
- turtle.turnLeft()
- end
- end
- elseif state == "riseAndScan" then
- if moveUp() then
- if moveForward() then
- local somethingDown, somethingForward, matchDown, matchForward = innerscan()
- if matchDown then
- -- one step up was a good assumption!
- state = "forwardAndScan"
- else
- state = "scanLeft-Right"
- end
- else
- state = "scanLeft-Right"
- end
- end
- elseif state == "forwardAndScan" then
- if moveForward() then
- local somethingDown, somethingForward, matchDown, matchForward = innerscan()
- if matchDown then
- -- right path, continue
- state = "forwardAndScan"
- else
- -- wrong path, is left or right
- moveBackTrack()
- state = "scanLeft-Right"
- end
- else
- state = "scanLeft-Right"
- end
- elseif state == "giveUp" then
- print("Giving up")
- return false
- elseif state == "scan" then
- scan()
- elseif state == "followLoop" then
- else
- state = "scan"
- end
- return true
- end
- while run() do
- -- nothing
- print("state: " .. state)
- end
- print("normal end")
Advertisement
Add Comment
Please, Sign In to add comment