Lion_Kor

move to coordinates (gps based)

Apr 6th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. local args = { ... }
  2.  
  3. function loc()
  4. return vector.new(gps.locate(5))
  5. end
  6.  
  7. -- function setOrientationToPlusX()
  8. -- while true do
  9. -- local old = loc()
  10. -- if not (turtle.forward()) then
  11. -- turtle.dig()
  12. -- turtle.forward()
  13. -- end
  14. -- local new = loc()
  15. -- turtle.back()
  16. -- turtle.place()
  17. -- local dif = old - new
  18. -- if dif.x == 1 then
  19. -- break
  20. -- else
  21. -- turtle.turnRight()
  22. -- end
  23. -- end
  24. -- end
  25.  
  26. function forwardX()
  27. turtle.down()
  28. while not turtle.detectDown() do
  29. turtle.down()
  30. end
  31. while not turtle.forward() do
  32. while not turtle.up() do
  33. while not turtle.back() do
  34. correctY()
  35. return
  36. end
  37. end
  38. end
  39. end
  40.  
  41. function forwardY()
  42. turtle.down()
  43. while not turtle.detectDown() do
  44. turtle.down()
  45. end
  46. while not turtle.forward() do
  47. while not turtle.up() do
  48. while not turtle.back() do
  49. return
  50. end
  51. end
  52. end
  53. end
  54.  
  55. function moveBy (x, y) -- no specific z movement
  56. correctH(1)
  57. if x > 0 then
  58. for i = 1, x, 1 do
  59. forward()
  60. end
  61. end
  62. if x < 0 then
  63. turtle.turnRight()
  64. turtle.turnRight()
  65. for i = -1, x, -1 do
  66. forward()
  67. end
  68. turtle.turnLeft()
  69. turtle.turnLeft()
  70. end
  71. if y > 0 then
  72. turtle.turnRight()
  73. for j = 1, y, 1 do
  74. forward()
  75. end
  76. turtle.turnLeft()
  77. end
  78. if y < 0 then
  79. turtle.turnLeft()
  80. for j = -1, y, -1 do
  81. forward()
  82. end
  83. turtle.turnRight()
  84. end
  85. end
  86.  
  87. function correctX()
  88. if loc().x > target.x then
  89. targetH = 1
  90. end
  91. if loc().x < target.x then
  92. targetH = 3
  93. end
  94.  
  95. if loc().x == target.x then
  96. return
  97. end
  98.  
  99. correctH(targetH)
  100. while true do
  101. if loc().x == target.x then
  102. return
  103. else
  104. forwardX()
  105. end
  106. end
  107. end
  108.  
  109. function correctY()
  110. if loc().y > target.y then
  111. targetH = 2
  112. end
  113. if loc().y < target.y then
  114. targetH = 0
  115. end
  116.  
  117. if loc().y == target.y then
  118. return
  119. end
  120.  
  121. correctH(targetH)
  122. while true do
  123. if loc().y == target.y then
  124. return
  125. else
  126. forwardY()
  127. end
  128. end
  129. end
  130.  
  131. function correctZ()
  132. while true do
  133. if loc().z > target.z then
  134. if not turtle.down() then
  135. --turtle.digDown()
  136. turtle.down()
  137. end
  138. end
  139. if loc().z < target.z then
  140. if not turtle.up() then
  141. --turtle.digUp()
  142. turtle.up()
  143. end
  144. end
  145. if loc().z == target.z then
  146. return
  147. end
  148. end
  149. end
  150.  
  151. function correctH(h)
  152. old = loc()
  153. while turtle.detect() do
  154. turtle.turnRight()
  155. end
  156. turtle.forward()
  157. new = loc()
  158. turtle.back()
  159. diff = old - new
  160. -- (0, -1, 0) is f = 0
  161. -- (1, 0, 0) is f = 1
  162. -- (0, 1, 0) is f = 2
  163. -- (-1, 0, 0) is f = 3
  164. if diff.y == -1 then
  165. f = 0
  166. end
  167. if diff.x == 1 then
  168. f = 1
  169. end
  170. if diff.y == 1 then
  171. f = 2
  172. end
  173. if diff.x == -1 then
  174. f = 3
  175. end
  176. while true do
  177. if f == h then
  178. return
  179. end
  180. f = f + 1
  181. turtle.turnRight()
  182. if f > 3 then
  183. f = 0
  184. end
  185. end
  186. end
  187.  
  188. function move()
  189. correctX()
  190. correctY()
  191. correctZ()
  192. correctH(h)
  193. end
  194.  
  195. x = tonumber(args[1])
  196. y = tonumber(args[2])
  197. z = tonumber(args[3])
  198. h = tonumber(args[4])
  199. target = vector.new(x, y, z)
  200. --moveBy (diff.x, diff.y)
  201. move()
  202. correctZ()
  203. correctH(h)
Advertisement
Add Comment
Please, Sign In to add comment