xXm0dzXx

Untitled

Dec 2nd, 2012
401
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. local tOpen = { left = false, right = false, top = false, bottom = false, front = false, back = false }
  2. local tValid = { left = true, right = true, top = true, bottom = true, front = true, back = true }
  3.  
  4. function open( sSide )
  5. if type( sSide ) ~= "string" then
  6. error( "string expected" )
  7. end
  8. if not tValid[sSide] then
  9. error( "Invalid side" )
  10. end
  11. tOpen[ sSide ] = true
  12. end
  13.  
  14. function close( sSide )
  15. if type( sSide ) ~= "string" then
  16. error( "string expected" )
  17. end
  18. if not tValid[sSide] then
  19. error( "Invalid side" )
  20. end
  21. tOpen[ sSide ] = false
  22. end
  23.  
  24. function send( sID, sMessage )
  25. local hasSent = false
  26. for i,v in pairs( tOpen ) do
  27. if v == true then
  28. local sCable = peripheral.wrap( i )
  29. sCable.send( sID, sMessage )
  30. hasSent = v
  31. end
  32. end
  33. return hasSent
  34. end
  35.  
  36. function broadcast( sMessage )
  37. return send( -1, sMessage )
  38. end
  39.  
  40. function announce()
  41. return broadcast( "" )
  42. end
  43.  
  44. function receive( sTimeOut )
  45. local timer = nil
  46. local sEvent = "cable_message"
  47. if nTimeout then
  48. timer = os.startTimer( nTimeout )
  49. sEvent = nil
  50. end
  51. while true do
  52. local e, p1, p2, p3 = os.pullEvent( sEvent )
  53. if e == "cable_message" then
  54. return p1, p2, p3
  55. elseif e == "timer" and p1 == timer then
  56. return nil, nil, nil
  57. end
  58. end
  59. end
Advertisement
Add Comment
Please, Sign In to add comment