Advertisement
Guest User

finalctrl

a guest
Apr 18th, 2015
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.08 KB | None | 0 0
  1. local args = {...};
  2.  
  3. local BRControl = { protocol = "BRControl" }
  4.  
  5. local screen = "monitor_2"
  6. local reactor = "BigReactors-Reactor_3"
  7.  
  8. function BRControl:new(o)
  9.     o = o or {}; o.info = {};
  10.     setmetatable(o,self);
  11.     self.__index = self;
  12.     return o:init();
  13. end
  14.  
  15. function BRControl:init() return self; end
  16.  
  17. function BRControl:run()
  18.     while true do
  19.         self:tick();
  20.         self:show();
  21.         self:wait();
  22.     end
  23. end
  24.  
  25. function BRControl:ratio()
  26.     return math.min( 100, 100*math.pow((self.info.energy_p+25)/100, 3) );
  27. end
  28.  
  29.  
  30. local BRServer = BRControl:new{monitors={}};
  31. function BRServer:init()
  32.     local monitors = self.monitors or {};
  33.   rednet.open("back")
  34.    self.reactor = peripheral.wrap(reactor);
  35.    monitors[#monitors+1] = peripheral.wrap(screen)
  36.    
  37.     self.monitors = monitors;
  38.     return self;
  39. end
  40.  
  41. function BRServer:tick()
  42.     self.info = {
  43.         active = self.reactor.getActive(),
  44.  
  45.         temp   = self.reactor.getCasingTemperature(),
  46.         waste  = self.reactor.getWasteAmount(),
  47.  
  48.         fuel      = self.reactor.getFuelAmount(),
  49.         fuel_max  = self.reactor.getFuelAmountMax(),
  50.         fuel_reac = self.reactor.getFuelReactivity(),
  51.         fuel_temp = self.reactor.getFuelTemperature(),
  52.         fuel_tick = self.reactor.getFuelConsumedLastTick(),
  53.   cool_p    = 100 / self.reactor.getCoolantAmountMax() * self.reactor.getCoolantAmount(),
  54.   steam_p   = 100 / self.reactor.getHotFluidAmountMax() * self.reactor.getHotFluidAmount(),
  55.   cool_max  = self.reactor.getCoolantAmountMax(),
  56.   cool_am   = self.reactor.getCoolantAmount(),
  57.  
  58.         energy      = self.reactor.getEnergyStored(),
  59.         energy_p    = self.reactor.getEnergyStored()/100000,
  60.         energy_tick = self.reactor.getEnergyProducedLastTick()
  61.     };
  62.     self.info.ratio = self:ratio();
  63.     self.reactor.setAllControlRodLevels(self:ratio());
  64.     rednet.broadcast(textutils.serialize(self.info),self.protocole);
  65. end
  66.  
  67. function BRServer:show()
  68.     local kiby = function ( i ,s )
  69.         local k = 0;
  70.         while ( i >= 1000 ) do
  71.             k = k+1; i = i/1000;
  72.         end
  73.         local suf = {[0] = "", [1] = "k", [2] = "M", [3] = "G", [4] = "T", [5] = "E"};
  74.         if ( k>5 ) then
  75.             suf[k] = "{"..k.."}";
  76.         end
  77.         return string.format("%i %s%s", i, suf[k], s );
  78.     end
  79.  
  80.     self.turn = self.turn ~= nil and self.turn + 1 or 1;
  81.     term.current().setBackgroundColor( colors.gray );
  82.     term.current().clear();
  83.  
  84.     local b,a = term.current().getSize();
  85.     local p = {[0]="-",[1]="/",[2]="|",[3]="\\"};
  86.     UI.rectangle( term.current(), 1,1,b,1, self.info.active and colors.blue or colors.orange );
  87.     UI.print.center( term.current(), 1, "BigReactor Server", colors.white );
  88.     UI.print.right( term.current(), 1, p[math.fmod(self.turn,4)], colors.white );
  89.  
  90.     for i,monitor in pairs(self.monitors) do
  91.         local w,h = monitor.getSize();
  92.         monitor.setBackgroundColor(colors.black);
  93.         monitor.clear();
  94.  
  95.         UI.rectangle( monitor, 1,1,w,1, self.info.active and colors.blue or colors.orange );
  96. if self.info.active then
  97.   UI.print.center(monitor, 1, "Reactor active", colors.white);
  98.   else
  99.   UI.print.center(monitor, 1, "Reactor inactive", colors.red);
  100. end
  101. --      UI.print.center( monitor, 1, "Reactor Panel", colors.white );
  102.  
  103.         UI.progress_bar( monitor, w-5, 3, 1, h-3, self.info.cool_p, colors.gray, colors.orange );
  104.   UI.progress_bar( monitor, w-3, 3, 1, h-3, self.info.steam_p, colors.combine(colors.red, colors.orange), colors.gray);
  105.   UI.progress_bar( monitor, w-1, 3, 1, h-3, 100-self.info.ratio, colors.blue, colors.gray);
  106.  
  107.         monitor.setBackgroundColor( colors.black );
  108.         local align = w-8;
  109.  
  110.         UI.print.title( monitor, 3, 2, w-6, colors.gray, "ENERGY", colors.white );
  111.         monitor.setBackgroundColor( colors.black );
  112.  
  113.   UI.print.left( monitor, 5, "Coolant:", colors.lightGray, 1);
  114.         UI.print.left( monitor, 5, self.info.cool_am, colors.lightBlue,10 );
  115.         UI.print.left( monitor, 5, "/", colors.lightGray, 18);
  116.         UI.print.left( monitor, 5, self.info.cool_max, colors.lightBlue, 20);
  117.   UI.print.left( monitor, 5, "mB", colors.lightGray, 28);
  118.  
  119.   UI.print.left(monitor, 12, self.info.fuel, colors.lime, 1);
  120.   UI.print.left(monitor, 12, "/", colors.lightGray, 10);
  121.   UI.print.left(monitor, 12, self.info.fuel_max, colors.lime, 12);
  122.   UI.print.left(monitor, 12, "(       )",colors.lightGray, 23);
  123.   UI.print.left(monitor, 12, self.info.waste, colors.orange, 25);
  124.  
  125.  
  126.         UI.print.title( monitor, 8, 2, w-6, colors.gray, "FUEL", colors.white );
  127.         monitor.setBackgroundColor( colors.black );
  128.  
  129.         UI.bar( monitor, 2, 10, w-8, 1, colors.combine(colors.red, colors.yellow), {
  130.             {["p"] = self.info.waste, ["color"] = colors.cyan },
  131.             {["p"] = self.info.fuel,  ["color"] = colors.lime    },
  132.         }, self.info.fuel_max );
  133.  
  134.         UI.print.title( monitor, 14, 2, w-6, colors.gray, "INFO", colors.white );
  135.         monitor.setBackgroundColor( colors.black );
  136.  
  137.   UI.print.right (monitor, 6, "Control Rod Level: ", colors.lightGray, align -12);
  138.   UI.print.left(monitor, 6 , string.format("%i %",self.info.ratio),colors.orange, 21);
  139.   UI.print.left(monitor,6, "%", colors.lightGray, 24);
  140.  
  141.         UI.print.right( monitor, 16, "Case:", colors.lightGray, align +2 );
  142.         UI.print.left( monitor, 16, string.format("%i C",self.info.temp), colors.orange, 7 );
  143.  
  144.         UI.print.right( monitor, 16, "Core:", colors.lightGray, align - 13 );
  145.         UI.print.left( monitor, 16, string.format("%i C",self.info.fuel_temp), colors.red, 22 );
  146.  
  147.  
  148.         UI.print.right( monitor, 17, "Burn:", colors.lightGray, align +2 );
  149.         UI.print.left( monitor, 17, string.format("%i ml/t",math.floor(self.info.fuel_tick*1000)), colors.orange, 7 );
  150.  
  151.         UI.print.right( monitor, 17, "E:", colors.lightGray, align -13 );
  152.         UI.print.left( monitor, 17, string.format("%i rf/ml",self.info.energy_tick/math.floor(self.info.fuel_tick*1000)), colors.orange, 22 );
  153.  
  154.  
  155.         UI.print.right( monitor, 18, "Reac:", colors.lightGray, align +2 );
  156.         UI.print.left( monitor, 18, string.format("%i",self.info.fuel_reac), colors.yellow, 7 );
  157.   UI.print.left( monitor, 18, "%", colors.lightGray, 10 );
  158.     end
  159. end
  160.  
  161. function BRServer:wait()
  162.     os.sleep(1);
  163. end
  164.  
  165. local BRClient = BRControl:new{ inrange = false };
  166. function BRControl:init()
  167.     return self;
  168. end
  169.  
  170. function BRClient:tick() end
  171.  
  172. function BRClient:show()
  173.  
  174. end
  175.  
  176. function BRClient:wait()
  177.     local s, m, p;
  178.     if ( self.inrange ) then
  179.         s, m, p = rednet.receive( self.protocol, 5 );
  180.     else
  181.         s, m, p = rednet.receive( self.protocol );
  182.     end
  183.     if ( m ~= nil ) then
  184.         self.inrange = true;
  185.         self:update( m );
  186.     else
  187.         self.inrange = false;
  188.     end
  189. end
  190.  
  191. function BRClient:update( m )
  192.     m = textutils.unserialize(m);
  193.  
  194.     self.info.active = m.active;
  195.  
  196.     self.info.temp   = m.temp;
  197.     self.info.ratio  = m.ratio;
  198.     self.info.waste  = m.waste;
  199.  
  200.     self.info.fuel      = m.fuel;
  201.     self.info.fuel_max  = m.fuel_max;
  202.     self.info.fuel_reac = m.fuel_reac;
  203.     self.info.fuel_temp = m.fuel_temp;
  204.     self.info.fuel_tick = m.fuel_tick;
  205.  
  206.  
  207.     self.info.energy      = m.energy;
  208.     self.info.energy_tick = m.energy_tick;
  209. end
  210.  
  211. UI = { print = {} };
  212. function UI.print.rich( wd, x, y, text, color )
  213.     local a, b = wd.getCursorPos();
  214.     wd.setTextColor( color );
  215.     wd.setCursorPos( x, y );
  216.     wd.write(text);
  217.     wd.setCursorPos( a, b );
  218. end
  219.  
  220. function UI.print.left( wd, line, text, color, offset )
  221.     UI.print.rich( wd, 1 + (offset or 0), line, text, color );
  222. end
  223.  
  224. function UI.print.center( wd, line, text, color, offset )
  225.     local w,h = wd.getSize();
  226.     UI.print.rich( wd, 1+(offset or 0) + w/2 - string.len(text)/2, line, text, color );
  227. end
  228.  
  229. function UI.print.right( wd, line, text, color, offset )
  230.     local w,h = wd.getSize();
  231.     UI.print.rich( wd,  1-(offset or 0) + w - string.len(text), line, text, color );
  232. end
  233.  
  234. function UI.print.title( wd, line, y1, y2, c1, title, c2)
  235.     local t = UI.rectangle( wd, y1, line, y2-y1, 1, c1 );
  236.     UI.print.center( t, 1, title, c2 );
  237. end
  238.  
  239. function UI.rectangle( wd, x, y, w, h, color )
  240.     if type( wd ) ~= "table" or
  241.     type( x ) ~= "number" or
  242.     type( y ) ~= "number" or
  243.     type( w ) ~= "number" or
  244.     type( h ) ~= "number" then
  245.         error( "Expected object, number, number, number, number, [boolean]", 2 )
  246.     end
  247.     local r = window.create( wd, x, y, w, h );
  248.     r.setBackgroundColor( color ); r.clear();
  249.     return r;
  250. end
  251.  
  252. function UI.bar( wd, x, y, w, h, c1, table, m )
  253.     local bar = UI.rectangle( wd, x, y, w, h, c1 );
  254.     local max, i = m or 100, ( h < w ) and 1 or 0;
  255.     for k, pi in pairs( table ) do
  256.         local p = pi.p / max;
  257.         if ( h < w ) then
  258.             UI.rectangle( bar, i, 1, p*w, h, pi.color );
  259.             i = i + p*w;
  260.         else
  261.             i = i + p*h;
  262.             UI.rectangle( bar, 1, 2+h-i, w, math.ceil(p*h), pi.color );
  263.         end
  264.     end
  265. end
  266.  
  267. function UI.progress_bar( wd, x, y, w, h, p, c1, c2 )
  268.     UI.bar( wd, x, y, w, h, c1, {{ ["p"] = p+5, ["color"] = c2 }});
  269. end
  270.  
  271. function UI.modal( wd, x, y, w, h, title )
  272.     if ( type(title) == "table" ) then
  273.         local head = window.create( wd, x, y, w, 3 );
  274.         head.setBackgroundColor( title.background );
  275.         head.clear(); head.setCursorPos(2,1);
  276.         UI.print.center( head, title.text, title.color );
  277.     end
  278.     return window.create(wd, x+3, y, w, h );
  279. end
  280.  
  281.  
  282. -- Main []
  283. local reactor = BRServer:new();
  284. for i,arg in pairs(args) do
  285.     if ( arg == "--client" ) then reactor = BRClient:new(); break; end;
  286. end
  287. for i,arg in pairs(args) do
  288.     for protocol in arg:gmatch( "-p:(.*)" ) do
  289.         reactor.protocole = protocol; break;
  290.     end
  291. end;
  292. reactor:run();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement