ecco7777

goTo gps function

Jul 27th, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.80 KB | None | 0 0
  1. function go(side,distance)
  2. for i=1, distance do
  3. if side=="up" then turtle.up() end
  4. if side=="down" then turtle.down() end
  5. if side=="forward" then turtle.forward() end
  6. if side=="back" then turtle.back() end
  7. end
  8. end
  9.  
  10. function goTo(x,y,z)
  11. c=peripheral.wrap("left")
  12. myX,myZ,myY=gps.locate()
  13. side=c.getFacing()
  14. if myX==nil then
  15. print("no gps found :(")
  16. end
  17.  
  18. divX=x-myX
  19. divY=y-myY
  20. divZ=z-myZ
  21.  
  22. print("divX "..divX)
  23. print("divY "..divY)
  24. print("divZ "..divZ)
  25.  
  26. if side==0 then if divZ>=1 then shell.run("go forward "..tostring(divZ)) else shell.run("go back "..tostring(math.sqrt(divZ*divZ))) end end
  27. if side==2 then if divZ>=1 then shell.run("go back "..tostring(divZ)) else shell.run("go forward "..tostring(math.sqrt(divZ*divZ))) end end
  28. if side==3 then if divX>=1 then shell.run("go forward "..tostring(divX)) else shell.run("go back "..tostring(math.sqrt(divX*divX))) end end
  29. if side==1 then if divX>=1 then shell.run("go back "..tostring(divX)) else shell.run("go forward "..tostring(math.sqrt(divX*divX))) end end
  30. turtle.turnLeft()
  31. side=c.getFacing()
  32. if side==0 then if divZ>=1 then shell.run("go forward "..tostring(divZ)) else shell.run("go back "..tostring(math.sqrt(divZ*divZ))) end end
  33. if side==2 then if divZ>=1 then shell.run("go back "..tostring(divZ)) else shell.run("go forward "..tostring(math.sqrt(divZ*divZ))) end end
  34. if side==3 then if divX>=1 then shell.run("go forward "..tostring(divX)) else shell.run("go back "..tostring(math.sqrt(divX*divX))) end end
  35. if side==1 then if divX>=1 then shell.run("go back "..tostring(divX)) else shell.run("go forward "..tostring(math.sqrt(divX*divX))) end end
  36. if divY>=1 then shell.run("go up "..tostring(divY)) else shell.run("go down "..tostring(math.sqrt(divY*divY))) end
  37. end
  38.  
  39. args = {...}
  40. x=args[1]
  41. y=args[2]
  42. z=args[3]
  43. if args~=nil then
  44. goTo(x,y,z)
  45. end
Add Comment
Please, Sign In to add comment