Jyzarc

Untitled

Dec 25th, 2013
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.13 KB | None | 0 0
  1. local message,id,messageID
  2. local i,l,_,side,channel
  3. local command,id,
  4. local modems = {}
  5. local sides = {}
  6. local channels = {}
  7. local data = {}
  8.  
  9. local function serialize(command,side,amount)
  10.     data[1] = command
  11.     data[2] = side
  12.     data[3] = amount
  13.     return textutils.serialize(data)
  14. end
  15.  
  16. --This function adds the modem to the list of modems the computer will send messages to
  17. function addModem(side)
  18.     if peripheral.getType(side) == "modem" then
  19.         sides[i] = side
  20.         modems[#modems+1] = peripheral.wrap(side)
  21.         return true
  22.     else
  23.         return false
  24.     end
  25. end
  26.  
  27. --This function removes the modem from the list of modems this computer will send messages to
  28. function removeModem(side)
  29.     for i = 1,#modems do
  30.         if sides[i] == side then
  31.             modems[i] = nil
  32.             sides[i] = nil
  33.             for i = 1,(#modems - i) do
  34.                 if modems[i+1] ~= nil then
  35.                     modems[i] = modems[i+1]
  36.                     sides[i] = sides[i+1]
  37.                 end
  38.             end
  39.         end
  40.     end
  41. end
  42.  
  43. --This function adds the channel to the list of channels this sends stuff to
  44. function addChannel(channel)
  45.     for i = 1,#modems do
  46.         if not modems[i].isOpen() then
  47.             modems[i].open(channel)
  48.         end
  49.     end
  50. end
  51.  
  52. --This function removes the  channel from the list of channels this sends stuff to
  53. function removeChannel(channel)
  54.     for i = 1,#modems do
  55.         if modems[i].isOpen(channel) then
  56.             modems[i].close(channel)
  57.         end
  58.     end
  59. end
  60.  
  61. local function sendMessage()
  62.     if #modems == 0 then
  63.         return false
  64.     else
  65.         for i = 1,#modems do
  66.             for l = 1,#channels do
  67.                 modems[i].transmit(channels[l],os.getComputerID(),message)
  68.             end
  69.         end
  70.         return true
  71.     end
  72. end
  73.  
  74. function getInput(id,side)
  75.     message = serialize("getInput",side,nil)
  76.     sendMessage()
  77.     _,_,_,messageID,message = os.pullEvent("modem_message")
  78.     data = textutils.unserialize()
  79.     if messageID == id then
  80.         if message then
  81.             return true
  82.         else
  83.             return false
  84.         end
  85.     end
  86. end
  87.  
  88. function getOutput(id,side)
  89.     message = serialize("getOutput",side,nil)
  90.     sendMessage()
  91.     _,_,_,messageID,message = os.pullEvent("modem_message")
  92.     data = textutils.unserialize()
  93.     if messageID == id then
  94.         if message then
  95.             return true
  96.         else
  97.             return false
  98.         end
  99.     end
  100. end
Advertisement
Add Comment
Please, Sign In to add comment