Guest User

Untitled

a guest
Aug 3rd, 2012
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.20 KB | None | 0 0
  1. -- BY INFINIKILLER64 (abc on computercraft.info)
  2. -- feel free to use with credit
  3. -- you can use the open,close,and convert functions without credit though
  4.  
  5. local sides={"right","left","front","back","top","bottom"}
  6. function open()
  7.     local l1
  8.     for l1=1,6 do
  9.         rednet.open(sides[l1])
  10.         if rednet.isOpen(sides[l1])==true then return true,sides[l1] end
  11.     end
  12.     return false
  13. end
  14. function close()
  15.     local l1
  16.     for l1=1,6 do
  17.         local first=rednet.isOpen(sides[l1])
  18.         rednet.close(sides[l1])
  19.         if first ~= rednet.isOpen(sides[l1]) then return true,sides[l1] end
  20.     end
  21.     return false
  22. end
  23. function send( ... )
  24.     local tArgs={ ... }
  25.     if #tArgs>3 or #tArgs<2 then
  26.         error("USAGE: snet.send(id,data[,channel])")
  27.     end
  28.     tArgs[3]=tArgs[3] or ""
  29.     if tArgs[1]==-1 then tArgs[1]=nil end
  30.     return rednet.send(tArgs[1],tostring(tArgs[3]:len()).."."..tArgs[3].."."..tArgs[2])
  31. end
  32. function broadcast( ... )
  33.     local tArgs={ ... }
  34.     if #tArgs>2 or #tArgs<1 then
  35.         error("USAGE: snet.broadcast(data[,channel])")
  36.     end
  37.     return snet.send(-1,tArgs[1],tArgs[2])
  38. end
  39. local function convert(p2)
  40.     local data=string.find(p2,"%.")
  41.     if data == nil then return end
  42.     local data2=tonumber(string.sub(p2,1,data-1))
  43.     if data == nil then return end
  44.     local data3=string.sub(p2,data+1,data2+data)
  45.     if string.sub(p2,data2+data+1,data2+data+1)~="." then return end
  46.     return string.sub(p2,data2+data+2),data3
  47. end
  48. function receive( ... )
  49.     local tArgs={ ... }
  50.     local erro=false
  51.     local eArgs={}
  52.     if type(tArgs[1]) == "number" then
  53.         eArgs[1]=tArgs[1]
  54.     elseif type(tArgs[1]) == "string" then
  55.         eArgs[2]=tArgs[1]
  56.     else
  57.         erro=true
  58.     end
  59.     if type(tArgs[1]) == "string" then
  60.         eArgs[2]=tArgs[1]
  61.     else
  62.         erro=true
  63.     end
  64.     if #tArgs==0 then erro=false end
  65.     if erro then
  66.         error("USAGE: id,data=snet.receive([timeout][,channel])")
  67.     end
  68.     if eArgs[1]~=nil then
  69.         local timer = os.startTimer(eArgs[1])
  70.     end
  71.     local data, tye, p1, p2, p3
  72.     while true do
  73.         while true do
  74.             tye, p1, p2, p3 = os.pullEvent()
  75.             if tye == "rednet_message" or ( tye == "timer" and p1 == timer ) then
  76.                 break
  77.             end
  78.         end
  79.         if typeo == "timer" then return nil,nil end
  80.         data,tag=convert(p2)
  81.         if data and tag==(eArgs[2] or "") then break end
  82.     end
  83.     return p1,data
  84. end
Advertisement
Add Comment
Please, Sign In to add comment