SHARE
TWEET

repeater

tmoflash Jun 3rd, 2016 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --Repeater
  2.  
  3. function messageisnew()
  4. for ticn = 1, ticmemcounter do
  5.   if timeandid == ticmem[ticn]
  6.   then
  7.    return false
  8.   end
  9. end
  10. return true
  11. end
  12.  
  13. function addtoticmem(newti)
  14. ticmemcounter = ticmemcounter + 1
  15. ticmem[ticmemcounter] = newti
  16. end
  17.  
  18. function resetticmem()
  19. ticmemcounter = 0
  20. ticmem = {}
  21. os.setAlarm(0)
  22. end
  23.  
  24.  
  25. function skynetrepeater()
  26. resetticmem()
  27. while true do
  28.   event, id, message = os.pullEvent()
  29.   if event == "rednet_message"
  30.   then
  31.    if string.sub(message,1,8) == "[skynet]"
  32.    then
  33.         timeandid = string.sub(message,9,21) -- [TTTTTTIIIII]
  34.         if messageisnew()
  35.         then
  36.          addtoticmem(timeandid)
  37.          rednet.broadcast(message)
  38.         end
  39.    end
  40.   elseif event == "alarm"
  41.   then
  42.    resetticmem()
  43.   end
  44. end
  45. end
  46.  
  47.  
  48. --Sender
  49.  
  50. function getticode()
  51. time = os.time()
  52. for n=1, 6 do
  53.   if string.len(time) < n
  54.   then time = time.."0"
  55.   end
  56. end
  57.  
  58. id = os.getComputerID()
  59. for n=1, 5 do
  60.   if string.len(id) < n
  61.   then id = "0"..id
  62.   end
  63. end
  64. timeandidcode = ("["..time..id.."]")
  65. return timeandidcode
  66. end
  67.  
  68. function gettargetid(targetidf)
  69. targetidf = tostring(targetidf)
  70. for tidlenc=1, 4 do
  71.   if string.len(targetidf) == tidlenc
  72.   then
  73.    targetidf = ("0"..targetidf)
  74.   end
  75. end
  76. targetidf = "["..targetidf.."]"
  77. return targetidf
  78. end
  79.  
  80. function skynetsendmessage(message, targetid)
  81. if message and targetid then
  82.   rednet.broadcast("[skynet]"..getticode()..gettargetid(targetid)..message) --[skynet][TTTTTTIIIII][IIIII]
  83. end
  84. end
  85.  
  86.  
  87. --Receiver
  88.  
  89. function istargetid()
  90. targetid = tonumber(string.sub(skynettags,23,27))
  91. if targetid == os.getComputerID()
  92. then
  93.   return true
  94. else
  95.   return false
  96. end
  97. end
  98.  
  99. function skynetreceivemessage()
  100. while true do
  101.   event, id, message = os.pullEvent()
  102.   if event == "rednet_message"
  103.   then
  104.    skynettags = string.sub(message,1,28)
  105.    if string.sub(skynettags,1,8) == "[skynet]" then
  106.         if istargetid() then
  107.          message = string.sub(message,29)
  108.          return message
  109.         end
  110.    end
  111.   end
  112. end
  113. end
  114.  
  115. function skyneteventmessage()
  116. while true do
  117.   event, id, message = os.pullEvent()
  118.   if event == "rednet_message"
  119.   then
  120.    skynettags = string.sub(message,1,28)
  121.    if string.sub(skynettags,1,8) == "[skynet]" then
  122.         if istargetid() then
  123.          message = string.sub(message,29)
  124.          return "skynet", message
  125.         end
  126.    end
  127.   else
  128.    return event, id, message
  129.   end
  130. end
  131. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top