Advertisement
MaximumFrank

Reactor

Jun 14th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.44 KB | None | 0 0
  1. ---first thing to do is to schedule events
  2.  
  3. local array = {};
  4. local max = 0;
  5.  
  6. for i = 0, 40 do
  7.     array[i] = 0;
  8. end
  9.  
  10. --set the task frequencies
  11. local tempTick = 2;
  12. local powerTick = 2;
  13. local energyTick = 3;
  14. local fuelTick = 2;
  15. local powerGraphTick = 5;
  16.  
  17. --energy bounds
  18. local lowBound = 7500000;
  19. local highBound = 9500000;
  20. local targOut = 600;
  21.  
  22. --set limits
  23. local tempLimit = 3000;
  24.  
  25. --find the reactor
  26. local r = peripheral.find("BigReactors-Reactor")
  27.  
  28. --redirect to monitor
  29. local mon = peripheral.find("monitor");
  30. term.redirect(mon);
  31. mon.setTextScale(0.5);
  32.  
  33. --get terminal width and height
  34. local width, height = mon.getSize();
  35.  
  36. ---define functions
  37. --draw rectangle
  38. function rect(x1, y1, x2, y2, col)
  39.     local i, ii;
  40.     term.setBackgroundColor(col);
  41.  
  42.     for i = x1, x2 do
  43.         for ii = y1, y2 do
  44.             term.setCursorPos(i, ii);
  45.             term.write(' ');
  46.         end
  47.     end
  48. end
  49.  
  50. --clear the terminal
  51. function clear()
  52.     term.setBackgroundColor(colors.black);
  53.     term.clear();
  54. end
  55.  
  56. --reset colors
  57. function resetCols()
  58.     term.setBackgroundColor(colors.black);
  59.     term.setTextColor(colors.lightGray);
  60. end
  61.  
  62. function drawTemps()
  63.     local temp = math.ceil(r.getFuelTemperature());
  64.  
  65.     --core temperature
  66.     resetCols();
  67.     term.setTextColor(colors.white);
  68.     term.setCursorPos(4, 6);
  69.     term.write("Core Temp: ")
  70.  
  71.     if temp > tempLimit then
  72.         term.setTextColor(colors.red);
  73.     elseif temp > tempLimit * 0.75 then
  74.         term.setTextColor(colors.orange);
  75.     else
  76.         term.setTextColor(colors.green);
  77.     end
  78.     term.write(temp);
  79.     term.setTextColor(colors.white);
  80.     term.write(" C    ");
  81.  
  82.     resetCols();
  83.     term.setTextColor(colors.white);
  84.     term.setCursorPos(4, 7);
  85.     term.write("Case Temp: ")
  86.     temp = math.ceil(r.getCasingTemperature());
  87.  
  88.     if temp > tempLimit then
  89.         term.setTextColor(colors.red);
  90.     elseif temp > tempLimit * 0.75 then
  91.         term.setTextColor(colors.yellow);
  92.     else
  93.         term.setTextColor(colors.green);
  94.     end
  95.     term.write(temp);
  96.     term.setTextColor(colors.white);
  97.     term.write(" C    ");
  98. end
  99.  
  100. function drawStatus()
  101.     resetCols();
  102.  
  103.     term.setCursorPos(4, 4);
  104.  
  105.     --reactor status
  106.     term.setTextColor(colors.white);
  107.     term.write("Reactor is ");
  108.     if r.getActive() then
  109.         term.setTextColor(colors.green);
  110.         term.write("Active  ");
  111.     else
  112.         term.setTextColor(colors.red);
  113.         term.write("Inactive");
  114.     end
  115.  
  116.     ---draw boxes
  117.     --upper left top
  118.     rect(2, 2, (width * 0.5) - 1, 2, colors.lightGray);
  119.     --upper left bottom
  120.     rect(2, 9, (width * 0.5) - 1, 9, colors.lightGray);
  121.     --upper left left
  122.     rect(2, 2, 2, 8, colors.lightGray);
  123.     --upper left right
  124.     rect((width * 0.5) - 1, 2, (width * 0.5) - 1, 9, colors.lightGray);
  125.  
  126.     resetCols();
  127.     term.setTextColor(colors.lightBlue);
  128.     term.setCursorPos(((width * 0.5) - string.len(" STATUS ")) / 2, 2);
  129.     term.write(" Status ");
  130.  
  131.     drawTemps();
  132. end
  133.  
  134. function drawFuel()
  135.     --upper right top
  136.     rect((width * 0.5) + 1, 2, width - 1, 2, colors.lightGray);
  137.     --upper right bottom
  138.     rect((width * 0.5) + 1, 9, width - 1, 9, colors.lightGray);
  139.     --upper right left
  140.     rect((width * 0.5) + 1, 2, (width * 0.5) + 1, 9, colors.lightGray);
  141.     --upper right right
  142.     rect(width - 1, 2, width - 1, 9, colors.lightGray);
  143.  
  144.     local fuelPercent = math.floor((r.getFuelAmount() / r.getFuelAmountMax()) * 100);
  145.     local wastePercent = math.ceil((r.getWasteAmount() / r.getFuelAmountMax()) * 100);
  146.  
  147.     resetCols();
  148.     term.setTextColor(colors.lightBlue);
  149.     term.setCursorPos((width * 0.5) + (((width * 0.5) - string.len(" Fuel "))) / 2, 2);
  150.     term.write(" Fuel ");
  151.  
  152.     resetCols();
  153.  
  154.     term.setTextColor(colors.white);
  155.     term.setCursorPos((width * 0.5) + 3, 4);
  156.     term.write("Fuel Usage: ");
  157.     term.write(math.ceil(r.getFuelConsumedLastTick() * 100) / 100);
  158.     term.write(" mB/t   ")
  159.  
  160.     term.setCursorPos((width * 0.5) + 3, 6);
  161.     term.setTextColor(colors.yellow)
  162.     term.write(math.ceil((r.getFuelAmountMax() - (r.getFuelAmount() + r.getWasteAmount())) / r.getFuelAmountMax() * 100));
  163.  
  164.     resetCols();
  165.  
  166.     term.write("% | ");
  167.  
  168.     term.setTextColor(colors.lightBlue);
  169.     term.write(fuelPercent);
  170.  
  171.     resetCols();
  172.  
  173.     term.write("% | ");
  174.  
  175.     term.setTextColor(colors.blue);
  176.     term.write(wastePercent);
  177.  
  178.     resetCols();
  179.  
  180.     term.write("%   ");
  181.  
  182.     local percent = (((width - 2) - ((width * 0.5) + 3)) / 100)
  183.  
  184.     --draw free space bar
  185.     rect((width * 0.5) + 3, 7, width - 2, 7, colors.yellow);
  186.     --draw fuel bar
  187.     rect((width * 0.5) + 3 + math.ceil(percent * (100 - (fuelPercent + wastePercent))), 7, width - 2, 7, colors.lightBlue);
  188.     --draw waste bar
  189.     rect((width * 0.5) + 3 + math.ceil(percent * (100 - wastePercent)), 7, width - 2, 7, colors.blue);
  190. end
  191.  
  192. function drawRod()
  193.     rect(width - 9, 12, width - 2, height - 2, colors.black)
  194.  
  195.     --percent
  196.     local percent = ((height - 4) - 15) / 100;
  197.  
  198.     --lower right top
  199.     rect(width - 10, 11, width - 1, 11, colors.lightGray);
  200.     --lower right bottom
  201.     rect(width - 10, height - 1, width - 1, height - 1, colors.lightGray);
  202.     --lower right left
  203.     rect(width - 10, 11, width - 10, height - 1, colors.lightGray);
  204.     --lower right right
  205.     rect(width - 1, 11, width - 1, height - 1, colors.lightGray);
  206.  
  207.     resetCols();
  208.  
  209.     term.setTextColor(colors.lightBlue);
  210.     term.setCursorPos(width - 10 + math.ceil(((width - 1) - (width - 10) - (string.len(" Rods "))) / 2), 11);
  211.     term.write(" Rods ");
  212.  
  213.     resetCols();
  214.  
  215.     --draw bar
  216.     rect(width - 6, 15, width - 4, height - 3, colors.lightBlue);
  217.     if r.getControlRodLevel(0) > 0 then
  218.         rect(width - 5, 15, width - 5 , height - (4 + ((percent * 100) - (percent * r.getControlRodLevel(0)))), colors.blue);
  219.     end
  220.  
  221.     resetCols();
  222.  
  223.     term.setTextColor(colors.white);
  224.     term.setCursorPos(width - 8, 13);
  225.     term.write(r.getControlRodLevel(0));
  226.     term.write("% ");
  227. end
  228.  
  229. function drawPower()
  230.     rect(3, 12, width - 13, 14, colors.black);
  231.  
  232.     --reset the max if applicable
  233.     if r.getEnergyProducedLastTick() > max then
  234.         max = r.getEnergyProducedLastTick();
  235.     end
  236.  
  237.     --bottom left top
  238.     rect(2, 11, width - 12, 11, colors.lightGray);
  239.     --bottom left bottom
  240.     rect(2, height - 1, width - 12, height - 1, colors.lightGray);
  241.     --bottom left left
  242.     rect(2, 11, 2, height - 1, colors.lightGray);
  243.     --bottom left right
  244.     rect(width - 12, 11, width - 12, height - 1, colors.lightGray);
  245.  
  246.     resetCols();
  247.  
  248.     term.setTextColor(colors.lightBlue);
  249.     term.setCursorPos((((width - 14) - string.len(" Power ")) / 2), 11);
  250.     term.write(" Power ");
  251.  
  252.     resetCols();
  253.  
  254.     term.setTextColor(colors.white);
  255.     term.setCursorPos(4, 13);
  256.     term.write("Power Produced: ");
  257.     term.setTextColor(colors.green);
  258.     term.write(math.ceil(r.getEnergyProducedLastTick() * 100) / 100);
  259.     term.setTextColor(colors.white);
  260.     term.write(" RF/t         ");
  261.  
  262.     term.setTextColor(colors.white);
  263.     term.setCursorPos(4, 14);
  264.     term.write("Stored Energy: ");
  265.     term.setTextColor(colors.green);
  266.     term.write(tostring(r.getEnergyStored()));
  267.     term.setTextColor(colors.white);
  268.     term.write(" RF         ");
  269. end
  270.  
  271. function drawGraph()
  272.     rect(3, 15, width - 13, height - 2, colors.black)
  273.  
  274.     local percent = ((height - 3) - 17) / 100
  275.  
  276.     for i = 0, 39 do
  277.         if i < 39 then
  278.             array[i] = array[i + 1];
  279.             rect(4 + i, 18 + ((percent * 100) - (percent * math.ceil((array[i] / max) * 100))), 4 + i, height - 2, colors.blue);
  280.         else
  281.             array[i] = math.ceil(r.getEnergyProducedLastTick() * 100) / 100;
  282.             rect(4 + i, 18 + ((percent * 100) - (percent * math.ceil((array[i] / max) * 100))), 4 + i, height - 2, colors.blue);
  283.         end
  284.     end
  285.  
  286.     rect(3, height - 2, 44, height - 2, colors.black);
  287. end
  288.  
  289. calTick = 0;
  290.  
  291. function calibrate(case)
  292.     if targOut > 0 then
  293.         if case == 0 then
  294.             r.setAllControlRodLevels(50)
  295.             calTick = os.startTimer(12)
  296.         elseif case == 1  and r.getActive() == true then
  297.             if r.getEnergyProducedLastTick() > targOut + 10 then
  298.                 r.setAllControlRodLevels(r.getControlRodLevel(0) + 1);
  299.                 calTick = os.startTimer(3);
  300.             elseif r.getEnergyProducedLastTick() < targOut - 10 then
  301.                 r.setAllControlRodLevels(r.getControlRodLevel(0) - 1);
  302.                 calTick = os.startTimer(3);
  303.             else
  304.                 calTick = os.startTimer(60);
  305.             end
  306.         end
  307.     else
  308.         r.setAllControlRodLevels(0);
  309.     end
  310. end
  311.  
  312. local loading = 0;
  313.  
  314. clear();
  315.  
  316. --turn it on
  317. if r.getActive() ~= true then
  318.     r.setActive(true);
  319.     while loading < 100 do
  320.         rect(1, 1, width, 1, colors.gray);
  321.         term.setCursorPos((width - string.len(" Reactor Warmup in Progress... ")) / 2, 1);
  322.         term.write(" Reactor Warmup in Progress... ")
  323.  
  324.         local percent = (width - 3) / 100;
  325.  
  326.         rect(2, height / 2, width - 1, height / 2, colors.lightBlue);
  327.         rect(2, height / 2, 2 + (loading * percent), height / 2, colors.blue);
  328.  
  329.         loading = loading + 2;
  330.         os.sleep(0.5);
  331.     end
  332. end
  333.  
  334.  
  335. clear();
  336.  
  337. drawStatus();
  338. drawFuel();
  339. drawRod();
  340. drawPower();
  341.  
  342. calibrate(0);
  343.  
  344. --set the timers
  345. temp = os.startTimer(tempTick);
  346. power = os.startTimer(powerTick);
  347. energy = os.startTimer(energyTick);
  348. fuel = os.startTimer(fuelTick);
  349. pgraph = os.startTimer(powerGraphTick);
  350.  
  351. while(true) do
  352.  
  353.     if r.getEnergyStored() < lowBound and r.getActive() == false then
  354.         r.setActive(true);
  355.         calibrate(0);
  356.     elseif r.getEnergyStored() > highBound then
  357.         r.setActive(false);
  358.     end
  359.  
  360.     ---start the program
  361.     local event, timerID = os.pullEvent("timer");
  362.     if timerID == temp then
  363.         drawStatus();
  364.         temp = os.startTimer(tempTick);
  365.     elseif timerID == fuel then
  366.         drawFuel();
  367.         fuel = os.startTimer(fuelTick);
  368.     elseif timerID == energy then
  369.         drawRod();
  370.         energy = os.startTimer(energyTick);
  371.     elseif timerID == power then
  372.         drawPower();
  373.         power = os.startTimer(powerTick);
  374.     elseif timerID == pgraph then
  375.         drawGraph();
  376.         pgraph = os.startTimer(powerGraphTick);
  377.     elseif timerID == calTick then
  378.         calibrate(1);
  379.     end
  380. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement