xKevinn

Untitled

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