From 2c277565bd51e8aa96488e8954b69aefb4f68a0f Mon Sep 17 00:00:00 2001 From: Ryan Caskey Date: Mon, 30 Apr 2018 18:45:12 +0000 Subject: [PATCH] Fixed bug where oxidizer wasn't being properly detected when playing mostly stock before engine was turned on --- .../EMRController/EMRController.cs | 20 +++++++++--- .../EMRController/PropellantResource.cs | 2 +- .../EMRController/PropellantResources.cs | 31 +++++++++++++++---- .../EMRController/Properties/AssemblyInfo.cs | 4 +-- .../EMRController/Utils/MathUtils.cs | 3 ++ 5 files changed, 47 insertions(+), 13 deletions(-) diff --git a/Source/EMRController/EMRController/EMRController.cs b/Source/EMRController/EMRController/EMRController.cs index 2afd302..51f61c5 100644 --- a/Source/EMRController/EMRController/EMRController.cs +++ b/Source/EMRController/EMRController/EMRController.cs @@ -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)"; } @@ -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; @@ -278,6 +288,8 @@ private float GetOptimalRatioForRemainingFuel() remainingFuel += amount; } + EMRUtils.Log("Remaining Fuel: " + remainingFuel + ", Remaining Oxidier: " + remainingOxidizer); + if (remainingOxidizer == 0) { return CurrentNodePair.Min.ratio; } diff --git a/Source/EMRController/EMRController/PropellantResource.cs b/Source/EMRController/EMRController/PropellantResource.cs index 258f3ee..9646d03 100644 --- a/Source/EMRController/EMRController/PropellantResource.cs +++ b/Source/EMRController/EMRController/PropellantResource.cs @@ -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; } } diff --git a/Source/EMRController/EMRController/PropellantResources.cs b/Source/EMRController/EMRController/PropellantResources.cs index 3e6cd04..452559d 100644 --- a/Source/EMRController/EMRController/PropellantResources.cs +++ b/Source/EMRController/EMRController/PropellantResources.cs @@ -1,4 +1,5 @@ -using System; +using EMRController.Utils; +using System; using System.Collections; using System.Collections.Generic; using System.Linq; @@ -13,21 +14,31 @@ class PropellantResources : List 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; @@ -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); } } diff --git a/Source/EMRController/EMRController/Properties/AssemblyInfo.cs b/Source/EMRController/EMRController/Properties/AssemblyInfo.cs index 985182b..1feafce 100644 --- a/Source/EMRController/EMRController/Properties/AssemblyInfo.cs +++ b/Source/EMRController/EMRController/Properties/AssemblyInfo.cs @@ -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")] diff --git a/Source/EMRController/EMRController/Utils/MathUtils.cs b/Source/EMRController/EMRController/Utils/MathUtils.cs index 0814f9c..d6c23e7 100644 --- a/Source/EMRController/EMRController/Utils/MathUtils.cs +++ b/Source/EMRController/EMRController/Utils/MathUtils.cs @@ -1,4 +1,5 @@ //Taken from ProceduralParts +using EMRController.Utils; using System; using UnityEngine; @@ -69,7 +70,9 @@ public static string ToStringSI(this double value, int sigFigs = 3, int exponent /// 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); }