-
Notifications
You must be signed in to change notification settings - Fork 194
Expand file tree
/
Copy pathrepeat.lua
More file actions
91 lines (80 loc) · 3.12 KB
/
repeat.lua
File metadata and controls
91 lines (80 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
-- Find modems
local tModems = {}
for n,sModem in ipairs( peripheral.getNames() ) do
if peripheral.getType( sModem ) == "modem" then
table.insert( tModems, sModem )
end
end
if #tModems == 0 then
print( "No modems found." )
return
elseif #tModems == 1 then
print( "1 modem found." )
else
print( #tModems .. " modems found." )
end
function open( nChannel )
for n=1,#tModems do
local sModem = tModems[n]
peripheral.call( sModem, "open", nChannel )
end
end
function close( nChannel )
for n=1,#tModems do
local sModem = tModems[n]
peripheral.call( sModem, "close", nChannel )
end
end
-- Open channels
print( "0 messages repeated." )
open( rednet.CHANNEL_REPEAT )
-- Main loop (terminate to break)
local ok, error = pcall( function()
local tReceivedMessages = {}
local tReceivedMessageTimeouts = {}
local nTransmittedMessages = 0
while true do
local sEvent, sModem, nChannel, nReplyChannel, tMessage = os.pullEvent()
if sEvent == "modem_message" then
-- Got a modem message, rebroadcast it if it's a rednet thing
if nChannel == rednet.CHANNEL_REPEAT then
if type( tMessage ) == "table" and tMessage.nMessageID and tMessage.nRecipient and type(tMessage.nRecipient) == "number" then
if not tReceivedMessages[ tMessage.nMessageID ] then
-- Ensure we only repeat a message once
tReceivedMessages[ tMessage.nMessageID ] = true
tReceivedMessageTimeouts[ os.startTimer( 30 ) ] = tMessage.nMessageID
-- Send on all other open modems, to the target and to other repeaters
for n=1,#tModems do
local sOtherModem = tModems[n]
peripheral.call( sOtherModem, "transmit", rednet.CHANNEL_REPEAT, nReplyChannel, tMessage )
peripheral.call( sOtherModem, "transmit", tMessage.nRecipient, nReplyChannel, tMessage )
end
-- Log the event
nTransmittedMessages = nTransmittedMessages + 1
local x,y = term.getCursorPos()
term.setCursorPos( 1, y - 1 )
term.clearLine()
if nTransmittedMessages == 1 then
print( nTransmittedMessages .. " message repeated." )
else
print( nTransmittedMessages .. " messages repeated." )
end
end
end
end
elseif sEvent == "timer" then
-- Got a timer event, use it to clear the message history
local nTimer = sModem
local nMessageID = tReceivedMessageTimeouts[ nTimer ]
if nMessageID then
tReceivedMessageTimeouts[ nTimer ] = nil
tReceivedMessages[ nMessageID ] = nil
end
end
end
end )
if not ok then
printError( error )
end
-- Close channels
close( rednet.CHANNEL_REPEAT )