Advertisement
Silasko

Turtle Come

Apr 30th, 2022 (edited)
361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.71 KB | None | 0 0
  1. function listen()
  2.     local north,south,east,west = 0
  3.     local clientNorth, clientSouth, clientEast, clientWest = 0
  4.     rednet.open("left")
  5.         while true do
  6.             local sender, message, protocol = rednet.receive()
  7.             if message == "ping" then
  8.                 broadcastGPS()
  9.             end
  10.             if message == "come" then
  11.                 broadcastAndSendGPS()
  12.             end
  13.        
  14.         end
  15. end
  16.  
  17. function broadcastGPS()
  18.     rednet.open("left")
  19.     local x,y,z = gps.locate()
  20.     rednet.broadcast("gps: "..x.." "..y.." "..z)
  21. end
  22.  
  23. function broadcastAndSendGPS()
  24.     rednet.open("left")
  25.     local x,y,z = gps.locate()
  26.     rednet.broadcast("gps: "..x.." "..y.." "..z)
  27.     --x1 y1 z1 position of Caller
  28.     id,x1 = rednet.receive()
  29.     print("Device "..id..": ")
  30.     print(x1)
  31.     id,y1 = rednet.receive()
  32.     print("Device "..id..": ")
  33.     print(y1)
  34.     id,z1 = rednet.receive()
  35.     print("Device "..id..": ")
  36.     print(z1)
  37.     getHeading()
  38.     --moveToCaller()
  39. end
  40.  
  41.  
  42. function getHeading()
  43.     local x,y,z = gps.locate()
  44.     print("Getting Turtle heading...")
  45.     --move 1 block forward / dig and move forward
  46.     if(turtle.detect() == true) then
  47.         turtle.dig()
  48.         turtle.forward()
  49.     else
  50.         turtle.forward()
  51.     end
  52.     local newX,newY,newZ = gps.locate()
  53.     if newX == x then
  54.         if newZ > z then
  55.             print("Facing South")
  56.             south = 1
  57.         else
  58.             print("Facing North")
  59.             north = 1
  60.         end
  61.     else
  62.         if newX > x then
  63.             print("Facing East")
  64.             east = 1
  65.         else
  66.             print("Facing West")
  67.             west = 1
  68.         end
  69.     end
  70.     faceNorth()
  71. end
  72.  
  73. function faceNorth()
  74.     if north == 1 then
  75.         print("Facing North already..")
  76.     end
  77.     if south == 1 then
  78.         turtle.turnRight()
  79.         turtle.turnRight()
  80.     end
  81.     if east == 1 then
  82.         turtle.turnLeft()
  83.     end
  84.     if west == 1 then
  85.         turtle.turnRight()
  86.     end
  87.     getCallerPosition()
  88. end
  89.  
  90. function getCallerPosition()
  91.     local x,y,z = gps.locate()
  92.     --x1 y1 z1 are caller positions // x y z are turtle position
  93.     if x1 < x then
  94.         print("Caller is to the West")
  95.         clientWest = 1
  96.     else
  97.         print("Caller is to the East")
  98.         clientEast = 1
  99.     end
  100.     if z1 < z then
  101.         print("Caller is to the North")
  102.         clientNorth = 1
  103.     else
  104.         print("Caller is to the South")
  105.         clientSouth = 1
  106.     end
  107.     setHeading()
  108. end
  109.  
  110. function moveToCallerX()
  111.     local x,y,z = gps.locate()
  112.     diffX = math.abs(x1 - x)
  113.     --print("Caller is "..math.floor(diffX).." distant on X axis")
  114.     --diffZ = math.abs(z1 - z)
  115.     --print("Caller is "..math.floor(diffZ).." distant on Z axis")
  116.     --setHeading()
  117.     --clientNorth, clientSouth, clientEast, clientWest = 0
  118.     --north,south,east,west = 0
  119.     for step = 1, diffX, 1 do
  120.         turtle.forward()
  121.     end
  122.    
  123. end
  124.  
  125. function moveToCallerZ()
  126.     local x,y,z = gps.locate()
  127.     diffZ = math.abs(z1 - z)
  128.     for step = 1, diffZ, 1 do
  129.         turtle.forward()
  130.     end
  131.    
  132. end
  133.  
  134.  
  135.  
  136. --set heading based on caller position
  137. function setHeading()
  138.     setEastWest()
  139.     setNorthSouth()
  140. end
  141.  
  142. function setEastWest()
  143.     if clientEast == 1 then
  144.         turtle.turnRight()
  145.         print("Heading set to East")
  146.     else
  147.         turtle.turnLeft()
  148.         print("Heading set to West")
  149.     end
  150.     moveToCallerX()
  151. end
  152.  
  153. function setNorthSouth()
  154.     if clientNorth == 1 then
  155.         print("Heading already set to North")
  156.     else
  157.         turtle.turnRight()
  158.         turtle.turnRight()
  159.         print("Heading set to South")
  160.     end
  161.     moveToCallerZ()
  162. end
  163.  
  164.  
  165. listen()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement