MtnMCG

turtle control program

Jul 21st, 2024 (edited)
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.00 KB | None | 0 0
  1. -- wireless_computer.lua
  2.  
  3. local modem = peripheral.find("modem")
  4. if not modem then
  5.     print("No modem found")
  6.     return
  7. end
  8.  
  9. local basePort = 2469
  10. modem.open(basePort)
  11.  
  12. local turtles = {}
  13.  
  14. local function scanTurtles()
  15.     turtles = {}
  16.     modem.transmit(basePort, basePort, "ping")
  17.     print("Scanning for turtles...")
  18.     local timeout = os.startTimer(2)
  19.     while true do
  20.         local event, side, channel, replyChannel, message, distance = os.pullEvent()
  21.         if event == "modem_message" and channel == basePort and type(message) == "table" and message.command == "pong" then
  22.             table.insert(turtles, {id = message.id, channel = replyChannel})
  23.             print("Found turtle " .. message.id)
  24.         elseif event == "timer" and side == timeout then
  25.             break
  26.         end
  27.     end
  28. end
  29.  
  30. local function selectTurtle()
  31.     if #turtles == 0 then
  32.         print("No turtles found.")
  33.         return nil
  34.     end
  35.     print("Select a turtle:")
  36.     for i, turtle in ipairs(turtles) do
  37.         print(i .. ". Turtle " .. turtle.id)
  38.     end
  39.     local choice = tonumber(read())
  40.     return turtles[choice]
  41. end
  42.  
  43. local function waitForResponse(expectedCommand, turtleId)
  44.     local timer = os.startTimer(5)  -- 5-second timeout
  45.     while true do
  46.         local event, param1, param2, param3, message = os.pullEvent()
  47.         if event == "modem_message" and type(message) == "table" and message.command == expectedCommand and message.id == turtleId then
  48.             return message
  49.         elseif event == "timer" and param1 == timer then
  50.             print("No response received.")
  51.             return nil
  52.         end
  53.     end
  54. end
  55.  
  56. local function controlTurtle(turtle)
  57.     while true do
  58.         print("Controlling Turtle " .. turtle.id)
  59.         print("1. Smartmine")
  60.         print("2. Stop")
  61.         print("3. Stats")
  62.         print("4. Exit")
  63.         local choice = tonumber(read())
  64.  
  65.         if choice == 1 then
  66.             print("Enter length:")
  67.             local length = tonumber(read())
  68.             print("Enter width:")
  69.             local width = tonumber(read())
  70.             print("Enter height (positive for up, negative for down):")
  71.             local height = tonumber(read())
  72.             modem.transmit(basePort, basePort, {command = "smartmine", id = turtle.id, length = length, width = width, height = height})
  73.             print("Mining command sent to Turtle " .. turtle.id)
  74.         elseif choice == 2 then
  75.             modem.transmit(basePort, basePort, {command = "stop", id = turtle.id})
  76.             local response = waitForResponse("stopped", turtle.id)
  77.             if response then
  78.                 print("Turtle " .. turtle.id .. " stopped.")
  79.             end
  80.         elseif choice == 3 then
  81.             modem.transmit(basePort, basePort, {command = "stats", id = turtle.id})
  82.             local response = waitForResponse("stats", turtle.id)
  83.             if response then
  84.                 print("Turtle " .. turtle.id .. " stats:")
  85.                 print("Fuel level: " .. response.fuel)
  86.                 print("Current task: " .. (response.task or "None"))
  87.             end
  88.         elseif choice == 4 then
  89.             break
  90.         else
  91.             print("Invalid choice")
  92.         end
  93.     end
  94. end
  95.  
  96. local function mainMenu()
  97.     while true do
  98.         print("\nMain Menu:")
  99.         print("1. Scan for turtles")
  100.         print("2. Select a turtle")
  101.         print("3. Exit")
  102.         local choice = tonumber(read())
  103.  
  104.         if choice == 1 then
  105.             scanTurtles()
  106.         elseif choice == 2 then
  107.             if #turtles == 0 then
  108.                 print("No turtles found. Please scan first.")
  109.             else
  110.                 local selectedTurtle = selectTurtle()
  111.                 if selectedTurtle then
  112.                     controlTurtle(selectedTurtle)
  113.                 end
  114.             end
  115.         elseif choice == 3 then
  116.             print("Exiting program.")
  117.             break
  118.         else
  119.             print("Invalid choice")
  120.         end
  121.     end
  122. end
  123.  
  124. mainMenu()
Advertisement
Add Comment
Please, Sign In to add comment