Solar Status 5/5 (3)

  1. const String PANEL_NAME = "Solar LCD 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> solarpanels = new List<IMyTerminalBlock>();  
  10.     GridTerminalSystem.GetBlocksOfType<IMySolarPanel>(solarpanels);    
  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 solarRegex = new System.Text.RegularExpressions.Regex(  
  22.                     "Max Output: (\\d+\\.?\\d*) (\\w?)W.*Current Output: (\\d+\\.?\\d*) (\\w?)W",  
  23.                     System.Text.RegularExpressions.RegexOptions.Singleline);      
  24.     List<String> list = new List<String>();  
  25.     for (int i = 0; i < solarpanels.Count; i++)    
  26.     {    
  27.         double currentOutput = 0.0f;  
  28.         double maxOutput = 0.0f;  
  29.         System.Text.RegularExpressions.Match match = solarRegex.Match(solarpanels&#91;i].DetailedInfo);  
  30.         double parsedDouble;  
  31.         if (match.Success) {  
  32.             if (Double.TryParse(match.Groups&#91;1].Value, out parsedDouble))  
  33.                 maxOutput = parsedDouble * Math.Pow(1000.0, MULTIPLIERS.IndexOf(match.Groups&#91;2].Value));  
  34.        
  35.             if (Double.TryParse(match.Groups&#91;3].Value, out parsedDouble))  
  36.                 currentOutput = parsedDouble * Math.Pow(1000.0, MULTIPLIERS.IndexOf(match.Groups&#91;4].Value));  
  37.         }  
  38.         list.Add("\"" + solarpanels&#91;i].CustomName + "\"");
  39.         list.Add("Current Output: " + outputFormatter(currentOutput));
  40.  
  41.         list.Add("------------------------------------------------------");  
  42.     }  
  43.     list.Insert(0,"------------------------------------------------------");  
  44.     list.Insert(0,"Solar Panel Status");  
  45.     for (int o=0; o < lineOffset; o++) {  
  46.         String shiftedItem = list&#91;0];  
  47.         list.RemoveAt(0);  
  48.         list.Add(shiftedItem);  
  49.     }  
  50.     panel.WritePublicText(String.Join("\n",list.ToArray()), false);  
  51.    
  52.     panel.ShowTextureOnScreen();    
  53.     panel.ShowPublicTextOnScreen();  
  54.     if (list.Count > PANEL_LINES) {  
  55.         lineOffset++;  
  56.         if (list.Count - lineOffset < PANEL_LINES) {  
  57.            lineOffset = 0;  
  58.         }  
  59.     }  
  60. }  
  61.  
  62. String powerFormatter(double power) {  
  63.     int counter = 0;  
  64.     while (power > 1000.0) {  
  65.         power = power / 1000;  
  66.         counter++;  
  67.     }  
  68.     return "" + Math.Round((double)power,0).ToString("##0") + " " + MULTIPLIERS.Substring(counter,1) + "Wh";  
  69. }
  70.  
  71. String outputFormatter(double power) {  
  72.     int counter = 0;  
  73.     while (power > 1000.0) {  
  74.         power = power / 1000;  
  75.         counter++;  
  76.     }  
  77.     return "" + Math.Round((double)power,2).ToString("##0.00") + " " + MULTIPLIERS.Substring(counter,1) + "W";  
  78. }

Please rate this post

7 Responses

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

  2. This one is not working. I have 6 solar panels looking at sun, each produces 66 kW. I set timer to execute code each 1 sec, but LCD panel says that output is 0.0 kW.

  3. SantosLHalper

    Just taking a quick look at the code, it looks like all the left square brackets were transposed to ‘[’ (minus the bounding ‘s, of course), so you should be able to do a text replace and change those characters to a left square bracket. See if that gets it working.

    • Yup. Sorry about that. The program I *was* using to do syntax highlighting lost its marbles and did all sorts of crazy stuff to my code.

  4. NugunsKnight

    I have tried playing with your code so that it will scroll two lines instead of one but have been unsuccessful. I was looking to use this on the corner LCD which has two lines available. Your pointers would be appreciated.

    • I believe if you take lines 46 & 47, and duplicate them, like this:

      String shiftedItem = list[0];
      list.RemoveAt(0);
      String shiftedItem = list[0];
      list.RemoveAt(0);

      That should do the trick… let me know if it doesn’t… and I’ll try it when I’m in game (not in game right now)

Leave a Reply