Advertisement
Shaka01

remoteMobMover

Jun 29th, 2025
406
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.28 KB | None | 0 0
  1. -- Configuration
  2. local PLACE_MOB_DELAY = 0.1      -- Delay between placing mobs at a location
  3. local REDNET_PROTOCOL = "automata_cycle" -- Unique string for our communication protocol
  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. -- Peripheral definitions
  8. local port = peripheral.find("endAutomata")
  9. local rednetModem = peripheral.find("modem") -- Find any modem for Rednet
  10.  
  11. -- Ensure peripherals are found
  12. if not port then
  13.     error("Error: 'endAutomata' peripheral not found.")
  14. end
  15. if not rednetModem then
  16.     error("Error: A modem peripheral is required for Rednet (e.g., wired modem or wireless modem).")
  17. end
  18.  
  19. rednet.open("right") -- This connects Rednet through the modem on its attached side
  20.  
  21. local savedPoints = port.points() -- Load points once at startup
  22.  
  23. --- Displays all saved warp points.
  24. local function showPoints()
  25.     print("--- Saved Warp Points ---")
  26.     if #savedPoints == 0 then
  27.         print("No points saved.")
  28.     else
  29.         for i, pointName in ipairs(savedPoints) do
  30.             print(string.format("%d: %s", i, pointName))
  31.         end
  32.     end
  33.     print("-------------------------")
  34. end
  35.  
  36. --- Warps the turtle to a specified point.
  37. -- @param pointName string The name of the warp point.
  38. local function portTo(pointName)
  39.     if not port then return false, "Port peripheral not found." end
  40.  
  41.     local success, message = port.warpToPoint(pointName)
  42.  
  43.     if not success then
  44.         print(string.format("Warp failed to '%s': %s", pointName, tostring(message)))
  45.     end
  46.     return success
  47. end
  48.  
  49. --- Activates the spawner by toggling redstone and placing an item.
  50. local function activateSpawner()
  51.     print("Activating spawner...")
  52.     redstone.setOutput("bottom", true)
  53.     local success = false
  54.     repeat
  55.         sleep(0.1)
  56.         success = turtle.place()
  57.         success = turtle.placeDown()
  58.     until success
  59.     redstone.setOutput("bottom", false)
  60.     sleep(0.3)
  61. end
  62.  
  63. --- Picks up items from a spawner location.
  64. -- @param location string The name of the warp point for pickup.
  65. local function pickUp(location)
  66.     if not portTo(location) then return end
  67.     activateSpawner()
  68.     print(string.format("Picked up from '%s'.", location))
  69. end
  70.  
  71. --- Delivers items to a drop-off location and returns to Main.
  72. -- @param location string The name of the warp point for delivery.
  73. local function deliver(location)
  74.     if not portTo(location) then return end
  75.     turtle.placeUp() -- Assumes placing the collected items
  76.     print(string.format("Delivered to '%s'", location))
  77.     portTo("Main")
  78. end
  79.  
  80. --- Performs a full cycle of picking up and delivering items.
  81. -- @param pickUpLocation string The warp point to pick up items from.
  82. -- @param dropOffLocation string The warp point to drop off items at.
  83. local function fullCycle(pickUpLocation, dropOffLocation)
  84.     term.clear()
  85.     term.setCursorPos(1, 1)
  86.  
  87.     term.setTextColour(colors.yellow) -- Set color for the start message
  88.     print(string.format("Starting cycle: %s -> %s", pickUpLocation, dropOffLocation))
  89.     term.setTextColour(colors.white) -- Reset to white
  90.  
  91.     pickUp(pickUpLocation)
  92.     deliver(dropOffLocation)
  93.  
  94.     term.setTextColour(colors.green) -- Set color for the success message
  95.     print(string.format("Cycle complete: %s -> %s", pickUpLocation, dropOffLocation))
  96.     term.setTextColour(colors.white) -- Reset to white
  97. end
  98.  
  99. --- Rednet listener function to receive commands for full cycles or point requests.
  100. local function rednetListener()
  101.     print(string.format("Listening for Rednet messages with protocol '%s' or '%s'...", REDNET_PROTOCOL, REQUEST_POINTS_PROTOCOL))
  102.     while true do
  103.         local id, message, protocol = rednet.receive()
  104.  
  105.         if message and protocol == REDNET_PROTOCOL then
  106.             rednet.send(id, "yep", "ConfirmSuccess")
  107.             print(string.format("Received cycle command from ID %d with protocol '%s':", id, tostring(protocol)))
  108.             if type(message) == "table" and message.pickupLocation and message.dropOffLocation then
  109.                 print("Valid cycle command received. Executing cycle...")
  110.                 fullCycle(message.pickupLocation, message.dropOffLocation)
  111.             else
  112.                 print("Invalid message format. Expected a table with 'pickupLocation' and 'dropOffLocation'.")
  113.             end
  114.         elseif message and protocol == "getTurtleID" then
  115.             rednet.send(id, "it's a me", "IDReply")
  116.         elseif message and protocol == REQUEST_POINTS_PROTOCOL then
  117.             print(string.format("Received request for points from ID %d.", id))
  118.             -- Update savedPoints in case they changed since startup
  119.             savedPoints = port.points()
  120.             local success = rednet.send(id, savedPoints, REPLY_POINTS_PROTOCOL)
  121.             if success then
  122.                 term.clear()
  123.                 term.setCursorPos(1,1)
  124.                 print(string.format("Sent %d points to ID %d.", #savedPoints, id))
  125.             else
  126.                 print(string.format("Failed to send points to ID %d.", id))
  127.             end
  128.         end
  129.     end
  130. end
  131.  
  132. local function removeLocations()
  133.     for k, v in pairs(savedPoints)
  134.         do
  135.         port.deletePoint(v)
  136.     end
  137. end
  138.  
  139. -- Start the Rednet listener
  140. rednetListener()
  141.  
  142. -- removeLocations()
  143.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement