Advertisement
BombBloke

BitNet Rednet Repeater (MoarPeripherals)

Sep 5th, 2014
1,784
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.98 KB | None | 0 0
  1. -----------------------------------------
  2. --        BitNet Rednet Repeater       --
  3. -----------------------------------------
  4.  
  5. -- By Jeffrey Alexander, aka Bomb Bloke.
  6.  
  7. -- Acts as repeater software for BitNet transmitters:
  8. -- https://github.com/theoriginalbit/MoarPeripherals/wiki/BitNet-Mini-Antenna
  9. -- https://github.com/theoriginalbit/MoarPeripherals/wiki/BitNet-Communication-Tower
  10.  
  11. -- Pretty much the same thing as the default "repeat" script:
  12. -- http://www.computercraft.info/wiki/Repeat
  13. -- ... though it supports BitNet devices as well. Can work with any combination
  14. -- of modems/towers or alongside the vanilla repeat script itself.
  15.  
  16. ---------------------------------------------
  17.  
  18. local modem = {peripheral.find("modem")}
  19. for i = 1, #modem do modem[i].open(rednet.CHANNEL_REPEAT) end
  20.  
  21. local antenna = {peripheral.find("bitnet_antenna")}
  22. local tower = {peripheral.find("bitnet_tower", function(name, object) return object.isTowerComplete() end)}
  23.  
  24. if #modem == 0 then error("No modems found - can't send / receive rednet messages without one!") end
  25.  
  26. local repeated, msgID, timerID, myEvent = 0, {}, {}
  27.  
  28. print(#modem.." modem"..(#modem==1 and "" or "s").." found.")
  29. print(#antenna.." antenna"..(#antenna==1 and "" or "s").." found.")
  30. print(#tower.." tower"..(#tower==1 and "" or "s").." found.")
  31. print("0 messages repeated.")
  32.  
  33. -- Repeat a message:
  34. local function resend(message)
  35.     -- Determine if the message has already been repeated by this system:
  36.     if msgID[tostring(message.nMessageID)] then return end
  37.    
  38.     -- Flag the message as having passed through this system:
  39.     msgID[tostring(message.nMessageID)] = true
  40.     timerID[tostring(os.startTimer(30))] = tostring(message.nMessageID)
  41.    
  42.     -- Update the onscreen repeat counter:
  43.     local curX, curY = term.getCursorPos()
  44.     term.setCursorPos(1,curY-1)
  45.     repeated = repeated + 1
  46.     print(repeated.." messages repeated.")
  47.    
  48.     -- Send a copy via all attached BitNet devices:
  49.     for i = 1, #antenna do antenna[i].transmit(message) end
  50.     for i = 1, #tower do tower[i].transmit(message) end
  51.    
  52.     -- Send a copy via all attached modems:
  53.     for i = 1, #modem do
  54.         modem[i].transmit(message.nRecipient,    message.nSender, message)
  55.         modem[i].transmit(rednet.CHANNEL_REPEAT, message.nSender, message)
  56.     end
  57. end
  58.  
  59. -- Main loop:
  60. while true do
  61.     myEvent = {os.pullEvent()}
  62.    
  63.     -- Message arriving via tower:
  64.     if myEvent[1] == "bitnet_message" and type(myEvent[3]) == "table" and myEvent[3].nMessageID then
  65.         resend(myEvent[3])
  66.    
  67.     -- Message arriving via a modem:
  68.     elseif myEvent[1] == "modem_message" then
  69.         if not myEvent[5].nSender then myEvent[5].nSender = myEvent[4] end
  70.         resend(myEvent[5])
  71.        
  72.     -- Unmark a given message previously logged as repeated:
  73.     elseif myEvent[1] == "timer" and timerID[tostring(myEvent[2])] then
  74.         msgID[timerID[tostring(myEvent[2])]] = nil
  75.         timerID[tostring(myEvent[2])] = nil
  76.    
  77.     -- Quit:
  78.     elseif myEvent[1] == "key" and (myEvent[2] == keys.q or myEvent[2] == keys.x) then
  79.         os.pullEvent("char")
  80.         error()
  81.    
  82.     end
  83. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement