Advertisement
killer64

Untitled

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