Guest User

goto

a guest
Apr 3rd, 2013
1,400
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local tArgs = {...}
  2. local x = tonumber(tArgs[1])
  3. local y = tonumber(tArgs[2])
  4. local z = tonumber(tArgs[3])
  5. local curx, cury, curz, dir
  6.  
  7. function getPos()
  8.    test = gps.locate(3)
  9.    while test == nil do
  10.       print("Can't find me!")
  11.       sleep(5)
  12.       test = gps.locate(3)
  13.    end
  14.    return gps.locate(3)
  15. end
  16.  
  17. function getDir()
  18.    local dir, x, y, z
  19.    x, y, z = getPos()
  20.    --print("Old: "..x..","..y..","..z)
  21.    while not turtle.forward() do
  22.       while not turtle.up() do
  23.         turtle.digUp()
  24.       end
  25.    end
  26.    nx, ny, nz = getPos()
  27.    --print ("New: "..nx..","..ny..","..nz)
  28.    if (x == nx) then
  29.       if (nz > z) then
  30.          dir = 2
  31.       else
  32.          dir = 0
  33.       end
  34.    else
  35.       if (nx > x) then
  36.          dir = 3
  37.       else
  38.          dir = 1
  39.       end
  40.    end
  41.    return dir
  42. end
  43.  
  44.  
  45. function setDir(toDir)
  46. --print("toDir: "..toDir.." dir: "..dir)
  47.    while toDir ~= dir do
  48.       turtle.turnLeft()
  49.       if dir == 3 then
  50.          dir=0
  51.       else
  52.          dir=dir+1
  53.       end
  54.    end  
  55. end
  56.  
  57. function moveX()
  58.    distx = x - curx
  59.    --print(distx)
  60.    if (x > curx) then
  61.       setDir(3)
  62.    else
  63.       setDir(1)
  64.    end
  65.    distx = math.abs(distx)
  66.    --print(distx)
  67.  
  68.    for i = 1, distx do
  69.       while not turtle.forward() do
  70.          if math.random(1,2) == 1 then
  71.             if not turtle.up() then turtle.down() end
  72.          else
  73.             if not turtle.down() then turtle.up() end
  74.          end
  75.          --while not turtle.up() do
  76.          --   turtle.digUp()
  77.          --end
  78.       end
  79.    end
  80.  end
  81.  
  82. function moveZ()
  83.    distz = z - curz
  84.    if (z < curz) then
  85.       setDir(0)
  86.    else
  87.       setDir(2)
  88.    end
  89.    distz = math.abs(distz)
  90.    --print(distz)
  91.  
  92.    for i = 1, distz do
  93.       while not turtle.forward() do
  94.          if math.random(1,2) == 1 then
  95.             if not turtle.up() then turtle.down() end
  96.          else
  97.             if not turtle.up() then turtle.down() end
  98.          end
  99.          --while not turtle.up() do
  100.          --   turtle.digUp()
  101.          --end
  102.       end
  103.    end
  104. end
  105.  
  106. function moveY()
  107.    disty = y - cury
  108.    disty = math.abs(disty)
  109.    if (y < cury) then
  110.       for i = 1, disty do
  111.          while not turtle.down() do
  112.             turtle.digDown()
  113.          end
  114.       end
  115.    else
  116.       for i = 1, disty do
  117.          while not turtle.up() do
  118.             turtle.digUp()
  119.           end
  120.        end
  121.    end
  122.    
  123. end
  124.  
  125. --=====================--
  126. if not x or not y or not z then
  127.    print("Must supply X Y Z")
  128.    exit()
  129. end
  130.  
  131. rednet.open("right")
  132. --print (x..","..y..","..z)
  133. curx, cury, curz = getPos()
  134. if (curx == x) and (cury == y) and (curz == z) then
  135.    print("Already here.")
  136.    return
  137. end
  138. dir = getDir()
  139. curx, cury, curz = getPos()
  140. distx = x - curx
  141. disty = y - cury
  142. distz = z - curz
  143. --print ("Current: "..curx..","..cury..","..curz)
  144. --print ("Distance: "..distx..","..disty..","..distz)
  145.  
  146. if (x ~= curx) then
  147.    moveX()
  148.    curx, cury, curz = getPos()
  149. end
  150. if (z ~= curz) then
  151.    moveZ()
  152.    curx, cury, curz = getPos()
  153. end
  154. moveY()
  155. curx, cury, curz = getPos()
  156. --print ("Current: "..curx..","..cury..","..curz)
  157. setDir(0)
  158. rednet.close("right")
RAW Paste Data