Guest User

repeat

a guest
Jun 18th, 2016
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.16 KB | None | 0 0
  1.  
  2. -- Find modems
  3. local tModems = {}
  4. for n,sModem in ipairs( peripheral.getNames() ) do
  5.     if peripheral.getType( sModem ) == "modem" then
  6.         table.insert( tModems, sModem )
  7.     end
  8. end
  9. if #tModems == 0 then
  10.     print( "No modems found." )
  11.     return
  12. elseif #tModems == 1 then
  13.     print( "1 modem found." )
  14. else
  15.     print( #tModems .. " modems found." )
  16. end
  17.  
  18. function open( nChannel )
  19.     for n=1,#tModems do
  20.         local sModem = tModems[n]
  21.         peripheral.call( sModem, "open", nChannel )
  22.     end
  23. end
  24.  
  25. function close( nChannel )
  26.     for n=1,#tModems do
  27.         local sModem = tModems[n]
  28.         peripheral.call( sModem, "close", nChannel )
  29.     end
  30. end
  31.  
  32. -- Open channels
  33. print( "0 messages repeated." )
  34. open( rednet.CHANNEL_REPEAT )
  35.  
  36. -- Main loop (terminate to break)
  37. local ok, error = pcall( function()
  38.     local tReceivedMessages = {}
  39.     local tReceivedMessageTimeouts = {}
  40.     local nTransmittedMessages = 0
  41.  
  42.     while true do
  43.         local sEvent, sModem, nChannel, nReplyChannel, tMessage = os.pullEvent()
  44.         if sEvent == "modem_message" then
  45.             -- Got a modem message, rebroadcast it if it's a rednet thing
  46.             if nChannel == rednet.CHANNEL_REPEAT then
  47.                 if type( tMessage ) == "table" and tMessage.nMessageID and tMessage.nRecipient then
  48.                     if not tReceivedMessages[ tMessage.nMessageID ] then
  49.                         -- Ensure we only repeat a message once
  50.                         tReceivedMessages[ tMessage.nMessageID ] = true
  51.                         tReceivedMessageTimeouts[ os.startTimer( 30 ) ] = tMessage.nMessageID
  52.  
  53.                         -- Send on all other open modems, to the target and to other repeaters
  54.                         for n=1,#tModems do
  55.                             local sOtherModem = tModems[n]
  56.                             peripheral.call( sOtherModem, "transmit", rednet.CHANNEL_REPEAT, nReplyChannel, tMessage )
  57.                             peripheral.call( sOtherModem, "transmit", tMessage.nRecipient, nReplyChannel, tMessage )
  58.                         end
  59.  
  60.                         -- Log the event
  61.                         nTransmittedMessages = nTransmittedMessages + 1
  62.                         local x,y = term.getCursorPos()
  63.                         term.setCursorPos( 1, y - 1 )
  64.                         term.clearLine()
  65.                         if nTransmittedMessages == 1 then
  66.                             print( nTransmittedMessages .. " message repeated." )
  67.                         else
  68.                             print( nTransmittedMessages .. " messages repeated." )
  69.                         end
  70.                     end
  71.                 end
  72.             end
  73.  
  74.         elseif sEvent == "timer" then
  75.             -- Got a timer event, use it to clear the message history
  76.             local nTimer = sModem
  77.             local nMessageID = tReceivedMessageTimeouts[ nTimer ]
  78.             if nMessageID then
  79.                 tReceivedMessageTimeouts[ nTimer ] = nil
  80.                 tReceivedMessages[ nMessageID ] = nil
  81.             end
  82.  
  83.         end
  84.     end
  85. end )
  86. if not ok then
  87.     printError( error )
  88. end
  89.  
  90. -- Close channels
  91. close( rednet.CHANNEL_REPEAT )
Add Comment
Please, Sign In to add comment