Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- math.randomseed(os.time())
- local currentFrequency = 0
- local chars = {
- "a",
- "b",
- "c",
- "d",
- "e",
- "f",
- "g",
- "h",
- "i",
- "j",
- "k",
- "l",
- "m",
- "n",
- "o",
- "p",
- "q",
- "r",
- "s",
- "t",
- "u",
- "v",
- "w",
- "x",
- "y",
- "z",
- " ",
- "_",
- "#",
- "@",
- "!",
- "*",
- "^",
- "1",
- "2",
- "3",
- "4",
- "5",
- "6",
- "7",
- "8",
- "9",
- "0",
- "+",
- "-",
- "~",
- "`",
- "%",
- "$",
- "{",
- "}",
- "\\",
- "/",
- ">",
- "<",
- ";",
- ":",
- }
- function run()
- while true do
- local id, msg, distance = rednet.receive()
- msg = textutils.unserialize(msg)
- if type(msg) == "table" then
- local freq = msg[1]
- local data = msg[2]
- local offset = math.abs(currentFrequency-freq)
- --print("Got a message from "..id..", frequency "..freq.." data "..data.." freqOffset "..offset.." our freq is "..currentFrequency)
- if offset < 10 then
- local doScramble = true
- if distance <= 100 then
- if offset == 1 then
- if math.random(1,10) < 5 then
- doScramble = false
- end
- elseif offset == 2 then
- if math.random(1,100) < 25 then
- doScramble = false
- end
- end
- end
- local scrambleAmt = 0
- if doScramble then
- scrambleAmt = math.ceil(#data * (offset/10))
- end
- if distance > 100 then
- scrambleAmt = scrambleAmt + math.ceil(#data * (distance-100 / 100))
- end
- if scrambleAmt < #data then
- --print("Scrambling "..scrambleAmt.." characters")
- local scrambledPositions = {}
- for i=1, #data do
- scrambledPositions[i] = string.sub(data,i, i)
- end
- if doScramble then
- for i=1, scrambleAmt do
- local scramblePos = math.random(1, #data)
- if scrambledPositions[scramblePos] == string.sub(data, scramblePos, scramblePos) then
- if math.random(1,2) == 2 then
- scrambledPositions[scramblePos] = string.upper(chars[math.random(1, #chars)])
- else
- scrambledPositions[scramblePos] = chars[math.random(1, #chars)]
- end
- end
- end
- end
- local newData = ""
- for i=1, #scrambledPositions do
- newData = newData..scrambledPositions[i]
- end
- --print("Received radio transmission from "..id.." data: "..newData)
- os.queueEvent("radio_received", id, newData, distance, freq)
- end
- end
- end
- end
- end
- function receive(timeout)
- local timer = -1
- if timeout then
- timer = os.startTimer(timeout)
- end
- while true do
- local event, p1, p2, p3, p4 = os.pullEvent()
- if event == "timer" and p1 == timer then
- return
- elseif event == "radio_received" then
- return p1, p2, p3, p4
- end
- end
- end
- function transmit(message)
- rednet.broadcast(textutils.serialize({currentFrequency, message}))
- end
- function setFrequency(newFreq)
- currentFrequency = newFreq
- end
- function getFrequency()
- return currentFrequency
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement