Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Configuration for sending
- local TARGET_TURTLE_ID = nil
- local REDNET_PROTOCOL = "automata_cycle" -- MUST match the protocol in your automation turtle's script
- local REQUEST_POINTS_PROTOCOL = "request_points" -- New protocol for requesting points
- local REPLY_POINTS_PROTOCOL = "reply_points" -- New protocol for replying with points
- -- Find the modem peripheral on THIS computer/turtle
- local senderModem = peripheral.find("modem")
- local senderModemSide = nil
- -- Ensure modem is found
- if not senderModem then
- error("Error: No modem peripheral found on this computer/turtle. Please attach one.")
- end
- -- --- MODEM INITIALIZATION ---
- -- This section is your working implementation for finding and opening the modem.
- local modemFound = false
- local sides = {"left", "right", "top", "bottom", "front", "back"}
- for i, side in ipairs(sides) do
- local device = peripheral.getType(side)
- if device == "modem" then
- rednet.open(side) -- Opens Rednet on the detected side
- senderModem = peripheral.wrap(side) -- Updates senderModem to the one just opened
- senderModemSide = side
- if senderModem.isWireless() then -- Corrected this line to refer to senderModem
- modemFound = true
- break
- end
- end
- end
- -- Get the turtle's ID
- print("Broadcasting for Turtle ID...")
- rednet.broadcast("yo", "getTurtleID")
- local id, message, protocol = rednet.receive("IDReply", 1) -- Add a timeout for robustness
- if id and message == "it's a me" then
- TARGET_TURTLE_ID = tonumber(id)
- print(string.format("Found Turtle ID: %d", TARGET_TURTLE_ID))
- else
- error("Could not find automation turtle. Ensure it is running and in range.")
- end
- print("Rednet Sender Ready.")
- print(string.format("Sending orders to Turtle ID %d using protocol '%s'", TARGET_TURTLE_ID, REDNET_PROTOCOL))
- print(string.format("Modem detected and Rednet opened on side: %s", senderModemSide))
- print("--------------------------------------------------")
- -- Global variable to store received points for easy lookup
- local currentAvailablePoints = {}
- --- Requests and displays available warp points from the turtle.
- -- This function now also updates the 'currentAvailablePoints' global and displays numbers.
- local function getAndDisplayPoints()
- rednet.send(TARGET_TURTLE_ID, "get_points", REQUEST_POINTS_PROTOCOL)
- local id, receivedPoints, protocol = rednet.receive(REPLY_POINTS_PROTOCOL, 10) -- Wait up to 10 seconds
- if receivedPoints and protocol == REPLY_POINTS_PROTOCOL and id == TARGET_TURTLE_ID then
- term.setTextColour(colors.lightGray)
- print("--- Available Warp Points ---")
- if type(receivedPoints) == "table" and #receivedPoints > 0 then
- currentAvailablePoints = receivedPoints -- Store points for lookup
- for i, pointName in ipairs(receivedPoints) do
- print(string.format("%d: %s", i, pointName)) -- Display with number prefix
- end
- else
- print("No points found on the turtle.")
- currentAvailablePoints = {} -- Clear points if none found
- end
- print("-----------------------------")
- term.setTextColour(colors.white)
- return true -- Indicate success
- else
- term.setTextColour(colors.red)
- print("Failed to get points from turtle or received an invalid response.")
- print("Error: " .. tostring(receivedPoints))
- term.setTextColour(colors.white)
- currentAvailablePoints = {} -- Clear points on failure
- return false -- Indicate failure
- end
- end
- --- Custom input function to handle numeric indices or direct names.
- -- @param prompt string The text prompt to display.
- -- @param pointsTable table The table of available point names (currentAvailablePoints).
- -- @return string The resolved location name, or nil if aborted.
- local function getLocationInput(prompt, pointsTable)
- while true do
- write(prompt)
- local input = read()
- if not input or input:len() == 0 then
- print("Location cannot be empty.")
- return nil -- Abort or indicate failure
- end
- local numInput = tonumber(input)
- if numInput then
- -- User entered a number, try to map it to a point name
- if numInput >= 1 and numInput <= #pointsTable then
- return pointsTable[numInput] -- Return the actual name
- else
- term.setTextColour(colors.red)
- print(string.format("Invalid number. Please enter a number between 1 and %d or a name.", #pointsTable))
- term.setTextColour(colors.white)
- end
- else
- -- User entered text, assume it's the direct name
- -- Optional: Add a check if the name exists in the list for better validation
- local found = false
- for _, name in ipairs(pointsTable) do
- if name == input then
- found = true
- break
- end
- end
- if found then
- return input -- Return the text name directly
- else
- term.setTextColour(colors.orange)
- print(string.format("Warning: '%s' not found in known points. Proceeding anyway.", input))
- term.setTextColour(colors.white)
- -- You could make this an error if you want strict validation:
- -- error(string.format("Error: '%s' is not a valid location name.", input))
- return input -- Allow unknown names for flexibility, or change to `nil` to force selection from list
- end
- end
- end
- end
- local function sendOrder()
- term.clear()
- term.setCursorPos(1,1)
- getAndDisplayPoints() -- Display points and populate currentAvailablePoints
- print("\n--- Send Cycle Order ---")
- local pickupLocation = getLocationInput("Enter Pickup Location (Number or Name): ", currentAvailablePoints)
- if not pickupLocation then -- If getLocationInput returned nil (empty/aborted)
- return
- end
- local dropOffLocation = getLocationInput("Enter Drop-off Location (Number or Name): ", currentAvailablePoints)
- if not dropOffLocation then -- If getLocationInput returned nil (empty/aborted)
- return
- end
- local messageData = {
- pickupLocation = pickupLocation,
- dropOffLocation = dropOffLocation
- }
- print(string.format("Sending order: Pick up from '%s', Deliver to '%s'...", pickupLocation, dropOffLocation))
- local success, message = rednet.send(TARGET_TURTLE_ID, messageData, REDNET_PROTOCOL)
- local id, reply, protocol = rednet.receive("ConfirmSuccess", 1) -- Add a timeout for robustness
- if id then
- term.setTextColour(colors.green)
- print("Order sent successfully!")
- term.setTextColour(colors.white)
- else
- term.setTextColour(colors.red)
- print("Failed to send order: , retrying" .. tostring(message))
- term.setTextColour(colors.white)
- end
- print("--------------------------------------------------")
- end
- -- Main loop to allow sending multiple orders
- while true do
- sendOrder()
- write("Send another order? (y/n): ")
- local response = string.lower(read())
- if response ~= "y" then
- break
- end
- end
- print("Sender script finished.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement