public override void UpdateBeforeSimulation100()
        {
            var terminalEntity = (IMyTerminalBlock)Entity;

            if (string.IsNullOrEmpty(terminalEntity.CustomName) || !terminalEntity.ShowOnHUD)
            {
                return;
            }

            try
            {
                if (terminalEntity.CustomName.IndexOf("Az:", StringComparison.InvariantCultureIgnoreCase) >= 0 ||
                    terminalEntity.CustomName.IndexOf("El:", StringComparison.InvariantCultureIgnoreCase) >= 0)
                {
                    var definitionPanelOrientation = new Vector3(0, 0, -1);

                    // Need to rotate the worldmatrix to match the definition of the solar panel.
                    //Matrix matrix = Entity.WorldMatrix;
                    //Matrix matrix2 = Matrix.CreateFromDir(matrix.Forward, matrix.Up);
                    //var fix = Vector3.Normalize(Vector3.Transform(definitionPanelOrientation, matrix.GetOrientation()));
                    //float num = matrix.Forward.Dot(fix);

                    var ang = GetRotationAngle(Entity.WorldMatrix, Support.GetSunDirection());
                    //MyAPIGateway.Utilities.ShowMessage("Panel offset", String.Format("{0} {1}", ang.X, ang.Y));

                    _ignoreNameChange = true;

                    var match = Regex.Match(terminalEntity.CustomName, @"(?<A>(.*\s{1,}|^)Az:)(?<B>[+-]?((\d+(\.\d*)?)|(\.\d+)))(?<C>(\s{1,}.*|$))", RegexOptions.IgnoreCase);
                    if (match.Success)
                    {
                        terminalEntity.CustomName = string.Format("{0}{1:0.00}{2}", match.Groups["A"].Value, ang.Y, match.Groups["C"].Value);
                    }

                    match = Regex.Match(terminalEntity.CustomName, @"(?<A>(.*\s{1,}|^)El:)(?<B>[+-]?((\d+(\.\d*)?)|(\.\d+)))(?<C>(\s{1,}.*|$))", RegexOptions.IgnoreCase);
                    if (match.Success)
                    {
                        terminalEntity.CustomName = string.Format("{0}{1:0.00}{2}", match.Groups["A"].Value, ang.X, match.Groups["C"].Value);
                    }

                    _ignoreNameChange = false;
                }
            }
            catch (Exception ex)
            {
                var message = ex.Message.Replace("\r", " ").Replace("\n", " ");
                message = message.Substring(0, Math.Min(message.Length, 50));
                MyAPIGateway.Utilities.ShowMessage("Error", String.Format("{0}", message));
            }
        }
Ejemplo n.º 2
0
        public override void UpdateAfterSimulation()
        {
            Initilize();

            if (!_isInitialized)
            {
                return;
            }

            _frameCounter++;
            if (_frameCounter < 30)
            {
                return;
            }

            _frameCounter = 0;

            _debug = ((IMyTerminalBlock)Entity).IsWorking && ((IMyTerminalBlock)Entity).ShowOnHUD;

            if (!string.IsNullOrEmpty(_exceptionInfo))
            {
                WriteDebug("Info", _exceptionInfo);
            }

            if (!_isIsValid)
            {
                return;
            }

            if (!_autoTrackOn)
            {
                return;
            }

            var terminalEntity = (IMyTerminalBlock)Entity;

            if (string.IsNullOrEmpty(terminalEntity.CustomName) || !terminalEntity.IsWorking || !terminalEntity.IsFunctional)
            {
                return;
            }

            //WriteDebug("UpdateAfterSimulation", "Start = {0} {1} {2} {3}", _isInitialized, _isIsValid, _autoTrackOn, _attachedRotors.Count);

            Vector2 ang = Vector2.Zero;

            // TODO: run as background, if I can find a suitable world to test it on FIRST!

            MyAPIGateway.Parallel.Start(delegate
                                        // Background processing occurs within this block.
            {
                try
                {
                    var sunDirection = Support.GetSunDirection();
                    //ang = GetRotationAngle(Entity.WorldMatrix, sunDirection);
                    ang = GetRotationAngle(Entity.WorldMatrix.Forward, Entity.WorldMatrix.Up, Entity.WorldMatrix.Right, sunDirection);
                }
                catch (Exception ex)
                {
                }
            },
                                        // when the background processing is finished, this block will run foreground.
                                        delegate {
                try
                {
                    // Check Ownership.
                    var block = (IMyCubeBlock)Entity;

                    //WriteDebug("Panel offset", "{0} {1}", ang.X, ang.Y);

                    // ang.Y Azimuth  Yaw.
                    // any.X Elevation  Pitch.

                    if (_attachedRotors.Count >= 2)
                    {
                        var rotorBase = _attachedRotors[0] as IMyTerminalBlock;
                        if (rotorBase == null)
                        {
                            rotorBase = _attachedRotors[1] as IMyTerminalBlock;
                        }

                        if (rotorBase != null && !rotorBase.Closed && rotorBase.HasPlayerAccess(block.OwnerId))
                        {
                            //WriteDebug("Turn", "'{0}'", rotorBase.CustomName);
                            TurnRotor(_attachedRotors[0], _attachedRotors[1], ang, _rotorDirections[0]);
                        }
                    }
                    if (_attachedRotors.Count >= 3)
                    {
                        var rotorBase = _attachedRotors[2] as IMyTerminalBlock;
                        if (rotorBase == null)
                        {
                            rotorBase = _attachedRotors[3] as IMyTerminalBlock;
                        }

                        if (rotorBase != null && !rotorBase.Closed && rotorBase.HasPlayerAccess(block.OwnerId))
                        {
                            //WriteDebug("Turn", "'{0}'", rotorBase.CustomName);
                            TurnRotor(_attachedRotors[2], _attachedRotors[3], ang, _rotorDirections[1]);
                        }
                    }
                }
                catch (Exception ex)
                {
                    var message = ex.Message.Replace("\r", " ").Replace("\n", " ");
                    message     = message.Substring(0, Math.Min(message.Length, 100));
                    MyAPIGateway.Utilities.ShowMessage("Error", string.Format("{0}", message));
                }
            });
        }