Sarbian's patches for the fuel simulation.
--- a/KerbalEngineer/CelestialBodies.cs
+++ b/KerbalEngineer/CelestialBodies.cs
@@ -124,8 +124,8 @@
this.CelestialBody = body;
this.Name = body.bodyName;
this.Gravity = 9.81 * body.GeeASL;
- this.Atmosphere = body.atmosphere ? 101.325 * body.atmosphereMultiplier : 0;
- this.Parent = parent;
+ this.Atmosphere = body.atmosphere ? body.atmospherePressureSeaLevel : 0; // Check that one because I did not. S
+ this.Parent = parent;
// Set orbiting bodies information.
this.Children = new List<BodyInfo>();
--- a/KerbalEngineer/Editor/BuildAdvanced.cs
+++ b/KerbalEngineer/Editor/BuildAdvanced.cs
@@ -41,7 +41,7 @@
private GUIStyle areaSettingStyle;
private GUIStyle areaStyle;
private float atmosphericPercentage = 1.0f;
- private float atmosphericVelocity;
+ private float atmosphericMach;
private GUIStyle bodiesButtonActiveStyle;
private GUIStyle bodiesButtonStyle;
private DropDown bodiesList;
@@ -263,7 +263,7 @@
SimManager.Atmosphere = 0;
}
- SimManager.Velocity = this.atmosphericVelocity;
+ SimManager.Mach = this.atmosphericMach;
SimManager.RequestSimulation();
SimManager.TryStartSimulation();
@@ -305,9 +305,9 @@
GUILayout.Space(5.0f);
GUILayout.BeginVertical();
- GUILayout.Label("Velocity: " + this.atmosphericVelocity.ToString("F1") + "m/s", this.settingAtmoStyle, GUILayout.Width(125.0f * GuiDisplaySize.Offset));
+ GUILayout.Label("Mach: " + this.atmosphericMach.ToString("F1") + "m/s", this.settingAtmoStyle, GUILayout.Width(125.0f * GuiDisplaySize.Offset));
GUI.skin = HighLogic.Skin;
- this.atmosphericVelocity = GUILayout.HorizontalSlider(this.atmosphericVelocity, 0, 2500f);
+ this.atmosphericMach = GUILayout.HorizontalSlider(this.atmosphericMach, 0, 25f); // the game limits mach to 50 but I did not see curve with more than 25
GUI.skin = null;
GUILayout.EndVertical();
GUILayout.EndHorizontal();
--- a/KerbalEngineer/Extensions/PartExtensions.cs
+++ b/KerbalEngineer/Extensions/PartExtensions.cs
@@ -464,7 +464,7 @@
/// </summary>
public static bool IsSolidRocket(this Part part)
{
- return part.HasModule<ModuleEngines>() && part.GetModuleEngines().throttleLocked;
+ return (part.HasModule<ModuleEngines>() && part.GetModuleEngines().throttleLocked) || (part.HasModule<ModuleEnginesFX>() && part.GetModuleEnginesFx().throttleLocked);
}
#endregion
--- a/KerbalEngineer/Flight/Readouts/Surface/AtmosphericProcessor.cs
+++ b/KerbalEngineer/Flight/Readouts/Surface/AtmosphericProcessor.cs
@@ -126,7 +126,7 @@
var drag = FlightGlobals.ActiveVessel.parts.Sum(p => p.GetWetMass() * p.maximum_drag);
var grav = FlightGlobals.getGeeForceAtPosition(FlightGlobals.ship_position).magnitude;
var atmo = FlightGlobals.ActiveVessel.atmDensity;
- var coef = FlightGlobals.DragMultiplier;
+ var coef = FlightGlobals.ActiveVessel.parts.Sum(p => p.DragCubes.DragCoeff);
TerminalVelocity = Math.Sqrt((2 * mass * grav) / (atmo * drag * coef));
}
--- a/KerbalEngineer/Flight/Readouts/Vessel/AttitudeProcessor.cs
+++ b/KerbalEngineer/Flight/Readouts/Vessel/AttitudeProcessor.cs
@@ -123,7 +123,8 @@
// This code was derived from MechJeb2's implementation for getting the vessel's surface relative rotation.
this.centreOfMass = FlightGlobals.ActiveVessel.findWorldCenterOfMass();
this.up = (this.centreOfMass - FlightGlobals.ActiveVessel.mainBody.position).normalized;
- this.north = Vector3.Exclude(this.up, (FlightGlobals.ActiveVessel.mainBody.position + FlightGlobals.ActiveVessel.mainBody.transform.up * (float)FlightGlobals.ActiveVessel.mainBody.Radius) - this.centreOfMass).normalized;
+ this.north = Vector3.ProjectOnPlane((FlightGlobals.ActiveVessel.mainBody.position + FlightGlobals.ActiveVessel.mainBody.transform.up * (float)FlightGlobals.ActiveVessel.mainBody.Radius) - this.centreOfMass, this.up).normalized;
+
return Quaternion.Inverse(Quaternion.Euler(90.0f, 0.0f, 0.0f) * Quaternion.Inverse(FlightGlobals.ActiveVessel.transform.rotation) * Quaternion.LookRotation(this.north, this.up));
}
--- a/KerbalEngineer/Flight/Readouts/Vessel/SimulationProcessor.cs
+++ b/KerbalEngineer/Flight/Readouts/Vessel/SimulationProcessor.cs
@@ -119,7 +119,7 @@
SimManager.Gravity = FlightGlobals.ActiveVessel.mainBody.gravParameter /
Math.Pow(FlightGlobals.ActiveVessel.mainBody.Radius +
FlightGlobals.ActiveVessel.mainBody.GetAltitude(FlightGlobals.ActiveVessel.CoM), 2);
- SimManager.Velocity = FlightGlobals.ActiveVessel.srfSpeed;
+ SimManager.Mach = FlightGlobals.ActiveVessel.mach;
}
}
--- a/KerbalEngineer/VesselSimulator/EngineSim.cs
+++ b/KerbalEngineer/VesselSimulator/EngineSim.cs
@@ -47,15 +47,17 @@
public EngineSim(PartSim theEngine,
double atmosphere,
- double velocity,
- float maxThrust,
- float minThrust,
+ double machNumber,
+ float maxFuelFlow,
+ float minFuelFlow,
float thrustPercentage,
- float requestedThrust,
Vector3 vecThrust,
- float realIsp,
FloatCurve atmosphereCurve,
- FloatCurve velocityCurve,
+ bool atmChangeFlow,
+ FloatCurve atmCurve,
+ FloatCurve velCurve,
+ float currentThrottle,
+ float IspG,
bool throttleLocked,
List<Propellant> propellants,
bool active,
@@ -73,7 +75,7 @@
this.partSim = theEngine;
this.isActive = active;
- this.thrust = (maxThrust - minThrust) * (thrustPercentage / 100f) + minThrust;
+ //this.thrust = (maxThrust - minThrust) * (thrustPercentage / 100f) + minThrust;
//MonoBehaviour.print("thrust = " + thrust);
this.thrustVec = vecThrust;
@@ -82,68 +84,80 @@
if (this.partSim.hasVessel)
{
//MonoBehaviour.print("hasVessel is true");
- this.actualThrust = isActive ? requestedThrust : 0.0;
- if (velocityCurve != null)
- {
- this.actualThrust *= velocityCurve.Evaluate((float)velocity);
- //MonoBehaviour.print("actualThrust at velocity = " + actualThrust);
- }
-
- this.isp = atmosphereCurve.Evaluate((float)this.partSim.part.staticPressureAtm);
- if (this.isp == 0d)
- {
- MonoBehaviour.print("Isp at " + this.partSim.part.staticPressureAtm + " is zero. Flow rate will be NaN");
- }
-
- if (correctThrust && realIsp == 0)
- {
- float ispsl = atmosphereCurve.Evaluate(0);
- if (ispsl != 0)
- {
- this.thrust = this.thrust * this.isp / ispsl;
+
+ //this.actualThrust = this.isActive ? resultingThrust : 0.0;
+
+ this.isp = atmosphereCurve.Evaluate((float)atmosphere);
+
+ //if (this.isp == 0d)
+ //{
+ // MonoBehaviour.print("Isp at " + this.partSim.part.staticPressureAtm + " is zero. Flow rate will be NaN");
+ //}
+
+
+ // correctThrust is less usefull now that the stock engines do it. Keep or remove.
+
+ //if (correctThrust && realIsp == 0)
+ //{
+ // float ispsl = atmosphereCurve.Evaluate(0);
+ // if (ispsl != 0)
+ // {
+ // this.thrust = this.thrust * this.isp / ispsl;
+ // }
+ // else
+ // {
+ // MonoBehaviour.print("Isp at sea level is zero. Unable to correct thrust.");
+ // }
+ // //MonoBehaviour.print("corrected thrust = " + thrust);
+ //}
+
+ //if (velocityCurve != null)
+ //{
+ // this.thrust *= velocityCurve.Evaluate((float)velocity);
+ // //MonoBehaviour.print("thrust at velocity = " + thrust);
+ //}
+
+ float multiplier = 1;
+ if (atmChangeFlow)
+ {
+ multiplier = (float)(partSim.part.atmDensity / 1.225);
+ if (atmCurve != null)
+ {
+ multiplier = atmCurve.Evaluate(multiplier);
+ }
+ //MonoBehaviour.print("corrected thrust = " + thrust);
+ }
+ if (velCurve != null)
+ {
+ multiplier *= velCurve.Evaluate((float)machNumber);
+ }
+
+ if (throttleLocked)
+ {
+ //MonoBehaviour.print("throttleLocked is true");
+ //flowRate = this.thrust / (this.isp * 9.82);
+ flowRate = Mathf.Lerp(minFuelFlow, maxFuelFlow, (thrustPercentage / 100f)) * multiplier;
+ }
+ else
+ {
+ if (partSim.isLanded)
+ {
+ //MonoBehaviour.print("partSim.isLanded is true, mainThrottle = " + FlightInputHandler.state.mainThrottle);
+ flowRate = Mathf.Lerp(minFuelFlow, maxFuelFlow, FlightInputHandler.state.mainThrottle * (thrustPercentage / 100f)) * multiplier;
}
else
{
- MonoBehaviour.print("Isp at sea level is zero. Unable to correct thrust.");
- }
- //MonoBehaviour.print("corrected thrust = " + thrust);
- }
-
- if (velocityCurve != null)
- {
- this.thrust *= velocityCurve.Evaluate((float)velocity);
- //MonoBehaviour.print("thrust at velocity = " + thrust);
- }
-
- if (throttleLocked)
- {
- //MonoBehaviour.print("throttleLocked is true");
- flowRate = this.thrust / (this.isp * 9.82);
- }
- else
- {
- if (this.partSim.isLanded)
- {
- //MonoBehaviour.print("partSim.isLanded is true, mainThrottle = " + FlightInputHandler.state.mainThrottle);
- flowRate = Math.Max(0.000001d, this.thrust * FlightInputHandler.state.mainThrottle) / (this.isp * 9.82);
- }
- else
- {
- if (requestedThrust > 0)
- {
- if (velocityCurve != null)
- {
- requestedThrust *= velocityCurve.Evaluate((float)velocity);
- //MonoBehaviour.print("requestedThrust at velocity = " + requestedThrust);
- }
-
+ if (currentThrottle > 0)
+ {
//MonoBehaviour.print("requestedThrust > 0");
- flowRate = requestedThrust / (this.isp * 9.82);
+ //flowRate = requestedThrust / (this.isp * 9.82) * multiplier;
+ flowRate = Mathf.Lerp(minFuelFlow, maxFuelFlow, currentThrottle * (thrustPercentage / 100f)) * multiplier;
+
}
else
{
//MonoBehaviour.print("requestedThrust <= 0");
- flowRate = this.thrust / (this.isp * 9.82);
+ flowRate = Mathf.Lerp(minFuelFlow, maxFuelFlow, (thrustPercentage / 100f)) * multiplier;
}
}
}
@@ -156,27 +170,37 @@
{
MonoBehaviour.print("Isp at " + atmosphere + " is zero. Flow rate will be NaN");
}
- if (correctThrust)
- {
- float ispsl = atmosphereCurve.Evaluate(0);
- if (ispsl != 0)
- {
- this.thrust = this.thrust * this.isp / ispsl;
- }
- else
- {
- MonoBehaviour.print("Isp at sea level is zero. Unable to correct thrust.");
- }
- //MonoBehaviour.print("corrected thrust = " + thrust);
- }
-
- if (velocityCurve != null)
- {
- this.thrust *= velocityCurve.Evaluate((float)velocity);
- //MonoBehaviour.print("thrust at velocity = " + thrust);
- }
-
- flowRate = this.thrust / (this.isp * 9.82);
+ //if (correctThrust)
+ //{
+ // float ispsl = atmosphereCurve.Evaluate(0);
+ // if (ispsl != 0)
+ // {
+ // this.thrust = this.thrust * this.isp / ispsl;
+ // }
+ // else
+ // {
+ // MonoBehaviour.print("Isp at sea level is zero. Unable to correct thrust.");
+ // }
+ // //MonoBehaviour.print("corrected thrust = " + thrust);
+ //}
+
+ float multiplier = 1;
+ if (atmChangeFlow)
+ {
+ //multiplier = (float)(this.partSim.part.atmDensity / 1.225);
+ multiplier = (float)atmosphere; // technically wrong but the same for my Editor need
+ if (atmCurve != null)
+ {
+ multiplier = atmCurve.Evaluate(multiplier);
+ }
+ }
+
+ if (velCurve != null)
+ {
+ multiplier *= velCurve.Evaluate((float)machNumber);
+ }
+
+ flowRate = Mathf.Lerp(minFuelFlow, maxFuelFlow, (thrustPercentage / 100f)) * multiplier;
}
if (SimManager.logOutput)
@@ -184,6 +208,10 @@
buffer = new StringBuilder(1024);
buffer.AppendFormat("flowRate = {0:g6}\n", flowRate);
}
+
+ thrust = flowRate * (isp * IspG);
+ // I did not look into the diff between those 2 so I made them equal...
+ actualThrust = thrust;
float flowMass = 0f;
foreach (Propellant propellant in propellants)
--- a/KerbalEngineer/VesselSimulator/PartSim.cs
+++ b/KerbalEngineer/VesselSimulator/PartSim.cs
@@ -164,7 +164,7 @@
get { return this.resourceDrains; }
}
- public void CreateEngineSims(List<EngineSim> allEngines, double atmosphere, double velocity, bool vectoredThrust, LogMsg log)
+ public void CreateEngineSims(List<EngineSim> allEngines, double atmosphere, double mach, bool vectoredThrust, bool fullThrust, LogMsg log)
{
bool correctThrust = SimManager.DoesEngineUseCorrectedThrust(this.part);
if (log != null)
@@ -197,30 +197,32 @@
Vector3 thrustvec = this.CalculateThrustVector(vectoredThrust ? engine.thrustTransforms : null, log);
EngineSim engineSim = new EngineSim(this,
- atmosphere,
- velocity,
- engine.maxThrust,
- engine.minThrust,
- engine.thrustPercentage,
- engine.requestedThrust,
- thrustvec,
- engine.realIsp,
- engine.atmosphereCurve,
- engine.useVelocityCurve ? engine.velocityCurve : null,
- engine.throttleLocked,
- engine.propellants,
- engine.isOperational,
- correctThrust,
- engine.thrustTransforms);
+ atmosphere,
+ mach,
+ engine.maxFuelFlow,
+ engine.minFuelFlow,
+ engine.thrustPercentage,
+ thrustvec,
+ engine.atmosphereCurve,
+ engine.atmChangeFlow,
+ engine.useAtmCurve ? engine.atmCurve : null,
+ engine.useVelCurve ? engine.velCurve : null,
+ engine.currentThrottle,
+ engine.g,
+ engine.throttleLocked || fullThrust,
+ engine.propellants,
+ engine.isOperational,
+ correctThrust,
+ engine.thrustTransforms);
allEngines.Add(engineSim);
}
}
}
else
{
- if (this.hasModuleEnginesFX)
- {
- foreach (ModuleEnginesFX engine in this.part.GetModules<ModuleEnginesFX>())
+ if (this.hasModuleEngines)
+ {
+ foreach (ModuleEngines engine in this.part.GetModules<ModuleEngines>())
{
if (log != null)
{
@@ -230,52 +232,23 @@
Vector3 thrustvec = this.CalculateThrustVector(vectoredThrust ? engine.thrustTransforms : null, log);
EngineSim engineSim = new EngineSim(this,
- atmosphere,
- velocity,
- engine.maxThrust,
- engine.minThrust,
- engine.thrustPercentage,
- engine.requestedThrust,
- thrustvec,
- engine.realIsp,
- engine.atmosphereCurve,
- engine.useVelocityCurve ? engine.velocityCurve : null,
- engine.throttleLocked,
- engine.propellants,
- engine.isOperational,
- correctThrust,
- engine.thrustTransforms);
- allEngines.Add(engineSim);
- }
- }
-
- if (this.hasModuleEngines)
- {
- foreach (ModuleEngines engine in this.part.GetModules<ModuleEngines>())
- {
- if (log != null)
- {
- log.buf.AppendLine("Module: " + engine.moduleName);
- }
-
- Vector3 thrustvec = this.CalculateThrustVector(vectoredThrust ? engine.thrustTransforms : null, log);
-
- EngineSim engineSim = new EngineSim(this,
- atmosphere,
- velocity,
- engine.maxThrust,
- engine.minThrust,
- engine.thrustPercentage,
- engine.requestedThrust,
- thrustvec,
- engine.realIsp,
- engine.atmosphereCurve,
- engine.useVelocityCurve ? engine.velocityCurve : null,
- engine.throttleLocked,
- engine.propellants,
- engine.isOperational,
- correctThrust,
- engine.thrustTransforms);
+ atmosphere,
+ mach,
+ engine.maxFuelFlow,
+ engine.minFuelFlow,
+ engine.thrustPercentage,
+ thrustvec,
+ engine.atmosphereCurve,
+ engine.atmChangeFlow,
+ engine.useAtmCurve ? engine.atmCurve : null,
+ engine.useVelCurve ? engine.velCurve : null,
+ engine.currentThrottle,
+ engine.g,
+ engine.throttleLocked || fullThrust,
+ engine.propellants,
+ engine.isOperational,
+ correctThrust,
+ engine.thrustTransforms);
allEngines.Add(engineSim);
}
}
--- a/KerbalEngineer/VesselSimulator/SimManager.cs
+++ b/KerbalEngineer/VesselSimulator/SimManager.cs
@@ -84,7 +84,7 @@
public static Stage[] Stages { get; private set; }
- public static double Velocity { get; set; }
+ public static double Mach { get; set; }
public static String failMessage { get; private set; }
@@ -356,7 +356,7 @@
var sim = new Simulation();
// This call doesn't ever fail at the moment but we'll check and return a sensible error for display
- if (sim.PrepareSimulation(parts, Gravity, Atmosphere, Velocity, dumpTree, vectoredThrust))
+ if (sim.PrepareSimulation(parts, Gravity, Atmosphere, Mach, dumpTree, vectoredThrust))
{
ThreadPool.QueueUserWorkItem(RunSimulation, sim);
}
--- a/KerbalEngineer/VesselSimulator/Simulation.cs
+++ b/KerbalEngineer/VesselSimulator/Simulation.cs
@@ -67,7 +67,7 @@
private Vector3 vecActualThrust;
private Vector3 vecStageDeltaV;
private Vector3 vecThrust;
- private double velocity;
+ private double mach;
public String vesselName;
public VesselType vesselType;
@@ -113,7 +113,7 @@
// need during the simulation. All required data is copied from the core game data structures
// so that the simulation itself can be run in a background thread without having issues with
// the core game changing the data while the simulation is running.
- public bool PrepareSimulation(List<Part> parts, double theGravity, double theAtmosphere = 0, double theVelocity = 0, bool dumpTree = false, bool vectoredThrust = false)
+ public bool PrepareSimulation(List<Part> parts, double theGravity, double theAtmosphere = 0, double theMach = 0, bool dumpTree = false, bool vectoredThrust = false, bool fullThrust = false)
{
LogMsg log = null;
if (SimManager.logOutput)
@@ -128,7 +128,7 @@
this.partList = parts;
this.gravity = theGravity;
this.atmosphere = theAtmosphere;
- this.velocity = theVelocity;
+ this.mach = theMach;
this.lastStage = Staging.lastStage;
//MonoBehaviour.print("lastStage = " + lastStage);
@@ -175,7 +175,7 @@
}
if (partSim.isEngine)
{
- partSim.CreateEngineSims(this.allEngines, this.atmosphere, this.velocity, vectoredThrust, log);
+ partSim.CreateEngineSims(this.allEngines, this.atmosphere, this.mach, vectoredThrust, fullThrust, log);
}
partId++;