diff --git a/GravityTurn/GravityTurn/GravityTurner.cs b/GravityTurn/GravityTurn/GravityTurner.cs index 91e2877..a78ae3f 100644 --- a/GravityTurn/GravityTurn/GravityTurner.cs +++ b/GravityTurn/GravityTurn/GravityTurner.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Text; using System.Runtime.CompilerServices; @@ -759,13 +759,15 @@ void CalculateLosses(Vessel vessel) "GravityDrag:\t{1:0.00} m/s²\n" + "Thrust Vector Drag:\t{5:0.00} m/s²\n" + "Air Drag Loss:\t{2:0.00} m/s\n" + - "Gravity Drag Loss:\t{3:0.00} -> {4:0.00}m/s @AP\n\n" + + "Gravity Drag Loss:\t{3:0.00} -> {4:0.00} m/s @AP\n\n" + "Total Vector Loss:\t{6:0.00} m/s\n" + "Total Loss:\t{7:0.00} m/s\n" + "Total Burn:\t\t{8:0.0}\n\n" + "Apoapsis:\t\t{9}\n" + "Periapsis:\t\t{10}\n" + - "Inclination:\t\t{11:0.0} °\n", + "Inclination:\t\t{11:0.0} °\n\n" + + "Dynamic Pressure:\t{12:0.00} Pa\n" + + "Max Q:\t\t{13:0.00} Pa\n\n", vesselState.drag, GravityDrag, DragLoss, @@ -776,7 +778,9 @@ void CalculateLosses(Vessel vessel) TotalBurn, OrbitExtensions.FormatOrbitInfo(vessel.orbit.ApA, vessel.orbit.timeToAp), OrbitExtensions.FormatOrbitInfo(vessel.orbit.PeA, vessel.orbit.timeToPe), - vessel.orbit.inclination + vessel.orbit.inclination, + vesselState.dynamicPressure, + vesselState.maxQ ); } diff --git a/GravityTurn/GravityTurn/PartExtensions.cs b/GravityTurn/GravityTurn/PartExtensions.cs index 58a29f8..b91a666 100644 --- a/GravityTurn/GravityTurn/PartExtensions.cs +++ b/GravityTurn/GravityTurn/PartExtensions.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Text; //using System.Threading.Tasks; @@ -74,11 +74,17 @@ public static bool IsUnfiredDecoupler(this Part p) if (m is ModuleProceduralFairing) isFairing = true; - if (m is ModuleCargoBay && isFairing) + if(isFairing) { - ModuleCargoBay fairing = m as ModuleCargoBay; - if (fairing.ClosedAndLocked() && m.stagingEnabled && p.stagingOn) return true; + if (VesselState.isLoadedFAR && m.stagingEnabled && p.stagingOn) return true; + + else + if (m is ModuleCargoBay) + { + ModuleCargoBay fairing = m as ModuleCargoBay; + if (fairing.ClosedAndLocked() && m.stagingEnabled && p.stagingOn) return true; break; + } } if (VesselState.isLoadedProceduralFairing && m.moduleName == "ProceduralFairingDecoupler") diff --git a/GravityTurn/GravityTurn/StageController.cs b/GravityTurn/GravityTurn/StageController.cs index ad3958f..171725e 100644 --- a/GravityTurn/GravityTurn/StageController.cs +++ b/GravityTurn/GravityTurn/StageController.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using UnityEngine; using KSP.UI.Screens; @@ -46,10 +46,19 @@ public void Update() if (!topFairingDeployed) { Part fairing = GetTopmostFairing(vessel); + if (fairing == null) - GravityTurner.DebugMessage += " no top fairing\n"; - - if (fairing != null && fairing.IsUnfiredDecoupler() && (vesselState.dynamicPressure < turner.FairingPressure && Math.Abs(vesselState.dynamicPressure - vesselState.maxQ) > 0.1) && (vesselState.maxQ > vessel.mainBody.atmospherePressureSeaLevel/2)) + GravityTurner.DebugMessage += " No top fairing\n"; + + if (fairing != null) + GravityTurner.DebugMessage += " Has top fairing\n"; + + bool fairingReadyToDeploy = (vesselState.dynamicPressure < turner.FairingPressure && Math.Abs(vesselState.dynamicPressure - vesselState.maxQ) > 0.1) && ((VesselState.isLoadedFAR && (vesselState.maxQ > vessel.mainBody.GetPressure(0) * 1000 / 5)) || (vesselState.maxQ > vessel.mainBody.atmospherePressureSeaLevel / 2)); + + if (fairingReadyToDeploy) + GravityTurner.DebugMessage += " Fairing ready to be deployed\n"; + + if (fairing != null && fairing.IsUnfiredDecoupler() && fairingReadyToDeploy) { topFairingDeployed = true; fairing.DeployFairing(); diff --git a/GravityTurn/GravityTurn/VesselState.cs b/GravityTurn/GravityTurn/VesselState.cs index 5843485..7f56ef5 100644 --- a/GravityTurn/GravityTurn/VesselState.cs +++ b/GravityTurn/GravityTurn/VesselState.cs @@ -1,6 +1,8 @@ -using System; +using System; using System.Collections.Generic; using System.Text; +using System.Linq; +using System.Reflection; using UnityEngine; namespace GravityTurn @@ -8,6 +10,10 @@ namespace GravityTurn public class VesselState { public static bool isLoadedProceduralFairing = false; + public static bool isLoadedFAR = false; // initializing variable + + public static MethodInfo FAR_method_press = null; + public static MethodInfo FAR_method_force = null; private static bool isLoadedRCSFXExt = false; @@ -38,6 +44,8 @@ public class VesselState public Vector3d angularVelocity; public Vector3d angularMomentum; + + public Vector3d force; public Vector3d radialPlus; //unit vector in the plane of up and velocityVesselOrbit and perpendicular to velocityVesselOrbit public Vector3d radialPlusSurface; //unit vector in the plane of up and velocityVesselSurface and perpendicular to velocityVesselSurface @@ -237,7 +245,13 @@ static VesselState() gimbalExtDict.Add(typeof(ModuleGimbal), stockGimbal); isLoadedRCSFXExt = false;// (AssemblyLoader.loadedAssemblies.SingleOrDefault(a => a.assembly.GetName().Name == "MechJebRCSFXExt") != null); isLoadedProceduralFairing = isAssemblyLoaded("ProceduralFairings"); + isLoadedFAR = isAssemblyLoaded("FerramAerospaceResearch"); + + FAR_method_press = getFAR_method_press(); + FAR_method_force = getFAR_method_force(); + } + static bool isAssemblyLoaded(string assemblyName) { foreach (AssemblyLoader.LoadedAssembly assembly in AssemblyLoader.loadedAssemblies) @@ -256,6 +270,48 @@ static bool isAssemblyLoaded(string assemblyName) return false; } + + static MethodInfo getFAR_method_force() + { + if (isLoadedFAR == true) + { + AssemblyLoader.LoadedAssembly FAR = AssemblyLoader.loadedAssemblies.SingleOrDefault(a => a.dllName == "FerramAerospaceResearch"); + //try + //{ + return FAR.assembly.GetTypes().SingleOrDefault(t => t.Name == "FARAPI").GetMethod("CalculateVesselAeroForces", BindingFlags.Public | BindingFlags.Static); + //} + //catch (Exception e) + //{ + // UnityEngine.Debug.LogError("Error finding the method definition\n" + e.StackTrace); + //} + + } + else + { + return null; + } + } + + static MethodInfo getFAR_method_press() + { + if (isLoadedFAR == true) + { + AssemblyLoader.LoadedAssembly FAR = AssemblyLoader.loadedAssemblies.SingleOrDefault(a => a.dllName == "FerramAerospaceResearch"); + //try + //{ + return FAR.assembly.GetTypes().SingleOrDefault(t => t.Name == "FARAPI").GetMethod("ActiveVesselDynPres", BindingFlags.Public | BindingFlags.Static); + //} + //catch (Exception e) + //{ + // UnityEngine.Debug.LogError("Error finding the method definition\n" + e.StackTrace); + //} + } + else + { + return null; + } + } + public VesselState() { TerminalVelocityCall = TerminalVelocityStockKSP; @@ -498,7 +554,24 @@ void UpdateBasicInfo(Vessel vessel) double temperature = FlightGlobals.getExternalTemperature(altitudeASL); atmosphericDensity = FlightGlobals.getAtmDensity(atmosphericPressure, temperature); atmosphericDensityGrams = atmosphericDensity * 1000; - dynamicPressure = vessel.dynamicPressurekPa * 1000; + + // using the pressure calculated by the FARAPI class if FAR is installed + if (isLoadedFAR == true) + { + try + { + dynamicPressure = (double)FAR_method_press.Invoke(null, null) * 1000; + } + catch (Exception e) + { + UnityEngine.Debug.LogError("Error calculating dynamic pressure\n" + e.StackTrace); + } + } + else + { + dynamicPressure = vessel.dynamicPressurekPa * 1000; + } + if (maxQ < dynamicPressure) maxQ = dynamicPressure; @@ -787,8 +860,29 @@ void AnalyzeParts(Vessel vessel, EngineInfo einfo, IntakeInfo iinfo) pureLift = pureLiftV.magnitude; + // using the force calculated by the FARAPI class if FAR is installed + if (isLoadedFAR == true) + { + try + { + var parameters_FAR = new object[] { FlightGlobals.ActiveVessel, new Vector3(), new Vector3(), (Vector3)surfaceVelocity, (double)altitudeASL }; + FAR_method_force.Invoke(null, parameters_FAR); - Vector3d force = pureDragV + pureLiftV; + force = (Vector3)parameters_FAR[1]; + + // the /mass is there because in this plugin it's really an ACCELERATION, not a force + force = force / mass; + } + catch (Exception e) + { + UnityEngine.Debug.LogError("Error invoking method\n" + e.StackTrace); + } + } + else + { + force = pureDragV + pureLiftV; + } + Vector3d liftDir = -Vector3d.Cross(vessel.transform.right, -surfaceVelocity.normalized); // Drag is the part (pureDrag + PureLift) applied opposite of the surface vel diff --git a/GravityTurn/GravityTurn/Window/StatsWindow.cs b/GravityTurn/GravityTurn/Window/StatsWindow.cs index 3d60dd5..5df6ff7 100644 --- a/GravityTurn/GravityTurn/Window/StatsWindow.cs +++ b/GravityTurn/GravityTurn/Window/StatsWindow.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Text; using UnityEngine; @@ -19,7 +19,7 @@ public override void WindowGUI(int windowID) base.WindowGUI(windowID); GUILayout.BeginVertical(); - GUILayout.Label(turner.Message, GUILayout.Width(300), GUILayout.Height(200)); + GUILayout.Label(turner.Message, GUILayout.Width(300), GUILayout.Height(250)); GUILayout.EndVertical(); if (GameSettings.MODIFIER_KEY.GetKeyDown() && !GravityTurner.DebugShow) {