Advertisement
Someoneawesome78

ReactorControllerV3.lua

Mar 26th, 2023 (edited)
801
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.69 KB | None | 0 0
  1. --Reactor Controller
  2. --Version 3
  3. --Dependencies
  4. --ReactorConfigReader V1
  5. --ReactorMonitor V1
  6.  
  7. --Configuration
  8. local ConfigLocation = "disk/ReactorV3.settings";
  9. local ReactorConfigReader = require("ReactorConfigReader");
  10. local ReactorMonitor = require("ReactorMonitor");
  11. local config = nil;
  12.  
  13. --Status types
  14. local Status = {
  15.     IDLE = 0;
  16.     CHARGING = 1;
  17.     HIGH = 2;
  18. }
  19.  
  20. --Status variables
  21. local State = {
  22.     PrevEnergyLevel = nil;
  23.     PrevSleepTime = nil;
  24.     CurrentSystemStatus = Status.CHARGING;
  25.     CurrentSystemStatusStart = nil;
  26.     PrevSystemStatus = Status.IDLE;
  27.     PrevSystemStatusStart = nil;
  28.     PrevSystemStatusEnd = nil;
  29. }
  30.  
  31.  
  32. --functions
  33. local function ThrowFatalError(message)
  34.     if(config ~= nil and config.monitor ~= nil) then
  35.         config.monitor.setTextScale(3);
  36.         config.monitor.clear();
  37.         config.monitor.setTextColor(colors.purple);
  38.         config.monitor.write("Error " .. message);
  39.     end
  40.     error(message);
  41. end
  42.  
  43. local function Initialize()
  44.     print("Reactor Control Initializing");
  45.     config = ReactorConfigReader:LoadConfiguration(ConfigLocation);
  46.     if config == nil then
  47.         ThrowFatalError("Failed to Load Configuration");
  48.         return;
  49.     end
  50.  
  51.     State.PrevEnergyLevel = config.reactor.getEnergyStored() / config.maxPowerStore * 100;
  52.     State.PrevSleepTime = 0;
  53.     State.CurrentSystemStatusStart = os.clock();
  54.     State.PrevSystemStatusStart = os.clock();
  55.     State.PrevSystemStatusEnd = os.clock();
  56.     config.reactor.setActive(true);
  57.     config.reactor.setAllControlRodLevels(config.optimalInsertionPercent);
  58.     sleep(1);
  59. end
  60.  
  61. --increases rod length / decreases energy output
  62. local function IncreaseRodLength()
  63.     local numRods = config.reactor.getNumberOfControlRods();
  64.     local rodLevel = config.reactor.getControlRodLevel(0);
  65.  
  66.     if rodLevel >= config.optimalInsertionPercent then
  67.         return;
  68.     end
  69.     config.reactor.setAllControlRodLevels(rodLevel + 1);
  70. end
  71.  
  72. --decreases rod length / increases energy output
  73. local function DecreaseRodLength()
  74.     local numRods = config.reactor.getNumberOfControlRods();
  75.     local rodLevel = config.reactor.getControlRodLevel(0);
  76.  
  77.     if rodLevel == 0 then
  78.         return;
  79.     end
  80.     config.reactor.setAllControlRodLevels(rodLevel - 1);
  81. end
  82.  
  83. local function IsAllRodsOptimalLength()
  84.     sleep(1);
  85.     local numRods = config.reactor.getNumberOfControlRods();
  86.     for i = 0, numRods - 1 do
  87.         local rodLevel = config.reactor.getControlRodLevel(i)
  88.         if rodLevel < config.optimalInsertionPercent then
  89.             return false;
  90.         end
  91.     end
  92.     return true;
  93. end
  94.  
  95. local function SetNewState(newState, firstChange)
  96.     --set archival vars
  97.     if(firstChange) then
  98.         State.PrevSystemStatus = State.CurrentSystemStatus;
  99.         State.PrevSystemStatusStart = State.CurrentSystemStatusStart;
  100.         State.PrevSystemStatusEnd = os.clock();
  101.     end
  102.  
  103.     State.CurrentSystemStatus = newState;
  104.     State.CurrentSystemStatusStart = os.clock();
  105.    
  106.     if State.CurrentSystemStatus == Status.IDLE then
  107.         config.reactor.setAllControlRodLevels(100);
  108.     elseif State.CurrentSystemStatus == Status.CHARGING then
  109.         config.reactor.setAllControlRodLevels(config.optimalInsertionPercent);
  110.     end
  111. end
  112.  
  113. --returns the sleep time that should then be followed
  114. local function UpdateReactorState()
  115.     State.PrevEnergyLevel = config.reactor.getEnergyStored() / config.maxPowerStore * 100;
  116.     local firstChange = true;
  117.     local change = true;
  118.  
  119.     while(change) do
  120.         change = false;
  121.         sleep(1);
  122.         --if idle
  123.         if State.CurrentSystemStatus == Status.IDLE then
  124.             if State.PrevEnergyLevel <= config.powerLowerBounds then
  125.                 change = true;
  126.                 SetNewState(Status.CHARGING,firstChange);
  127.                 firstChange = false;
  128.             else
  129.                 return config.normalSleepTimeSeconds;
  130.             end
  131.         elseif State.CurrentSystemStatus == Status.CHARGING then
  132.             if State.PrevEnergyLevel >= config.powerUpperBounds then
  133.                 change = true;
  134.                 SetNewState(Status.IDLE,firstChange);
  135.                 firstChange = false;
  136.             elseif State.PrevEnergyLevel <= config.powerHighLBounds then
  137.                 change = true;
  138.                 SetNewState(Status.HIGH,firstChange);
  139.                 firstChange = false;
  140.             else
  141.                 return config.normalSleepTimeSeconds;
  142.             end
  143.         elseif State.CurrentSystemStatus == Status.HIGH then
  144.             if State.PrevEnergyLevel >= config.powerUpperBounds then
  145.                 change = true;
  146.                 SetNewState(Status.IDLE, firstChange)
  147.                 firstChange = false;
  148.             elseif State.PrevEnergyLevel >= config.powerHighUBounds then
  149.                 IncreaseRodLength();
  150.                 if IsAllRodsOptimalLength() then
  151.                     change = true;
  152.                     SetNewState(Status.CHARGING,firstChange);
  153.                     firstChange = false;
  154.                 else
  155.                     return config.frequentSleepTimeSeconds;
  156.                 end
  157.             elseif State.PrevEnergyLevel <= config.powerHighLBounds then
  158.                 DecreaseRodLength();
  159.                 return config.frequentSleepTimeSeconds;
  160.             else
  161.                 return config.frequentSleepTimeSeconds;
  162.             end
  163.         end
  164.     end
  165.     return config.normalSleepTimeSeconds;
  166. end
  167.  
  168. --Main loop
  169. Initialize();
  170. print("Starting Reactor Monitoring");
  171. while true do
  172.     local sleepTime = UpdateReactorState();
  173.  
  174.     sleep(1);
  175.     State.PrevSleepTime = sleepTime;
  176.     ReactorMonitor.updateMonitor(config, State);
  177.     sleep(sleepTime);
  178. end
  179.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement