blacke00

direGoto

Nov 24th, 2012
429
0
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.    return gps.locate(3)
  9. end
  10.  
  11. function getDir()
  12.    local dir, x, y, z
  13.    x, y, z = getPos()
  14.    --print("Old: "..x..","..y..","..z)
  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.    distx = 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.  
  106. end
  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)
  131. rednet.close("right")
Add Comment
Please, Sign In to add comment