Advertisement
Looong

Space Engineers - Power Manager

Jan 13th, 2016
577
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.39 KB | None | 0 0
  1. /*Special thanks to the Reactor Usage script created by "me 10 Jin" which I have blatantly ripped REGEX usage from
  2.   And also to Digi, whose Airlock Cycling script was used to figure out the timers for solar power output updating.
  3. */
  4. //regex   taken from 10Jin's Reactor Usage script
  5. System.Text.RegularExpressions.Regex pwrRegex = new System.Text.RegularExpressions.Regex(
  6. "Max Output: (\\d+\\.?\\d*) (\\w?)W.*Current Output: (\\d+\\.?\\d*) (\\w?)W"
  7. , System.Text.RegularExpressions.RegexOptions.Singleline);
  8.  
  9. //regex altered from the above, this will detect stored and available power on a battery.
  10. System.Text.RegularExpressions.Regex batteryRegex = new System.Text.RegularExpressions.Regex(
  11. "Max Stored Power: (\\d+\\.?\\d*) (\\w?)Wh.*Current Input: (\\d+\\.?\\d*) (\\w?)W.*Stored power: (\\d+\\.?\\d*) (\\w?)Wh"
  12. , System.Text.RegularExpressions.RegexOptions.Singleline);
  13.  
  14. void Main()
  15. {
  16.     //want battery set to charge if solar panels are outputting enough excess power to charge them.
  17.     //want battery set to discharge if solar panels are not outputting enough excess power(to save uranium)
  18.     //(stretch) enable reactors if batteries are all set to "discharge" and still not producing enough power.
  19.         //batteries should never charge from reactors.
  20.  
  21.     //time to wait after shutting batteries down to check solar panels.
  22.     int holdTime = 100;
  23.     //without this delay, the solar panels won't update their output fast enough.
  24.     //100ms is sufficient, less might work.
  25.  
  26.     //get all batteries and turn them off.
  27.     var batteries = new List<IMyTerminalBlock>();
  28.     GridTerminalSystem.GetBlocksOfType<IMyBatteryBlock>(batteries);
  29.  
  30.     //Grab all solar panels and batteries on the grid.
  31.     var solars = new List<IMyTerminalBlock>();
  32.     GridTerminalSystem.GetBlocksOfType<IMySolarPanel>(solars);
  33.  
  34.     double pwrMaxSum = 0.0;
  35.     double pwrNowSum = 0.0;
  36.     double solarPowerRatio = 0.0;
  37.     double batteryPowerRatio = 0.0;
  38.     double batteryCharge = 0.0;
  39.  
  40.     //These lines are to grab antennas for text ouptut.
  41.     //Don't use them if you don't want your antennas getting randomly renamed.
  42. //    var antennas = new List<IMyTerminalBlock>();
  43. //    GridTerminalSystem.GetBlocksOfType<IMyRadioAntenna>(antennas);
  44.  
  45.     //Get total solar power ratio(thanks again 10Jin)
  46.     for ( int i = 0; i <solars.Count ; i++ )
  47.     {
  48.         System.Text.RegularExpressions.Match match = pwrRegex.Match( solars[i].DetailedInfo );
  49.         Double n;
  50.         if ( match.Success )
  51.         {
  52.             if ( Double.TryParse( match.Groups[1].Value, out n ) )
  53.                 pwrMaxSum += n * Math.Pow( 1000.0, ".kMGTPEZY".IndexOf( match.Groups[2].Value ) );
  54.  
  55.             if ( Double.TryParse( match.Groups[3].Value, out n ) )
  56.                 pwrNowSum += n * Math.Pow( 1000.0, ".kMGTPEZY".IndexOf( match.Groups[4].Value ) );
  57.         }
  58.     }
  59.     //get total power draw due to battery charging
  60.     for ( int i = 0; i<batteries.Count ; i++)
  61.     {
  62.         System.Text.RegularExpressions.Match match = batteryRegex.Match( batteries[i].DetailedInfo);
  63.         if (match.Success)
  64.         {
  65.             Double n;
  66.             if( Double.TryParse(match.Groups[3].Value, out n))
  67.                 batteryCharge += n * Math.Pow( 1000.0, ".kMGTPEZY".IndexOf( match.Groups[4].Value));
  68.         }
  69.     }
  70.     pwrNowSum -= batteryCharge;
  71.     //make sure any solar panels were found.
  72.     if(pwrMaxSum>0.0)
  73.     {
  74.         solarPowerRatio = pwrNowSum/pwrMaxSum;
  75.         //antennas[0].SetCustomName("Solar power usage ratio: " + solarPowerRatio);
  76.     }
  77.     else
  78.     {
  79.         solarPowerRatio = 9999;
  80.         //antennas[0].SetCustomName("Error: no solar energy being produced.");
  81.     }
  82.     if(solarPowerRatio < 0.99)//approximate cutoff due to floating-point weirdness
  83.     {
  84.         //Grab all reactors, turn them off(to save uranium)
  85.         var reactors = new List<IMyTerminalBlock>();
  86.         GridTerminalSystem.GetBlocksOfType<IMyReactor>(reactors);
  87.         for(int i=0;i<reactors.Count;i++)
  88.         {
  89.             reactors[i].GetActionWithName("OnOff_Off").Apply(reactors[i]);
  90.         }
  91.         //grab all batteries, set them to "charge."
  92.         for(int i=0;i<batteries.Count;i++)
  93.         {
  94.             //turn battery on because it was turned off for the solar check.
  95. //            batteries[i].GetActionWithName("OnOff_On").Apply(batteries[i]);
  96.             bool recharge = true;
  97.             //Fun fact: Batteries don't have a field to tell you whether they're in "charge" mode or not!
  98.             //Fortunately, the DetailedInfo string will tell you how long it will take to recharge or discharge.
  99.             //checking which of these messages is in use will let us know whether we're
  100.             //charging or discharging.
  101.             string batteryInfo = batteries[i].DetailedInfo;
  102.             recharge = batteryInfo.Contains("recharged");
  103.             //if discharging
  104.             if(!recharge)
  105.             {
  106.                 batteries[i].GetActionWithName("Recharge").Apply(batteries[i]);
  107.             }
  108.         }
  109.     }
  110.     else//battery power needed.
  111.     {
  112.         for(int i=0;i<batteries.Count;i++)
  113.         {
  114.             //turn battery on because it was turned off for the solar check.
  115.             batteries[i].GetActionWithName("OnOff_On").Apply(batteries[i]);
  116.             bool recharge = true;
  117.             //Check whether we're currently in recharge mode
  118.             string batteryInfo = batteries[i].DetailedInfo;
  119.             recharge = batteryInfo.Contains("recharged");
  120.             //if it is, switch it out of recharge mode.
  121.             if(recharge)
  122.             {
  123.                 batteries[i].GetActionWithName("Recharge").Apply(batteries[i]);
  124.             }
  125.         }
  126.         //check battery current/max
  127.         for ( int i = 0; i <batteries.Count ; i++ )
  128.         {
  129.             //filter out batteries that are empty from being counted among supply.
  130.             IMyBatteryBlock thisBattery = batteries[i] as IMyBatteryBlock;
  131.             if(thisBattery.HasCapacityRemaining)
  132.             {
  133.                 System.Text.RegularExpressions.Match match = batteryRegex.Match( batteries[i].DetailedInfo );
  134.                 Double n;
  135.  
  136.                 if ( match.Success )
  137.                 {
  138.                     //check if battery is "empty."(<0.5% charge)
  139.                     Double pwrMaxStored = 0.0;
  140.                     Double pwrNowStored = 0.0;
  141.                     Double pwrStoredPercent = 0.0;
  142.  
  143.                     //Seriously, thanks "me 10 Jin." I couldn't have done this without your example.
  144.                     if ( Double.TryParse( match.Groups[1].Value, out n ) )
  145.                         pwrMaxStored = n * Math.Pow( 1000.0, ".kMGTPEZY".IndexOf( match.Groups[2].Value ) );
  146.  
  147.                     if ( Double.TryParse( match.Groups[3].Value, out n ) )
  148.                         pwrNowStored += n * Math.Pow( 1000.0, ".kMGTPEZY".IndexOf( match.Groups[4].Value ) );
  149.  
  150.                     pwrStoredPercent = pwrNowStored/pwrMaxStored*100;
  151.                     //antennas[0].SetCustomName("Battery is at " + pwrStoredPercent + " percent of capacity.");
  152.  
  153.                     if(pwrStoredPercent > 0.5)
  154.                     {
  155.                         //there's enough battery power to be usable, check the output.
  156.                         match = batteryRegex.Match( batteries[i].DetailedInfo );
  157.                         if(match.Success)
  158.                         {
  159.                             if ( Double.TryParse( match.Groups[1].Value, out n ) )
  160.                                 pwrMaxSum += n * Math.Pow( 1000.0, ".kMGTPEZY".IndexOf( match.Groups[2].Value ) );
  161.  
  162.                             if ( Double.TryParse( match.Groups[3].Value, out n ) )
  163.                                 pwrNowSum += n * Math.Pow( 1000.0, ".kMGTPEZY".IndexOf( match.Groups[4].Value ) );
  164.                         }
  165.                     }
  166.                 }
  167.             }
  168.         }
  169.         //if THAT's above 0.99, turn on the reactors.
  170.         if(pwrMaxSum > 0.0)
  171.         {
  172.             batteryPowerRatio = pwrNowSum/pwrMaxSum;
  173.         }
  174.         else
  175.         {
  176.             batteryPowerRatio = 9999;
  177.         }
  178.         if(batteryPowerRatio > 0.99)
  179.         {
  180.             var reactors = new List<IMyTerminalBlock>();
  181.             GridTerminalSystem.GetBlocksOfType<IMyReactor>(reactors);
  182.             for(int i=0;i<reactors.Count;i++)
  183.             {
  184.                 reactors[i].GetActionWithName("OnOff_On").Apply(reactors[i]);
  185.             }
  186.         }
  187.     }
  188.  
  189.     return;
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement