norway240

Computer Craft: Goto

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