public void Update() { if (HighLogic.LoadedSceneIsEditor) { float spotSize = (float)(1.44 * wavelength_num * Distance * 1000000 / SourceDishDia); double powerReceived = (spotSize > recvDiameter) ? recvDiameter / spotSize * BeamedPower * (CalcEfficiency / 100) * recvEfficiency : BeamedPower * (CalcEfficiency / 100) * recvEfficiency; string propellantname = engine.propellants[0].name; float shc = PartResourceLibrary.Instance.GetDefinition(propellantname).specificHeatCapacity * 1.5f / 1000f; // in kJ kg^-1 K^-1 Thrust = Mathf.Clamp((float)Math.Round(((powerReceived * thermalEfficiency * 0.4f) / (shc * 2400f)) * 9.8f * Isp, 1), 0f, engine.GetMaxThrust()); Thrust = (Thrust < 10f) ? 0f : Thrust * thrustMult; } else if (HighLogic.LoadedSceneIsFlight) { engine.thrustPercentage = Mathf.Clamp((float)Math.Round(percentThrust * 100, 2), 0f, 100f); LockGimbal(); } }
// only runs in editor (code for thrust calculator) public void Update() { if (HighLogic.LoadedSceneIsEditor) { engine.thrustPercentage = 100f; float spotArea = (float)Math.Pow((1.44 * wavelength_num * Distance * 1000000 / SourceDishDia) / 2, 2) * 3.14f; double powerReceived = (spotArea > SurfaceArea) ? (SurfaceArea / spotArea) * BeamedPower * (CalcEfficiency / 100) * Efficiency : BeamedPower * (CalcEfficiency / 100) * Efficiency; powerReceived *= Math.Cos(Angle * Math.PI / 180); string propellantname = engine.propellants[0].name; float shc = PartResourceLibrary.Instance.GetDefinition(propellantname).specificHeatCapacity * 1.5f / 1000f; // in kJ kg^-1 K^-1 Thrust = Mathf.Clamp((float)Math.Round((powerReceived * 0.2f / (shc * 8600f)) * 9.8d * Isp, 1), 0f, engine.GetMaxThrust()); Thrust = (Thrust < 20f) ? 0f : Thrust; } else if (HighLogic.LoadedSceneIsFlight) { engine.thrustPercentage = Mathf.Clamp((float)Math.Round(percentThrust * 100, 2), 0f, 100f); } }
//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; }
//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; }