Battery Status NAN/5 (4)

  1. const String PANEL_NAME = "Battery Panel";  
  2. const String MULTIPLIERS = ".kMGTPEZY" ;  
  3. const int PANEL_LINES = 22;  
  4. int lineOffset = 0;  
  5.  
  6. void Main()  
  7. {  
  8.     List<IMyTerminalBlock> work = new List<IMyTerminalBlock>();  
  9.     List<IMyTerminalBlock> batteries = new List<IMyTerminalBlock>();  
  10.     GridTerminalSystem.GetBlocksOfType<IMyBatteryBlock>(batteries);    
  11.     GridTerminalSystem.SearchBlocksOfName(PANEL_NAME, work);        
  12.     IMyTextPanel panel = null;  
  13.     for (int i = 0; i < work.Count; i++)    
  14.     {    
  15.         if (work&#91;i] is IMyTextPanel) {  
  16.            panel = (IMyTextPanel)work&#91;i];    
  17.            break;  
  18.         }  
  19.     }  
  20.  
  21.     System.Text.RegularExpressions.Regex batteryRegex = new System.Text.RegularExpressions.Regex(  
  22.                     "Max Stored Power: (\\d+\\.?\\d*) (\\w?)Wh.*Stored power: (\\d+\\.?\\d*) (\\w?)Wh",  
  23.                     System.Text.RegularExpressions.RegexOptions.Singleline);      
  24.     System.Text.RegularExpressions.Regex batteryRegex2 = new System.Text.RegularExpressions.Regex(  
  25.                     "Current Output: (\\d+\\.?\\d*) (\\w?)W",  
  26.                     System.Text.RegularExpressions.RegexOptions.Singleline);      
  27.     System.Text.RegularExpressions.Regex batteryRegex3 = new System.Text.RegularExpressions.Regex(  
  28.                     "(Fully depleted in: .*)",  
  29.                     System.Text.RegularExpressions.RegexOptions.Singleline);      
  30.     System.Text.RegularExpressions.Regex batteryRegex4 = new System.Text.RegularExpressions.Regex(  
  31.                     "(Fully recharged in: .*)",  
  32.                     System.Text.RegularExpressions.RegexOptions.Singleline);      
  33.     List<String> list = new List<String>();  
  34.     for (int i = 0; i < batteries.Count; i++)    
  35.     {    
  36.         double currentOutput = 0.0f;  
  37.         double maxStoredPower = 0.0f;  
  38.         double currentStoredPower = 0.0f;  
  39.         String currentStatus = "";
  40.         System.Text.RegularExpressions.Match match = batteryRegex.Match(batteries&#91;i].DetailedInfo);  
  41.         double parsedDouble;  
  42.         if (match.Success) {  
  43.             if (Double.TryParse(match.Groups&#91;1].Value, out parsedDouble))  
  44.                 maxStoredPower = parsedDouble * Math.Pow(1000.0, MULTIPLIERS.IndexOf(match.Groups&#91;2].Value));  
  45.        
  46.             if (Double.TryParse(match.Groups&#91;3].Value, out parsedDouble))  
  47.                 currentStoredPower = parsedDouble * Math.Pow(1000.0, MULTIPLIERS.IndexOf(match.Groups&#91;4].Value));  
  48.         }  
  49.         match = batteryRegex2.Match(batteries&#91;i].DetailedInfo);  
  50.         if (match.Success) {  
  51.             if (Double.TryParse(match.Groups&#91;1].Value, out parsedDouble))  
  52.                 currentOutput = parsedDouble * Math.Pow(1000.0, MULTIPLIERS.IndexOf(match.Groups&#91;2].Value));  
  53.         }  
  54.         match = batteryRegex3.Match(batteries&#91;i].DetailedInfo);  
  55.         if (match.Success) {  
  56.             currentStatus = match.Groups&#91;1].Value;
  57.         }  
  58.         match = batteryRegex4.Match(batteries&#91;i].DetailedInfo);  
  59.         if (match.Success) {  
  60.             currentStatus = match.Groups&#91;1].Value;
  61.         }  
  62.         list.Add("\"" + batteries&#91;i].CustomName + "\"");
  63.         list.Add("Power Level: " + Math.Round(currentStoredPower/maxStoredPower*100,2) + "%  ("
  64.                     + powerFormatter(currentStoredPower) + " / " + powerFormatter(maxStoredPower) + ")");  
  65.         list.Add("Current Output: " + outputFormatter(currentOutput));
  66.         list.Add(currentStatus);
  67.  
  68.         list.Add("------------------------------------------------------");  
  69.     }  
  70.     list.Insert(0,"------------------------------------------------------");  
  71.     list.Insert(0,"Battery Status");  
  72.     for (int o=0; o < lineOffset; o++) {  
  73.         String shiftedItem = list&#91;0];  
  74.         list.RemoveAt(0);  
  75.         list.Add(shiftedItem);  
  76.     }  
  77.     panel.WritePublicText(String.Join("\n",list.ToArray()), false);  
  78.    
  79.     panel.ShowTextureOnScreen();    
  80.     panel.ShowPublicTextOnScreen();  
  81.     if (list.Count > PANEL_LINES) {  
  82.         lineOffset++;  
  83.         if (list.Count - lineOffset < PANEL_LINES) {  
  84.            lineOffset = 0;  
  85.         }  
  86.     }  
  87. }  
  88.  
  89. String powerFormatter(double power) {  
  90.     int counter = 0;  
  91.     while (power > 1000.0) {  
  92.         power = power / 1000;  
  93.         counter++;  
  94.     }  
  95.     return "" + Math.Round((double)power,0).ToString("##0") + " " + MULTIPLIERS.Substring(counter,1) + "Wh";  
  96. }
  97.  
  98. String outputFormatter(double power) {  
  99.     int counter = 0;  
  100.     while (power > 1000.0) {  
  101.         power = power / 1000;  
  102.         counter++;  
  103.     }  
  104.     return "" + Math.Round((double)power,2).ToString("##0.00") + " " + MULTIPLIERS.Substring(counter,1) + "W";  
  105. }

Please rate this post

7 Responses

  1. […] Consolidated Refinery Status Assembler Status (unpublished, still tweaking) Consolidated Inventory – Improved (showing Refined Ore) Reactor Status Solar Status […]

  2. […] script below is similar to the Battery Status script. The difference is that this script handles groups of batteries as […]

  3. Detheagle666

    I cannot get this script to work, when ever I click “Check Script” in-game it pulls up 20-30 errors. I have noticed that this website appears to be un-updated, but I hope someone can tell me what I’m doing wrong/how to update and/or fix this script.

    • I haven’t had much time lately to update the site. I’ll check this script later, from home, but it’s probably the ModAPI changes that caused it to break.

      • Detheagle666

        Oh awesome. To be honest, I figured I would have to take this to reddit or something lol good to know the site isn’t dead. Yeah, most of the error seen to be syntax or misplacement… I think. My scripting experience has been almost entirely IF THEN text based, Pyhton and Ruby. C+ is a like chinese for me, I have no idea whats going on most of the time.

  4. Still doesnt work, brings up about 50 errors during compilation

  5. Thank you. Works perfectly!

Leave a Reply