Skip to content

Commit

Permalink
Fixed bug where oxidizer wasn't being properly detected when playing …
Browse files Browse the repository at this point in the history
…mostly stock before engine was turned on
  • Loading branch information
Ryan Caskey committed Apr 30, 2018
1 parent 8196111 commit 2c27756
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 13 deletions.
20 changes: 16 additions & 4 deletions Source/EMRController/EMRController/EMRController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,17 @@ private void UpdateInFlightEMRParams()
}

if (emrInClosedLoop) {
//EMRUtils.Log("Closed Loop Detected");
EMRUtils.Log("Closed Loop Detected");
float bestEMR = GetOptimalRatioForRemainingFuel();
//EMRUtils.Log("Best EMR computed to be ", bestEMR, ":1");
EMRUtils.Log("Best EMR computed to be ", bestEMR, ":1");
string bestEMRSuffix = "";
if (bestEMR > CurrentNodePair.Max.ratio) {
//EMRUtils.Log("EMR higher than ", CurrentNodePair.Max.ratio, ":1 (was ", bestEMR, ":1), capping");
EMRUtils.Log("EMR higher than ", CurrentNodePair.Max.ratio, ":1 (was ", bestEMR, ":1), capping");
bestEMR = CurrentNodePair.Max.ratio;
bestEMRSuffix = " (max)";
}
else if (bestEMR < CurrentNodePair.Min.ratio) {
//EMRUtils.Log("EMR lower than ", CurrentNodePair.Min.ratio, ":1 (was ", bestEMR, ":1), capping");
EMRUtils.Log("EMR lower than ", CurrentNodePair.Min.ratio, ":1 (was ", bestEMR, ":1), capping");
bestEMR = CurrentNodePair.Min.ratio;
bestEMRSuffix = " (min)";
}
Expand Down Expand Up @@ -265,9 +265,19 @@ private string FormatVolumeAndMass(double diff, double density)
private float GetOptimalRatioForRemainingFuel()
{
PropellantResources propResources = new PropellantResources(engineModule);
if (propResources == null) {
EMRUtils.Log("Could not find any connected resources");
return CurrentNodePair.Min.ratio;
}

double amount;
double maxAmount;
EMRUtils.Log("Getting resources totals from part");

if (part == null) {
//DELETE ME
EMRUtils.Log("I don't know how, but part is null");
}
part.GetConnectedResourceTotals(propResources.Oxidizer.Id, out amount, out maxAmount);

double remainingOxidizer = amount;
Expand All @@ -278,6 +288,8 @@ private float GetOptimalRatioForRemainingFuel()
remainingFuel += amount;
}

EMRUtils.Log("Remaining Fuel: " + remainingFuel + ", Remaining Oxidier: " + remainingOxidizer);

if (remainingOxidizer == 0) {
return CurrentNodePair.Min.ratio;
}
Expand Down
2 changes: 1 addition & 1 deletion Source/EMRController/EMRController/PropellantResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public float Density {
public float PropellantMassFlow {
get {
float pmf = Density * Ratio;
//EMRUtils.Log("Calculating PropMassFlow for ", Name, " as ", Density, "*", Ratio, "=", pmf);
EMRUtils.Log("Calculating PropMassFlow for ", Name, " as ", Density, "*", Ratio, "=", pmf);
return pmf;
}
}
Expand Down
31 changes: 25 additions & 6 deletions Source/EMRController/EMRController/PropellantResources.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using EMRController.Utils;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -13,21 +14,31 @@ class PropellantResources : List<PropellantResource>
public PropellantResource Oxidizer {
get {
if (_oxidizer == null) {
//EMRUtils.Log("Oxidizer detection needed");
// I tried doing the following, but was getting exceptions throw
EMRUtils.Log("Oxidizer detection needed");
// I tried doing the following, but was getting exceptions thrown
//_oxidizer = this.MaxAt(prop => prop.PropellantMassFlow);

EMRUtils.Log("Looking at " + this.Count + " different propellants");
foreach (var prop in this) {
EMRUtils.Log(prop.Name + " MassFlowRate: " + prop.PropellantMassFlow);
if (prop.Name == "Oxidizer") {
EMRUtils.Log("Found \"Oxidizer\", so let's use it.");
_oxidizer = prop;
}
}
}
if (_oxidizer == null) {
//Instead, I'll just find the max and do a find on use that.
var maxMassFlow = this.Max(prop => prop.PropellantMassFlow);
var oxidizerCandidates = this.FindAll(prop => prop.PropellantMassFlow == maxMassFlow);
if (oxidizerCandidates.Count == 1) {
_oxidizer = oxidizerCandidates[0];
//EMRUtils.Log("Oxidizer detected as ", _oxidizer.Name, " (with a mass flow of ", _oxidizer.PropellantMassFlow, ")");
EMRUtils.Log("Oxidizer detected as ", _oxidizer.Name, " (with a mass flow of ", _oxidizer.PropellantMassFlow, ")");
}
else {
//Multiple candidates found, looking for this first one with "ox" in the name
_oxidizer = oxidizerCandidates.Find(prop => prop.Name.ToLower().Contains("ox"));
//EMRUtils.Log("Multiple Oxidizer candidates found, using ", _oxidizer.Name);
EMRUtils.Log("Multiple Oxidizer candidates found, using ", _oxidizer.Name);
}
}
return _oxidizer;
Expand All @@ -54,7 +65,15 @@ public float RatioTotals {

public double AverageFuelDensity {
get {
return Fuels.Sum(fuel => fuel.Ratio * fuel.Density) / (RatioTotals - Oxidizer.Ratio);
foreach (PropellantResource fuel in Fuels) {
EMRUtils.Log("Fuel: " + fuel.Name + "/" + fuel.Id + ", Ratio: " + fuel.Ratio + ", Density: " + fuel.Density);
}

float sumOfFuelsTimesDensity = Fuels.Sum(fuel => fuel.Ratio * fuel.Density);
EMRUtils.Log("Sum of fuels * density: ", sumOfFuelsTimesDensity);
EMRUtils.Log("RatioTotals: ", RatioTotals);
EMRUtils.Log("OxidizerRatio: ", Oxidizer.Ratio);
return sumOfFuelsTimesDensity / (RatioTotals - Oxidizer.Ratio);
}
}

Expand Down
4 changes: 2 additions & 2 deletions Source/EMRController/EMRController/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.3.0.0")]
[assembly: AssemblyFileVersion("1.3.0.0")]
[assembly: AssemblyVersion("1.3.1.0")]
[assembly: AssemblyFileVersion("1.3.1.0")]
3 changes: 3 additions & 0 deletions Source/EMRController/EMRController/Utils/MathUtils.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//Taken from ProceduralParts
using EMRController.Utils;
using System;
using UnityEngine;

Expand Down Expand Up @@ -69,7 +70,9 @@ public static string ToStringSI(this double value, int sigFigs = 3, int exponent
/// <returns></returns>
public static string ToStringSI(this float value, int sigFigs = 3, int exponent = 0, string unit = null)
{
EMRUtils.Log("Getting prefix for exponent of " + exponent);
SIPrefix prefix = value.GetSIPrefix(exponent);
EMRUtils.Log("Found prefix of " + prefix);
return prefix.FormatSI(value, sigFigs, exponent, unit);
}

Expand Down

0 comments on commit 2c27756

Please sign in to comment.