Advertisement
downwind

goto.lua file for turtle movement in ComputerCraft

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