Battery “Bank” Status on LCD Panel 5/5 (3)

The script below is similar to the Battery Status script. The difference is that this script handles groups of batteries as “banks”.

It was written in response to a comment on Reddit where the commenter had a ship with 108 batteries divided into 4 banks of 27 batteries each.

The variables at the beginning of the script: numberOfBanks, batteriesPerBank and prefix should be set according to your naming convention, but the script assumes that the prefix is follow by the bank number, a dash, and the battery number, so in my example, the batteries should be named “Battery Bank 1-1” through “Battery Bank 1-27”, “Battery Bank 2-1” through “Battery Bank 2-27”, “Battery Bank 3-1” through “Battery Bank 3-27” and “Battery Bank 4-1” through “Battery Bank 4-27”.

If there are any gaps in the numbering system, it is likely the script will fail, but I can add error handling if you need it. I can also change the script to use the UI’s grouping mechanism instead of relying on individual block names. It’s really just a matter of choice of how you want to do things.

Let me know if you need any changes!

const String PANEL_NAME = "Battery Bank Panel"; 
const String MULTIPLIERS = ".kMGTPEZY" ; 
const int PANEL_LINES = 22; 
 
int numberOfBanks = 4;
int batteriesPerBank = 27;
const String prefix = "Battery Bank ";
 
void Main() 
{ 
    List<IMyTerminalBlock> work = new List<IMyTerminalBlock>(); 
    GridTerminalSystem.SearchBlocksOfName(PANEL_NAME, work); 
    IMyTextPanel panel = null; 
    for (int i = 0; i < work.Count; i++) 
    { 
        if (work[i] is IMyTextPanel) { 
            panel = (IMyTextPanel)work[i]; 
            break; 
        } 
    } 
 
    System.Text.RegularExpressions.Regex batteryRegex = new System.Text.RegularExpressions.Regex( 
        "Max Stored Power: (\\d+\\.?\\d*) (\\w?)Wh.*Stored power: (\\d+\\.?\\d*) (\\w?)Wh", 
        System.Text.RegularExpressions.RegexOptions.Singleline); 
    List<String> list = new List<String>(); 
    String line = "";
    for (int i = 1; i <= numberOfBanks; i++) 
    { 
        if (!line.Equals("")) {
            list.Add(line);
            line = "";
        }
        list.Add(" ");
        line = "Bank " + i + " -> ";
        double maxStoredPower = 0.0f; 
        double currentStoredPower = 0.0f; 
 
        for (int j = 1; j <= batteriesPerBank; j++) 
        { 
            IMyBatteryBlock batt = GridTerminalSystem.GetBlockWithName(prefix + i + "-" + j) as IMyBatteryBlock;
            System.Text.RegularExpressions.Match match = batteryRegex.Match(batt.DetailedInfo); 
            double parsedDouble; 
            if (match.Success) { 
                if (Double.TryParse(match.Groups[1].Value, out parsedDouble)) 
                    maxStoredPower = parsedDouble * Math.Pow(1000.0, MULTIPLIERS.IndexOf(match.Groups[2].Value)); 
 
                if (Double.TryParse(match.Groups[3].Value, out parsedDouble)) 
                    currentStoredPower = parsedDouble * Math.Pow(1000.0, MULTIPLIERS.IndexOf(match.Groups[4].Value)); 
            } 
            if (line.Length < 39) {
                line += Math.Round(currentStoredPower/maxStoredPower*100,0) + "% ";
            } else {
                list.Add(line);
                line = "";
                line = " " + Math.Round(currentStoredPower/maxStoredPower*100,0) + "% ";
            }
        }
    } 
    if (!line.Equals("")) {
        list.Add(line);
        line = "";
    }
    panel.WritePublicText(String.Join("\n",list.ToArray()), false); 
    panel.ShowTextureOnScreen(); 
    panel.ShowPublicTextOnScreen(); 
} 
 
   
 

Please rate this post

3 Responses

  1. TheBarret

    The concept is fine, but the way you write it is not good.
    You compact the script syntax is a bad practise, leaves no space for debugging or tracing the problem.

    I suggest you use more lines.
    P.s. I am a programmer myself, if you doubt me, contact me on steam using this nickname.

    • I didn’t write the code like that. 🙂

      The plugin I use to do syntax highlighting, for some reason freaked out (on a bunch of my posts) and wasn’t highlighting the code properly. It’s now highlighting it, but it screwed up all my indentation… *sigh*

  2. Does not work for me at all. followed to the letter and messed with it a bit. all other things on this site seem to work, this one seems dead

Leave a Reply