Advertisement
Shaka01

remoteMobRequester

Jun 29th, 2025
431
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.35 KB | None | 0 0
  1. -- Configuration for sending
  2. local TARGET_TURTLE_ID = nil
  3. local REDNET_PROTOCOL = "automata_cycle"    -- MUST match the protocol in your automation turtle's script
  4. local REQUEST_POINTS_PROTOCOL = "request_points" -- New protocol for requesting points
  5. local REPLY_POINTS_PROTOCOL = "reply_points"     -- New protocol for replying with points
  6.  
  7.  
  8. -- Find the modem peripheral on THIS computer/turtle
  9. local senderModem = peripheral.find("modem")
  10. local senderModemSide = nil
  11.  
  12. -- Ensure modem is found
  13. if not senderModem then
  14.     error("Error: No modem peripheral found on this computer/turtle. Please attach one.")
  15. end
  16.  
  17. -- --- MODEM INITIALIZATION ---
  18. -- This section is your working implementation for finding and opening the modem.
  19. local modemFound = false
  20. local sides = {"left", "right", "top", "bottom", "front", "back"}
  21. for i, side in ipairs(sides) do
  22.     local device = peripheral.getType(side)
  23.     if device == "modem" then
  24.         rednet.open(side) -- Opens Rednet on the detected side
  25.         senderModem = peripheral.wrap(side) -- Updates senderModem to the one just opened
  26.         senderModemSide = side
  27.         if senderModem.isWireless() then -- Corrected this line to refer to senderModem
  28.             modemFound = true
  29.             break
  30.         end
  31.     end
  32. end
  33.  
  34. -- Get the turtle's ID
  35. print("Broadcasting for Turtle ID...")
  36. rednet.broadcast("yo", "getTurtleID")
  37. local id, message, protocol = rednet.receive("IDReply", 1) -- Add a timeout for robustness
  38.  
  39. if id and message == "it's a me" then
  40.     TARGET_TURTLE_ID = tonumber(id)
  41.     print(string.format("Found Turtle ID: %d", TARGET_TURTLE_ID))
  42. else
  43.     error("Could not find automation turtle. Ensure it is running and in range.")
  44. end
  45.  
  46.  
  47. print("Rednet Sender Ready.")
  48. print(string.format("Sending orders to Turtle ID %d using protocol '%s'", TARGET_TURTLE_ID, REDNET_PROTOCOL))
  49. print(string.format("Modem detected and Rednet opened on side: %s", senderModemSide))
  50. print("--------------------------------------------------")
  51.  
  52. -- Global variable to store received points for easy lookup
  53. local currentAvailablePoints = {}
  54.  
  55. --- Requests and displays available warp points from the turtle.
  56. -- This function now also updates the 'currentAvailablePoints' global and displays numbers.
  57. local function getAndDisplayPoints()
  58.     rednet.send(TARGET_TURTLE_ID, "get_points", REQUEST_POINTS_PROTOCOL)
  59.  
  60.     local id, receivedPoints, protocol = rednet.receive(REPLY_POINTS_PROTOCOL, 10) -- Wait up to 10 seconds
  61.  
  62.     if receivedPoints and protocol == REPLY_POINTS_PROTOCOL and id == TARGET_TURTLE_ID then
  63.         term.setTextColour(colors.lightGray)
  64.         print("--- Available Warp Points ---")
  65.         if type(receivedPoints) == "table" and #receivedPoints > 0 then
  66.             currentAvailablePoints = receivedPoints -- Store points for lookup
  67.             for i, pointName in ipairs(receivedPoints) do
  68.                 print(string.format("%d: %s", i, pointName)) -- Display with number prefix
  69.             end
  70.         else
  71.             print("No points found on the turtle.")
  72.             currentAvailablePoints = {} -- Clear points if none found
  73.         end
  74.         print("-----------------------------")
  75.         term.setTextColour(colors.white)
  76.         return true -- Indicate success
  77.     else
  78.         term.setTextColour(colors.red)
  79.         print("Failed to get points from turtle or received an invalid response.")
  80.         print("Error: " .. tostring(receivedPoints))
  81.         term.setTextColour(colors.white)
  82.         currentAvailablePoints = {} -- Clear points on failure
  83.         return false -- Indicate failure
  84.     end
  85. end
  86.  
  87. --- Custom input function to handle numeric indices or direct names.
  88. -- @param prompt string The text prompt to display.
  89. -- @param pointsTable table The table of available point names (currentAvailablePoints).
  90. -- @return string The resolved location name, or nil if aborted.
  91. local function getLocationInput(prompt, pointsTable)
  92.     while true do
  93.         write(prompt)
  94.         local input = read()
  95.  
  96.         if not input or input:len() == 0 then
  97.             print("Location cannot be empty.")
  98.             return nil -- Abort or indicate failure
  99.         end
  100.  
  101.         local numInput = tonumber(input)
  102.         if numInput then
  103.             -- User entered a number, try to map it to a point name
  104.             if numInput >= 1 and numInput <= #pointsTable then
  105.                 return pointsTable[numInput] -- Return the actual name
  106.             else
  107.                 term.setTextColour(colors.red)
  108.                 print(string.format("Invalid number. Please enter a number between 1 and %d or a name.", #pointsTable))
  109.                 term.setTextColour(colors.white)
  110.             end
  111.         else
  112.             -- User entered text, assume it's the direct name
  113.             -- Optional: Add a check if the name exists in the list for better validation
  114.             local found = false
  115.             for _, name in ipairs(pointsTable) do
  116.                 if name == input then
  117.                     found = true
  118.                     break
  119.                 end
  120.             end
  121.  
  122.             if found then
  123.                 return input -- Return the text name directly
  124.             else
  125.                 term.setTextColour(colors.orange)
  126.                 print(string.format("Warning: '%s' not found in known points. Proceeding anyway.", input))
  127.                 term.setTextColour(colors.white)
  128.                 -- You could make this an error if you want strict validation:
  129.                 -- error(string.format("Error: '%s' is not a valid location name.", input))
  130.                 return input -- Allow unknown names for flexibility, or change to `nil` to force selection from list
  131.             end
  132.         end
  133.     end
  134. end
  135.  
  136.  
  137. local function sendOrder()
  138.     term.clear()
  139.     term.setCursorPos(1,1)
  140.  
  141.     getAndDisplayPoints() -- Display points and populate currentAvailablePoints
  142.  
  143.     print("\n--- Send Cycle Order ---")
  144.     local pickupLocation = getLocationInput("Enter Pickup Location (Number or Name): ", currentAvailablePoints)
  145.     if not pickupLocation then -- If getLocationInput returned nil (empty/aborted)
  146.         return
  147.     end
  148.  
  149.     local dropOffLocation = getLocationInput("Enter Drop-off Location (Number or Name): ", currentAvailablePoints)
  150.     if not dropOffLocation then -- If getLocationInput returned nil (empty/aborted)
  151.         return
  152.     end
  153.  
  154.     local messageData = {
  155.         pickupLocation = pickupLocation,
  156.         dropOffLocation = dropOffLocation
  157.     }
  158.  
  159.     print(string.format("Sending order: Pick up from '%s', Deliver to '%s'...", pickupLocation, dropOffLocation))
  160.  
  161.     local success, message = rednet.send(TARGET_TURTLE_ID, messageData, REDNET_PROTOCOL)
  162.     local id, reply, protocol = rednet.receive("ConfirmSuccess", 1) -- Add a timeout for robustness
  163.  
  164.     if id then
  165.         term.setTextColour(colors.green)
  166.         print("Order sent successfully!")
  167.         term.setTextColour(colors.white)
  168.     else
  169.         term.setTextColour(colors.red)
  170.         print("Failed to send order: , retrying" .. tostring(message))
  171.         term.setTextColour(colors.white)
  172.     end
  173.     print("--------------------------------------------------")
  174. end
  175.  
  176. -- Main loop to allow sending multiple orders
  177. while true do
  178.     sendOrder()
  179.     write("Send another order? (y/n): ")
  180.     local response = string.lower(read())
  181.     if response ~= "y" then
  182.         break
  183.     end
  184. end
  185.  
  186. print("Sender script finished.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement