Advertisement
Tatantyler

Rednet Radio

Dec 2nd, 2012
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.84 KB | None | 0 0
  1. math.randomseed(os.time())
  2. local currentFrequency = 0
  3.  
  4. local chars = {
  5.     "a",
  6.     "b",
  7.     "c",
  8.     "d",
  9.     "e",
  10.     "f",
  11.     "g",
  12.     "h",
  13.     "i",
  14.     "j",
  15.     "k",
  16.     "l",
  17.     "m",
  18.     "n",
  19.     "o",
  20.     "p",
  21.     "q",
  22.     "r",
  23.     "s",
  24.     "t",
  25.     "u",
  26.     "v",
  27.     "w",
  28.     "x",
  29.     "y",
  30.     "z",
  31.     " ",
  32.     "_",
  33.     "#",
  34.     "@",
  35.     "!",
  36.     "*",
  37.     "^",
  38.     "1",
  39.     "2",
  40.     "3",
  41.     "4",
  42.     "5",
  43.     "6",
  44.     "7",
  45.     "8",
  46.     "9",
  47.     "0",
  48.     "+",
  49.     "-",
  50.     "~",
  51.     "`",
  52.     "%",
  53.     "$",
  54.     "{",
  55.     "}",
  56.     "\\",
  57.     "/",
  58.     ">",
  59.     "<",
  60.     ";",
  61.     ":",
  62. }
  63.  
  64.  
  65. function run()
  66.     while true do
  67.         local id, msg, distance = rednet.receive()
  68.         msg = textutils.unserialize(msg)
  69.         if type(msg) == "table" then
  70.             local freq = msg[1]
  71.             local data = msg[2]
  72.             local offset = math.abs(currentFrequency-freq)
  73.             --print("Got a message from "..id..", frequency "..freq.." data "..data.." freqOffset "..offset.." our freq is "..currentFrequency)
  74.             if offset < 10 then
  75.                 local doScramble = true
  76.                 if distance <= 100 then
  77.                     if offset == 1 then
  78.                         if math.random(1,10) < 5 then
  79.                             doScramble = false
  80.                         end
  81.                     elseif offset == 2 then
  82.                         if math.random(1,100) < 25 then
  83.                             doScramble = false
  84.                         end
  85.                     end
  86.                 end
  87.                 local scrambleAmt = 0
  88.                 if doScramble then
  89.                     scrambleAmt = math.ceil(#data * (offset/10))
  90.                 end
  91.                 if distance > 100 then
  92.                     scrambleAmt = scrambleAmt + math.ceil(#data * (distance-100 / 100))
  93.                 end
  94.                 if scrambleAmt < #data then
  95.                     --print("Scrambling "..scrambleAmt.." characters")
  96.                     local scrambledPositions = {}
  97.                     for i=1, #data do
  98.                         scrambledPositions[i] = string.sub(data,i, i)
  99.                     end
  100.                     if doScramble then
  101.                         for i=1, scrambleAmt do
  102.                             local scramblePos = math.random(1, #data)
  103.                             if scrambledPositions[scramblePos] == string.sub(data, scramblePos, scramblePos) then
  104.                                 if math.random(1,2) == 2 then
  105.                                     scrambledPositions[scramblePos] = string.upper(chars[math.random(1, #chars)])
  106.                                 else
  107.                                     scrambledPositions[scramblePos] = chars[math.random(1, #chars)]
  108.                                 end
  109.                             end
  110.                         end
  111.                     end
  112.                     local newData = ""
  113.                     for i=1, #scrambledPositions do
  114.                         newData = newData..scrambledPositions[i]
  115.                     end
  116.                     --print("Received radio transmission from "..id.." data: "..newData)
  117.                     os.queueEvent("radio_received", id, newData, distance, freq)
  118.                 end
  119.             end
  120.         end
  121.     end
  122. end
  123.  
  124. function receive(timeout)
  125.     local timer = -1
  126.     if timeout then
  127.         timer = os.startTimer(timeout)
  128.     end
  129.     while true do
  130.         local event, p1, p2, p3, p4 = os.pullEvent()
  131.         if event == "timer" and p1 == timer then
  132.             return
  133.         elseif event == "radio_received" then
  134.             return p1, p2, p3, p4
  135.         end
  136.     end
  137. end
  138.  
  139. function transmit(message)
  140.     rednet.broadcast(textutils.serialize({currentFrequency, message}))
  141. end
  142.  
  143. function setFrequency(newFreq)
  144.     currentFrequency = newFreq
  145. end
  146.  
  147. function getFrequency()
  148.     return currentFrequency
  149. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement