Advertisement
jille_Jr

CC: Goto

Oct 18th, 2012
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.82 KB | None | 0 0
  1. -- Don't use this program yet!
  2. -- It's not ready for use!!
  3.  
  4. local tArgs = {...}
  5. if #tArgs < 3 then
  6.   write("Usage: ")
  7.   write(shell.getRunningProgram())
  8.   print(" <x> <y> <z>")
  9.   return
  10. end
  11.  
  12. if tonumber(tArgs[2]) < 0 then
  13.   print("Too low y value!")
  14.   return
  15. end
  16.  
  17. --[[ Using relative pos ]]--
  18.  
  19. --local posX, posY, posZ = 0
  20. --dir = 0
  21.  
  22. --[[ End ]]--
  23.  
  24. --[[ Using GPS ]]--
  25.  
  26. -- Direction:
  27. -- +------ z ------+
  28. -- |       0       |
  29. -- x  1  turtle  3 |
  30. -- |       2       |
  31. -- +---------------+
  32.  
  33. local dir
  34.  
  35. rednet.open("right")
  36. local posX, posY, posZ = gps.locate(.5)
  37. local curX, curY, curZ = posX, posY, posZ
  38. rednet.close("right")
  39. if not posX then
  40.   print("Could not locate position!")
  41.   return
  42. end
  43.  
  44. function checkGPS()
  45.   rednet.open("right")
  46.   curX, curY, curZ = gps.locate(.5)
  47.   rednet.close("right")
  48. end
  49.  
  50. function checkDir()
  51.   if curX ~= posX then
  52.     if curX < posX then
  53.       dir = 3
  54.     elseif curX > posX then
  55.       dir = 1
  56.     end
  57.   elseif curZ ~= posZ then
  58.     if curZ < posZ then
  59.       dir = 2
  60.     elseif curZ > posZ then
  61.       dir = 0
  62.     end
  63.   end
  64. end
  65.  
  66. function invertDir(dirVariable)
  67.   if type(dirVariable) ~= "number" then
  68.     error("Number expected, got "..type(dirVariable))
  69.   end
  70.   if dirVariable < 2 then
  71.     return dirVariable + 2
  72.   else
  73.     return dirVariable - 2
  74.   end
  75. end
  76.  
  77. if not turtle.forward() then
  78.   if not turtle.back() then
  79.     turtle.turnRight()
  80.     if not turtle.forward() then
  81.       if not turtle.back() then
  82.         turtle.turnLeft()
  83.         print("No empty space found!")
  84.         return
  85.       else
  86.         checkGPS()
  87.         checkDir()
  88.         while not turtle.forward() do
  89.           if not turtle.dig() then
  90.             turtle.attack()
  91.           end
  92.         end
  93.       end
  94.     else
  95.       checkGPS()
  96.       checkDir()
  97.       for q = 1, 2 do
  98.         turtle.turnRight()
  99.       end
  100.       while not turtle.forward() do
  101.         if not turtle.dig() then
  102.           turtle.attack()
  103.         end
  104.       end
  105.       dir = invertDir(dir)
  106.     end
  107.   else
  108.     checkGPS()
  109.     checkDir()
  110.     while not turtle.forward() do
  111.       if not turtle.dig() then
  112.         turtle.attack()
  113.       end
  114.     end
  115.   end
  116. else
  117.   checkGPS()
  118.   checkDir()
  119.   for w = 1, 2 do
  120.     turtle.turnRight()
  121.   end
  122.   while not turtle.forward() do
  123.     if not turtle.dig() then
  124.       turtle.attack()
  125.     end
  126.   end
  127.   dir = invertDir(dir)
  128. end
  129.  
  130. --[[ End ]]--
  131.  
  132. function goUp()
  133.   while not turtle.up() do
  134.     if not turtle.digUp() then
  135.       if not turtle.attackUp() then
  136.         sleep(.1)
  137.       end
  138.     end
  139.   end
  140.   posY = posY + 1
  141. end
  142. function goDown()
  143.   while not turtle.down() do
  144.     if not turtle.digDown() then
  145.       if not turtle.attackDown() then
  146.         sleep(.1)
  147.       end
  148.     end
  149.   end
  150.   posY = posY - 1
  151. end
  152. function left()
  153.   if dir > 3-1 then
  154.     dir = 0
  155.   else
  156.     dir = dir + 1
  157.   end
  158.   turtle.turnLeft()
  159. end
  160. function right()
  161.   if dir < 0+1 then
  162.     dir = 3
  163.   else
  164.     dir = dir - 1
  165.   end
  166.   turtle.turnRight()
  167. end
  168. function goForward()
  169.   while not turtle.forward() do
  170.     goUp()
  171.   end
  172.   if dir == 0 then
  173.     posZ = posZ + 1
  174.   elseif dir == 1 then
  175.     posX = posX + 1
  176.   elseif dir == 2 then
  177.     posZ = posZ - 1
  178.   elseif dir == 3 then
  179.     posX = posX - 1
  180.   else
  181.     error("Undefined direction.")
  182.   end
  183. end
  184. function goBack()
  185.   while not turtle.back() do
  186.     goUp()
  187.   end
  188.   if dir == 0 then
  189.     posZ = posZ - 1
  190.   elseif dir == 1 then
  191.     posX = posX - 1
  192.   elseif dir == 2 then
  193.     posZ = posZ + 1
  194.   elseif dir == 3 then
  195.     posX = posX + 1
  196.   else
  197.     error("Undefined direction.")
  198.   end
  199. end
  200. function faceZ()
  201.   if dir ~= 0 then
  202.     if dir == 1 then
  203.       right()
  204.     else
  205.       repeat
  206.         left()
  207.       until dir == 0
  208.     end
  209.   end
  210. end
  211. function faceNZ()
  212.   if dir ~= 2 then
  213.     if dir == 3 then
  214.       right()
  215.     else
  216.       repeat
  217.         left()
  218.       until dir == 2
  219.     end
  220.   end
  221. end
  222. function faceX()
  223.   if dir ~= 1 then
  224.     if dir == 2 then
  225.       right()
  226.     else
  227.       repeat
  228.         left()
  229.       until dir == 1
  230.     end
  231.   end
  232. end
  233. function faceNX()
  234.   if dir ~= 3 then
  235.     if dir == 0 then
  236.       right()
  237.     else
  238.       repeat
  239.         left()
  240.       until dir == 3
  241.     end
  242.   end
  243. end
  244. function goto(x,y,z)
  245.   print("Heading to x"..x..", y"..y..", z"..z.."!")
  246.   repeat
  247.     if x < posX then
  248.       faceNX()
  249.     elseif x > posX then
  250.       faceX()
  251.     end
  252.     while x ~= posX do
  253.       goForward()
  254.     end
  255.     if z < posZ then
  256.       faceNZ()
  257.     elseif z > posZ then
  258.       faceZ()
  259.     end
  260.     while z ~= posZ do
  261.       goForward()
  262.     end
  263.     while y < posY do
  264.       goDown()
  265.     end
  266.     while y > posY do
  267.       goUp()
  268.     end
  269.   until x == posX and y == posY and z == posZ
  270. end
  271.  
  272. local param1 = tonumber(tArgs[1])
  273. local param2 = tonumber(tArgs[2])
  274. local param3 = tonumber(tArgs[3])
  275. --goto(param1,param2,param3)
  276. faceZ()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement