Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private readonly StringBuilder _Output = new StringBuilder();
- private readonly bool _Setup = false;
- private readonly string _IniSection = "ess_door_lights";
- private readonly List<IMyDoor> _Doors;
- private readonly List<IMyLightingBlock> _Lights;
- /****/
- public Program()
- {
- try
- {
- //
- MyIni oConfig = this.GetCustomData(Me);
- // Doors
- string sDoorsGroupName = oConfig.Get(_IniSection, "doors_group_name").ToString();
- this._Doors = this.GetBlocksByTypeAndGroup<IMyDoor>(sDoorsGroupName);
- // Lights
- string sLightsGroupName = oConfig.Get(_IniSection, "lights_group_name").ToString();
- this._Lights = this.GetBlocksByTypeAndGroup<IMyLightingBlock>(sLightsGroupName);
- }
- catch (Exception oException)
- {
- this._Output.AppendLine("SETUP FAILED\n").AppendLine(oException.Message);
- this.FlushOutput();
- return;
- }
- this._Setup = true;
- Runtime.UpdateFrequency = UpdateFrequency.Update10;
- }
- /****/
- public void Main()
- {
- //
- if (!this._Setup)
- {
- this._Output.AppendLine("SETUP FAILED");
- this.FlushOutput();
- return;
- }
- //
- this._Output.Clear();
- // Output some debug
- Random oRandom = new Random();
- this._Output.AppendLine("Random: " + oRandom.Next());
- this._Output.AppendLine("Doors: " + this._Doors.Count);
- this._Output.AppendLine("Lights: " + this._Lights.Count);
- // Check if a door is open
- bool bLightsOn = false;
- foreach (IMyDoor oDoor in this._Doors)
- {
- if (oDoor.Status == DoorStatus.Open || oDoor.Status == DoorStatus.Opening)
- {
- bLightsOn = true;
- }
- }
- // Toggle lights
- foreach (IMyLightingBlock oLight in this._Lights)
- {
- oLight.Enabled = bLightsOn;
- }
- //
- this.FlushOutput();
- }
- /**
- * Return all blocks of a specific type within a group
- * @param sGroupName The name of the block group
- * @return List<T> A list of blocks found
- */
- private List<T> GetBlocksByTypeAndGroup<T>(string sGroupName) where T : class
- {
- List<T> oBlocks = new List<T>();
- IMyBlockGroup oGroup = GridTerminalSystem.GetBlockGroupWithName(sGroupName);
- if (oGroup == null)
- {
- throw new Exception(sGroupName + " group missing");
- }
- oGroup.GetBlocksOfType<T>(oBlocks, oBlock => {
- //IMyTerminalBlock oCastBlock = oBlock as IMyTerminalBlock;
- // Ignore different grids
- //if (oCastBlock.CubeGrid != oCubeGrid) return false;
- return true;
- });
- return oBlocks;
- }
- /****/
- private MyIni GetCustomData(IMyTerminalBlock oBlock)
- {
- MyIni oConfig = new MyIni();
- MyIniParseResult oParseResult;
- if (!oConfig.TryParse(oBlock.CustomData, out oParseResult))
- {
- throw new Exception("Failed to parse custom data");
- }
- return oConfig;
- }
- /****/
- private void FlushOutput()
- {
- //
- Echo(this._Output.ToString());
- //
- IMyTextSurface oSurface = Me.GetSurface(0);
- oSurface.ContentType = ContentType.TEXT_AND_IMAGE;
- oSurface.WriteText(this._Output.ToString());
- //
- this._Output.Clear();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement