Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local home = {x = -208, y = 65, z = 197}
- local function goHomeByGPS(turned)
- local function travel(dist, positive)
- if ((math.abs(dist) == dist) and positive) or ((math.abs(dist) ~= dist) and not positive) then
- print("Heading in the right direction!")
- else
- print("Eek, wrong way!")
- turtle.turnLeft()
- turtle.turnLeft()
- turtle.forward()
- turtle.forward()
- print("There we go!")
- end
- print("I need to travel "..math.abs(dist)-1)
- print("Press a key to continue...")
- os.pullEvent("key")
- for i = 1, math.abs(dist)-1 do
- if not turtle.forward() then
- if turned then
- return false
- end
- turtle.turnLeft()
- local ret = goHomeByGPS(true)
- return ret
- end
- end
- turtle.turnLeft()
- return goHomeByGPS()
- end
- local initX, initY, initZ = gps.locate()
- local diffX = (initX - home.x)*-1
- local diffY = (initY - home.y)*-1
- local diffZ = (initZ - home.z)*-1
- print("I'm X:"..diffX.." and Z:"..diffZ.." away from home!")
- if (diffX == 0) and (diffZ == 0) then
- if math.abs(diffY) == diffY then
- for i = 1, diffY do
- turtle.up()
- end
- else
- for i = 1, math.abs(diffY) do
- turtle.down()
- end
- end
- print("I should be home!")
- local initX, initY, initZ = gps.locate()
- if home.x == initX and home.y == initY and home.z == initZ then
- print("Now, time to re-orientate...")
- for i = 1, 3 do
- turtle.forward()
- local x = gps.locate()
- turtle.back()
- if x > home.x then
- print("Alright, I'm home! Awesome!")
- return true
- end
- turtle.turnLeft()
- end
- print("I should 100% be theoretically home")
- return true
- else
- print("Ah crap, I miscalculated :/")
- print("Well, I'm screwed")
- return false
- end
- elseif turtle.forward() then
- secondX, _, secondZ = gps.locate()
- if secondX > initX then
- if diffX == 0 then
- print("Oh nope, im already in the right X position")
- turtle.back()
- turtle.turnLeft()
- local ret = goHomeByGPS()
- else
- return travel(diffX, true)
- end
- elseif secondX < initX then
- if diffX == 0 then
- print("Oh nope, im already in the right X position")
- turtle.back()
- turtle.turnLeft()
- local ret = goHomeByGPS()
- else
- return travel(diffX, false)
- end
- elseif secondZ > initZ then
- if diffZ == 0 then
- print("Oh nope, im already in the right Z position")
- turtle.back()
- turtle.turnLeft()
- local ret = goHomeByGPS()
- else
- return travel(diffZ, true)
- end
- elseif secondZ < initZ then
- if diffZ == 0 then
- print("Oh nope, im already in the right Z position")
- turtle.back()
- turtle.turnLeft()
- local ret = goHomeByGPS()
- else
- return travel(diffZ, false)
- end
- end
- elseif turtle.back() then
- print("Wait, lemmi just turn around")
- turtle.forward()
- turtle.turnLeft()
- turtle.turnLeft()
- local ret = goHomeByGPS()
- return ret
- elseif turned then
- print("Crap, I'm stuck!")
- print("D: Turtle is sad")
- return false
- else
- print("Friggen wall...")
- turtle.turnLeft()
- local ret = goHomeByGPS()
- return ret
- end
- end
- print(goHomeByGPS())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement