Advertisement
gur111

remote_commands

Jun 12th, 2025 (edited)
523
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.51 KB | None | 0 0
  1. -- Function to open a modem on any side
  2. local function openModem()
  3.   local sides = {"left", "right", "top", "bottom", "front", "back"}
  4.   for _, side in ipairs(sides) do
  5.     if peripheral.getType(side) == "modem" then
  6.       rednet.open(side)
  7.       return true
  8.     end
  9.   end
  10.   return false
  11. end
  12.  
  13. -- Function to get the current GPS position
  14. local function getCurrentPosition()
  15.   local x, y, z = gps.locate(5)  -- Wait up to 5 seconds for a GPS fix
  16.   if x and y and z then
  17.     return {x = x, y = y, z = z}
  18.   else
  19.     print("Unable to determine GPS position.")
  20.     return nil
  21.   end
  22. end
  23.  
  24. -- Function to determine the turtle's current heading
  25. local function getHeading()
  26.   local initialPos = getCurrentPosition()
  27.   if not initialPos then return nil end
  28.  
  29.   -- Try moving forward to determine heading
  30.   if turtle.forward() then
  31.     local newPos = getCurrentPosition()
  32.     turtle.back()  -- Return to original position
  33.  
  34.     if newPos.x > initialPos.x then
  35.       return 1  -- East
  36.     elseif newPos.x < initialPos.x then
  37.       return 3  -- West
  38.     elseif newPos.z > initialPos.z then
  39.       return 2  -- South
  40.     elseif newPos.z < initialPos.z then
  41.       return 0  -- North
  42.     end
  43.   end
  44.  
  45.   -- If forward is blocked, try moving up
  46.   if turtle.up() then
  47.     turtle.down()  -- Return to original position
  48.     return getHeading()  -- Retry after moving up
  49.   end
  50.  
  51.   -- If all directions are blocked, return nil
  52.   return nil
  53. end
  54.  
  55. -- Function to face a specific direction
  56. local function faceDirection(targetHeading)
  57.   local currentHeading = getHeading()
  58.   if currentHeading == nil then
  59.     print("Unable to determine current heading.")
  60.     return
  61.   end
  62.  
  63.   local turns = (targetHeading - currentHeading) % 4
  64.   if turns == 1 then
  65.     turtle.turnRight()
  66.   elseif turns == 2 then
  67.     turtle.turnRight()
  68.     turtle.turnRight()
  69.   elseif turns == 3 then
  70.     turtle.turnLeft()
  71.   end
  72. end
  73.  
  74. -- Function to move to a specific GPS location
  75. local function goTo(x, y, z)
  76.   local currentPos = getCurrentPosition()
  77.   if not currentPos then
  78.     print("Cannot move without GPS position.")
  79.     return
  80.   end
  81.  
  82.   -- Move vertically to the target y level first
  83.   while currentPos.y < y do
  84.     if not turtle.up() then turtle.digUp() end
  85.     currentPos.y = currentPos.y + 1
  86.   end
  87.   while currentPos.y > y do
  88.     if not turtle.down() then turtle.digDown() end
  89.     currentPos.y = currentPos.y - 1
  90.   end
  91.  
  92.   -- Move in the x direction
  93.   if currentPos.x < x then
  94.     faceDirection(1)  -- East
  95.     while currentPos.x < x do
  96.       if not turtle.forward() then turtle.dig() end
  97.       currentPos.x = currentPos.x + 1
  98.     end
  99.   elseif currentPos.x > x then
  100.     faceDirection(3)  -- West
  101.     while currentPos.x > x do
  102.       if not turtle.forward() then turtle.dig() end
  103.       currentPos.x = currentPos.x - 1
  104.     end
  105.   end
  106.  
  107.   -- Move in the z direction
  108.   if currentPos.z < z then
  109.     faceDirection(2)  -- South
  110.     while currentPos.z < z do
  111.       if not turtle.forward() then turtle.dig() end
  112.       currentPos.z = currentPos.z + 1
  113.     end
  114.   elseif currentPos.z > z then
  115.     faceDirection(0)  -- North
  116.     while currentPos.z > z do
  117.       if not turtle.forward() then turtle.dig() end
  118.       currentPos.z = currentPos.z - 1
  119.     end
  120.   end
  121. end
  122.  
  123. -- Start the rednet server
  124. local function startRednetServer()
  125.   if not openModem() then
  126.     print("No modem found. Please attach a modem to the turtle.")
  127.     return
  128.   end
  129.  
  130.   print("Rednet server started. Listening for commands...")
  131.  
  132.   while true do
  133.     local senderId, message = rednet.receive()
  134.     print("Received command from " .. senderId .. ": " .. message)
  135.  
  136.     if message == "get location" then
  137.       local pos = getCurrentPosition()
  138.       if pos then
  139.         rednet.send(senderId, "Current location: (" .. pos.x .. ", " .. pos.y .. ", " .. pos.z .. ")")
  140.       else
  141.         rednet.send(senderId, "Unable to determine GPS position.")
  142.       end
  143.     elseif message:match("^go to location") then
  144.       local x, y, z = message:match("go to location ([%-]?%d+) ([%-]?%d+) ([%-]?%d+)")
  145.       if x and y and z then
  146.         rednet.send(senderId, "Moving to location: (" .. x .. ", " .. y .. ", " .. z .. ")")
  147.         goTo(tonumber(x), tonumber(y), tonumber(z))
  148.         rednet.send(senderId, "Arrived at location: (" .. x .. ", " .. y .. ", " .. z .. ")")
  149.       else
  150.         rednet.send(senderId, "Invalid location format. Use: go to location <x> <y> <z>")
  151.       end
  152.     else
  153.       rednet.send(senderId, "Unknown command.")
  154.     end
  155.   end
  156. end
  157.  
  158. -- Run the server
  159. startRednetServer()
  160.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement