Morphik08

Space Engineers - Hydrogen/Oxygen Tank Monitor

Aug 20th, 2016
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.86 KB | None | 0 0
  1. // 1. Replace the LCD_NAME value with a part of your LCD IDs  
  2. const string LCD_NAME = "LCD-Tank";
  3. // 2. Replace the LCD_NAME value with a part of your Oxygen/Hydrogen Tank ID that is unique  
  4. const string tank_TAG = "Tnk";
  5. // 3. Add to both names a '#' followed by a number between '000' and '999'  
  6. // 4. Run the program.  
  7.  
  8. void Main()  
  9. {  
  10.     List<IMyTerminalBlock> _screens = new List<IMyTerminalBlock>();  
  11.     GridTerminalSystem.SearchBlocksOfName (LCD_NAME, _screens);  
  12.      
  13.     const string imagePrefix1 = "Oxygen ";
  14.     const string imagePrefix2 = "Hydrogen ";  
  15.  
  16.     const string nonFunctionalImage = "Construction";
  17.     const string warningImage = "Danger";
  18.     const int roundpercent = 1;  
  19.      
  20.     for(int i = 0; i < _screens.Count; i++)  
  21.     {  
  22.         if(_screens [i] != null) {
  23.             List<IMyTerminalBlock> _tanks = new List<IMyTerminalBlock>();    
  24.             GridTerminalSystem.SearchBlocksOfName (tank_TAG, _tanks);    
  25.  
  26.             IMyTextPanel screen = (IMyTextPanel) _screens[i];  
  27.  
  28.             string _name = screen.CustomName;
  29.  
  30.             if(_name.Contains ("#")) {
  31.                 string _tankID = _name.Substring (_name.IndexOf ('#'), 4);
  32.  
  33.                 for(int a = 0; a < _tanks.Count; a++) {
  34.                     if(_tanks [a] != null) {
  35.                         if (_tanks [a].CustomName.Contains (_tankID)) {
  36.                             // Get current visible Image
  37.                             string _oldImage = screen.CurrentlyShownImage;
  38.                             string _imagename = screen.CurrentlyShownImage;
  39.  
  40.                             if (_tanks [a].IsFunctional) {
  41.                                 IMyOxygenTank tank = (IMyOxygenTank) _tanks [a];  
  42.                                 float currentLevel;  
  43.                                 if(tank != null) {  
  44.                                 if(tank.BlockDefinition.SubtypeId.Contains("Hydrogen")){
  45.                                     currentLevel = tank.GetOxygenLevel();    
  46.                                     var roundfill = (int)System.Math.Round(currentLevel * 100 / roundpercent) * roundpercent;    
  47.                                     _imagename = imagePrefix2 + roundfill.ToString("D3");
  48.  
  49.                                     if (!_tanks [a].IsWorking) {
  50.                                         // If new Image == null, load former image
  51.                                         if (screen.CurrentlyShownImage != warningImage) {
  52.                                             _imagename = warningImage;
  53.                                         }
  54.                                     }
  55.  
  56.                                     Echo("Tank " + _tankID + " - " + roundfill.ToString("D3") + "% - '" + _imagename + "'");
  57.                                 } else {
  58.                                     currentLevel = tank.GetOxygenLevel();    
  59.                                     var roundfill = (int)System.Math.Round(currentLevel * 100 / roundpercent) * roundpercent;      
  60.                                     _imagename = imagePrefix1 + roundfill.ToString("D3");  
  61.  
  62.                                     if (!_tanks [a].IsWorking) {  
  63.                                         // If new Image == null, load former image  
  64.                                         if (screen.CurrentlyShownImage != warningImage) {  
  65.                                             _imagename = warningImage;  
  66.                                         }  
  67.                                     }  
  68.  
  69.                                     Echo("Tank " + _tankID + " - " + roundfill.ToString("D3") + "% - '" + _imagename + "'");  
  70.                                 }
  71.                                
  72.  
  73.                                 }  
  74.                             } else {
  75.                                 // If new Image == null, load former image
  76.                                 if (screen.CurrentlyShownImage == warningImage) {
  77.                                     _imagename = nonFunctionalImage;
  78.                                 } else {
  79.                                     _imagename = warningImage;
  80.                                 }
  81.                             }
  82.  
  83.                             screen.ClearImagesFromSelection();
  84.                             screen.AddImageToSelection(_imagename);
  85.                              
  86.                             // Check if new Image does exist, else load former Image
  87.                             if (screen.CurrentlyShownImage == null) {
  88.                                 Echo (_tankID + " '" + screen.CurrentlyShownImage + "' does not exist!");
  89.                                 screen.AddImageToSelection(_oldImage);
  90.                             }
  91.  
  92.                             screen.ShowTextureOnScreen();
  93.                         }
  94.                     }
  95.                 }
  96.             }
  97.         }
  98.     }  
  99. }
Advertisement
Add Comment
Please, Sign In to add comment