Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Configuration
- local PLACE_MOB_DELAY = 0.1 -- Delay between placing mobs at a location
- local REDNET_PROTOCOL = "automata_cycle" -- Unique string for our communication protocol
- local REQUEST_POINTS_PROTOCOL = "request_points" -- New protocol for requesting points
- local REPLY_POINTS_PROTOCOL = "reply_points" -- New protocol for replying with points
- -- Peripheral definitions
- local port = peripheral.find("endAutomata")
- local rednetModem = peripheral.find("modem") -- Find any modem for Rednet
- -- Ensure peripherals are found
- if not port then
- error("Error: 'endAutomata' peripheral not found.")
- end
- if not rednetModem then
- error("Error: A modem peripheral is required for Rednet (e.g., wired modem or wireless modem).")
- end
- rednet.open("right") -- This connects Rednet through the modem on its attached side
- local savedPoints = port.points() -- Load points once at startup
- --- Displays all saved warp points.
- local function showPoints()
- print("--- Saved Warp Points ---")
- if #savedPoints == 0 then
- print("No points saved.")
- else
- for i, pointName in ipairs(savedPoints) do
- print(string.format("%d: %s", i, pointName))
- end
- end
- print("-------------------------")
- end
- --- Warps the turtle to a specified point.
- -- @param pointName string The name of the warp point.
- local function portTo(pointName)
- if not port then return false, "Port peripheral not found." end
- local success, message = port.warpToPoint(pointName)
- if not success then
- print(string.format("Warp failed to '%s': %s", pointName, tostring(message)))
- end
- return success
- end
- --- Activates the spawner by toggling redstone and placing an item.
- local function activateSpawner()
- print("Activating spawner...")
- redstone.setOutput("bottom", true)
- local success = false
- repeat
- sleep(0.1)
- success = turtle.place()
- success = turtle.placeDown()
- until success
- redstone.setOutput("bottom", false)
- sleep(0.3)
- end
- --- Picks up items from a spawner location.
- -- @param location string The name of the warp point for pickup.
- local function pickUp(location)
- if not portTo(location) then return end
- activateSpawner()
- print(string.format("Picked up from '%s'.", location))
- end
- --- Delivers items to a drop-off location and returns to Main.
- -- @param location string The name of the warp point for delivery.
- local function deliver(location)
- if not portTo(location) then return end
- turtle.placeUp() -- Assumes placing the collected items
- print(string.format("Delivered to '%s'", location))
- portTo("Main")
- end
- --- Performs a full cycle of picking up and delivering items.
- -- @param pickUpLocation string The warp point to pick up items from.
- -- @param dropOffLocation string The warp point to drop off items at.
- local function fullCycle(pickUpLocation, dropOffLocation)
- term.clear()
- term.setCursorPos(1, 1)
- term.setTextColour(colors.yellow) -- Set color for the start message
- print(string.format("Starting cycle: %s -> %s", pickUpLocation, dropOffLocation))
- term.setTextColour(colors.white) -- Reset to white
- pickUp(pickUpLocation)
- deliver(dropOffLocation)
- term.setTextColour(colors.green) -- Set color for the success message
- print(string.format("Cycle complete: %s -> %s", pickUpLocation, dropOffLocation))
- term.setTextColour(colors.white) -- Reset to white
- end
- --- Rednet listener function to receive commands for full cycles or point requests.
- local function rednetListener()
- print(string.format("Listening for Rednet messages with protocol '%s' or '%s'...", REDNET_PROTOCOL, REQUEST_POINTS_PROTOCOL))
- while true do
- local id, message, protocol = rednet.receive()
- if message and protocol == REDNET_PROTOCOL then
- rednet.send(id, "yep", "ConfirmSuccess")
- print(string.format("Received cycle command from ID %d with protocol '%s':", id, tostring(protocol)))
- if type(message) == "table" and message.pickupLocation and message.dropOffLocation then
- print("Valid cycle command received. Executing cycle...")
- fullCycle(message.pickupLocation, message.dropOffLocation)
- else
- print("Invalid message format. Expected a table with 'pickupLocation' and 'dropOffLocation'.")
- end
- elseif message and protocol == "getTurtleID" then
- rednet.send(id, "it's a me", "IDReply")
- elseif message and protocol == REQUEST_POINTS_PROTOCOL then
- print(string.format("Received request for points from ID %d.", id))
- -- Update savedPoints in case they changed since startup
- savedPoints = port.points()
- local success = rednet.send(id, savedPoints, REPLY_POINTS_PROTOCOL)
- if success then
- term.clear()
- term.setCursorPos(1,1)
- print(string.format("Sent %d points to ID %d.", #savedPoints, id))
- else
- print(string.format("Failed to send points to ID %d.", id))
- end
- end
- end
- end
- local function removeLocations()
- for k, v in pairs(savedPoints)
- do
- port.deletePoint(v)
- end
- end
- -- Start the Rednet listener
- rednetListener()
- -- removeLocations()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement