Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions GravityTurn/GravityTurn/GravityTurner.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.CompilerServices;
Expand Down Expand Up @@ -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,
Expand All @@ -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
);
}

Expand Down
14 changes: 10 additions & 4 deletions GravityTurn/GravityTurn/PartExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Text;
//using System.Threading.Tasks;
Expand Down Expand Up @@ -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")
Expand Down
17 changes: 13 additions & 4 deletions GravityTurn/GravityTurn/StageController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using UnityEngine;
using KSP.UI.Screens;
Expand Down Expand Up @@ -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();
Expand Down
100 changes: 97 additions & 3 deletions GravityTurn/GravityTurn/VesselState.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
using System;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Reflection;
using UnityEngine;

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;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions GravityTurn/GravityTurn/Window/StatsWindow.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
Expand All @@ -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)
{
Expand Down