Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- wireless_computer.lua
- local modem = peripheral.find("modem")
- if not modem then
- print("No modem found")
- return
- end
- local basePort = 2469
- modem.open(basePort)
- local turtles = {}
- local function scanTurtles()
- turtles = {}
- modem.transmit(basePort, basePort, "ping")
- print("Scanning for turtles...")
- local timeout = os.startTimer(2)
- while true do
- local event, side, channel, replyChannel, message, distance = os.pullEvent()
- if event == "modem_message" and channel == basePort and type(message) == "table" and message.command == "pong" then
- table.insert(turtles, {id = message.id, channel = replyChannel})
- print("Found turtle " .. message.id)
- elseif event == "timer" and side == timeout then
- break
- end
- end
- end
- local function selectTurtle()
- if #turtles == 0 then
- print("No turtles found.")
- return nil
- end
- print("Select a turtle:")
- for i, turtle in ipairs(turtles) do
- print(i .. ". Turtle " .. turtle.id)
- end
- local choice = tonumber(read())
- return turtles[choice]
- end
- local function waitForResponse(expectedCommand, turtleId)
- local timer = os.startTimer(5) -- 5-second timeout
- while true do
- local event, param1, param2, param3, message = os.pullEvent()
- if event == "modem_message" and type(message) == "table" and message.command == expectedCommand and message.id == turtleId then
- return message
- elseif event == "timer" and param1 == timer then
- print("No response received.")
- return nil
- end
- end
- end
- local function controlTurtle(turtle)
- while true do
- print("Controlling Turtle " .. turtle.id)
- print("1. Smartmine")
- print("2. Stop")
- print("3. Stats")
- print("4. Exit")
- local choice = tonumber(read())
- if choice == 1 then
- print("Enter length:")
- local length = tonumber(read())
- print("Enter width:")
- local width = tonumber(read())
- print("Enter height (positive for up, negative for down):")
- local height = tonumber(read())
- modem.transmit(basePort, basePort, {command = "smartmine", id = turtle.id, length = length, width = width, height = height})
- print("Mining command sent to Turtle " .. turtle.id)
- elseif choice == 2 then
- modem.transmit(basePort, basePort, {command = "stop", id = turtle.id})
- local response = waitForResponse("stopped", turtle.id)
- if response then
- print("Turtle " .. turtle.id .. " stopped.")
- end
- elseif choice == 3 then
- modem.transmit(basePort, basePort, {command = "stats", id = turtle.id})
- local response = waitForResponse("stats", turtle.id)
- if response then
- print("Turtle " .. turtle.id .. " stats:")
- print("Fuel level: " .. response.fuel)
- print("Current task: " .. (response.task or "None"))
- end
- elseif choice == 4 then
- break
- else
- print("Invalid choice")
- end
- end
- end
- local function mainMenu()
- while true do
- print("\nMain Menu:")
- print("1. Scan for turtles")
- print("2. Select a turtle")
- print("3. Exit")
- local choice = tonumber(read())
- if choice == 1 then
- scanTurtles()
- elseif choice == 2 then
- if #turtles == 0 then
- print("No turtles found. Please scan first.")
- else
- local selectedTurtle = selectTurtle()
- if selectedTurtle then
- controlTurtle(selectedTurtle)
- end
- end
- elseif choice == 3 then
- print("Exiting program.")
- break
- else
- print("Invalid choice")
- end
- end
- end
- mainMenu()
Advertisement
Add Comment
Please, Sign In to add comment