Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local args = {...};
- local BRControl = { protocol = "BRControl" }
- local screen = "monitor_2"
- local reactor = "BigReactors-Reactor_3"
- function BRControl:new(o)
- o = o or {}; o.info = {};
- setmetatable(o,self);
- self.__index = self;
- return o:init();
- end
- function BRControl:init() return self; end
- function BRControl:run()
- while true do
- self:tick();
- self:show();
- self:wait();
- end
- end
- function BRControl:ratio()
- return math.min( 100, 100*math.pow((self.info.energy_p+25)/100, 3) );
- end
- local BRServer = BRControl:new{monitors={}};
- function BRServer:init()
- local monitors = self.monitors or {};
- rednet.open("back")
- self.reactor = peripheral.wrap(reactor);
- monitors[#monitors+1] = peripheral.wrap(screen)
- self.monitors = monitors;
- return self;
- end
- function BRServer:tick()
- self.info = {
- active = self.reactor.getActive(),
- temp = self.reactor.getCasingTemperature(),
- waste = self.reactor.getWasteAmount(),
- fuel = self.reactor.getFuelAmount(),
- fuel_max = self.reactor.getFuelAmountMax(),
- fuel_reac = self.reactor.getFuelReactivity(),
- fuel_temp = self.reactor.getFuelTemperature(),
- fuel_tick = self.reactor.getFuelConsumedLastTick(),
- cool_p = 100 / self.reactor.getCoolantAmountMax() * self.reactor.getCoolantAmount(),
- steam_p = 100 / self.reactor.getHotFluidAmountMax() * self.reactor.getHotFluidAmount(),
- cool_max = self.reactor.getCoolantAmountMax(),
- cool_am = self.reactor.getCoolantAmount(),
- energy = self.reactor.getEnergyStored(),
- energy_p = self.reactor.getEnergyStored()/100000,
- energy_tick = self.reactor.getEnergyProducedLastTick()
- };
- self.info.ratio = self:ratio();
- self.reactor.setAllControlRodLevels(self:ratio());
- rednet.broadcast(textutils.serialize(self.info),self.protocole);
- end
- function BRServer:show()
- local kiby = function ( i ,s )
- local k = 0;
- while ( i >= 1000 ) do
- k = k+1; i = i/1000;
- end
- local suf = {[0] = "", [1] = "k", [2] = "M", [3] = "G", [4] = "T", [5] = "E"};
- if ( k>5 ) then
- suf[k] = "{"..k.."}";
- end
- return string.format("%i %s%s", i, suf[k], s );
- end
- self.turn = self.turn ~= nil and self.turn + 1 or 1;
- term.current().setBackgroundColor( colors.gray );
- term.current().clear();
- local b,a = term.current().getSize();
- local p = {[0]="-",[1]="/",[2]="|",[3]="\\"};
- UI.rectangle( term.current(), 1,1,b,1, self.info.active and colors.blue or colors.orange );
- UI.print.center( term.current(), 1, "BigReactor Server", colors.white );
- UI.print.right( term.current(), 1, p[math.fmod(self.turn,4)], colors.white );
- for i,monitor in pairs(self.monitors) do
- local w,h = monitor.getSize();
- monitor.setBackgroundColor(colors.black);
- monitor.clear();
- UI.rectangle( monitor, 1,1,w,1, self.info.active and colors.blue or colors.orange );
- if self.info.active then
- UI.print.center(monitor, 1, "Reactor active", colors.white);
- else
- UI.print.center(monitor, 1, "Reactor inactive", colors.red);
- end
- -- UI.print.center( monitor, 1, "Reactor Panel", colors.white );
- UI.progress_bar( monitor, w-5, 3, 1, h-3, self.info.cool_p, colors.gray, colors.orange );
- UI.progress_bar( monitor, w-3, 3, 1, h-3, self.info.steam_p, colors.combine(colors.red, colors.orange), colors.gray);
- UI.progress_bar( monitor, w-1, 3, 1, h-3, 100-self.info.ratio, colors.blue, colors.gray);
- monitor.setBackgroundColor( colors.black );
- local align = w-8;
- UI.print.title( monitor, 3, 2, w-6, colors.gray, "ENERGY", colors.white );
- monitor.setBackgroundColor( colors.black );
- UI.print.left( monitor, 5, "Coolant:", colors.lightGray, 1);
- UI.print.left( monitor, 5, self.info.cool_am, colors.lightBlue,10 );
- UI.print.left( monitor, 5, "/", colors.lightGray, 18);
- UI.print.left( monitor, 5, self.info.cool_max, colors.lightBlue, 20);
- UI.print.left( monitor, 5, "mB", colors.lightGray, 28);
- UI.print.left(monitor, 12, self.info.fuel, colors.lime, 1);
- UI.print.left(monitor, 12, "/", colors.lightGray, 10);
- UI.print.left(monitor, 12, self.info.fuel_max, colors.lime, 12);
- UI.print.left(monitor, 12, "( )",colors.lightGray, 23);
- UI.print.left(monitor, 12, self.info.waste, colors.orange, 25);
- UI.print.title( monitor, 8, 2, w-6, colors.gray, "FUEL", colors.white );
- monitor.setBackgroundColor( colors.black );
- UI.bar( monitor, 2, 10, w-8, 1, colors.combine(colors.red, colors.yellow), {
- {["p"] = self.info.waste, ["color"] = colors.cyan },
- {["p"] = self.info.fuel, ["color"] = colors.lime },
- }, self.info.fuel_max );
- UI.print.title( monitor, 14, 2, w-6, colors.gray, "INFO", colors.white );
- monitor.setBackgroundColor( colors.black );
- UI.print.right (monitor, 6, "Control Rod Level: ", colors.lightGray, align -12);
- UI.print.left(monitor, 6 , string.format("%i %",self.info.ratio),colors.orange, 21);
- UI.print.left(monitor,6, "%", colors.lightGray, 24);
- UI.print.right( monitor, 16, "Case:", colors.lightGray, align +2 );
- UI.print.left( monitor, 16, string.format("%i C",self.info.temp), colors.orange, 7 );
- UI.print.right( monitor, 16, "Core:", colors.lightGray, align - 13 );
- UI.print.left( monitor, 16, string.format("%i C",self.info.fuel_temp), colors.red, 22 );
- UI.print.right( monitor, 17, "Burn:", colors.lightGray, align +2 );
- UI.print.left( monitor, 17, string.format("%i ml/t",math.floor(self.info.fuel_tick*1000)), colors.orange, 7 );
- UI.print.right( monitor, 17, "E:", colors.lightGray, align -13 );
- UI.print.left( monitor, 17, string.format("%i rf/ml",self.info.energy_tick/math.floor(self.info.fuel_tick*1000)), colors.orange, 22 );
- UI.print.right( monitor, 18, "Reac:", colors.lightGray, align +2 );
- UI.print.left( monitor, 18, string.format("%i",self.info.fuel_reac), colors.yellow, 7 );
- UI.print.left( monitor, 18, "%", colors.lightGray, 10 );
- end
- end
- function BRServer:wait()
- os.sleep(1);
- end
- local BRClient = BRControl:new{ inrange = false };
- function BRControl:init()
- return self;
- end
- function BRClient:tick() end
- function BRClient:show()
- end
- function BRClient:wait()
- local s, m, p;
- if ( self.inrange ) then
- s, m, p = rednet.receive( self.protocol, 5 );
- else
- s, m, p = rednet.receive( self.protocol );
- end
- if ( m ~= nil ) then
- self.inrange = true;
- self:update( m );
- else
- self.inrange = false;
- end
- end
- function BRClient:update( m )
- m = textutils.unserialize(m);
- self.info.active = m.active;
- self.info.temp = m.temp;
- self.info.ratio = m.ratio;
- self.info.waste = m.waste;
- self.info.fuel = m.fuel;
- self.info.fuel_max = m.fuel_max;
- self.info.fuel_reac = m.fuel_reac;
- self.info.fuel_temp = m.fuel_temp;
- self.info.fuel_tick = m.fuel_tick;
- self.info.energy = m.energy;
- self.info.energy_tick = m.energy_tick;
- end
- UI = { print = {} };
- function UI.print.rich( wd, x, y, text, color )
- local a, b = wd.getCursorPos();
- wd.setTextColor( color );
- wd.setCursorPos( x, y );
- wd.write(text);
- wd.setCursorPos( a, b );
- end
- function UI.print.left( wd, line, text, color, offset )
- UI.print.rich( wd, 1 + (offset or 0), line, text, color );
- end
- function UI.print.center( wd, line, text, color, offset )
- local w,h = wd.getSize();
- UI.print.rich( wd, 1+(offset or 0) + w/2 - string.len(text)/2, line, text, color );
- end
- function UI.print.right( wd, line, text, color, offset )
- local w,h = wd.getSize();
- UI.print.rich( wd, 1-(offset or 0) + w - string.len(text), line, text, color );
- end
- function UI.print.title( wd, line, y1, y2, c1, title, c2)
- local t = UI.rectangle( wd, y1, line, y2-y1, 1, c1 );
- UI.print.center( t, 1, title, c2 );
- end
- function UI.rectangle( wd, x, y, w, h, color )
- if type( wd ) ~= "table" or
- type( x ) ~= "number" or
- type( y ) ~= "number" or
- type( w ) ~= "number" or
- type( h ) ~= "number" then
- error( "Expected object, number, number, number, number, [boolean]", 2 )
- end
- local r = window.create( wd, x, y, w, h );
- r.setBackgroundColor( color ); r.clear();
- return r;
- end
- function UI.bar( wd, x, y, w, h, c1, table, m )
- local bar = UI.rectangle( wd, x, y, w, h, c1 );
- local max, i = m or 100, ( h < w ) and 1 or 0;
- for k, pi in pairs( table ) do
- local p = pi.p / max;
- if ( h < w ) then
- UI.rectangle( bar, i, 1, p*w, h, pi.color );
- i = i + p*w;
- else
- i = i + p*h;
- UI.rectangle( bar, 1, 2+h-i, w, math.ceil(p*h), pi.color );
- end
- end
- end
- function UI.progress_bar( wd, x, y, w, h, p, c1, c2 )
- UI.bar( wd, x, y, w, h, c1, {{ ["p"] = p+5, ["color"] = c2 }});
- end
- function UI.modal( wd, x, y, w, h, title )
- if ( type(title) == "table" ) then
- local head = window.create( wd, x, y, w, 3 );
- head.setBackgroundColor( title.background );
- head.clear(); head.setCursorPos(2,1);
- UI.print.center( head, title.text, title.color );
- end
- return window.create(wd, x+3, y, w, h );
- end
- -- Main []
- local reactor = BRServer:new();
- for i,arg in pairs(args) do
- if ( arg == "--client" ) then reactor = BRClient:new(); break; end;
- end
- for i,arg in pairs(args) do
- for protocol in arg:gmatch( "-p:(.*)" ) do
- reactor.protocole = protocol; break;
- end
- end;
- reactor:run();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement