Ignius12

gps_test

Jul 14th, 2021 (edited)
953
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.49 KB | None | 0 0
  1. DIR = 1
  2. function getOrientation()
  3.     loc1 = vector.new(gps.locate(2, false))
  4.     if not turtle.forward() then
  5.         for j=1,6 do
  6.             if not turtle.forward() then
  7.                 turtle.dig()
  8.          else break end
  9.         end
  10.     end
  11.     loc2 = vector.new(gps.locate(2, false))
  12.     heading = loc2 - loc1
  13.     return ((heading.x + math.abs(heading.x) * 2) + (heading.z + math.abs(heading.z) * 3))
  14. end
  15.  
  16. function left()
  17.     if turtle then turtle.turnLeft() end
  18.     DIR = DIR - 1
  19.     if DIR < 1 then DIR = DIR + 4 end
  20. end
  21.  
  22. function right()
  23.     if turtle then turtle.turnRight() end
  24.     DIR = DIR + 1
  25.     if DIR > 4 then DIR = DIR - 4 end
  26. end
  27.  
  28. function face(target)
  29.     if(DIR== target) then return end
  30.     diff = DIR - target
  31.    
  32.     if(diff == 1 or diff == -3) then left()
  33.         else
  34.             if(math.abs(diff) == 2) then right() right()
  35.             else right() end
  36.     end
  37. end
  38.  
  39. local modem = peripheral.find("modem")
  40. rednet.open("left")
  41.  
  42. local pos = vector.new(gps.locate())
  43.  
  44. local target = vector.new(452, 69, -438)
  45.  
  46. local start = vector.new(gps.locate())
  47. DIR = getOrientation()
  48. print("Now facing " .. DIR)
  49. sleep(5)
  50. local diff = target - pos
  51. if(target.x > pos.x) then
  52.     face(3)
  53. else
  54.     face(1)
  55. end
  56. print("Now facing " .. DIR)
  57. while(math.abs(diff.x) > 0) do
  58.     turtle.forward()
  59.     pos = vector.new(gps.locate())
  60.     diff = target - pos
  61.     print(diff.x)
  62.     rednet.send(6, os.getComputerLabel() .. " | " .. turtle.getFuelLevel() .. " | (" .. pos.x .. ", " .. pos.y ..", " .. pos.z .. ") | " .. "Inactive", "miners")
  63. end
  64.  
  65. if(target.z > pos.z) then
  66.     face(4)
  67. else
  68.     face(2)
  69. end
  70. print("Now facing " .. DIR)
  71. while(math.abs(diff.z)> 0) do
  72.     turtle.forward()
  73.     pos = vector.new(gps.locate())
  74.     diff = target - pos
  75.     print(diff.z)
  76.     rednet.send(6, os.getComputerLabel() .. " | " .. turtle.getFuelLevel() .. " | (" .. pos.x .. ", " .. pos.y ..", " .. pos.z .. ") | " .. "Inactive", "miners")
  77. end
  78.  
  79. if(target.y > pos.y) then
  80.     while(math.abs(diff.y)> 0) do
  81.         turtle.up()
  82.         pos = vector.new(gps.locate())
  83.         diff = target - pos
  84.         print(diff.y)
  85.         rednet.send(6, os.getComputerLabel() .. " | " .. turtle.getFuelLevel() .. " | (" .. pos.x .. ", " .. pos.y ..", " .. pos.z .. ") | " .. "Inactive", "miners")
  86.     end
  87. end
  88.  
  89. if(target.y < pos.y) then
  90.     while(math.abs(diff.y)> 0) do
  91.         turtle.down()
  92.         pos = vector.new(gps.locate())
  93.         diff = target - pos
  94.         print(diff.y)
  95.         rednet.send(6, os.getComputerLabel() .. " | " .. turtle.getFuelLevel() .. " | (" .. pos.x .. ", " .. pos.y ..", " .. pos.z .. ") | " .. "Inactive", "miners")
  96.     end
  97. end
  98.  
  99.  
Add Comment
Please, Sign In to add comment