Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -----------------------------------------
- -- BitNet Rednet Repeater --
- -----------------------------------------
- -- By Jeffrey Alexander, aka Bomb Bloke.
- -- Acts as repeater software for BitNet transmitters:
- -- https://github.com/theoriginalbit/MoarPeripherals/wiki/BitNet-Mini-Antenna
- -- https://github.com/theoriginalbit/MoarPeripherals/wiki/BitNet-Communication-Tower
- -- Pretty much the same thing as the default "repeat" script:
- -- http://www.computercraft.info/wiki/Repeat
- -- ... though it supports BitNet devices as well. Can work with any combination
- -- of modems/towers or alongside the vanilla repeat script itself.
- ---------------------------------------------
- local modem = {peripheral.find("modem")}
- for i = 1, #modem do modem[i].open(rednet.CHANNEL_REPEAT) end
- local antenna = {peripheral.find("bitnet_antenna")}
- local tower = {peripheral.find("bitnet_tower", function(name, object) return object.isTowerComplete() end)}
- if #modem == 0 then error("No modems found - can't send / receive rednet messages without one!") end
- local repeated, msgID, timerID, myEvent = 0, {}, {}
- print(#modem.." modem"..(#modem==1 and "" or "s").." found.")
- print(#antenna.." antenna"..(#antenna==1 and "" or "s").." found.")
- print(#tower.." tower"..(#tower==1 and "" or "s").." found.")
- print("0 messages repeated.")
- -- Repeat a message:
- local function resend(message)
- -- Determine if the message has already been repeated by this system:
- if msgID[tostring(message.nMessageID)] then return end
- -- Flag the message as having passed through this system:
- msgID[tostring(message.nMessageID)] = true
- timerID[tostring(os.startTimer(30))] = tostring(message.nMessageID)
- -- Update the onscreen repeat counter:
- local curX, curY = term.getCursorPos()
- term.setCursorPos(1,curY-1)
- repeated = repeated + 1
- print(repeated.." messages repeated.")
- -- Send a copy via all attached BitNet devices:
- for i = 1, #antenna do antenna[i].transmit(message) end
- for i = 1, #tower do tower[i].transmit(message) end
- -- Send a copy via all attached modems:
- for i = 1, #modem do
- modem[i].transmit(message.nRecipient, message.nSender, message)
- modem[i].transmit(rednet.CHANNEL_REPEAT, message.nSender, message)
- end
- end
- -- Main loop:
- while true do
- myEvent = {os.pullEvent()}
- -- Message arriving via tower:
- if myEvent[1] == "bitnet_message" and type(myEvent[3]) == "table" and myEvent[3].nMessageID then
- resend(myEvent[3])
- -- Message arriving via a modem:
- elseif myEvent[1] == "modem_message" then
- if not myEvent[5].nSender then myEvent[5].nSender = myEvent[4] end
- resend(myEvent[5])
- -- Unmark a given message previously logged as repeated:
- elseif myEvent[1] == "timer" and timerID[tostring(myEvent[2])] then
- msgID[timerID[tostring(myEvent[2])]] = nil
- timerID[tostring(myEvent[2])] = nil
- -- Quit:
- elseif myEvent[1] == "key" and (myEvent[2] == keys.q or myEvent[2] == keys.x) then
- os.pullEvent("char")
- error()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement