gur111

turtle_server

Jun 12th, 2025 (edited)
1,413
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.57 KB | None | 0 0
  1. -- URL: https://pastebin.com/QbSGDWvG
  2.  
  3. -- Run this on startup:
  4. -- Get the script: pastebin get QbSGDWvG turtle_server
  5. -- edit startup
  6. -- paste: shell.run("turtle_server")
  7. -- Reboot to check it's working
  8.  
  9. -- Run update script on startup
  10. print("=== Turtle Server Starting ===")
  11. print("Running update script...")
  12. shell.run("update")
  13. print("Update complete. Starting turtle server...")
  14.  
  15. -- Mainframe communication
  16. local MAINFRAME_ID = nil
  17.  
  18. -- Function to discover mainframe ID via broadcast
  19. local function discoverMainframe()
  20.   if MAINFRAME_ID then return MAINFRAME_ID end
  21.  
  22.   print("Broadcasting to discover mainframe...")
  23.   rednet.broadcast("discover_mainframe")
  24.  
  25.   -- Wait for responses for up to 3 seconds
  26.   local timeout = os.clock() + 3
  27.   while os.clock() < timeout do
  28.     local senderId, message = rednet.receive(0.1)
  29.     if senderId and message == "mainframe_here" then
  30.       MAINFRAME_ID = senderId
  31.       print("Discovered mainframe at ID: " .. MAINFRAME_ID)
  32.       return MAINFRAME_ID
  33.     end
  34.   end
  35.  
  36.   print("No mainframe found via broadcast")
  37.   return nil
  38. end
  39.  
  40. -- Function to report current GPS location to mainframe
  41. local function reportLocationToMainframe()
  42.   local mainframeId = discoverMainframe()
  43.   if mainframeId then
  44.     local gpsX, gpsY, gpsZ = gps.locate()
  45.     if gpsX and gpsY and gpsZ then
  46.       local message = {
  47.         type = "location",
  48.         x = gpsX,
  49.         y = gpsY,
  50.         z = gpsZ
  51.       }
  52.       rednet.send(mainframeId, message)
  53.       print("Reported location to mainframe " .. mainframeId .. ": (" .. gpsX .. ", " .. gpsY .. ", " .. gpsZ .. ")")
  54.     else
  55.       print("Cannot report location - GPS not available")
  56.     end
  57.   else
  58.     print("Cannot report location - mainframe not available")
  59.   end
  60. end
  61.  
  62. -- Function to open a modem on any side
  63. local function openModem()
  64.   local sides = {"left", "right", "top", "bottom", "front", "back"}
  65.   for _, side in ipairs(sides) do
  66.     if peripheral.getType(side) == "modem" then
  67.       rednet.open(side)
  68.       return true
  69.     end
  70.   end
  71.   return false
  72. end
  73.  
  74. -- Function to get the current GPS position
  75. local function getCurrentPosition()
  76.   local x, y, z = gps.locate(5)  -- Wait up to 5 seconds for a GPS fix
  77.   if x and y and z then
  78.     return {x = x, y = y, z = z}
  79.   else
  80.     print("Unable to determine GPS position.")
  81.     return nil
  82.   end
  83. end
  84.  
  85. -- Function to determine the turtle's current heading
  86. local function getHeading()
  87.   local initialPos = getCurrentPosition()
  88.   if not initialPos then return nil end
  89.  
  90.   -- Try moving forward to determine heading
  91.   if turtle.forward() then
  92.     local newPos = getCurrentPosition()
  93.     turtle.back()  -- Return to original position
  94.  
  95.     if newPos.x > initialPos.x then
  96.       return 1  -- East
  97.     elseif newPos.x < initialPos.x then
  98.       return 3  -- West
  99.     elseif newPos.z > initialPos.z then
  100.       return 2  -- South
  101.     elseif newPos.z < initialPos.z then
  102.       return 0  -- North
  103.     end
  104.   end
  105.  
  106.   -- If forward is blocked, try moving up
  107.   if turtle.up() then
  108.     turtle.down()  -- Return to original position
  109.     return getHeading()  -- Retry after moving up
  110.   end
  111.  
  112.   -- If all directions are blocked, return nil
  113.   return nil
  114. end
  115.  
  116. -- Function to face a specific direction
  117. local function faceDirection(targetHeading)
  118.   local currentHeading = getHeading()
  119.   if currentHeading == nil then
  120.     print("Unable to determine current heading.")
  121.     return
  122.   end
  123.  
  124.   local turns = (targetHeading - currentHeading) % 4
  125.   if turns == 1 then
  126.     turtle.turnRight()
  127.   elseif turns == 2 then
  128.     turtle.turnRight()
  129.     turtle.turnRight()
  130.   elseif turns == 3 then
  131.     turtle.turnLeft()
  132.   end
  133. end
  134.  
  135. -- Function to move to a specific GPS location
  136. local function goTo(x, y, z)
  137.   local currentPos = getCurrentPosition()
  138.   if not currentPos then
  139.     print("Cannot move without GPS position.")
  140.     return
  141.   end
  142.  
  143.   -- Move vertically to the target y level first
  144.   while currentPos.y < y do
  145.     if not turtle.up() then turtle.digUp() end
  146.     currentPos.y = currentPos.y + 1
  147.     reportLocationToMainframe()
  148.   end
  149.   while currentPos.y > y do
  150.     if not turtle.down() then turtle.digDown() end
  151.     currentPos.y = currentPos.y - 1
  152.     reportLocationToMainframe()
  153.   end
  154.  
  155.   -- Move in the x direction
  156.   if currentPos.x < x then
  157.     faceDirection(1)  -- East
  158.     while currentPos.x < x do
  159.       if not turtle.forward() then turtle.dig() end
  160.       currentPos.x = currentPos.x + 1
  161.       reportLocationToMainframe()
  162.     end
  163.   elseif currentPos.x > x then
  164.     faceDirection(3)  -- West
  165.     while currentPos.x > x do
  166.       if not turtle.forward() then turtle.dig() end
  167.       currentPos.x = currentPos.x - 1
  168.       reportLocationToMainframe()
  169.     end
  170.   end
  171.  
  172.   -- Move in the z direction
  173.   if currentPos.z < z then
  174.     faceDirection(2)  -- South
  175.     while currentPos.z < z do
  176.       if not turtle.forward() then turtle.dig() end
  177.       currentPos.z = currentPos.z + 1
  178.       reportLocationToMainframe()
  179.     end
  180.   elseif currentPos.z > z then
  181.     faceDirection(0)  -- North
  182.     while currentPos.z > z do
  183.       if not turtle.forward() then turtle.dig() end
  184.       currentPos.z = currentPos.z - 1
  185.       reportLocationToMainframe()
  186.     end
  187.   end
  188. end
  189.  
  190. -- Start the rednet server
  191. local function startRednetServer()
  192.   if not openModem() then
  193.     print("No modem found. Please attach a modem to the turtle.")
  194.     return
  195.   end
  196.  
  197.   print("Rednet server started. Listening for commands...")
  198.  
  199.   -- Report initial location to mainframe
  200.   reportLocationToMainframe()
  201.  
  202.   while true do
  203.     local senderId, message = rednet.receive()
  204.     print("Received command from " .. senderId .. ": " .. message)
  205.  
  206.     if message == "get location" then
  207.       local pos = getCurrentPosition()
  208.       if pos then
  209.         -- Report current location to mainframe as well
  210.         reportLocationToMainframe()
  211.         rednet.send(senderId, "Current location: (" .. pos.x .. ", " .. pos.y .. ", " .. pos.z .. ")")
  212.       else
  213.         rednet.send(senderId, "Unable to determine GPS position.")
  214.       end
  215.     elseif message:match("^go to location") then
  216.       local x, y, z = message:match("go to location ([%-]?%d+) ([%-]?%d+) ([%-]?%d+)")
  217.       if x and y and z then
  218.         rednet.send(senderId, "Moving to location: (" .. x .. ", " .. y .. ", " .. z .. ")")
  219.         goTo(tonumber(x), tonumber(y), tonumber(z))
  220.         rednet.send(senderId, "Arrived at location: (" .. x .. ", " .. y .. ", " .. z .. ")")
  221.       else
  222.         rednet.send(senderId, "Invalid location format. Use: go to location <x> <y> <z>")
  223.       end
  224.     end
  225.   end
  226. end
  227.  
  228. -- Run the server
  229. startRednetServer()
  230.  
Advertisement