Clem105

gps

Jul 2nd, 2014
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.83 KB | None | 0 0
  1.    
  2.  
  3.    
  4.  
  5.    
  6.    
  7.    
  8.    
  9.    
  10.    
  11.    rednet.open("right") --Open rednet and clear the console
  12. term.clear()
  13. term.setCursorPos(1,1)
  14. local tArgs = { ... } --Get the coordinates from alongside the program launch (goto x y z)
  15. local gox = tonumber(tArgs[1])
  16. local goy = tonumber(tArgs[2])
  17. local goz = tonumber(tArgs[3])
  18. if #tArgs ~= 3 then --Make sure there are 3 coordinates entered else the program will return an error
  19. print("Usage: goto <x> <y> <z>")
  20. end
  21. local digBlocks = false --Extra parameters: whether to dig blocks or attempt to go over
  22. local goneUp = 0 --dir and goneUp are used to keep track of position
  23. local dir = 0
  24. function forward()
  25. while not turtle.forward() do --If turtle cannot go forward (either out of fuel or block blocking)
  26. print("Can't move, checking fuel")
  27.   if turtle.getFuelLevel() == 0 then
  28.    turtle.select(16)
  29.    turtle.refuel(1)
  30.   end
  31.   if digBlocks then --If digBlocks var was true the turtle will dig thorugh the blockage otherwise will go over
  32.    turtle.dig()
  33.   else
  34.    turtle.up()
  35.    goneUp = goneUp + 1
  36.   end
  37. end
  38. while goneUp > 0 and not turtle.detectDown() do --Make sure to compensate for going up and over blocks by going down when next possible
  39.   turtle.down()
  40.   goneUp = goneUp - 1
  41. end
  42. end
  43. function up() --Same as forward, for up
  44. while not turtle.up() do
  45. print("Can't move, checking fuel")
  46.   if turtle.getFuelLevel() == 0 then
  47.    turtle.select(16)
  48.    turtle.refuel(1)
  49.   end
  50.   if digBlocks then
  51.    turtle.digUp()
  52.   end
  53. end
  54. end
  55. function down() --Same as forward, for down
  56. while not turtle.down() do
  57. print("Can't move, checking fuel")
  58.   if turtle.getFuelLevel() == 0 then
  59.    turtle.select(16)
  60.    turtle.refuel(1)
  61.   end
  62.   if digBlocks then
  63.    turtle.digDown()
  64.   end
  65. end
  66. end
  67. function getPos() --Gets the position of the turtle from local GPS towers
  68. print("Getting position")
  69. cx, cy, cz = gps.locate(10)
  70. print(cx, cy, cz)
  71. end
  72. function getDir() --Gets the heading of the turtle by taking position, moving forward 1 and comparing the 2 positions
  73. print("Getting direction")
  74. getPos()
  75. ox, oy, oz = cx, cy, cz
  76. turtle.back()
  77. getPos()
  78. if oz < cz then dir = 0
  79. elseif oz > cz then dir = 2
  80. elseif ox > cx then dir = 1
  81. elseif ox < cx then dir = 3 end
  82. print(dir)
  83. forward()
  84. getPos()
  85. end
  86. function turn(d) --Turns to heading "d", uses getDir() to calculate how many turns are needed
  87. getDir()
  88. print("Aligning")
  89. print(dir, d)
  90. while dir ~= d do
  91.   turtle.turnRight()
  92.   dir = dir + 1
  93.   if dir == 4 then dir = 0 end
  94. end
  95. end
  96. function moveX() --Combine the past functions to move along the x axis
  97. print("Moving X")
  98. getPos()
  99. if gox > cx then --The current and destination coordinates are compared to decide which heading is needed and distance to move
  100.   turn(1)
  101.   for x = 1, gox - cx do
  102.    forward()
  103.    cx = cx + 1
  104.   end
  105. elseif gox < cx then
  106.   turn(3)
  107.   for x = 1, cx - gox do
  108.    forward()
  109.    cx = cx - 1
  110.   end
  111. end
  112. end
  113. function moveZ() --The same as moveX() but for the Z axis
  114. print("Moving Z")
  115. getPos()
  116. if goz > cz then
  117.   turn(2)
  118.   for z = 1, goz - cz do
  119.    forward()
  120.    cz = cz + 1
  121.   end
  122. elseif goz < cz then
  123.   turn(0)
  124.   for z = 1, cz - goz do
  125.    forward()
  126.    cz = cz - 1
  127.   end
  128. end
  129. end
  130. function moveY() --The same as moveX() but for the Y axis, as the movement is vertical no turn calcuations are needed so this function is shorter
  131. print("Moving Y")
  132. getPos()
  133. if goy > cy then
  134.   for z = 1, goy - cy do
  135.    up()
  136.    cy = cy + 1
  137.   end
  138. elseif goy < cy then
  139.   for z = 1, cy - goy do
  140.    down()
  141.    cy = cy - 1
  142.   end
  143. end
  144. end
  145. getPos()
  146. if goy > cy then --If the turtle has to move upwards to get to the destination if moves up first, if it needs to move down if moves down last
  147. moveY()
  148. moveX()
  149. moveZ()
  150. for i = 2,13 do
  151.     turtle.select(i)
  152.     turtle.drop()
  153. end
  154. turn(3)
  155. else
  156. moveX()
  157. moveZ()
  158. moveY()
  159. for i = 2,13 do
  160.     turtle.select(i)
  161.     turtle.drop()
  162. end
  163. turn(3)
  164. end
Advertisement
Add Comment
Please, Sign In to add comment