zortag

moveto

May 13th, 2014
1,298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.38 KB | None | 0 0
  1. -- ComputerCraft Turtle moving program
  2. -- Needs a GPS tower (search http://computercraft.info for instructions)
  3. -- I use this for my 5x5farm script ()
  4. -- This script is not mine
  5.  
  6. local tArgs = {...}
  7. local x = tonumber(tArgs[1])
  8. local y = tonumber(tArgs[2])
  9. local z = tonumber(tArgs[3])
  10. local curx, cury, curz, dir
  11.  
  12. function getPos()
  13.   return gps.locate(3)
  14. end
  15.  
  16. function getDir()
  17.   local dir, x, y, z
  18.   x, y, z = getPos()
  19.   while not turtle.forward() do
  20.     while not turtle.up() do
  21.       turtle.digUp()
  22.     end
  23.   end
  24.   nx, ny, nz = getPos()
  25.   --print ("New: "..nx..","..ny..","..nz)
  26.   if (x == nx) then
  27.     if (nz > z) then
  28.       dir = 2
  29.     else
  30.       dir = 0
  31.     end
  32.   else
  33.     if (nx > x) then
  34.        dir = 3
  35.     else
  36.        dir = 1
  37.     end
  38.   end
  39.   return dir
  40. end
  41.  
  42.  
  43. function setDir(toDir)
  44.   while toDir ~= dir do
  45.     turtle.turnLeft()
  46.     if dir == 3 then
  47.        dir = 0
  48.     else
  49.        dir = dir+1
  50.     end
  51.   end
  52. end
  53.  
  54. function moveX()
  55.   distxx = x - curx
  56.   --print(distx)
  57.   if (x > curx) then
  58.      setDir(3)
  59.   else
  60.      setDir(1)
  61.   end
  62.   distx = math.abs(distx)
  63.   --print(distx)
  64.  
  65.   for i = 1, distx do
  66.     while not turtle.forward() do
  67.       while not turtle.up() do
  68.         turtle.digUp()
  69.       end
  70.     end
  71.   end
  72. end
  73.  
  74. function moveZ()
  75.   distz = z - curz
  76.   if (z < curz) then
  77.      setDir(0)
  78.   else
  79.      setDir(2)
  80.   end
  81.   distz = math.abs(distz)
  82.   -- print(distz)
  83.  
  84.   for i = 1, distz do
  85.     while not turtle.forward() do
  86.       while not turtle.up() do
  87.         turtle.digUp()
  88.       end
  89.     end
  90.   end
  91. end
  92.  
  93. function moveY()
  94.   disty = y - cury
  95.   disty = math.abs(disty)
  96.   if (y < cury) then
  97.     for i = 1, disty do
  98.       while not turtle.down() do
  99.         turtle.digDown()
  100.       end
  101.     end
  102.   else
  103.     for i = 1, disty do
  104.       while not turtle.up() do
  105.         turtle.digUp()
  106.       end
  107.     end
  108.   end
  109. end
  110.  
  111.  
  112.  
  113. if not x or not y or not z then
  114.   print("Must supply X Y Z")
  115.   exit()
  116. end
  117.  
  118. rednet.open("right")
  119. --print (x..","..y..","..z)
  120. dir = getDir()
  121. curx, cury, curz = getPos()
  122. distx = x - curx
  123. disty = y - cury
  124. distz = z - curz
  125. --print (" Current: "..curx..","..cury..","..curz)
  126. --print ("Distance: ",, distx,,",".. disty..","..distz)
  127.  
  128. moveX()
  129. curx, cury, curz = getPos()
  130. moveZ()
  131. curx, cury, curz = getPos()
  132. moveY()
  133. curx, cury, curz = getPos()
  134. print("Current: "..curx..","..cury..","..curz)
Advertisement
Add Comment
Please, Sign In to add comment