Advertisement
Terrah

Untitled

Sep 1st, 2014
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. PORT_SEND = PORT_SEND or 777;
  2. PORT_RECV = 778 + os.getComputerID();
  3.  
  4. write( "Send port " .. PORT_SEND .. "\n" )
  5. write( "Recv port " .. PORT_RECV .. "\n" )
  6.  
  7. MODEM = nil;
  8. REACTOR = nil;
  9.  
  10. local periList = peripheral.getNames();
  11.  
  12. for i = 1, #periList do
  13.  
  14. if peripheral.getType(periList[i]) == "modem" then
  15.  
  16. local modem = peripheral.wrap(periList[i]);
  17.  
  18. if modem.isWireless() then
  19.  
  20. if modem.isOpen(PORT_SEND) then
  21. modem.close(PORT_SEND);
  22. end
  23.  
  24. MODEM = modem;
  25. end
  26.  
  27. elseif peripheral.getType(periList[i]) == "BigReactors-Reactor" then
  28.  
  29. REACTOR = peripheral.wrap(periList[i]);
  30.  
  31. end
  32. end
  33.  
  34.  
  35. if MODEM then
  36. MODEM.open(PORT_SEND);
  37. else
  38. print("Terrah: No modem found!");
  39. return;
  40. end
  41.  
  42. function Call(func,param)
  43.  
  44. if MODEM.isOpen(PORT_SEND) then
  45. MODEM.close(PORT_SEND);
  46. end
  47.  
  48. if param == nil then
  49. param = "$";
  50. end
  51.  
  52. if not MODEM.isOpen(PORT_RECV) then
  53. MODEM.open(PORT_RECV);
  54. end
  55.  
  56. MODEM.transmit( PORT_SEND, PORT_RECV, func .. "|" .. tostring(param) );
  57. local event, modemSide, senderChannel,replyChannel, message, senderDistance = os.pullEvent("modem_message")
  58.  
  59. --only respond to your channel
  60. while senderChannel ~= PORT_RECV do
  61. event, modemSide, senderChannel,replyChannel, message, senderDistance = os.pullEvent("modem_message")
  62. end
  63.  
  64. if message == "$" then
  65. message = nil;
  66. end
  67.  
  68. return message;
  69. end
  70.  
  71. function Event( event, modemSide, senderChannel, replyChannel, message, senderDistance )
  72.  
  73. if PORT_SEND ~= senderChannel then
  74. return;
  75. end
  76.  
  77. local pos = message:find("|", 0, true );
  78.  
  79. local xfunc = message:sub( 0, pos-1 );
  80. local param = message:sub( 1+pos );
  81.  
  82. if param == "$" then
  83. param = nil;
  84. end
  85.  
  86. local func = REACTOR[xfunc];
  87.  
  88. local ret = nil;
  89.  
  90. write( tostring(replyChannel) .. " - " .. xfunc .. "("..tostring(param) .. ") = " );
  91.  
  92. if type(func) == "function" then
  93.  
  94. if param == nil then
  95. ret = func();
  96. elseif xfunc == "setActive" then
  97. ret = func( param:lower() == "true" );
  98. elseif xfunc == "getControlRodLevel" or xfunc == "getControlRodName" then
  99.  
  100. param = tonumber(param);
  101.  
  102. if param < REACTOR.getNumberOfControlRods() and param >= 0 then
  103. ret = func(tonumber(param));
  104. end
  105.  
  106. else
  107. ret = func(tonumber(param));
  108. end
  109. end
  110.  
  111. write( tostring( ret ) .. "\n" );
  112.  
  113. if MODEM.isOpen(replyChannel) then
  114. MODEM.close(replyChannel);
  115. end
  116.  
  117. if ret == nil then
  118. ret = "$";
  119. end
  120.  
  121. --return to sender
  122. MODEM.transmit( replyChannel, senderChannel, tostring(ret) );
  123. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement