Advertisement
Corbinhol

Fusion Reactor Controller

Mar 10th, 2023 (edited)
584
2
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.06 KB | None | 2 0
  1. --Importing all important API's
  2. local component = require("component");
  3. local filesystem = require("filesystem");
  4. local sides = require("sides");
  5. local event = require("event");
  6. local serialization = require("serialization");
  7. local term = require("term");
  8. local thread = require("thread");
  9. local computer = require("computer");
  10. local text = require("text");
  11. local shell = require("shell");
  12.  
  13. local gpu = component.gpu
  14. local tank;
  15.  
  16. api = {};  
  17. local version = "0.4"
  18. api["status"] = "Running";
  19. local run = true;
  20.  
  21. local hasTank = false;
  22. local tankSide;
  23. local reactor;
  24.  
  25. local function firstToUpper(str) --Capitalizes first letter of a string. Used for fuel names.
  26.     return (str:gsub("^%l", string.upper))
  27. end
  28.  
  29. local function checkComponents() --Check all components.
  30.     if component.isAvailable("tank_controller") then --check if tank upgrade/adapter is attatched
  31.         tank = component.tank_controller;
  32.         for i=0,5 do
  33.             if tank.getTankCapacity(i) > 0 then --find side with tank
  34.                 tankSide = i;
  35.                 print("Found one.")
  36.                 break;
  37.             end
  38.         end
  39.         if tankSide == nil then --if no side has a tank, assume tank doesn't exist. remove tank.
  40.             print("No Sides Found.")
  41.             tank = nil;
  42.         end
  43.     end
  44.  
  45.     if component.isAvailable("nc_fusion_reactor") then --if no reactor,
  46.         reactor = component.nc_fusion_reactor;
  47.     else    
  48.         print("Reactor not connected. Please connect reactor before starting program");
  49.         os.exit();
  50.     end
  51. end
  52.  
  53. local function closeListener(_, _, key) --Kill Switch
  54.     if key == nil then key = 96; end
  55.     if key == 96 then
  56.         print("Killing Program...");
  57.         run = false;
  58.         filesystem.setAutorunEnabled(false)
  59.         event.ignore("key_up", closeListener);
  60.     end
  61. end
  62. event.listen("key_up", closeListener);
  63.  
  64. function start()
  65.  
  66. end
  67.  
  68. function stop()
  69.  
  70. end
  71.  
  72. local function indexTime(time) --Convert Uptime to digital time
  73.     local days = math.floor(time/86400)
  74.     local hours = math.floor((time % 86400)/3600)
  75.     local minutes = math.floor((time % 3600)/60)
  76.     local seconds = math.floor((time % 60))
  77.     return string.format("%02d:%02d:%02d:%02d",days,hours,minutes,seconds)
  78. end
  79.  
  80. --Set colors for display
  81. local color = {};
  82. color["Disabled"] = 0xff0000;
  83. color["Running"] = 0x00ff08;
  84. color["Warming Up"] = 0xff9500
  85. color["green"] = 0x00ff08;
  86. color["red"] = 0xff0000;
  87.  
  88. local logBuffer = {};
  89. local function log(input, from, color, nameColor);
  90.     if color == nil then color = 0xffffff; end
  91.     if nameColor == nil then nameColor = 0x8fe2f2; end
  92.     local outTable = {};
  93.     outTable["text"] = input;
  94.     outTable["from"] = from;
  95.     outTable["timestamp"] = math.floor(computer.uptime());
  96.     outTable["textColor"] = color;
  97.     outTable["nameColor"] = nameColor;
  98.     table.insert(logBuffer, 1, outTable);
  99.     if #logBuffer > 17 then
  100.         table.remove(logBuffer, 19)
  101.     end
  102. end
  103.  
  104. local function updateDisplay() --Update display in background.
  105.     term.clear();
  106.     while run do
  107.         gpu.set(1,1, string.rep("โ•", 80));
  108.         gpu.set(2,2, "Reactor Controller [Version " .. version .. "]");
  109.         gpu.set(2,3, "Reactor Status: ")
  110.         gpu.setForeground(color[api["status"]]); --Set the color based on the status
  111.         gpu.set(2 + string.len("Reactor Status: "),3, api["status"])
  112.         gpu.setForeground(0xffffff); --Set color back to white
  113.         local uptime = indexTime(math.floor(computer.uptime())); -- Get the total uptime of computer, and format it.
  114.         gpu.set(80 - string.len("Uptime: " .. uptime), 2, "Uptime: ");
  115.         gpu.setForeground(color["Warming Up"]); --Set color of timestamp to orange (is same color as Warming Up)
  116.         gpu.set(80 - string.len(uptime), 2, uptime);
  117.         gpu.setForeground(0xffffff); --Set Color back to white
  118.         --Get total reactor temperature.
  119.         local fusionHeat = "           Temperature: " .. math.floor(reactor.getTemperature() / 1000) .. "kK";
  120.         gpu.set(80 - string.len(fusionHeat), 3, fusionHeat);
  121.         -- gpu.set(2, 4, "1st Fission Fuel: " .. firstToUpper(reactor.getFirstFusionFuel()));
  122.         -- gpu.set(2, 5, "2nd Fission Fuel: " .. firstToUpper(reactor.getSecondFusionFuel()));
  123.         if tank ~= nil then --Only print fuel information, if tank controller exist.
  124.             local fusionFluidInTank = tank.getFluidInTank(tankSide);
  125.             local fusionFuel1 = "          " .. math.floor(fusionFluidInTank[1].amount) .. "/" .. math.floor(fusionFluidInTank[1].capacity);
  126.             local fusionFuel2 = "          " .. math.floor(fusionFluidInTank[2].amount) .. "/" .. math.floor(fusionFluidInTank[2].capacity);
  127.             gpu.set(80 - string.len(fusionFuel1),4, fusionFuel1);
  128.             gpu.set(80 - string.len(fusionFuel2),5, fusionFuel2);
  129.         end
  130.         --get amount of rf in reactor
  131.         local rfAmount = "Rf Buffer: " .. math.floor(reactor.getEnergyStored()) .. "/" .. math.floor(reactor.getMaxEnergyStored());
  132.         gpu.set(2, 6, rfAmount);
  133.         -- local reactorEnergyChange = reactor.getEnergyChange(); --get rf change
  134.         -- local rfChange = "                " .. math.floor(reactorEnergyChange) .. " rf/t";
  135.  
  136.         -- if reactorEnergyChange > 0 then gpu.setForeground(color["green"]); else gpu.setForeground(color["red"]) end
  137.         -- gpu.set(80 - string.len(rfChange), 6, rfChange);
  138.         -- gpu.setForeground(0xffffff)
  139.         -- gpu.set(1,7, string.rep("โ•", 80));
  140.         -- gpu.set(1,25, string.rep("โ•", 80))
  141.         -- gpu.set(2,24, ">");
  142.         -- gpu.set(1,23, string.rep("โ•", 80))
  143.        
  144.         -- for i=1,17 do --prints log
  145.         --     if logBuffer[i] ~= nil then
  146.         --         local line = 23-i;
  147.         --         local logText = logBuffer[i]["text"];
  148.         --         local logFrom = logBuffer[i]["from"];
  149.         --         local logTimestamp = logBuffer[i]["timestamp"];
  150.         --         local logTextColor = logBuffer[i]["textColor"];
  151.         --         local logNameColor = logBuffer[i]["nameColor"];
  152.         --         local x = 3;
  153.         --         gpu.set(2,line,"[");
  154.         --         gpu.setForeground(0x707070);
  155.         --         gpu.set(3,line,indexTime(logTimestamp))
  156.         --         x = x + string.len(indexTime(logTimestamp));
  157.         --         gpu.setForeground(0xffffff)
  158.         --         gpu.set(x,line,"]");
  159.         --         x = x + 1;
  160.         --         gpu.set(x, line, "[");
  161.         --         x = x + 1;
  162.         --         gpu.setForeground(logNameColor);
  163.         --         gpu.set(x, line, logFrom);
  164.         --         x = x + string.len(logFrom);
  165.         --         gpu.setForeground(0xffffff);
  166.         --         gpu.set(x, line, "] ");
  167.         --         x = x + 2;
  168.         --         gpu.setForeground(logTextColor)
  169.         --         gpu.set(x, line, logText .. string.rep(" ", 160));
  170.         --         gpu.setForeground(0xffffff)
  171.         --         --gpu.set(2, line, logText .. string.rep(" ", 160))
  172.         --     end
  173.         -- end
  174.         os.sleep(1);
  175.     end
  176.    
  177. end
  178.  
  179. checkComponents()
  180. display = thread.create(updateDisplay);
  181. --updateDisplay();
  182. local function say(text, color)
  183.     if color == nil then color = 0xffffff; end
  184.     log(text, "Client", color, 0x6ac482);
  185. end
  186. local history = {};
  187. history["nowrap"] = true;
  188. local function getCommand()
  189.     while true do
  190.         term.setCursor(4,24);
  191.         local command = term.read(history, false, hint):sub(1, -2);
  192.         if command ~= "" then
  193.             local args, ops = shell.parse(command);
  194.             say(command);
  195.             if args[1] == "exit" then
  196.                 os.sleep(1);
  197.                 event.ignore("key_up", closeListener);
  198.                 run = false;
  199.                 term.clear();
  200.                 os.exit();
  201.             elseif args[1] == "stop" then
  202.                 --stop()
  203.             else
  204.  
  205.             end
  206.             gpu.set(4,24, string.rep(" ", 160));
  207.         end
  208.         os.sleep(0);
  209.     end
  210. end
  211. --commandInput = thread.create(getCommand);
  212. getCommand();
  213. while run do
  214.     os.sleep(.1);
  215. end
  216.  
  217. --Program Close
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement