Advertisement
birdini

Router Script

Mar 6th, 2014
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.66 KB | None | 0 0
  1. keys = {2,120,34,87,45,89,67,16,123}
  2. root=262144
  3. channelStart = 46
  4.  
  5. modem = peripheral.wrap("right")
  6. if not modem.isOpen(root) then modem.open(root) end
  7. for j=channelStart,channelStart+125 do modem.open(j) end
  8.  
  9.  
  10. function headerPull(str)
  11. local tble={}
  12. for i=1,#str do
  13. local char=string.sub(str,i,i)
  14. if char == "$" then return tonumber(table.concat(tble)) end
  15. table.insert(tble,char)
  16. end
  17. end
  18.  
  19. function headerDel(str)
  20. local tble={}
  21. for k=1,#str do
  22. if string.sub(str,k,k) == "$" then loc=(k+1) break end
  23. end
  24. for i=loc,#str do
  25. local char=string.sub(str,i,i)
  26. table.insert(tble,char)
  27. msg=table.concat(tble)
  28. end
  29. return msg
  30. end
  31.  
  32. function headerAdd(str,dest)
  33. local msg=tostring(dest.."$"..str)
  34. return msg
  35. end
  36.  
  37. function keyPull(str)
  38. local tble={}
  39. for k=1,#str do
  40. if string.sub(str,k,k) == "_" then loc=(k+1) break end
  41. end
  42. for i=loc,#str do
  43. char=string.sub(str,i,i)
  44. table.insert(tble,char)
  45. end
  46. return tonumber(table.concat(tble))
  47. end
  48.  
  49. function keyAdd(str,key)
  50. local msg=tostring(str.."_"..key)
  51. return msg
  52. end
  53.  
  54. function keyDel(str)
  55. local tble={}
  56. for i=1,#str do
  57. local char=string.sub(str,i,i)
  58. if char == "_" then return table.concat(tble) end
  59. table.insert(tble,char)
  60. end
  61. end
  62.  
  63. function checkKey(key)
  64. for i=1,#keys do
  65. if key == keys[i] then return true end
  66. end
  67. return false
  68. end
  69.  
  70. function route(event,side,freq,rplyFreq,msg,dist)
  71. if not checkKey(keyPull(msg)) then return false end
  72. local dest = headerPull(msg)
  73. local key = keyPull(msg)
  74. local str = keyDel(headerDel(msg))
  75. modem.transmit(dest,freq,str)
  76. print("Routed message to channel: "..dest.." From channel: "..freq)
  77. end
  78.  
  79. while true do
  80. route(os.pullEvent("modem_message")
  81. sleep(0.01)
  82. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement