Advertisement
jille_Jr

CC: Nuclear Generator Relay

Sep 7th, 2013
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.90 KB | None | 0 0
  1.  
  2.  
  3. --[[--
  4. message form = {
  5.     from = sender name,
  6.     to = reciver name,
  7.     subject = operation,
  8.     info = additional information,
  9. }
  10. --]]--
  11.  
  12. --(( Variables ))--
  13.  
  14. local running = true
  15.  
  16. local controllerid = -1
  17. local controllername = "controller"
  18. local relayid = os.getComputerID()
  19. local relayname = "relay"
  20.  
  21. local wireless = "bottom" -- side
  22. local channel = 61234
  23.  
  24. local refreshrate = .2 -- seconds
  25.  
  26. local loading = {"|","/","-","\\"}
  27. local subs = {
  28.     getid = "get id",
  29.     gotid = "got id",
  30.     gotidack = "got id, acknowledge",
  31.     setnuke = "set nuke state",
  32.     getoutput = "get nuke output",
  33.     gotoutput = "got nuke output",
  34.     getnukestate = "get nuke state",
  35.     gotnukestate = "got nuke state",
  36. }
  37.  
  38. local modem = peripheral.wrap(wireless) or error("Unable to wrap wireless modem!",0)
  39. if not modem.isWireless() then error("Wireless modem is required! ("..wireless.." side)",0) end
  40.  
  41. -- computer
  42. local w,h = term.getSize()
  43.  
  44. -- nuke
  45. local nukeside = "top"
  46. local nuke = peripheral.wrap(nukeside) or error("Unable to wrap nuclear reactor!",0)
  47. local nukestate = false
  48. local nukeoutput = 0 -- EU/t
  49.  
  50.  
  51. --(( Functions ))--
  52.  
  53. local function setColor(fg,bg)
  54.     if term.isColor then
  55.         if term.isColor() then
  56.             if type(fg) == "number" then
  57.                 term.setTextColor(fg)
  58.             end if type(bg) == "number" then
  59.                 term.setBackground(bg)
  60.             end
  61.         end
  62.     end
  63. end
  64.  
  65. local function sendMsg(from,to,subject,info)
  66.     info = info or ""
  67.     local message = {
  68.         from = from,
  69.         to = to,
  70.         subject = subject,
  71.         info = info,
  72.     }
  73.     message = textutils.serialize(message)
  74.     modem.transmit(channel,channel,message)
  75. end
  76.  
  77. local function init()
  78.     modem.open(channel)
  79.    
  80.     local timeout = 10 -- seconds
  81.     local timeouttimer = os.startTimer(timeout)
  82.     local refreshtimer = os.startTimer(refreshrate)
  83.     local refreshcount = 0
  84.    
  85.     local x,y = term.getCursorPos()
  86.     local controllerknows = false
  87.    
  88.     repeat
  89.         local ev,p1,p2,p3,p4,p5 = os.pullEvent()
  90.        
  91.         -- Loading message
  92.         term.setCursorPos(x,y)
  93.         setColor(colors.yellow)
  94.         term.write("Initializing ")
  95.         setColor(colors.orange)
  96.         term.write(loading[(refreshcount%#loading)+1])
  97.        
  98.         -- Event handling
  99.         if ev == "modem_message" then
  100.             -- Message handling
  101.             local msg = textutils.unserialize(p4)
  102.             if type(msg) == "table" then
  103.                 if msg.from == controllername
  104.                 and msg.to == relayname then
  105.                     if msg.subject == subs.getid then
  106.                         -- Reply with id
  107.                         sendMsg(relayname,controllername,subs.gotid,relayid)
  108.                     elseif msg.subject == subs.gotid then
  109.                         if controllerid == -1 then
  110.                             -- Save id (into variable)
  111.                             if type(msg.info) == "number" then
  112.                                 controllerid = msg.info
  113.                                 -- Reply with acknowledge confirm
  114.                                 sendMsg(relayname,controllername,subs.gotidack)
  115.                             end
  116.                         end
  117.                     elseif msg.subject == subs.gotidack then
  118.                         -- Controller got my id
  119.                         controllerknows = true
  120.                     end
  121.                 end
  122.             end
  123.         elseif ev == "timer" then
  124.             -- Timer handling
  125.             local timer = p1
  126.             if timer == refreshtimer then
  127.                 -- Loading count
  128.                 refreshtimer = os.startTimer(refreshrate)
  129.                 refreshcount = refreshcount + 1
  130.                 -- Ask for id
  131.                 if controllerid == -1 then
  132.                     sendMsg(relayname,controllername,subs.getid)
  133.                 end
  134.             elseif timer == timeouttimer then
  135.                 -- Timeout
  136.                 term.setCursorPos(x,y)
  137.                 setColor(colors.yellow)
  138.                 term.write("Initializing ")
  139.                 setColor(colors.orange)
  140.                 term.write("X")
  141.                 print()
  142.                 setColor(colors.red)
  143.                 error("Operation timed out.",0)
  144.             end
  145.         end
  146.     until controllerid ~= -1 and controllerknows
  147.    
  148.     term.setCursorPos(x,y)
  149.     setColor(colors.yellow)
  150.     term.write("Initializing ")
  151.     setColor(colors.orange)
  152.     term.write("X")
  153.     setColor(colors.lime)
  154.     term.write(" Done!")
  155.     print()
  156. end
  157.  
  158. local function updateNuke()
  159.     -- Nuke on/off
  160.     rs.setOutput(nukeside,nukestate)
  161.    
  162.     -- Nuke output
  163.     nukeoutput = nuke.getOutput()
  164. end
  165.  
  166. --(( Main program ))--
  167.  
  168. init()
  169. setColor(colors.yellow)
  170. print("Initiating complete!")
  171.  
  172. local refreshtimer = os.startTimer(refreshrate)
  173. local refreshcount = 0
  174.  
  175. while running do
  176.     local ev,p1,p2,p3,p4,p5 = os.pullEvent()
  177.    
  178.     if ev == "modem_message" then
  179.         -- Message handling
  180.         local msg = textutils.unserialize(p4)
  181.         if type(msg) == "table" then
  182.             if msg.to == relayname then
  183.                 if msg.from == controllername then
  184.                     -- Get nuke state
  185.                     if msg.subject == subs.getnukestate then
  186.                         sendMsg(relayname,controllername,subs.gotnukestate,nukestate)
  187.                     end
  188.                     -- Set nuke state
  189.                     if msg.subject == subs.setnuke then
  190.                         if type(msg.info) == "boolean" then
  191.                             nukestate = msg.info
  192.                         end
  193.                     end
  194.                     -- Get nuke output
  195.                     if msg.subject == subs.getoutput then
  196.                         sendMsg(relayname,controllername,subs.gotoutput,nukeoutput)
  197.                     end
  198.                 end
  199.             end
  200.         end
  201.     elseif ev == "timer" then
  202.         if p1 == refreshtimer then
  203.             refreshtimer = os.startTimer(refreshrate)
  204.             refreshcount = refreshcount + 1
  205.             updateNuke()
  206.         end
  207.     end
  208. end
  209. --(( EOF ))--
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement