Advertisement
TheOddByte

[ComputerCraft][API] rNet

Sep 28th, 2013
1,771
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.02 KB | None | 0 0
  1. --[[
  2.     [API] rNet
  3.     @version 1.0, 02/05/2014, TOB
  4.     @author TheOddByte, TOB
  5. --]]
  6.  
  7.  
  8.  
  9.  
  10.  
  11. --[[
  12.     @description    "Opens all modems available"
  13.    
  14.     @return         nil
  15. --]]
  16. function openSides()
  17.     local bool = false
  18.     for _, side in pairs(rs.getSides()) do
  19.         if peripheral.getType(side) == "modem" then
  20.             rednet.open( side )
  21.         bool = true
  22.     end
  23.     end
  24.     return bool
  25. end
  26.  
  27.  
  28. --[[
  29.     @description    "Closes all modems available"
  30.    
  31.     @return         nil
  32. --]]
  33. function closeSides()
  34.     for _,side in pairs(rs.getSides()) do
  35.         if peripheral.getType(side) == "modem" then
  36.             rednet.close( side )
  37.     end
  38.     end
  39. end
  40.  
  41.  
  42. --[[
  43.     @description    "Checks all modems available"
  44.    
  45.     @return         table
  46. --]]
  47. function checkOpenSides()
  48.     local tOpenSides = {}
  49.     for _,side in pairs(rs.getSides()) do
  50.         if peripheral.getType(side) == "modem" then
  51.             if rednet.isOpen(side) then
  52.             table.insert(tOpenSides,side)
  53.         end
  54.     end
  55.     end
  56.     return tOpenSides
  57. end
  58.  
  59.  
  60. --[[
  61.     @description    "Receive messages from only allowed IDs"
  62.    
  63.     @param          tIDs,    table
  64.     @param       timeout,   number
  65.    
  66.     @return         table, boolean
  67. --]]
  68. function receive( tIDs, timeout )
  69.     local tData = {}
  70.     local response = false
  71.     for i = 1,#tIDs do
  72.         local rID, msg = rednet.receive( timeout )
  73.         if rID == tIDs[i] then
  74.             nMsg = {}
  75.             nMsg.id = rID
  76.             nMsg.msg = msg
  77.             table.insert(tData,nMsg)
  78.             response = true
  79.         end
  80.     end
  81.     return tData, response
  82. end
  83.  
  84.  
  85. --[[
  86.     @description    "Sending messages to multiple computers"
  87.    
  88.     @param          msg,    string
  89.     @param         tIDs,    table
  90.    
  91.     @return         nil
  92. --]]
  93. function send(msg,tIDs)
  94.     for i = 1,#tIDs do
  95.         rednet.send(tIDs[i],msg)
  96.     end
  97. end
  98.  
  99.  
  100. --[[
  101.     @description    "Sends multiple messages to one computer"
  102.    
  103.     @param          messages,    table
  104.     @param                id,    number
  105.    
  106.     @return         nil
  107. --]]
  108. function sendMessages(messages,id)
  109.     for i = 1,#messages do
  110.         rednet.send(id,messages[i])
  111.     end
  112. end
  113.  
  114.  
  115. --[[
  116.     @description    "Sends a table over rednet"
  117.    
  118.     @param          id,    number
  119.     @param           t,    table
  120. --]]
  121. function sendTable( id, t )
  122.     rednet.send( id, textutils.serialize(t) )
  123. end
  124.  
  125. --[[
  126.     @description    "Receives a table over rednet"
  127.    
  128.     @return         table
  129. --]]
  130. function receiveTable( timeout )
  131.     local id, msg = rednet.receive( timeout )
  132.     msg = textutils.unserialize( msg )
  133.     return id, msg
  134. end
  135.  
  136.  
  137. --[[
  138.     @description    "Waits for one message"
  139.    
  140.     @param          tIDs,    table
  141.    
  142.     @return         id, string
  143. --]]
  144. function waitForMessage(tIDs)
  145.     evt, rID, msg = os.pullEvent("rednet_message")
  146.     for i = 1,#tIDs do
  147.         if rID == tIDs[i] then
  148.             return rID, msg
  149.         end
  150.     end
  151. end
  152.  
  153.  
  154. --[[
  155.     @description    "Waiting until all messages are received from all computers"
  156.    
  157.     @param          tIDs,    table
  158.     @return         table
  159. --]]
  160. function waitForAllMessages(tIDs)
  161.     local tData = {}
  162.     while true do
  163.         evt, rID, msg = os.pullEvent("rednet_message")
  164.         for i = 1,#tIDs do
  165.             if rID == tIDs[i] then
  166.                 local add = true
  167.    
  168.                 for k = 1,#tData do
  169.                 if rID == tData[k].id then
  170.                     add = false
  171.                 end
  172.             end
  173.    
  174.             if add then
  175.                 local nData = {}
  176.                 nData.id = rID
  177.                 nData.msg = msg
  178.                 table.insert(tData,nData)
  179.                end
  180.        end
  181.        if #tData == #tIDs then return tData end
  182.        end
  183.    end
  184. end
  185.  
  186.  
  187. --[[
  188.     @description    "Waits for a specific message and returns the ID"
  189.    
  190.     @param          msg,    string
  191.     @return         id
  192. --]]
  193. function getID(msg)
  194.     local id, message
  195.     repeat
  196.         id, message = rednet.receive()
  197.     until message == msg
  198.     return id
  199. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement