/// <summary>
        /// Returns the maximum thrust in kiloNewtons for the selected group.
        /// </summary>
        /// <param name="groupId">A number from 1 to 31 (inclusive) to select a specific group, or 0 to select all groups.</param>
        /// <returns>Max thrust in kN.</returns>
        public double MaxThrustkN(double groupId)
        {
            float thrust = 0.0f;
            int   id     = (int)groupId;

            if (id == 0)
            {
                for (int i = 0; i < vc.engineGroup.Length; ++i)
                {
                    ModuleEngines me = vc.engineGroup[i].engine;

                    if (me.EngineIgnited)
                    {
                        thrust += me.GetMaxThrust();
                    }
                }
            }
            else
            {
                for (int i = 0; i < vc.engineGroup.Length; ++i)
                {
                    if (vc.engineGroup[i].partId == id)
                    {
                        ModuleEngines me = vc.engineGroup[i].engine;

                        if (me.EngineIgnited)
                        {
                            thrust += me.GetMaxThrust();
                        }
                    }
                }
            }

            return(thrust);
        }
        public void Update()
        {
            if (HighLogic.LoadedSceneIsEditor)
            {
                float  spotArea      = (float)(Math.Pow((1.44 * wavelength_num * Distance * 1000000d / SourceDishDia), 2) * 3.14);
                double powerReceived = Math.Cos(Angle * Math.PI / 180) * ((spotArea > SurfaceArea) ?
                                                                          SurfaceArea / spotArea * BeamedPower * (CalcEfficiency / 100) : BeamedPower * (CalcEfficiency / 100));

                double impulse = powerReceived * 1000f / c;
                Thrust = Mathf.Clamp((float)Math.Round((impulse * Reflectivity *
                                                        HighLogic.CurrentGame.Parameters.CustomParams <BPSettings>().photonthrust), 3), 0f, engine.GetMaxThrust() * 1000f);
            }
            else if (HighLogic.LoadedSceneIsFlight)
            {
                engine.Fields["propellantReqMet"].guiActive = false;
            }
        }
        //This method runs every physics frame
        //============================================================================================================================================
        private void FixedUpdate()
        {
            //We can reduce compute times by testing only engines from a list rather than every part on the ship
            if (PartCount != FlightGlobals.ActiveVessel.parts.Count ()) {
                //Make a new list of Engines if the Part Count has changed since last frame
                ActiveEngines.Clear ();
                foreach (Part Engine in FlightGlobals.ActiveVessel.parts) {
                    if (Engine.Modules.Contains ("ModuleEngines") | Engine.Modules.Contains ("ModuleEnginesFX")) {
                        ActiveEngines.Add (Engine);
                    }
                }
                //Set Part count to current count
                PartCount = FlightGlobals.ActiveVessel.parts.Count ();
            }

            //Find Global Variables
            CurrentMach = Convert.ToSingle(FlightGlobals.ActiveVessel.mach);
            CurrentAtm = Convert.ToSingle (FlightGlobals.ActiveVessel.atmDensity);

            //Perform calculations for each engine
            foreach (Part SingleEngine in ActiveEngines) {
                //ModuleEngines
                //------------------------------------------------------------------------------------------------------------------------------
                if (SingleEngine.Modules.Contains ("ModuleEngines")) {
                    ModuleEngines ME1 = new ModuleEngines ();
                    ModuleEngines ME2 = new ModuleEngines ();
                    ME1 = SingleEngine.FindModulesImplementing<ModuleEngines> ().First ();
                    ME2 = SingleEngine.FindModulesImplementing<ModuleEngines> ().Last ();
                    //Do Calculations for Module 1 if it is operational
                    if (ME1.isOperational == true) {
                        //Jet engines use Atmosphere & Velocity Curves to modify performance at different altitudes/speeds.  Use these calculations for those engines, otherwise use base values from part cfg.
                        if (ME1.useAtmCurve == true && ME1.useVelCurve == true) {
                            //If the Thrust Multiplier from VelCurve * AtmCurve is more than 1, evaluate the curves and determine max thrust for that scenario.  Otherwise, use the base method and assume Max Thrust from cfg file.
                            if ((ME1.velCurve.Evaluate (CurrentMach) * ME1.atmCurve.Evaluate (CurrentAtm)) > 1) {
                                TotalCapableThrust = TotalCapableThrust + ((ME1.GetMaxThrust () * (ME1.thrustPercentage / 100)) * (ME1.velCurve.Evaluate (CurrentMach)) * (ME1.atmCurve.Evaluate (CurrentAtm)));
                            } else {
                                TotalCapableThrust = TotalCapableThrust + ((ME1.GetMaxThrust () * (ME1.thrustPercentage / 100)));
                            }
                            TotalCurrentThrust = TotalCurrentThrust + ME1.GetCurrentThrust ();
                        } else {
                            TotalCapableThrust = TotalCapableThrust + (ME1.GetMaxThrust () * (ME1.thrustPercentage / 100));
                            TotalCurrentThrust = TotalCurrentThrust + ME1.GetCurrentThrust ();
                        }
                    }
                    //If Module 2 is different from 1 (by engineID) do calculations for Module 2 if it is operational
                    if (ME1.engineID != ME2.engineID) {
                        if (ME2.isOperational == true) {
                            //Jet engines use Atmosphere & Velocity Curves to modify performance at different altitudes/speeds.  Use these calculations for those engines, otherwise use base values from part cfg.
                            if (ME2.useAtmCurve == true && ME2.useVelCurve == true) {
                                //If the Thrust Multiplier from VelCurve * AtmCurve is more than 1, evaluate the curves and determine max thrust for that scenario.  Otherwise, use the base method and assume Max Thrust from cfg file.
                                if ((ME2.velCurve.Evaluate (CurrentMach) * ME2.atmCurve.Evaluate (CurrentAtm)) > 1) {
                                    TotalCapableThrust = TotalCapableThrust + ((ME2.GetMaxThrust () * (ME2.thrustPercentage / 100)) * (ME2.velCurve.Evaluate (CurrentMach)) * (ME2.atmCurve.Evaluate (CurrentAtm)));
                                } else {
                                    TotalCapableThrust = TotalCapableThrust + ((ME2.GetMaxThrust () * (ME2.thrustPercentage / 100)));
                                }
                                TotalCurrentThrust = TotalCurrentThrust + ME2.GetCurrentThrust ();
                            } else {
                                TotalCapableThrust = TotalCapableThrust + (ME2.GetMaxThrust () * (ME2.thrustPercentage / 100));
                                TotalCurrentThrust = TotalCurrentThrust + ME2.GetCurrentThrust ();
                            }
                        }
                    }
                }
                //ModuleEnginesFX
                //------------------------------------------------------------------------------------------------------------------------------
                if (SingleEngine.Modules.Contains ("ModuleEnginesFX")) {
                    ModuleEnginesFX MEFX1 = new ModuleEnginesFX ();
                    ModuleEnginesFX MEFX2 = new ModuleEnginesFX ();
                    MEFX1 = SingleEngine.FindModulesImplementing<ModuleEnginesFX> ().First ();
                    MEFX2 = SingleEngine.FindModulesImplementing<ModuleEnginesFX> ().Last ();
                    //Do Calculations for Module 1 if it is operational
                    if (MEFX1.isOperational == true) {
                        //Jet engines use Atmosphere & Velocity Curves to modify performance at different altitudes/speeds.  Use these calculations for those engines, otherwise use base values from part cfg.
                        if (MEFX1.useAtmCurve == true && MEFX1.useVelCurve == true) {
                            //If the Thrust Multiplier from VelCurve * AtmCurve is more than 1, evaluate the curves and determine max thrust for that scenario.  Otherwise, use the base method and assume Max Thrust from cfg file.
                            if ((MEFX1.velCurve.Evaluate (CurrentMach) * MEFX1.atmCurve.Evaluate (CurrentAtm)) > 1) {
                                TotalCapableThrust = TotalCapableThrust + ((MEFX1.GetMaxThrust () * (MEFX1.thrustPercentage / 100)) * (MEFX1.velCurve.Evaluate (CurrentMach)) * (MEFX1.atmCurve.Evaluate (CurrentAtm)));
                            } else {
                                TotalCapableThrust = TotalCapableThrust + ((MEFX1.GetMaxThrust () * (MEFX1.thrustPercentage / 100)));
                            }
                            TotalCurrentThrust = TotalCurrentThrust + MEFX1.GetCurrentThrust ();
                        } else {
                            TotalCapableThrust = TotalCapableThrust + (MEFX1.GetMaxThrust () * (MEFX1.thrustPercentage / 100));
                            TotalCurrentThrust = TotalCurrentThrust + MEFX1.GetCurrentThrust ();
                        }
                    }
                    //If Module 2 is different from 1 (by engineID) do calculations for Module 2 if it is operational
                    if (MEFX1.engineID != MEFX2.engineID) {
                        if (MEFX2.isOperational == true) {
                            //Jet engines use Atmosphere & Velocity Curves to modify performance at different altitudes/speeds.  Use these calculations for those engines, otherwise use base values from part cfg.
                            if (MEFX2.useAtmCurve == true && MEFX2.useVelCurve == true) {
                                //If the Thrust Multiplier from VelCurve * AtmCurve is more than 1, evaluate the curves and determine max thrust for that scenario.  Otherwise, use the base method and assume Max Thrust from cfg file.
                                if ((MEFX2.velCurve.Evaluate (CurrentMach) * MEFX2.atmCurve.Evaluate (CurrentAtm)) > 1) {
                                    TotalCapableThrust = TotalCapableThrust + ((MEFX2.GetMaxThrust () * (MEFX2.thrustPercentage / 100)) * (MEFX2.velCurve.Evaluate (CurrentMach)) * (MEFX2.atmCurve.Evaluate (CurrentAtm)));
                                } else {
                                    TotalCapableThrust = TotalCapableThrust + ((MEFX2.GetMaxThrust () * (MEFX2.thrustPercentage / 100)));
                                }
                                TotalCurrentThrust = TotalCurrentThrust + MEFX2.GetCurrentThrust ();
                            } else {
                                TotalCapableThrust = TotalCapableThrust + (MEFX2.GetMaxThrust () * (MEFX2.thrustPercentage / 100));
                                TotalCurrentThrust = TotalCurrentThrust + MEFX2.GetCurrentThrust ();
                            }
                        }
                    }
                }
            }
            //Prevent Divide by 0 Errors
            if (TotalCapableThrust == 0) {
                TotalCapableThrust = 0.0001f;
            }
            //Not truly a percentage, gauge works between 0 and 1, NOT 0 and 100
            ThrustPercentage = ((TotalCurrentThrust / TotalCapableThrust));

            if (UseSplitNeedle == false) {
                //Gently Fade the alpha channel of the stock gauge so we can see TIM if it is near/behind
                if (FlightUIController.fetch.thr.rawValue >= ThrustPercentage) {
                    StockGaugeColor.a = Mathf.Max (Mathf.Min (((((FlightUIController.fetch.thr.rawValue - ThrustPercentage) - 0.015f) * 2.3f) + StockGaugeColorAlpha), 1.0f), StockGaugeColorAlpha);
                }
                if (FlightUIController.fetch.thr.rawValue <= ThrustPercentage) {
                    StockGaugeColor.a = Mathf.Max (Mathf.Min (((((FlightUIController.fetch.thr.rawValue - ThrustPercentage) + 0.015f) * -2.3f) + StockGaugeColorAlpha), 1.0f), StockGaugeColorAlpha);
                }
                StockGauge.transform.GetChild (0).renderer.material.color = StockGaugeColor;
                TIMGauge.transform.GetChild (0).renderer.material.color = TIMGaugeColor;
                //Render the StockGauge after TIM so that it shows in front of TIM
                StockGauge.transform.GetChild (0).renderer.material.renderQueue = TIMGauge.transform.GetChild (0).renderer.material.renderQueue + 1;
            }
            //Set Gauge to Calculated Value
            TIMGauge.setValue (ThrustPercentage);
            //Reset Thrust Values Each Compute Cycle
            TotalCapableThrust = 0;
            TotalCurrentThrust = 0;
        }
Exemple #4
0
        //This method runs every physics frame
        //============================================================================================================================================
        private void FixedUpdate()
        {
            //We can reduce compute times by testing only engines from a list rather than every part on the ship
            if (PartCount != FlightGlobals.ActiveVessel.parts.Count())
            {
                //Make a new list of Engines if the Part Count has changed since last frame
                ActiveEngines.Clear();
                foreach (Part Engine in FlightGlobals.ActiveVessel.parts)
                {
                    if (Engine.Modules.Contains("ModuleEngines") | Engine.Modules.Contains("ModuleEnginesFX"))
                    {
                        ActiveEngines.Add(Engine);
                    }
                }
                //Set Part count to current count
                PartCount = FlightGlobals.ActiveVessel.parts.Count();
            }

            //Find Global Variables
            CurrentMach = Convert.ToSingle(FlightGlobals.ActiveVessel.mach);
            CurrentAtm  = Convert.ToSingle(FlightGlobals.ActiveVessel.atmDensity);

            //Perform calculations for each engine
            foreach (Part SingleEngine in ActiveEngines)
            {
                //ModuleEngines
                //------------------------------------------------------------------------------------------------------------------------------
                if (SingleEngine.Modules.Contains("ModuleEngines"))
                {
                    ModuleEngines ME1 = new ModuleEngines();
                    ModuleEngines ME2 = new ModuleEngines();
                    ME1 = SingleEngine.FindModulesImplementing <ModuleEngines> ().First();
                    ME2 = SingleEngine.FindModulesImplementing <ModuleEngines> ().Last();
                    //Do Calculations for Module 1 if it is operational
                    if (ME1.isOperational == true)
                    {
                        //Jet engines use Atmosphere & Velocity Curves to modify performance at different altitudes/speeds.  Use these calculations for those engines, otherwise use base values from part cfg.
                        if (ME1.useAtmCurve == true && ME1.useVelCurve == true)
                        {
                            //If the Thrust Multiplier from VelCurve * AtmCurve is more than 1, evaluate the curves and determine max thrust for that scenario.  Otherwise, use the base method and assume Max Thrust from cfg file.
                            if ((ME1.velCurve.Evaluate(CurrentMach) * ME1.atmCurve.Evaluate(CurrentAtm)) > 1)
                            {
                                TotalCapableThrust = TotalCapableThrust + ((ME1.GetMaxThrust() * (ME1.thrustPercentage / 100)) * (ME1.velCurve.Evaluate(CurrentMach)) * (ME1.atmCurve.Evaluate(CurrentAtm)));
                            }
                            else
                            {
                                TotalCapableThrust = TotalCapableThrust + ((ME1.GetMaxThrust() * (ME1.thrustPercentage / 100)));
                            }
                            TotalCurrentThrust = TotalCurrentThrust + ME1.GetCurrentThrust();
                        }
                        else
                        {
                            TotalCapableThrust = TotalCapableThrust + (ME1.GetMaxThrust() * (ME1.thrustPercentage / 100));
                            TotalCurrentThrust = TotalCurrentThrust + ME1.GetCurrentThrust();
                        }
                    }
                    //If Module 2 is different from 1 (by engineID) do calculations for Module 2 if it is operational
                    if (ME1.engineID != ME2.engineID)
                    {
                        if (ME2.isOperational == true)
                        {
                            //Jet engines use Atmosphere & Velocity Curves to modify performance at different altitudes/speeds.  Use these calculations for those engines, otherwise use base values from part cfg.
                            if (ME2.useAtmCurve == true && ME2.useVelCurve == true)
                            {
                                //If the Thrust Multiplier from VelCurve * AtmCurve is more than 1, evaluate the curves and determine max thrust for that scenario.  Otherwise, use the base method and assume Max Thrust from cfg file.
                                if ((ME2.velCurve.Evaluate(CurrentMach) * ME2.atmCurve.Evaluate(CurrentAtm)) > 1)
                                {
                                    TotalCapableThrust = TotalCapableThrust + ((ME2.GetMaxThrust() * (ME2.thrustPercentage / 100)) * (ME2.velCurve.Evaluate(CurrentMach)) * (ME2.atmCurve.Evaluate(CurrentAtm)));
                                }
                                else
                                {
                                    TotalCapableThrust = TotalCapableThrust + ((ME2.GetMaxThrust() * (ME2.thrustPercentage / 100)));
                                }
                                TotalCurrentThrust = TotalCurrentThrust + ME2.GetCurrentThrust();
                            }
                            else
                            {
                                TotalCapableThrust = TotalCapableThrust + (ME2.GetMaxThrust() * (ME2.thrustPercentage / 100));
                                TotalCurrentThrust = TotalCurrentThrust + ME2.GetCurrentThrust();
                            }
                        }
                    }
                }
                //ModuleEnginesFX
                //------------------------------------------------------------------------------------------------------------------------------
                if (SingleEngine.Modules.Contains("ModuleEnginesFX"))
                {
                    ModuleEnginesFX MEFX1 = new ModuleEnginesFX();
                    ModuleEnginesFX MEFX2 = new ModuleEnginesFX();
                    MEFX1 = SingleEngine.FindModulesImplementing <ModuleEnginesFX> ().First();
                    MEFX2 = SingleEngine.FindModulesImplementing <ModuleEnginesFX> ().Last();
                    //Do Calculations for Module 1 if it is operational
                    if (MEFX1.isOperational == true)
                    {
                        //Jet engines use Atmosphere & Velocity Curves to modify performance at different altitudes/speeds.  Use these calculations for those engines, otherwise use base values from part cfg.
                        if (MEFX1.useAtmCurve == true && MEFX1.useVelCurve == true)
                        {
                            //If the Thrust Multiplier from VelCurve * AtmCurve is more than 1, evaluate the curves and determine max thrust for that scenario.  Otherwise, use the base method and assume Max Thrust from cfg file.
                            if ((MEFX1.velCurve.Evaluate(CurrentMach) * MEFX1.atmCurve.Evaluate(CurrentAtm)) > 1)
                            {
                                TotalCapableThrust = TotalCapableThrust + ((MEFX1.GetMaxThrust() * (MEFX1.thrustPercentage / 100)) * (MEFX1.velCurve.Evaluate(CurrentMach)) * (MEFX1.atmCurve.Evaluate(CurrentAtm)));
                            }
                            else
                            {
                                TotalCapableThrust = TotalCapableThrust + ((MEFX1.GetMaxThrust() * (MEFX1.thrustPercentage / 100)));
                            }
                            TotalCurrentThrust = TotalCurrentThrust + MEFX1.GetCurrentThrust();
                        }
                        else
                        {
                            TotalCapableThrust = TotalCapableThrust + (MEFX1.GetMaxThrust() * (MEFX1.thrustPercentage / 100));
                            TotalCurrentThrust = TotalCurrentThrust + MEFX1.GetCurrentThrust();
                        }
                    }
                    //If Module 2 is different from 1 (by engineID) do calculations for Module 2 if it is operational
                    if (MEFX1.engineID != MEFX2.engineID)
                    {
                        if (MEFX2.isOperational == true)
                        {
                            //Jet engines use Atmosphere & Velocity Curves to modify performance at different altitudes/speeds.  Use these calculations for those engines, otherwise use base values from part cfg.
                            if (MEFX2.useAtmCurve == true && MEFX2.useVelCurve == true)
                            {
                                //If the Thrust Multiplier from VelCurve * AtmCurve is more than 1, evaluate the curves and determine max thrust for that scenario.  Otherwise, use the base method and assume Max Thrust from cfg file.
                                if ((MEFX2.velCurve.Evaluate(CurrentMach) * MEFX2.atmCurve.Evaluate(CurrentAtm)) > 1)
                                {
                                    TotalCapableThrust = TotalCapableThrust + ((MEFX2.GetMaxThrust() * (MEFX2.thrustPercentage / 100)) * (MEFX2.velCurve.Evaluate(CurrentMach)) * (MEFX2.atmCurve.Evaluate(CurrentAtm)));
                                }
                                else
                                {
                                    TotalCapableThrust = TotalCapableThrust + ((MEFX2.GetMaxThrust() * (MEFX2.thrustPercentage / 100)));
                                }
                                TotalCurrentThrust = TotalCurrentThrust + MEFX2.GetCurrentThrust();
                            }
                            else
                            {
                                TotalCapableThrust = TotalCapableThrust + (MEFX2.GetMaxThrust() * (MEFX2.thrustPercentage / 100));
                                TotalCurrentThrust = TotalCurrentThrust + MEFX2.GetCurrentThrust();
                            }
                        }
                    }
                }
            }
            //Prevent Divide by 0 Errors
            if (TotalCapableThrust == 0)
            {
                TotalCapableThrust = 0.0001f;
            }
            //Not truly a percentage, gauge works between 0 and 1, NOT 0 and 100
            ThrustPercentage = ((TotalCurrentThrust / TotalCapableThrust));

            if (UseSplitNeedle == false)
            {
                //Gently Fade the alpha channel of the stock gauge so we can see TIM if it is near/behind
                if (FlightUIController.fetch.thr.rawValue >= ThrustPercentage)
                {
                    StockGaugeColor.a = Mathf.Max(Mathf.Min(((((FlightUIController.fetch.thr.rawValue - ThrustPercentage) - 0.015f) * 2.3f) + StockGaugeColorAlpha), 1.0f), StockGaugeColorAlpha);
                }
                if (FlightUIController.fetch.thr.rawValue <= ThrustPercentage)
                {
                    StockGaugeColor.a = Mathf.Max(Mathf.Min(((((FlightUIController.fetch.thr.rawValue - ThrustPercentage) + 0.015f) * -2.3f) + StockGaugeColorAlpha), 1.0f), StockGaugeColorAlpha);
                }
                StockGauge.transform.GetChild(0).renderer.material.color = StockGaugeColor;
                TIMGauge.transform.GetChild(0).renderer.material.color   = TIMGaugeColor;
                //Render the StockGauge after TIM so that it shows in front of TIM
                StockGauge.transform.GetChild(0).renderer.material.renderQueue = TIMGauge.transform.GetChild(0).renderer.material.renderQueue + 1;
            }
            //Set Gauge to Calculated Value
            TIMGauge.setValue(ThrustPercentage);
            //Reset Thrust Values Each Compute Cycle
            TotalCapableThrust = 0;
            TotalCurrentThrust = 0;
        }