Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Function to open a modem on any side
- local function openModem()
- local sides = {"left", "right", "top", "bottom", "front", "back"}
- for _, side in ipairs(sides) do
- if peripheral.getType(side) == "modem" then
- rednet.open(side)
- return true
- end
- end
- return false
- end
- -- Function to get the current GPS position
- local function getCurrentPosition()
- local x, y, z = gps.locate(5) -- Wait up to 5 seconds for a GPS fix
- if x and y and z then
- return {x = x, y = y, z = z}
- else
- print("Unable to determine GPS position.")
- return nil
- end
- end
- -- Function to determine the turtle's current heading
- local function getHeading()
- local initialPos = getCurrentPosition()
- if not initialPos then return nil end
- -- Try moving forward to determine heading
- if turtle.forward() then
- local newPos = getCurrentPosition()
- turtle.back() -- Return to original position
- if newPos.x > initialPos.x then
- return 1 -- East
- elseif newPos.x < initialPos.x then
- return 3 -- West
- elseif newPos.z > initialPos.z then
- return 2 -- South
- elseif newPos.z < initialPos.z then
- return 0 -- North
- end
- end
- -- If forward is blocked, try moving up
- if turtle.up() then
- turtle.down() -- Return to original position
- return getHeading() -- Retry after moving up
- end
- -- If all directions are blocked, return nil
- return nil
- end
- -- Function to face a specific direction
- local function faceDirection(targetHeading)
- local currentHeading = getHeading()
- if currentHeading == nil then
- print("Unable to determine current heading.")
- return
- end
- local turns = (targetHeading - currentHeading) % 4
- if turns == 1 then
- turtle.turnRight()
- elseif turns == 2 then
- turtle.turnRight()
- turtle.turnRight()
- elseif turns == 3 then
- turtle.turnLeft()
- end
- end
- -- Function to move to a specific GPS location
- local function goTo(x, y, z)
- local currentPos = getCurrentPosition()
- if not currentPos then
- print("Cannot move without GPS position.")
- return
- end
- -- Move vertically to the target y level first
- while currentPos.y < y do
- if not turtle.up() then turtle.digUp() end
- currentPos.y = currentPos.y + 1
- end
- while currentPos.y > y do
- if not turtle.down() then turtle.digDown() end
- currentPos.y = currentPos.y - 1
- end
- -- Move in the x direction
- if currentPos.x < x then
- faceDirection(1) -- East
- while currentPos.x < x do
- if not turtle.forward() then turtle.dig() end
- currentPos.x = currentPos.x + 1
- end
- elseif currentPos.x > x then
- faceDirection(3) -- West
- while currentPos.x > x do
- if not turtle.forward() then turtle.dig() end
- currentPos.x = currentPos.x - 1
- end
- end
- -- Move in the z direction
- if currentPos.z < z then
- faceDirection(2) -- South
- while currentPos.z < z do
- if not turtle.forward() then turtle.dig() end
- currentPos.z = currentPos.z + 1
- end
- elseif currentPos.z > z then
- faceDirection(0) -- North
- while currentPos.z > z do
- if not turtle.forward() then turtle.dig() end
- currentPos.z = currentPos.z - 1
- end
- end
- end
- -- Start the rednet server
- local function startRednetServer()
- if not openModem() then
- print("No modem found. Please attach a modem to the turtle.")
- return
- end
- print("Rednet server started. Listening for commands...")
- while true do
- local senderId, message = rednet.receive()
- print("Received command from " .. senderId .. ": " .. message)
- if message == "get location" then
- local pos = getCurrentPosition()
- if pos then
- rednet.send(senderId, "Current location: (" .. pos.x .. ", " .. pos.y .. ", " .. pos.z .. ")")
- else
- rednet.send(senderId, "Unable to determine GPS position.")
- end
- elseif message:match("^go to location") then
- local x, y, z = message:match("go to location ([%-]?%d+) ([%-]?%d+) ([%-]?%d+)")
- if x and y and z then
- rednet.send(senderId, "Moving to location: (" .. x .. ", " .. y .. ", " .. z .. ")")
- goTo(tonumber(x), tonumber(y), tonumber(z))
- rednet.send(senderId, "Arrived at location: (" .. x .. ", " .. y .. ", " .. z .. ")")
- else
- rednet.send(senderId, "Invalid location format. Use: go to location <x> <y> <z>")
- end
- else
- rednet.send(senderId, "Unknown command.")
- end
- end
- end
- -- Run the server
- startRednetServer()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement