Advertisement
Dillikins

Door Lights

Apr 1st, 2020
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.88 KB | None | 0 0
  1. private readonly StringBuilder _Output = new StringBuilder();
  2. private readonly bool _Setup = false;
  3. private readonly string _IniSection = "ess_door_lights";
  4.  
  5. private readonly List<IMyDoor> _Doors;
  6. private readonly List<IMyLightingBlock> _Lights;
  7.  
  8. /****/
  9. public Program()
  10. {
  11.     try
  12.     {
  13.         //
  14.         MyIni oConfig = this.GetCustomData(Me);
  15.  
  16.         // Doors
  17.         string sDoorsGroupName = oConfig.Get(_IniSection, "doors_group_name").ToString();
  18.         this._Doors = this.GetBlocksByTypeAndGroup<IMyDoor>(sDoorsGroupName);
  19.  
  20.         // Lights
  21.         string sLightsGroupName = oConfig.Get(_IniSection, "lights_group_name").ToString();
  22.         this._Lights = this.GetBlocksByTypeAndGroup<IMyLightingBlock>(sLightsGroupName);
  23.     }
  24.     catch (Exception oException)
  25.     {
  26.         this._Output.AppendLine("SETUP FAILED\n").AppendLine(oException.Message);
  27.         this.FlushOutput();
  28.         return;
  29.     }
  30.  
  31.     this._Setup = true;
  32.     Runtime.UpdateFrequency = UpdateFrequency.Update10;
  33. }
  34.  
  35. /****/
  36. public void Main()
  37. {
  38.     //
  39.     if (!this._Setup)
  40.     {
  41.         this._Output.AppendLine("SETUP FAILED");
  42.         this.FlushOutput();
  43.         return;
  44.     }
  45.  
  46.     //
  47.     this._Output.Clear();
  48.  
  49.     // Output some debug
  50.     Random oRandom = new Random();
  51.     this._Output.AppendLine("Random: " + oRandom.Next());
  52.     this._Output.AppendLine("Doors: " + this._Doors.Count);
  53.     this._Output.AppendLine("Lights: " + this._Lights.Count);
  54.  
  55.     // Check if a door is open
  56.     bool bLightsOn = false;
  57.     foreach (IMyDoor oDoor in this._Doors)
  58.     {
  59.         if (oDoor.Status == DoorStatus.Open || oDoor.Status == DoorStatus.Opening)
  60.         {
  61.             bLightsOn = true;
  62.         }
  63.     }
  64.  
  65.     // Toggle lights
  66.     foreach (IMyLightingBlock oLight in this._Lights)
  67.     {
  68.         oLight.Enabled = bLightsOn;
  69.     }
  70.  
  71.     //
  72.     this.FlushOutput();
  73. }
  74.  
  75. /**
  76.     * Return all blocks of a specific type within a group
  77.     * @param        sGroupName      The name of the block group
  78.     * @return       List<T>         A list of blocks found
  79.     */
  80. private List<T> GetBlocksByTypeAndGroup<T>(string sGroupName) where T : class
  81. {
  82.     List<T> oBlocks = new List<T>();
  83.  
  84.     IMyBlockGroup oGroup = GridTerminalSystem.GetBlockGroupWithName(sGroupName);
  85.     if (oGroup == null)
  86.     {
  87.         throw new Exception(sGroupName + " group missing");
  88.     }
  89.  
  90.     oGroup.GetBlocksOfType<T>(oBlocks, oBlock => {
  91.         //IMyTerminalBlock oCastBlock = oBlock as IMyTerminalBlock;
  92.  
  93.         // Ignore different grids
  94.         //if (oCastBlock.CubeGrid != oCubeGrid) return false;
  95.  
  96.         return true;
  97.     });
  98.  
  99.     return oBlocks;
  100. }
  101.  
  102. /****/
  103. private MyIni GetCustomData(IMyTerminalBlock oBlock)
  104. {
  105.     MyIni oConfig = new MyIni();
  106.     MyIniParseResult oParseResult;
  107.     if (!oConfig.TryParse(oBlock.CustomData, out oParseResult))
  108.     {
  109.         throw new Exception("Failed to parse custom data");
  110.     }
  111.     return oConfig;
  112. }
  113.  
  114. /****/
  115. private void FlushOutput()
  116. {
  117.     //
  118.     Echo(this._Output.ToString());
  119.  
  120.     //
  121.     IMyTextSurface oSurface = Me.GetSurface(0);
  122.     oSurface.ContentType = ContentType.TEXT_AND_IMAGE;
  123.     oSurface.WriteText(this._Output.ToString());
  124.  
  125.     //
  126.     this._Output.Clear();
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement