Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function listen()
- local north,south,east,west = 0
- local clientNorth, clientSouth, clientEast, clientWest = 0
- rednet.open("left")
- while true do
- local sender, message, protocol = rednet.receive()
- if message == "ping" then
- broadcastGPS()
- end
- if message == "come" then
- broadcastAndSendGPS()
- end
- end
- end
- function broadcastGPS()
- rednet.open("left")
- local x,y,z = gps.locate()
- rednet.broadcast("gps: "..x.." "..y.." "..z)
- end
- function broadcastAndSendGPS()
- rednet.open("left")
- local x,y,z = gps.locate()
- rednet.broadcast("gps: "..x.." "..y.." "..z)
- --x1 y1 z1 position of Caller
- id,x1 = rednet.receive()
- print("Device "..id..": ")
- print(x1)
- id,y1 = rednet.receive()
- print("Device "..id..": ")
- print(y1)
- id,z1 = rednet.receive()
- print("Device "..id..": ")
- print(z1)
- getHeading()
- --moveToCaller()
- end
- function getHeading()
- local x,y,z = gps.locate()
- print("Getting Turtle heading...")
- --move 1 block forward / dig and move forward
- if(turtle.detect() == true) then
- turtle.dig()
- turtle.forward()
- else
- turtle.forward()
- end
- local newX,newY,newZ = gps.locate()
- if newX == x then
- if newZ > z then
- print("Facing South")
- south = 1
- else
- print("Facing North")
- north = 1
- end
- else
- if newX > x then
- print("Facing East")
- east = 1
- else
- print("Facing West")
- west = 1
- end
- end
- faceNorth()
- end
- function faceNorth()
- if north == 1 then
- print("Facing North already..")
- end
- if south == 1 then
- turtle.turnRight()
- turtle.turnRight()
- end
- if east == 1 then
- turtle.turnLeft()
- end
- if west == 1 then
- turtle.turnRight()
- end
- getCallerPosition()
- end
- function getCallerPosition()
- local x,y,z = gps.locate()
- --x1 y1 z1 are caller positions // x y z are turtle position
- if x1 < x then
- print("Caller is to the West")
- clientWest = 1
- else
- print("Caller is to the East")
- clientEast = 1
- end
- if z1 < z then
- print("Caller is to the North")
- clientNorth = 1
- else
- print("Caller is to the South")
- clientSouth = 1
- end
- setHeading()
- end
- function moveToCallerX()
- local x,y,z = gps.locate()
- diffX = math.abs(x1 - x)
- --print("Caller is "..math.floor(diffX).." distant on X axis")
- --diffZ = math.abs(z1 - z)
- --print("Caller is "..math.floor(diffZ).." distant on Z axis")
- --setHeading()
- --clientNorth, clientSouth, clientEast, clientWest = 0
- --north,south,east,west = 0
- for step = 1, diffX, 1 do
- turtle.forward()
- end
- end
- function moveToCallerZ()
- local x,y,z = gps.locate()
- diffZ = math.abs(z1 - z)
- for step = 1, diffZ, 1 do
- turtle.forward()
- end
- end
- --set heading based on caller position
- function setHeading()
- setEastWest()
- setNorthSouth()
- end
- function setEastWest()
- if clientEast == 1 then
- turtle.turnRight()
- print("Heading set to East")
- else
- turtle.turnLeft()
- print("Heading set to West")
- end
- moveToCallerX()
- end
- function setNorthSouth()
- if clientNorth == 1 then
- print("Heading already set to North")
- else
- turtle.turnRight()
- turtle.turnRight()
- print("Heading set to South")
- end
- moveToCallerZ()
- end
- listen()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement