Advertisement
jille_Jr

CC: Nuclear Generator Controller

Sep 13th, 2013
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.29 KB | None | 0 0
  1.  
  2. --[[--
  3.    
  4.     Wireless modem: back
  5.     Wired modem: front
  6.    
  7.     Relay id: 131
  8.     Monitor name: monitor_1
  9.     MFE name: batbox_1
  10.     Self name: computer_4
  11.    
  12.     Controller nickname: controller
  13.     Relay nickname: relay
  14.    
  15. --]]--
  16.  
  17. --(( Variables ))--
  18.  
  19. local running = true
  20.  
  21. local controllerid = os.getComputerID()
  22. local controllername = "controller"
  23. local relayid = -1
  24. local relayname = "relay"
  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 wireless = "back" -- side
  39. local cable = "front" -- side
  40. local channel = 61234
  41.  
  42. local refreshrate = .2 -- seconds
  43.  
  44. local modem = peripheral.wrap(wireless) or error("Unable to wrap wireless modem!",0)
  45. if not modem.isWireless() then error("Wireless modem is required! ("..wireless.." side)",0) end
  46.  
  47. local wired = peripheral.wrap(cable) or error("Unable to wrap wired modem!",0)
  48. if wired.isWireless() then error("Wired modem is required! ("..cable.." side)",0) end
  49.  
  50. -- nuke
  51. local nukestate = false -- false=off, true=on
  52. local nukeoutput = 0 -- EU/t
  53.  
  54. -- mfe
  55. local mfepowerstored = 0 -- EU
  56. local mfepowermax = 0 -- EU
  57. local mfeoutput = 0 -- EU/t
  58. local mfemaxoutput = 0 -- EU
  59. local mfeside = "batbox_1"
  60. local mfe = peripheral.wrap(mfeside) or error("Unable to wrap storage device!",0)
  61.  
  62. -- monitor
  63. local monitorside = "monitor_1"
  64. local monitor = peripheral.wrap(monitorside) or error("Unable to wrap monitor display!",0)
  65. local monitorw,monitorh = monitor.getSize()
  66.  
  67. -- computer
  68. local w,h = term.getSize()
  69.  
  70. --(( Functions ))--
  71.  
  72. local function setColor(fg,bg,obj)
  73.     local obj = obj or term
  74.     if obj.isColor then
  75.         if obj.isColor() then
  76.             if type(fg) == "number" then
  77.                 obj.setTextColor(fg)
  78.             end if type(bg) == "number" then
  79.                 obj.setBackground(bg)
  80.             end
  81.         end
  82.     end
  83. end
  84.  
  85. local function sendMsg(from,to,subject,info)
  86.     info = info or ""
  87.     local message = {
  88.         from = from,
  89.         to = to,
  90.         subject = subject,
  91.         info = info,
  92.     }
  93.     message = textutils.serialize(message)
  94.     modem.transmit(channel,channel,message)
  95. end
  96.  
  97. local function init()
  98.     modem.open(channel)
  99.    
  100.     local timeout = 10 -- seconds
  101.     local timeouttimer = os.startTimer(timeout)
  102.     local refreshtimer = os.startTimer(refreshrate)
  103.     local refreshcount = 0
  104.    
  105.     local x,y = term.getCursorPos()
  106.     local relayknows = false
  107.    
  108.     repeat
  109.         local ev,p1,p2,p3,p4,p5 = os.pullEvent()
  110.        
  111.         -- Loading message
  112.         term.setCursorPos(x,y)
  113.         setColor(colors.yellow)
  114.         term.write("Initializing ")
  115.         setColor(colors.orange)
  116.         term.write(loading[(refreshcount%#loading)+1])
  117.        
  118.         -- Event handling
  119.         if ev == "modem_message" then
  120.             -- Message handling
  121.             local msg = textutils.unserialize(p4)
  122.             if type(msg) == "table" then
  123.                 if msg.from == relayname
  124.                 and msg.to == controllername then
  125.                     if msg.subject == subs.getid then
  126.                         -- Reply with id
  127.                         sendMsg(controllername,relayname,subs.gotid,controllerid)
  128.                     elseif msg.subject == subs.gotid then
  129.                         if relayid == -1 then
  130.                             -- Save id (into variable)
  131.                             if type(msg.info) == "number" then
  132.                                 relayid = msg.info
  133.                                 -- Reply with acknowledge confirm
  134.                                 sendMsg(controllername,relayname,subs.gotidack)
  135.                             end
  136.                         end
  137.                     elseif msg.subject == subs.gotidack then
  138.                         -- Relay got my id
  139.                         relayknows = true
  140.                     end
  141.                 end
  142.             end
  143.         elseif ev == "timer" then
  144.             -- Timer handling
  145.             local timer = p1
  146.             if timer == refreshtimer then
  147.                 -- Loading count
  148.                 refreshtimer = os.startTimer(refreshrate)
  149.                 refreshcount = refreshcount + 1
  150.                 -- Ask for id
  151.                 if relayid == -1 then
  152.                     sendMsg(controllername,relayname,subs.getid)
  153.                 end
  154.             elseif timer == timeouttimer then
  155.                 -- Timeout
  156.                 term.setCursorPos(x,y)
  157.                 setColor(colors.yellow)
  158.                 term.write("Initializing ")
  159.                 setColor(colors.orange)
  160.                 term.write("X")
  161.                 print()
  162.                 setColor(colors.red)
  163.                 error("Operation timed out.",0)
  164.             end
  165.         end
  166.     until relayid ~= -1 and relayknows
  167.    
  168.     term.setCursorPos(x,y)
  169.     setColor(colors.yellow)
  170.     term.write("Initializing ")
  171.     setColor(colors.orange)
  172.     term.write("X")
  173.     setColor(colors.lime)
  174.     term.write(" Done!")
  175.     print()
  176. end
  177.  
  178. local function setNuke(state)
  179.     sendMsg(controllername,relayname,subs.setnuke,state)
  180. end
  181. local function getNukeOutput()
  182.     sendMsg(controllername,relayname,subs.getoutput)
  183. end
  184.  
  185. local function updateMFE()
  186.     -- output
  187.     mfeoutput = mfe.getOutput()
  188.     mfemaxoutput = mfe.getMaxEnergyOutput()
  189.    
  190.     -- storage
  191.     mfepowerstored = mfe.getStored()
  192.     mfepowermax = mfe.getCapacity()
  193. end
  194.  
  195. local function drawMonitor()
  196.     -- for debugging, raw info
  197.     local info = {
  198.         mfeoutput = tostring(mfeoutput) .. "/" .. tostring(mfemaxoutput),
  199.         mfepower = tostring(mfepowerstored) .. "/" .. tostring(mfepowermax),
  200.         nukeoutput = tostring(nukeoutput),
  201.     }
  202.     if nukestate then
  203.         info.nukestate = "ON"
  204.     else
  205.         info.nukestate = "OFF"
  206.     end
  207.    
  208.     local count = 0
  209.     for index,value in pairs(info) do
  210.         monitor.setCursorPos(2,1+count)
  211.         setColor(colors.orange,_,monitor)
  212.         monitor.write(index)
  213.         setColor(colors.red,_,monitor)
  214.         monitor.write(" = ")
  215.         setColor(colors.yellow,_,monitor)
  216.         monitor.write(tostring(value))
  217.         count = count + 1
  218.     end
  219. end
  220.  
  221. --(( Main program ))--
  222.  
  223. init()
  224. print("Initiating complete!")
  225.  
  226. local refreshtimer = os.startTimer(refreshrate)
  227. local refreshcount = 0
  228.  
  229. while running do
  230.     local ev,p1,p2,p3,p4,p5 = os.pullEvent()
  231.    
  232.     if ev == "modem_message" then
  233.         -- Message handling
  234.         local msg = textutils.unserialize(p4)
  235.         if type(msg) == "table" then
  236.             if msg.to == controllername then
  237.                 if msg.from == relayname then
  238.                     -- Receive nuke state
  239.                     if msg.subject == subs.gotnukestate then
  240.                         if type(msg.info) == "boolean" then
  241.                             nukestate = msg.info
  242.                         end
  243.                     end
  244.                     -- Receive nuke output
  245.                     if msg.subject == subs.gotoutput then
  246.                         if type(msg.info) == "number" then
  247.                             nukeoutput = msg.info
  248.                         end
  249.                     end
  250.                 end
  251.             end
  252.         end
  253.     elseif ev == "timer" then
  254.         if p1 == refreshtimer then
  255.             refreshtimer = os.startTimer(refreshrate)
  256.             refreshcount = refreshcount + 1
  257.             setNuke(nukestate)
  258.             getNukeOutput()
  259.             updateMFE()
  260.             drawMonitor()
  261.         end
  262.     end
  263. end
  264.  
  265. --(( EOF ))--
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement