Exemple #1
0
        /// <summary>
        /// Called on physics update. Determine if we should issue a warning.
        /// </summary>
        public void FixedUpdate()
        {
            if (vessel == null)
            {
                return;
            }

            double ut = Planetarium.GetUniversalTime();

            radarAltitude = FlightHistSample.GetRadarAltitude(vessel);
            if (ut - _prevTime >= SAMPLE_INTERVAL)
            {
                double deltaTime = ut - _prevTime;
                _prevTime = ut;
                FlightHistSample sample = new FlightHistSample(vessel);
                if (fltrRadar && radarAltitude < warnAltitudeMax * 1.2f && vessel.situation != Vessel.Situations.LANDED)
                {
                    part.RequestResource("ElectricCharge", deltaTime * fltrChargeRate);
                    sample.RadarContact = ForwardRadar(Math.PI / 4);
                }
                radarDistance = sample.RadarContact;
                _flightHist.Add(sample);

                while (_flightHist.Count > 2 && (sample.UT - _flightHist [0].UT) > sampleWindow)
                {
                    _flightHist.RemoveAt(0);
                }
            }
            if (_flightHist.Count < 2)
            {
                return;
            }

            FlightHistSample oldest = _flightHist [0];
            FlightHistSample newest = _flightHist [_flightHist.Count - 1];

            double descentRate = (oldest.RadarHeight - newest.RadarHeight) / (newest.UT - oldest.UT);

            if (descentRate < -vessel.verticalSpeed)
            {
                descentRate = -vessel.verticalSpeed;
            }
            measuredDescentRate = (float)descentRate;

            bool overHeight = newest.RadarHeight > warnAltitudeMax && newest.RadarContact > warnAltitudeMax;

            double tolerance = 1.0d;
            bool   gearDown  = IsGearDown();

            if (gearDown)
            {
                tolerance = (double)landingTolerance;
            }

            double descentRateThreshold = (warnApproachVelMax - warnApproachVelMin) * (newest.RadarHeight / warnAltitudeMax) * tolerance +
                                          warnApproachVelMin;
            double approachRateThreshold = 0.0d;

            if (!float.IsNaN(newest.RadarContact))
            {
                approachRateThreshold = (warnApproachVelMax - warnApproachVelMin) * (newest.RadarContact / warnAltitudeMax) * tolerance +
                                        warnApproachVelMin;
            }

            if (!overHeight && descentRate > descentRateThreshold)
            {
                if (!_warningActive)
                {
                    _warningActive = true;
                    _warningTime   = ut;
                    Debug.Log("TAWS - Descent Rate:" + descentRate + " Gear Down:" + gearDown);
                }
                TerrainWarning();
            }
            else if (!overHeight && !float.IsNaN(newest.RadarContact) && newest.RadarContact < warnAltitudeMax &&
                     Vector3.Dot(vessel.srf_velocity, _transform.forward) > approachRateThreshold)
            {
                if (!_warningActive)
                {
                    _warningActive = true;
                    _warningTime   = ut;
                    Debug.Log("TAWS - Approach Rate:" + Vector3.Dot(vessel.srf_velocity, _transform.forward) + " Gear Down:" + gearDown);
                }
                TerrainWarning();
            }
            else
            {
                _playing = null;
                _audioSource.Stop();
                _warningActive = false;
                if (_warningActiveModule == this)
                {
                    _warningActiveModule = null;
                }
            }
        }
        /// <summary>
        /// Called on physics update. Determine if we should issue a warning.
        /// </summary>
        public void FixedUpdate()
        {
            if (vessel == null) {
                return;
            }

            double ut = Planetarium.GetUniversalTime ();
            radarAltitude = FlightHistSample.GetRadarAltitude (vessel);
            if (ut - _prevTime >= SAMPLE_INTERVAL) {
                double deltaTime = ut - _prevTime;
                _prevTime = ut;
                FlightHistSample sample = new FlightHistSample (vessel);
                if (fltrRadar && radarAltitude <  warnAltitudeMax * 1.2f && vessel.situation != Vessel.Situations.LANDED) {
                    part.RequestResource ("ElectricCharge", deltaTime * fltrChargeRate);
                    sample.RadarContact = ForwardRadar (Math.PI / 4);
                }
                radarDistance = sample.RadarContact;
                _flightHist.Add (sample);

                while (_flightHist.Count > 2 && (sample.UT - _flightHist [0].UT) > sampleWindow) {
                    _flightHist.RemoveAt (0);
                }
            }
            if (_flightHist.Count < 2)
                return;

            FlightHistSample oldest = _flightHist [0];
            FlightHistSample newest = _flightHist [_flightHist.Count - 1];

            double descentRate = (oldest.RadarHeight - newest.RadarHeight) / (newest.UT - oldest.UT);
            if (descentRate < -vessel.verticalSpeed)
                descentRate = -vessel.verticalSpeed;
            measuredDescentRate = (float)descentRate;

            bool overHeight = newest.RadarHeight > warnAltitudeMax && newest.RadarContact > warnAltitudeMax;

            double tolerance = 1.0d;
            bool gearDown = IsGearDown ();
            if (gearDown)
                tolerance = (double)landingTolerance;

            double descentRateThreshold = (warnApproachVelMax - warnApproachVelMin) * (newest.RadarHeight / warnAltitudeMax) * tolerance +
                warnApproachVelMin;
            double approachRateThreshold = 0.0d;
            if (!float.IsNaN (newest.RadarContact)) {
                approachRateThreshold = (warnApproachVelMax - warnApproachVelMin) * (newest.RadarContact / warnAltitudeMax) * tolerance +
                    warnApproachVelMin;
            }

            if (!overHeight && descentRate > descentRateThreshold ) {
                if (!_warningActive) {
                    _warningActive = true;
                    _warningTime = ut;
                    Debug.Log("TAWS - Descent Rate:" + descentRate + " Gear Down:"+gearDown);
                }
                TerrainWarning ();
            } else if (!overHeight &&  !float.IsNaN(newest.RadarContact) && newest.RadarContact < warnAltitudeMax &&
                Vector3.Dot(vessel.srf_velocity,_transform.forward) > approachRateThreshold) {
                if (!_warningActive) {
                    _warningActive = true;
                    _warningTime = ut;
                    Debug.Log("TAWS - Approach Rate:" + Vector3.Dot(vessel.srf_velocity,_transform.forward)+ " Gear Down:"+gearDown);
                }
                TerrainWarning ();
            } else {
                _playing = null;
                _audioSource.Stop ();
                _warningActive = false;
                if (_warningActiveModule == this)
                    _warningActiveModule = null;
            }
        }