Skip to content

Commit

Permalink
Merge pull request #65 from RCrockford/prop-flow-fix
Browse files Browse the repository at this point in the history
Corrected mass drain rates for propellants with ignoreForIsp set
  • Loading branch information
jrbudda authored Dec 30, 2020
2 parents 83f8282 + daaf7c3 commit 00038f6
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 20 deletions.
Binary file modified Assets/Plugins/KerbalEngineer.Unity.dll
Binary file not shown.
46 changes: 30 additions & 16 deletions KerbalEngineer/VesselSimulator/EngineSim.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ public class EngineSim
{
private static readonly Pool<EngineSim> pool = new Pool<EngineSim>(Create, Reset);

private readonly ResourceContainer resourceConsumptions = new ResourceContainer();
// track draining for mass and isp separately, since some fuels are not used for isp calcs but do need to be included for mass draining.
private readonly ResourceContainer resourceConsumptionsForMass = new ResourceContainer();
private readonly ResourceContainer resourceConsumptionsForIsp = new ResourceContainer();
private readonly ResourceContainer resourceFlowModes = new ResourceContainer();

public double actualThrust = 0;
Expand All @@ -54,7 +56,8 @@ private static EngineSim Create()

private static void Reset(EngineSim engineSim)
{
engineSim.resourceConsumptions.Reset();
engineSim.resourceConsumptionsForMass.Reset();
engineSim.resourceConsumptionsForIsp.Reset();
engineSim.resourceFlowModes.Reset();
engineSim.partSim = null;
engineSim.actualThrust = 0;
Expand Down Expand Up @@ -126,7 +129,8 @@ public static EngineSim New(PartSim theEngine,
engineSim.isActive = active;
engineSim.thrustVec = vecThrust;
engineSim.isFlamedOut = isFlamedOut;
engineSim.resourceConsumptions.Reset();
engineSim.resourceConsumptionsForMass.Reset();
engineSim.resourceConsumptionsForIsp.Reset();
engineSim.resourceFlowModes.Reset();
engineSim.appliedForces.Clear();

Expand Down Expand Up @@ -194,8 +198,7 @@ public static EngineSim New(PartSim theEngine,
for (int i = 0; i < propellants.Count; ++i)
{
Propellant propellant = propellants[i];
if (!propellant.ignoreForIsp)
flowMass += propellant.ratio * ResourceContainer.GetResourceDensity(propellant.id);
flowMass += propellant.ratio * ResourceContainer.GetResourceDensity(propellant.id);
}

if (log != null) log.buf.AppendFormat("flowMass = {0:g6}\n", flowMass);
Expand All @@ -204,7 +207,7 @@ public static EngineSim New(PartSim theEngine,
{
Propellant propellant = propellants[i];

if (propellant.ignoreForIsp || propellant.name == "ElectricCharge" || propellant.name == "IntakeAir")
if (propellant.name == "ElectricCharge" || propellant.name == "IntakeAir")
{
continue;
}
Expand All @@ -216,7 +219,10 @@ public static EngineSim New(PartSim theEngine,
theEngine.name,
theEngine.partId,
consumptionRate);
engineSim.resourceConsumptions.Add(propellant.id, consumptionRate);
// Add all for mass
engineSim.resourceConsumptionsForMass.Add(propellant.id, consumptionRate);
if (!propellant.ignoreForIsp)
engineSim.resourceConsumptionsForIsp.Add(propellant.id, consumptionRate);
engineSim.resourceFlowModes.Add(propellant.id, (double)propellant.GetFlowMode());
}

Expand Down Expand Up @@ -259,11 +265,19 @@ private static Vector3 CalculateThrustVector(List<Transform> thrustTransforms, L
return thrustvec;
}

public ResourceContainer ResourceConsumptions
public ResourceContainer ResourceConsumptionsForMass
{
get
{
return resourceConsumptionsForMass;
}
}

public ResourceContainer ResourceConsumptionsForIsp
{
get
{
return resourceConsumptions;
return resourceConsumptionsForIsp;
}
}

Expand Down Expand Up @@ -368,9 +382,9 @@ public bool SetResourceDrains(LogMsg log, List<PartSim> allParts, List<PartSim>
}
//DumpSourcePartSets(log, "after clear");

for (int index = 0; index < this.resourceConsumptions.Types.Count; index++)
for (int index = 0; index < this.resourceConsumptionsForMass.Types.Count; index++)
{
int type = this.resourceConsumptions.Types[index];
int type = this.resourceConsumptionsForMass.Types[index];

HashSet<PartSim> sourcePartSet;
if (!sourcePartSets.TryGetValue(type, out sourcePartSet))
Expand Down Expand Up @@ -484,9 +498,9 @@ public bool SetResourceDrains(LogMsg log, List<PartSim> allParts, List<PartSim>
}

// If we don't have sources for all the needed resources then return false without setting up any drains
for (int i = 0; i < this.resourceConsumptions.Types.Count; i++)
for (int i = 0; i < this.resourceConsumptionsForMass.Types.Count; i++)
{
int type = this.resourceConsumptions.Types[i];
int type = this.resourceConsumptionsForMass.Types[i];
HashSet<PartSim> sourcePartSet;
if (!sourcePartSets.TryGetValue(type, out sourcePartSet) || sourcePartSet.Count == 0)
{
Expand All @@ -497,12 +511,12 @@ public bool SetResourceDrains(LogMsg log, List<PartSim> allParts, List<PartSim>
}

// Now we set the drains on the members of the sets and update the draining parts set
for (int i = 0; i < this.resourceConsumptions.Types.Count; i++)
for (int i = 0; i < this.resourceConsumptionsForMass.Types.Count; i++)
{
int type = this.resourceConsumptions.Types[i];
int type = this.resourceConsumptionsForMass.Types[i];
HashSet<PartSim> sourcePartSet = sourcePartSets[type];
ResourceFlowMode mode = (ResourceFlowMode)resourceFlowModes[type];
double consumption = resourceConsumptions[type];
double consumption = resourceConsumptionsForMass[type];
double amount = 0d;
double total = 0d;
if (mode == ResourceFlowMode.ALL_VESSEL_BALANCE ||
Expand Down
8 changes: 4 additions & 4 deletions KerbalEngineer/VesselSimulator/Simulation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -713,8 +713,8 @@ private void CalculateThrustAndISP() {
vecThrust += ((float)engine.thrust * engine.thrustVec);
vecActualThrust += ((float)engine.actualThrust * engine.thrustVec);

totalStageFlowRate += engine.ResourceConsumptions.Mass;
totalStageIspFlowRate += engine.ResourceConsumptions.Mass * engine.isp;
totalStageFlowRate += engine.ResourceConsumptionsForIsp.Mass;
totalStageIspFlowRate += engine.ResourceConsumptionsForIsp.Mass * engine.isp;

for (int j = 0; j < engine.appliedForces.Count; ++j) {
totalStageThrustForce.AddForce(engine.appliedForces[j]);
Expand Down Expand Up @@ -943,8 +943,8 @@ private void UpdateResourceDrains() {
// Set the resource drains for this engine
if (engine.SetResourceDrains(log, allParts, allFuelLines, drainingParts)) {
// If it is active then add the consumed resource types to the set
for (int j = 0; j < engine.ResourceConsumptions.Types.Count; ++j) {
drainingResources.Add(engine.ResourceConsumptions.Types[j]);
for (int j = 0; j < engine.ResourceConsumptionsForMass.Types.Count; ++j) {
drainingResources.Add(engine.ResourceConsumptionsForMass.Types[j]);
}
}
}
Expand Down
Binary file modified Output/KerbalEngineer/KerbalEngineer.Unity.dll
Binary file not shown.
Binary file modified Output/KerbalEngineer/KerbalEngineer.dll
Binary file not shown.

0 comments on commit 00038f6

Please sign in to comment.