Advertisement
Volaik

hub.lua

Oct 5th, 2023 (edited)
1,186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.39 KB | Source Code | 0 0
  1. clients = {}
  2.  
  3. local modem = peripheral.find("modem")
  4. rednet.open("back")
  5.  
  6. HUBID = os.getComputerID()
  7. print("HUB Computer ID: "..HUBID)
  8.  
  9. local command = nil
  10.  
  11. function remoteControl()
  12.  
  13.     io.write("Select a turtle to Control: ")
  14.     slave = tonumber(read())
  15.    
  16.     rednet.send(slave, "rcConnect", rc)
  17.  
  18.     while true do
  19.         local event, key, isHeld = os.pullEvent("key")
  20.         if key == keys.w then
  21.             rednet.send(slave, "forward")
  22.         elseif key == keys.s then
  23.             rednet.send(slave, "backward")
  24.         elseif key == keys.a then
  25.             rednet.send(slave, "left")
  26.         elseif key == keys.d then
  27.             rednet.send(slave, "right")
  28.         elseif key == keys.q then
  29.             rednet.send(slave, "up")
  30.         elseif key == keys.e then
  31.             rednet.send(slave, "down")
  32.         elseif key == keys.numPad5 then
  33.             rednet.send(slave, "dig")
  34.         elseif key == keys.numPad8 then
  35.             rednet.send(slave, "digUp")
  36.         elseif key == keys.numPad2 then
  37.             rednet.send(slave, "digDown")
  38.         elseif key == keys.insert then
  39.             rednet.send(slave, "place")
  40.         elseif key == keys.up then
  41.             rednet.send(slave, "placeUp")
  42.         elseif key == keys.down then
  43.             rednet.send(slave, "placeDown")
  44.         elseif key == keys.right then
  45.             rednet.send(slave, "slotNext")
  46.         elseif key == keys.left then
  47.             rednet.send(slave, "slotPrev")
  48.         elseif key == keys.home then
  49.             rednet.send(slave, "rcDisc")
  50.             print("Connection from "..slave.." terminated.")
  51.             break
  52.         end
  53.     end
  54. end
  55.  
  56. function list()
  57.     for i,v in ipairs(clients) do
  58.         print("ID "..v)
  59.     end
  60. end
  61.  
  62. function newConnection()
  63.     event, sender, message, protocol = os.pullEvent("rednet_message")
  64.     if message == "connectMsg" then
  65.     table.insert(clients, sender)
  66.     print(sender.." has connected.")
  67.     rednet.send(sender, "connectSuccess",  connect)
  68.     end
  69. end
  70.  
  71. function userInput()
  72.     command = io.read()
  73. end
  74.  
  75. while true do
  76.     parallel.waitForAny(newConnection,userInput)
  77.  
  78.     if command == "list" then
  79.         if #clients == 0 then
  80.             print("No connections found.")
  81.         else
  82.             list()
  83.             command = nil
  84.         end
  85.     end
  86.  
  87.     if command == "RC" then
  88.         remoteControl()
  89.     end
  90. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement