LDShadowLord

TransCeiver

Feb 21st, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.23 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. term.clear()
  33. term.setCursorPos(1,1)
  34. print( "Messages Repeated:     0" )
  35. open( rednet.CHANNEL_REPEAT )
  36.  
  37. -- Main loop (terminate to break)
  38. local ok, error = pcall( function()
  39.     local tReceivedMessages = {}
  40.     local tReceivedMessageTimeouts = {}
  41.     local nTransmittedMessages = 0
  42.  
  43.     while true do
  44.         local sEvent, sModem, nChannel, nReplyChannel, tMessage = os.pullEvent()
  45.         if sEvent == "modem_message" then
  46.             -- Got a modem message, rebroadcast it if it's a rednet thing
  47.             if nChannel == rednet.CHANNEL_REPEAT then
  48.                 if type( tMessage ) == "table" and tMessage.nMessageID and tMessage.nRecipient then
  49.                     if not tReceivedMessages[ tMessage.nMessageID ] then
  50.                         -- Ensure we only repeat a message once
  51.                         tReceivedMessages[ tMessage.nMessageID ] = true
  52.                         tReceivedMessageTimeouts[ os.startTimer( 30 ) ] = tMessage.nMessageID
  53.  
  54.                         -- Send on all other open modems, to the target and to other repeaters
  55.                         for n=1,#tModems do
  56.                             local sOtherModem = tModems[n]
  57.                             peripheral.call( sOtherModem, "transmit", rednet.CHANNEL_REPEAT, nReplyChannel, tMessage )
  58.                             peripheral.call( sOtherModem, "transmit", tMessage.nRecipient, nReplyChannel, tMessage )
  59.                         end
  60.  
  61.                         -- Log the event
  62.                         nTransmittedMessages = nTransmittedMessages + 1
  63.                         local x,y = term.getCursorPos()
  64.                         term.setCursorPos( 1, y - 1 )
  65.                         term.clearLine()
  66.                         if nTransmittedMessages == 1 then
  67.                            
  68.                             print( "Messages Repeated:     " .. nTransmittedMessages)
  69.                         else
  70.                             print( "Messages Repeated:     " .. nTransmittedMessages)
  71.                         end
  72.                     end
  73.                 end
  74.             end
  75.  
  76.         elseif sEvent == "timer" then
  77.             -- Got a timer event, use it to clear the message history
  78.             local nTimer = sModem
  79.             local nMessageID = tReceivedMessageTimeouts[ nTimer ]
  80.             if nMessageID then
  81.                 tReceivedMessageTimeouts[ nTimer ] = nil
  82.                 tReceivedMessages[ nMessageID ] = nil
  83.             end
  84.  
  85.         end
  86.     end
  87. end )
  88. if not ok then
  89.     printError( error )
  90. end
  91.  
  92. -- Close channels
  93. close( rednet.CHANNEL_REPEAT )
Advertisement
Add Comment
Please, Sign In to add comment