Advertisement
Guest User

Wolololooo

a guest
Dec 1st, 2015
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.84 KB | None | 0 0
  1. void Main(string argument)        
  2. {        
  3.     bool error = false;        
  4.     int tankLevel = 0;        
  5.     String text = "";        
  6.            
  7.     IMyTextPanel     panel = (IMyTextPanel) GridTerminalSystem.GetBlockWithName("Panel");        
  8.        
  9.     /*var tanks = new List<IMyTerminalBlock>();        
  10.     GridTerminalSystem.GetBlocksOfType<IMyOxygenTank>(tanks);*/        
  11.        
  12.     var bats = new List<IMyTerminalBlock>();      
  13.     GridTerminalSystem.GetBlocksOfType<IMyBatteryBlock>(bats);        
  14.        
  15.     panel.WritePublicText("  -----------------------------------------\n    meQa Planetary Station #01\n  -----------------------------------------", false);        
  16.            
  17.     /*if (tanks.Count == 0) error = true;        
  18.     else {        
  19.         float totalLevel = 0;        
  20.             for (int i = 0; i < tanks.Count; i++) {        
  21.                 totalLevel += (tanks[i] as IMyOxygenTank).GetOxygenLevel();        
  22.             }      
  23.         tankLevel = (int) ((totalLevel/tanks.Count)*100);        
  24.         text += "\n  - Hydrogen Level: " + tankLevel + "%";      
  25.     }*/        
  26.     if (bats.Count == 0) error = true;        
  27.     else {      
  28.         double totalPower = .0;
  29.         double maxStoredPower = .0;    
  30.         for (int i = 0; i < bats.Count; i++) {
  31.             BatteryInfo batInfo = new BatteryInfo((bats[i] as IMyBatteryBlock));    
  32.             totalPower += batInfo.GetPowerLevel();
  33.             maxStoredPower += batInfo.GetMaxStoredPower();  
  34.         }    
  35.         text += "\n  - Battery Level: " + BatteryInfo.PowerToStringUnit(totalPower) + " of max " + BatteryInfo.PowerToStringUnit(maxStoredPower);  
  36.         text += "\n  - Number of Battteries: " + bats.Count;    
  37.     }      
  38.        
  39.     if(error) panel.WritePublicText("ERROR - ERROR - ERROR", true);        
  40.     else panel.WritePublicText(text, true);        
  41.        
  42. }  
  43.    
  44. public class BatteryInfo    
  45. {      
  46.     private String[] info;    
  47.     private double PowerLevel;    
  48.     private double PowerIn;    
  49.     private double PowerOut;    
  50.     private double MaxStoredPower;    
  51.    
  52.     public BatteryInfo(IMyBatteryBlock bat)    
  53.     {    
  54.         info = bat.DetailedInfo.Split(new Char[]{'\n'}, StringSplitOptions.RemoveEmptyEntries);    
  55.    
  56.         MaxStoredPower  = findDoubleInLine(3);    
  57.         PowerOut        = findDoubleInLine(4);    
  58.         PowerIn         = findDoubleInLine(5);    
  59.         PowerLevel      = findDoubleInLine(6);    
  60.     }    
  61.    
  62.     private double findDoubleInLine(int n){    
  63.         System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex(@"\d+(.\d+)?");    
  64.         System.Text.RegularExpressions.Match m = r.Match(info[n]);    
  65.         if (m.Success)    
  66.         {      
  67.             double v = (double) Convert.ToDecimal(m.Value);    
  68.             switch ( info[6].Substring( info[6].Length-3 )) {    
  69.                 case " Wh": break;    
  70.                 case "kWh": v = v*1000.0; break;    
  71.                 case "MWh": v = v*1000000.0; break;    
  72.             }    
  73.             return v;    
  74.         }else{      
  75.             return .0;      
  76.         }    
  77.     }    
  78.      public static String PowerToStringUnit(double v){  
  79.         if( v >= 1000000.0){    
  80.             return Math.Round(v / 1000000.0 , 2)+ " MWh";    
  81.         } else if( v >= 1000.0 ){    
  82.             return Math.Round(v / 1000.0 , 2)  + " kWh";    
  83.         } else {    
  84.             return Math.Round(v , 2)  + " Wh";  
  85.         }    
  86.     }  
  87.    
  88.     public double GetMaxStoredPower()   { return this.MaxStoredPower; }  
  89.     public double GetPowerOut()         { return this.PowerOut; }  
  90.     public double GetPowerIn()          { return this.PowerIn; }  
  91.     public double GetPowerLevel()       { return this.PowerLevel; }  
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement