Sir_Popsilots

FollowMe

Jul 2nd, 2021 (edited)
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.62 KB | None | 0 0
  1. local modem = peripheral.find("modem")
  2. modem.open(2)
  3. modem.open(42069)
  4. local rotation = 1 --1 = north , 2 = east, 3 = south, 4 = west
  5. local rotationtable1 = {north = {0,-1}, east = {1,0}, south = {0,1}, west = {-1,0}}
  6. local rotationtable = {{0,-1},{1,0},{0,1},{-1,0}}
  7. local x,z,y = gps.locate()
  8.  
  9. local function findrotation()
  10.     print("executing findrotation")
  11.     local tempx , tempz , tempy = gps.locate()
  12.     if turtle.forward() then
  13.         local tempxx , tempzz , tempyy = gps.locate()
  14.         turtle.back()
  15.         if tempzz < tempz then
  16.             return 1
  17.         elseif tempzz > tempz then
  18.             return 3
  19.         elseif tempxx < tempx then
  20.             return 4
  21.         elseif tempxx > tempx then
  22.             return 2
  23.         end
  24.     else
  25.         turtle.turnRight()
  26.         return findrotation()
  27.     end
  28. end
  29.  
  30.  
  31. local function f()
  32.     if turtle.forward() then
  33.         x = x + rotationtable[rotation][1]
  34.         z = z + rotationtable[rotation][2]
  35.     else
  36.         if turtle.up() then
  37.             f()
  38.             y = y + 1
  39.         else
  40.             print(x,z,y,rotation)
  41.         end
  42.     end
  43. end
  44.  
  45. local function b()
  46.     if turtle.back() then
  47.         x = x - rotationtable[rotation][1]
  48.         z = z - rotationtable[rotation][2]
  49.     end
  50. end
  51.  
  52. local function CheckNeededRotation(xcords , zcords, ycords)
  53.     print("executing CheckNeededRotation")
  54.     local tempx , tempz , tempy = gps.locate()
  55.     f()
  56.     if tempz > zcords then
  57.         b()
  58.         return 1
  59.     elseif tempz < zcords then
  60.         b()
  61.         return 3
  62.     elseif tempx < xcords then
  63.         b()
  64.         return 2
  65.     elseif tempx > xcords then
  66.         b()
  67.         return 4
  68.     end
  69. end
  70.  
  71.  
  72. local function rotate(cardinaldirectionNum)
  73.     print("executing rotation")
  74.     if rotation == 1 then
  75.         rotation = cardinaldirectionNum
  76.         if cardinaldirectionNum == 2 then
  77.             turtle.turnRight()
  78.         elseif cardinaldirectionNum == 3 then
  79.             turtle.turnRight()
  80.             turtle.turnRight()
  81.         elseif cardinaldirectionNum == 4 then
  82.             turtle.turnLeft()
  83.         end
  84.     elseif rotation == 2 then
  85.         rotation = cardinaldirectionNum
  86.         if cardinaldirectionNum == 1 then
  87.             turtle.turnLeft()
  88.         elseif cardinaldirectionNum == 3 then
  89.             turtle.turnRight()
  90.         elseif cardinaldirectionNum == 4 then
  91.             turtle.turnRight()
  92.             turtle.turnRight()
  93.         end
  94.     elseif rotation == 3 then
  95.         rotation = cardinaldirectionNum
  96.         if cardinaldirectionNum == 1 then
  97.             turtle.turnLeft()
  98.             turtle.turnLeft()
  99.         elseif cardinaldirectionNum == 2 then
  100.             turtle.turnLeft()
  101.         elseif cardinaldirectionNum == 4 then
  102.             turtle.turnRight()
  103.         end
  104.     elseif rotation == 4 then
  105.         rotation = cardinaldirectionNum
  106.         if cardinaldirectionNum == 1 then
  107.             turtle.turnRight()
  108.         elseif cardinaldirectionNum == 2 then
  109.             turtle.turnRight()
  110.             turtle.turnRight()
  111.         elseif cardinaldirectionNum == 3 then
  112.             turtle.turnLeft()
  113.         end
  114.     end
  115. end
  116.  
  117. local function IsThere(xcords,zcords,ycords)
  118.     print("executing IsThere\n"
  119.     , x,z,y ,"\n"
  120.     , xcords,zcords,ycords)
  121.     if x == xcords and zcords == z then
  122.         if ycords > y then
  123.             while turtle.down() do
  124.             end
  125.             print("IsThere is true")
  126.             return true
  127.         end
  128.     end
  129.     print("IsThere is false")
  130.     return false
  131. end
  132.  
  133. local function GoUntilNeeded(xcords,zcords,ycords)
  134.     print("executing GoUntilNeeded")
  135.     local temp = CheckNeededRotation(xcords,zcords,ycords)
  136.     rotate(temp)
  137.     if zcords ~= z then
  138.         print("executing moving in z direction")
  139.         while zcords ~= z do
  140.             f()
  141.         end
  142.         print("done executing moving in z direction")
  143.     elseif xcords ~= x then
  144.         print("executing moving in x direction")
  145.         while xcords ~= x do
  146.             f()
  147.         end
  148.         print("done executing moving in x direction")
  149.     end
  150.    
  151. end
  152.  
  153. while true do
  154.     local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
  155.     if message == "b" then
  156.         break
  157.     end
  158.     print("")
  159.     local xcord = message[1]
  160.     local zcord = message[2]
  161.     local ycord = message[3]
  162.     rotation = findrotation()
  163.     print(rotation)
  164.     while IsThere(xcord,zcord,zcord)~=true do
  165.         GoUntilNeeded(xcord,zcord,ycord)
  166.     end
  167.     print("---------------")
  168.     print(x,z,y)
  169.     print(message[1],message[2],message[3])
  170.     print("got there")
  171. end
  172. modem.closeAll()
Add Comment
Please, Sign In to add comment