Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Jump Drive Charge With Images by Morphik
- Special Thanks to d4rky1989 on the KSH forums for helping me.
- Display the percentage charge average of all Jump Drives on the grid. */
- /* To use:
- 1. Place or have any number of Jump Drives you want on your ship.
- 2. Set up a program block with this script (You already have if you see this.)
- 3. Set up a LCD/Text Panel and name it whatever you want.
- 4. Set up a timer block set to 1 second to run this program and itself.
- 5. Start the Timer.
- NOTE: If you get the error: "Attempted to Divide by Zero" this means you need to check the ownership
- of the Jump Drive and the Programmable Block, they need to be the same.
- NOTE: If you add any jump drives to the grid after the program is already running,
- you will have to recompile the script.
- */
- // Change 'LCD Jump' to the name of the LCD you want the textures to show on.
- const string LCD_NAME = "LCD Jump";
- //** No need to edit below this line **
- int _lastIndex = -1;
- void Main()
- {
- var jumpDrives = new List<IMyTerminalBlock>();
- GridTerminalSystem.GetBlocksOfType<IMyJumpDrive>(jumpDrives, b => b.CubeGrid == Me.CubeGrid);
- var screens = new List<IMyTerminalBlock>();
- GridTerminalSystem.GetBlocksOfType<IMyTextPanel>(screens, b => b.CubeGrid == Me.CubeGrid);
- for(int i = 0; i < screens.Count; i++) {
- IMyTextPanel jumpLcd = (IMyTextPanel) screens[i];
- if(jumpLcd.CustomName.IndexOf(LCD_NAME) == 0) {
- Decimal totalMaxPower = new Decimal (0);
- Decimal totalPower = new Decimal (0);
- jumpDrives.ForEach (drive => {
- var detailedInfoArray = drive.DetailedInfo.Split (':');
- totalPower += GetNum (detailedInfoArray[5]);
- totalMaxPower += GetNum (detailedInfoArray[3]);
- });
- Decimal chargePercentage = totalPower * 100 / totalMaxPower;
- // Debug Info
- jumpLcd.WritePublicText ( string.Format (
- " Number of jump drives: {0}"
- + "\n\n Max. power: {1} W"
- + "\n\n Current power: {2} W"
- + "\n\n Percentage: {3} %"
- + "\n\n Test: " + chargePercentage.ToString ("000"),
- jumpDrives.Count, totalMaxPower.ToString ("#"),
- totalPower.ToString ("#"), chargePercentage.ToString ("#.00")));
- int index = (int)(chargePercentage / 5);
- if(index > 20 || index < 0)
- return; Echo ("Program Running");
- if(index == _lastIndex)
- return;
- jumpLcd.ClearImagesFromSelection();
- jumpLcd.AddImageToSelection(string.Format("Jump {0}",(index*5).ToString("000")));
- }
- }
- }
- public static Decimal GetNum (string strValue) {
- const string regExpr = @"(?<Num>[0-9.]+) (?<Unit>[a-zA-Z]+)";
- var match = System.Text.RegularExpressions.Regex.Match (strValue, regExpr);
- if (! match.Success)
- throw new Exception ("Input has an invalid format");
- return Decimal.Parse (match.Groups["Num"].Value) * UnitToDecimal (match.Groups["Unit"].Value);
- }
- public static Decimal UnitToDecimal (string unit) {
- switch (unit[0]) {
- case 'k': return new Decimal (10e3);
- case 'M': return new Decimal (10e6);
- default: return new Decimal (1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment