Morphik08

Space Engineers - Jump Drive charge with Images

Jan 19th, 2016
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.70 KB | None | 0 0
  1. /*  Jump Drive Charge With Images by Morphik  
  2.     Special Thanks to d4rky1989 on the KSH forums for helping me.  
  3.    
  4.     Display the percentage charge average of all Jump Drives on the grid. */  
  5.    
  6. /* To use:  
  7.     1. Place or have any number of Jump Drives you want on your ship.  
  8.     2. Set up a program block with this script (You already have if you see this.)  
  9.     3. Set up a LCD/Text Panel and name it whatever you want.  
  10.     4. Set up a timer block set to 1 second to run this program and itself.  
  11.     5. Start the Timer.  
  12.    
  13.     NOTE: If you get the error: "Attempted to Divide by Zero" this means you need to check the ownership
  14.                 of the Jump Drive and the Programmable Block, they need to be the same.
  15.  
  16.     NOTE: If you add any jump drives to the grid after the program is already running,  
  17.                 you will have to recompile the script.  
  18.     */  
  19.    
  20. // Change 'LCD Jump' to the name of the LCD you want the textures to show on.  
  21. const string LCD_NAME = "LCD Jump";  
  22.    
  23. //** No need to edit below this line **    
  24. int _lastIndex = -1;      
  25.      
  26. void Main()    
  27. {    
  28.     var jumpDrives = new List<IMyTerminalBlock>();    
  29.     GridTerminalSystem.GetBlocksOfType<IMyJumpDrive>(jumpDrives, b => b.CubeGrid == Me.CubeGrid);    
  30.  
  31.     var screens = new List<IMyTerminalBlock>();  
  32.     GridTerminalSystem.GetBlocksOfType<IMyTextPanel>(screens, b => b.CubeGrid == Me.CubeGrid);
  33.      
  34.     for(int i = 0; i < screens.Count; i++) {  
  35.         IMyTextPanel jumpLcd = (IMyTextPanel) screens[i];  
  36.         if(jumpLcd.CustomName.IndexOf(LCD_NAME) == 0) {  
  37.             Decimal totalMaxPower = new Decimal (0);    
  38.             Decimal totalPower = new Decimal (0);  
  39.      
  40.             jumpDrives.ForEach (drive => {    
  41.                 var detailedInfoArray = drive.DetailedInfo.Split (':');    
  42.                 totalPower += GetNum (detailedInfoArray[5]);    
  43.                 totalMaxPower += GetNum (detailedInfoArray[3]);    
  44.             });    
  45.      
  46.             Decimal chargePercentage = totalPower * 100 / totalMaxPower;    
  47.      
  48.             // Debug Info  
  49.             jumpLcd.WritePublicText ( string.Format (    
  50.             "  Number of jump drives: {0}"    
  51.             + "\n\n  Max. power: {1} W"    
  52.             + "\n\n  Current power: {2} W"    
  53.             + "\n\n  Percentage: {3} %"  
  54.             + "\n\n  Test: " + chargePercentage.ToString ("000"),    
  55.             jumpDrives.Count, totalMaxPower.ToString ("#"),    
  56.             totalPower.ToString ("#"), chargePercentage.ToString ("#.00")));    
  57.      
  58.             int index = (int)(chargePercentage / 5);      
  59.                 if(index > 20 || index < 0)      
  60.                     return; Echo ("Program Running");      
  61.                 if(index == _lastIndex)      
  62.                     return;      
  63.             jumpLcd.ClearImagesFromSelection();      
  64.             jumpLcd.AddImageToSelection(string.Format("Jump {0}",(index*5).ToString("000")));  
  65.         }  
  66.     }
  67.  
  68. }  
  69.      
  70. public static Decimal GetNum (string strValue) {      
  71.     const string regExpr = @"(?<Num>[0-9.]+) (?<Unit>[a-zA-Z]+)";      
  72.     var match = System.Text.RegularExpressions.Regex.Match (strValue, regExpr);      
  73.     if (! match.Success)      
  74.         throw new Exception ("Input has an invalid format");      
  75.     return Decimal.Parse (match.Groups["Num"].Value) * UnitToDecimal (match.Groups["Unit"].Value);      
  76. }    
  77.      
  78. public static Decimal UnitToDecimal (string unit) {    
  79.     switch (unit[0]) {    
  80.     case 'k': return new Decimal (10e3);    
  81.     case 'M': return new Decimal (10e6);    
  82.     default: return new Decimal (1);    
  83.     }    
  84. }
Advertisement
Add Comment
Please, Sign In to add comment