Advertisement
thatparadox

Turtle Goto

Jan 12th, 2015
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.29 KB | None | 0 0
  1. for a,b in pairs(rs.getSides()) do
  2.   if peripheral.getType(b) == 'modem' then
  3.    rednet.open(b)
  4.    break
  5.   end
  6. end
  7.  
  8. tArgs = {...}
  9.  
  10. function compass()
  11.     x,y,z = gps.locate()
  12.     while turtle.forward() == false do
  13.         turtle.turnRight()
  14.     end
  15.     x1,y1,z1 = gps.locate()
  16.     direction = {x = x - x1, y = y - y1, z = z - z1}
  17.     for k,v in pairs( direction ) do
  18.         if k == "x" and v == 1 then
  19.             facing = 1 -- west
  20.         elseif k == "x" and v == -1 then
  21.             facing = 3 -- east
  22.         elseif k == "z" and v == 1 then
  23.             facing = 2 -- north
  24.         elseif k == "z" and v == -1 then
  25.             facing = 0 -- south
  26.         end
  27.     end
  28.     turtle.back()
  29.     return facing
  30. end
  31.  
  32. function goTo()
  33. xL,yL,zL = gps.locate()
  34. print("Starting from: "..xL.." "..yL.." "..zL)
  35. print("Going to: "..tArgs[1].." "..tArgs[2].." "..tArgs[3])
  36. while true do
  37.     xL,yL,zL = gps.locate()
  38.     x = xL - tArgs[1]
  39.     y = yL - tArgs[2]
  40.     z = zL - tArgs[3]
  41.     if y > 0 then
  42.         while tArgs[4] == "dig" and turtle.down() == false do
  43.             turtle.digDown()
  44.         end
  45.     elseif y < 0 then
  46.         while tArgs[4] == "dig" and turtle.up() == false do
  47.             turtle.digUp()
  48.             turtle.up()
  49.         end
  50.     elseif z < 0 then
  51.         currentDir = compass()
  52.         if currentDir ~= 0 then
  53.             turnTo(0)
  54.         end
  55.         while tArgs[4] == "dig" and turtle.forward() == false do
  56.             turtle.dig()
  57.             turtle.forward()
  58.         end
  59.     elseif z > 0 then
  60.         currentDir = compass()
  61.         if currentDir ~= 2 then
  62.             turnTo(2)
  63.         end
  64.         while tArgs[4] == "dig" and turtle.forward() == false do
  65.             turtle.dig()
  66.             turtle.forward()
  67.         end
  68.     elseif x < 0 then
  69.         currentDir = compass()
  70.         if currentDir ~= 3 then
  71.             turnTo(3)
  72.         end
  73.         while tArgs[4] == "dig" and turtle.forward() == false do
  74.             turtle.dig()
  75.             turtle.forward()
  76.         end
  77.     elseif x > 0 then
  78.         currentDir = compass()
  79.         if currentDir ~= 1 then
  80.             turnTo(1)
  81.         end
  82.         while tArgs[4] == "dig" and turtle.forward() == false do
  83.             turtle.dig()
  84.             turtle.forward()
  85.         end
  86.     else
  87.         break
  88.     end
  89. end
  90. end
  91.        
  92.        
  93. function turnTo(Int)
  94.     if facing ~= Int then
  95.         local current = facing - Int
  96.         if current < 0 then
  97.             for i = 1, math.abs(current) do
  98.                 turtle.turnRight()
  99.             end
  100.         else
  101.             for i = 1,current do
  102.             turtle.turnLeft()
  103.             end
  104.         end
  105.     end
  106. end
  107.  
  108. if #tArgs < 3 or #tArgs > 4 then
  109.   print ("Usage: "..shell.getRunningProgram().." <x> <y> <z> <'dig' or leave blank>")
  110. else
  111.   goTo()
  112. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement