Advertisement
1lann

repeat

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