Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- pastebin ID: kfHc79KV
- -- URL: https://pastebin.com/kfHc79KV
- -- Mainframe discovery
- local mainframeId = nil
- -- Function to open a modem on any side
- local function openModem()
- local sides = {"left", "right", "top", "bottom", "front", "back"}
- for _, side in ipairs(sides) do
- if peripheral.getType(side) == "modem" then
- rednet.open(side)
- return true
- end
- end
- return false
- end
- -- Function to discover mainframe ID via broadcast
- local function discoverMainframe()
- if mainframeId then return mainframeId end
- if not openModem() then
- print("No modem found. Please attach a modem to the computer.")
- return nil
- end
- print("Broadcasting to discover mainframe...")
- rednet.broadcast("discover_mainframe")
- -- Wait for responses for up to 3 seconds
- local timeout = os.clock() + 3
- while os.clock() < timeout do
- local senderId, message = rednet.receive(0.1)
- if senderId and message == "mainframe_here" then
- mainframeId = senderId
- print("Discovered mainframe at ID: " .. mainframeId)
- rednet.close()
- return mainframeId
- end
- end
- print("No mainframe found via broadcast")
- rednet.close()
- return nil
- end
- -- CLI function to send commands to the turtle
- local function turtleCLI(turtleId)
- if not openModem() then
- print("No modem found. Please attach a modem to the computer.")
- return
- end
- print("Connected to turtle " .. turtleId .. ". Type 'exit' to quit.")
- while true do
- io.write("> ")
- local command = read()
- if command == "exit" then
- break
- end
- rednet.send(turtleId, command)
- local senderId, response = rednet.receive(5) -- Wait for up to 5 seconds for a response
- if senderId == turtleId then
- print("Response: " .. response)
- else
- print("No response from turtle " .. turtleId)
- end
- end
- rednet.close()
- end
- -- Function to get turtle locations from mainframe
- local function getLocations()
- local mainframeId = discoverMainframe()
- if not mainframeId then return end
- if not openModem() then
- print("No modem found. Please attach a modem to the computer.")
- return
- end
- print("Requesting turtle locations from mainframe " .. mainframeId .. "...")
- rednet.send(mainframeId, "get_locations")
- local senderId, response = rednet.receive(5) -- Wait for up to 5 seconds for a response
- if senderId == mainframeId then
- if type(response) == "table" and next(response) then
- print("Turtle Locations:")
- print("================")
- for turtleId, location in pairs(response) do
- print("Turtle " .. turtleId .. ": (" .. location.x .. ", " .. location.y .. ", " .. location.z .. ")")
- end
- else
- print("No turtle location data available")
- end
- else
- print("No response from mainframe " .. mainframeId)
- end
- rednet.close()
- end
- -- Function to get specific turtle location from mainframe
- local function getLocation(targetTurtleId)
- local mainframeId = discoverMainframe()
- if not mainframeId then return end
- if not openModem() then
- print("No modem found. Please attach a modem to the computer.")
- return
- end
- print("Requesting location for turtle " .. targetTurtleId .. " from mainframe " .. mainframeId .. "...")
- rednet.send(mainframeId, "get_location " .. targetTurtleId)
- local senderId, response = rednet.receive(5) -- Wait for up to 5 seconds for a response
- if senderId == mainframeId then
- if type(response) == "table" and response.x then
- print("Turtle " .. targetTurtleId .. " location: (" .. response.x .. ", " .. response.y .. ", " .. response.z .. ")")
- else
- print("Response: " .. tostring(response))
- end
- else
- print("No response from mainframe " .. mainframeId)
- end
- rednet.close()
- end
- -- Function to get all precious block reports from mainframe
- local function getPreciousReports()
- local mainframeId = discoverMainframe()
- if not mainframeId then return end
- if not openModem() then
- print("No modem found. Please attach a modem to the computer.")
- return
- end
- print("Requesting precious block reports from mainframe " .. mainframeId .. "...")
- rednet.send(mainframeId, "get_precious")
- local senderId, response = rednet.receive(5) -- Wait for up to 5 seconds for a response
- if senderId == mainframeId then
- if type(response) == "table" and next(response) then
- print("Precious Block Reports:")
- print("======================")
- for turtleId, reports in pairs(response) do
- print("Turtle " .. turtleId:gsub("turtle_", "") .. " (" .. #reports .. " reports):")
- for i, report in ipairs(reports) do
- print(" " .. i .. ". " .. report)
- end
- print()
- end
- else
- print("No precious block reports available")
- end
- else
- print("No response from mainframe " .. mainframeId)
- end
- rednet.close()
- end
- -- Function to get precious block reports for specific turtle
- local function getPreciousReportsForTurtle(targetTurtleId)
- local mainframeId = discoverMainframe()
- if not mainframeId then return end
- if not openModem() then
- print("No modem found. Please attach a modem to the computer.")
- return
- end
- print("Requesting precious block reports for turtle " .. targetTurtleId .. " from mainframe " .. mainframeId .. "...")
- rednet.send(mainframeId, "get_precious " .. targetTurtleId)
- local senderId, response = rednet.receive(5) -- Wait for up to 5 seconds for a response
- if senderId == mainframeId then
- if type(response) == "table" and #response > 0 then
- print("Precious Block Reports for Turtle " .. targetTurtleId .. ":")
- print("================================================")
- for i, report in ipairs(response) do
- print(i .. ". " .. report)
- end
- else
- print("Response: " .. tostring(response))
- end
- else
- print("No response from mainframe " .. mainframeId)
- end
- rednet.close()
- end
- -- Function to scrub precious block reports for specific turtle
- local function scrubPreciousReports(targetTurtleId)
- local mainframeId = discoverMainframe()
- if not mainframeId then return end
- if not openModem() then
- print("No modem found. Please attach a modem to the computer.")
- return
- end
- print("Scrubbing precious block reports for turtle " .. targetTurtleId .. " from mainframe " .. mainframeId .. "...")
- rednet.send(mainframeId, "scrub_precious " .. targetTurtleId)
- local senderId, response = rednet.receive(5) -- Wait for up to 5 seconds for a response
- if senderId == mainframeId then
- print("Response: " .. tostring(response))
- else
- print("No response from mainframe " .. mainframeId)
- end
- rednet.close()
- end
- -- Main function to start the CLI
- local args = { ... }
- if #args < 1 then
- print("Usage:")
- print(" remote <turtle-id> - Connect to turtle")
- print(" remote locations - Get all turtle locations")
- print(" remote location <turtle-id> - Get specific turtle location")
- print(" remote precious - Get all precious block reports")
- print(" remote precious <turtle-id> - Get precious reports for specific turtle")
- print(" remote scrub <turtle-id> - Clear precious reports for specific turtle")
- elseif args[1] == "locations" then
- getLocations()
- elseif args[1] == "location" and #args >= 2 then
- local targetTurtleId = tonumber(args[2])
- if targetTurtleId then
- getLocation(targetTurtleId)
- else
- print("Invalid turtle ID.")
- end
- elseif args[1] == "precious" then
- if #args >= 2 then
- local targetTurtleId = tonumber(args[2])
- if targetTurtleId then
- getPreciousReportsForTurtle(targetTurtleId)
- else
- print("Invalid turtle ID.")
- end
- else
- getPreciousReports()
- end
- elseif args[1] == "scrub" and #args >= 2 then
- local targetTurtleId = tonumber(args[2])
- if targetTurtleId then
- scrubPreciousReports(targetTurtleId)
- else
- print("Invalid turtle ID.")
- end
- else
- local turtleId = tonumber(args[1])
- if turtleId then
- turtleCLI(turtleId)
- else
- print("Invalid turtle ID.")
- end
- end
Advertisement