Advertisement
1lann

turtle

Apr 14th, 2014
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.12 KB | None | 0 0
  1. local home = {x = -208, y = 65, z = 197}
  2.  
  3. local function goHomeByGPS(turned)
  4.     local function travel(dist, positive)
  5.         if ((math.abs(dist) == dist) and positive) or ((math.abs(dist) ~= dist) and not positive) then
  6.             print("Heading in the right direction!")
  7.         else
  8.             print("Eek, wrong way!")
  9.             turtle.turnLeft()
  10.             turtle.turnLeft()
  11.             turtle.forward()
  12.             turtle.forward()
  13.             print("There we go!")
  14.         end
  15.         print("I need to travel "..math.abs(dist)-1)
  16.         print("Press a key to continue...")
  17.         os.pullEvent("key")
  18.         for i = 1, math.abs(dist)-1 do
  19.             if not turtle.forward() then
  20.                 if turned then
  21.                     return false
  22.                 end
  23.                 turtle.turnLeft()
  24.                 local ret = goHomeByGPS(true)
  25.                 return ret
  26.             end
  27.         end
  28.         turtle.turnLeft()
  29.         return goHomeByGPS()
  30.     end
  31.  
  32.     local initX, initY, initZ = gps.locate()
  33.     local diffX = (initX - home.x)*-1
  34.     local diffY  = (initY - home.y)*-1
  35.     local diffZ = (initZ - home.z)*-1
  36.     print("I'm X:"..diffX.." and Z:"..diffZ.." away from home!")
  37.     if (diffX == 0) and (diffZ == 0) then
  38.         if math.abs(diffY) == diffY then
  39.             for i = 1, diffY do
  40.                 turtle.up()
  41.             end
  42.         else
  43.             for i = 1, math.abs(diffY) do
  44.                 turtle.down()
  45.             end
  46.         end
  47.             print("I should be home!")
  48.             local initX, initY, initZ = gps.locate()
  49.             if home.x == initX and home.y == initY and home.z == initZ then
  50.                 print("Now, time to re-orientate...")
  51.                 for i = 1, 3 do
  52.                     turtle.forward()
  53.                     local x = gps.locate()
  54.                     turtle.back()
  55.                     if x > home.x then
  56.                         print("Alright, I'm home! Awesome!")
  57.                         return true
  58.                     end
  59.                     turtle.turnLeft()
  60.                 end
  61.                 print("I should 100% be theoretically home")
  62.                 return true
  63.             else
  64.                 print("Ah crap, I miscalculated :/")
  65.                 print("Well, I'm screwed")
  66.                 return false
  67.             end
  68.     elseif turtle.forward() then
  69.         secondX, _, secondZ = gps.locate()
  70.         if secondX > initX then
  71.             if diffX == 0 then
  72.                 print("Oh nope, im already in the right X position")
  73.                 turtle.back()
  74.                 turtle.turnLeft()
  75.                 local ret = goHomeByGPS()
  76.             else
  77.                 return travel(diffX, true)
  78.             end
  79.         elseif secondX < initX then
  80.             if diffX == 0 then
  81.                 print("Oh nope, im already in the right X position")
  82.                 turtle.back()
  83.                 turtle.turnLeft()
  84.                 local ret = goHomeByGPS()
  85.             else
  86.                 return travel(diffX, false)
  87.             end
  88.         elseif secondZ > initZ then
  89.             if diffZ == 0 then
  90.                 print("Oh nope, im already in the right Z position")
  91.                 turtle.back()
  92.                 turtle.turnLeft()
  93.                 local ret = goHomeByGPS()
  94.             else
  95.                 return travel(diffZ, true)
  96.             end
  97.         elseif secondZ < initZ then
  98.             if diffZ == 0 then
  99.                 print("Oh nope, im already in the right Z position")
  100.                 turtle.back()
  101.                 turtle.turnLeft()
  102.                 local ret = goHomeByGPS()
  103.             else
  104.                 return travel(diffZ, false)
  105.             end
  106.         end
  107.     elseif turtle.back() then
  108.         print("Wait, lemmi just turn around")
  109.         turtle.forward()
  110.         turtle.turnLeft()
  111.         turtle.turnLeft()
  112.         local ret = goHomeByGPS()
  113.         return ret
  114.     elseif turned then
  115.         print("Crap, I'm stuck!")
  116.         print("D: Turtle is sad")
  117.         return false
  118.     else
  119.         print("Friggen wall...")
  120.         turtle.turnLeft()
  121.         local ret = goHomeByGPS()
  122.         return ret
  123.     end
  124. end
  125. print(goHomeByGPS())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement