Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Set the Turtle home location and heading. X Y Z Heading
- local home ={382,9,69,0}
- local facing=4
- -- Sends the Turtle home, and faces it properly.
- function goHome()
- turtle.select(1)
- turtle.refuel()
- local startingLocation= gps.locate()
- while (startingLocation[2]<(home[2]+1)) do
- turtle.up()
- end
- turtle.refuel()
- if facing==4 then
- facing=getOrientation()
- end
- startingLocation=gps.locate()
- -- Moves turtle to correct X
- if startingLocation[1]~=home[1] then
- while facing~= 1 do
- turtle.turnLeft()
- facing = (facing+1)%4
- end
- while startingLocation[1]>home[1] do
- turtle.forward()
- end
- while startingLocation[1]<home[1] do
- turtle.back()
- end
- end
- -- Moves turtle to correct Z
- if startingLocation[3]~=home[3] then
- while facing ~= 0 do
- turtle.turnLeft()
- facing = (facing+1)%4
- end
- while startingLocation[3]<home[3] do
- turtle.forward()
- end
- while startingLocation[3]>home[3] do
- turtle.back()
- end
- end
- -- Sets turtle to correct facing direction
- while facing ~= home[4] do
- turtle.turnLeft()
- facing = (facing+1)%4
- end
- turtle.down()
- print("I am home.")
- end
- -- Finds turtle facing direction North/-z=2 South/+z=0 East/+x=3 West/-x=1
- function getOrientation()
- loc1 = vector.new(gps.locate(2, false))
- if not turtle.forward() then
- for j=1,6 do
- if not turtle.forward() then
- turtle.dig()
- else break end
- end
- end
- loc2 = vector.new(gps.locate(2, false))
- heading = loc2 - loc1
- turtle.back()
- return ((heading.x + math.abs(heading.x) * 2) + (heading.z + math.abs(heading.z) * 3))%4
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement