2020-02-13 05:34:17 +00:00
|
|
|
-- brednet - better rednet
|
|
|
|
-- if you're going to be using rednet, at least use this
|
|
|
|
|
|
|
|
local brednet = {}
|
|
|
|
|
2020-02-13 05:36:44 +00:00
|
|
|
if ccemux and not peripheral.find("modem") then
|
|
|
|
ccemux.attach("top", "wireless_modem")
|
|
|
|
end
|
2020-02-13 05:34:17 +00:00
|
|
|
|
2020-02-18 02:33:35 +00:00
|
|
|
-- stores the last 'maxUsed' amount of received messages
|
|
|
|
local maxUsed = 1000
|
2020-02-13 05:34:17 +00:00
|
|
|
local USED = {}
|
|
|
|
|
2020-02-18 02:33:35 +00:00
|
|
|
local computerID = os.getComputerID()
|
|
|
|
|
|
|
|
local cycleIntoUsed = function(msgID)
|
|
|
|
USED[msgID] = maxUsed
|
2020-02-13 05:34:17 +00:00
|
|
|
for k,v in pairs(USED) do
|
|
|
|
if v == 0 then
|
|
|
|
USED[k] = nil
|
|
|
|
else
|
|
|
|
USED[k] = -1 + v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
brednet.open = function(CHANNEL, MODEMS)
|
|
|
|
local session = {
|
|
|
|
channel = CHANNEL or 65530,
|
|
|
|
modems = MODEMS or {},
|
|
|
|
}
|
|
|
|
|
|
|
|
-- make virtual modem that is, in fact, every modem in the list
|
|
|
|
session.modem = {}
|
|
|
|
local modem = session.modem
|
|
|
|
for k,v in next, {"open", "close", "transmit"} do
|
|
|
|
local list = (#session.modems ~= 0) and sessions.modems or {peripheral.find("modem")}
|
|
|
|
modem[v] = function(...)
|
|
|
|
for i = 1, #list do
|
|
|
|
list[i][v](...)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-18 02:33:35 +00:00
|
|
|
session.send = function(message, recipient, channel)
|
2020-02-13 05:34:17 +00:00
|
|
|
local msgID = math.random(1, 2^31-1)
|
|
|
|
cycleIntoUsed(msgID)
|
2020-02-18 02:33:35 +00:00
|
|
|
if channel then
|
|
|
|
modem.open(channel)
|
|
|
|
end
|
2020-02-13 05:34:17 +00:00
|
|
|
modem.transmit(
|
|
|
|
channel or session.channel,
|
|
|
|
channel or session.channel,
|
|
|
|
{
|
|
|
|
msg = message,
|
2020-02-18 02:33:35 +00:00
|
|
|
msgID = msgID,
|
|
|
|
id = computerID,
|
|
|
|
recipient = recipient,
|
2020-02-13 05:34:17 +00:00
|
|
|
time = os.time()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2020-02-18 02:33:35 +00:00
|
|
|
session.check = function(input)
|
2020-02-13 05:34:17 +00:00
|
|
|
-- check types
|
|
|
|
if type(input) == "table" then
|
2020-02-18 02:33:35 +00:00
|
|
|
if type(input.id) == "number" and type(input.msgID) == "number" and input.msg then
|
|
|
|
if not USED[input.msgID] then
|
|
|
|
return input
|
2020-02-13 05:34:17 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-18 02:33:35 +00:00
|
|
|
session.receive = function(senderID, channel)
|
|
|
|
local evt, output
|
|
|
|
if channel then
|
|
|
|
modem.open(channel)
|
|
|
|
end
|
|
|
|
-- keep receiving and repeating all messages, regardless of recipient
|
|
|
|
while true do
|
|
|
|
-- only return if you ARE the recipient
|
|
|
|
output = nil
|
|
|
|
while not output do
|
|
|
|
evt = {os.pullEvent("modem_message")}
|
|
|
|
output = session.check(evt[5])
|
|
|
|
end
|
|
|
|
cycleIntoUsed(output.msgID)
|
|
|
|
modem.transmit(
|
|
|
|
channel or session.channel,
|
|
|
|
channel or session.channel,
|
|
|
|
{
|
|
|
|
msg = output.msg,
|
|
|
|
msgID = output.msgID,
|
|
|
|
id = output.id,
|
|
|
|
recipient = output.recipient,
|
|
|
|
time = output.time,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
if (
|
|
|
|
((not output.recipient) or (output.recipient == computerID)) and
|
|
|
|
((not senderID) or (output.id == senderID))
|
|
|
|
) then
|
|
|
|
return output.msg, output.id
|
|
|
|
end
|
2020-02-13 05:34:17 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
modem.open(session.channel)
|
|
|
|
|
|
|
|
return session
|
|
|
|
end
|
|
|
|
|
|
|
|
return brednet
|