Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- URL: https://pastebin.com/QbSGDWvG
- -- Run this on startup:
- -- Get the script: pastebin get QbSGDWvG turtle_server
- -- edit startup
- -- paste: shell.run("turtle_server")
- -- Reboot to check it's working
- -- Run update script on startup
- print("=== Turtle Server Starting ===")
- print("Running update script...")
- shell.run("update")
- print("Update complete. Starting turtle server...")
- -- Mainframe communication
- local MAINFRAME_ID = nil
- -- Function to discover mainframe ID via broadcast
- local function discoverMainframe()
- if MAINFRAME_ID then return MAINFRAME_ID end
- print("Broadcasting to discover mainframe...")
- rednet.broadcast("discover_mainframe")
- -- Wait for responses for up to 3 seconds
- local timeout = os.clock() + 3
- while os.clock() < timeout do
- local senderId, message = rednet.receive(0.1)
- if senderId and message == "mainframe_here" then
- MAINFRAME_ID = senderId
- print("Discovered mainframe at ID: " .. MAINFRAME_ID)
- return MAINFRAME_ID
- end
- end
- print("No mainframe found via broadcast")
- return nil
- end
- -- Function to report current GPS location to mainframe
- local function reportLocationToMainframe()
- local mainframeId = discoverMainframe()
- if mainframeId then
- local gpsX, gpsY, gpsZ = gps.locate()
- if gpsX and gpsY and gpsZ then
- local message = {
- type = "location",
- x = gpsX,
- y = gpsY,
- z = gpsZ
- }
- rednet.send(mainframeId, message)
- print("Reported location to mainframe " .. mainframeId .. ": (" .. gpsX .. ", " .. gpsY .. ", " .. gpsZ .. ")")
- else
- print("Cannot report location - GPS not available")
- end
- else
- print("Cannot report location - mainframe not available")
- end
- end
- -- 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
- reportLocationToMainframe()
- end
- while currentPos.y > y do
- if not turtle.down() then turtle.digDown() end
- currentPos.y = currentPos.y - 1
- reportLocationToMainframe()
- 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
- reportLocationToMainframe()
- 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
- reportLocationToMainframe()
- 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
- reportLocationToMainframe()
- 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
- reportLocationToMainframe()
- 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...")
- -- Report initial location to mainframe
- reportLocationToMainframe()
- 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
- -- Report current location to mainframe as well
- reportLocationToMainframe()
- 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
- end
- end
- end
- -- Run the server
- startRednetServer()
Advertisement