Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix stage:liquidfuel #1082

Merged
merged 1 commit into from
Jul 1, 2015
Merged
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
38 changes: 31 additions & 7 deletions src/kOS/Suffixed/StageValues.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System.Linq;
using kOS.Safe.Encapsulation;
using kOS.Safe.Encapsulation;
using kOS.Safe.Encapsulation.Suffixes;
using kOS.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;

namespace kOS.Suffixed
{
Expand All @@ -21,7 +23,6 @@ private void InitializeSuffixes()
AddSuffix("NUMBER", new Suffix<int>(() => Staging.CurrentStage));
AddSuffix("READY", new Suffix<bool>(() => shared.Vessel.isActiveVessel && Staging.separate_ready));
AddSuffix("RESOURCES", new Suffix<ListValue<ActiveResourceValue>>(GetResourceManifest));

}

private ListValue<ActiveResourceValue> GetResourceManifest()
Expand Down Expand Up @@ -56,9 +57,32 @@ private bool IsResource(string suffixName)

private double? GetResourceOfCurrentStage(string resourceName)
{
var resource = shared.Vessel.GetActiveResources();
var match = resource.FirstOrDefault(r => string.Equals(r.info.name, resourceName, StringComparison.InvariantCultureIgnoreCase));
return match == null ? null : (double?) Math.Round(match.amount, 2);
PartResourceDefinition resourceDef = PartResourceLibrary.Instance.resourceDefinitions.FirstOrDefault(pr => string.Equals(pr.name, resourceName, StringComparison.CurrentCultureIgnoreCase));
List<PartResource> list = new List<PartResource>();
if (resourceDef.resourceFlowMode == ResourceFlowMode.STACK_PRIORITY_SEARCH)
{
var engines = VesselUtils.GetListOfActivatedEngines(shared.Vessel);
foreach (var engine in engines)
{
engine.GetConnectedResources(resourceDef.id, resourceDef.resourceFlowMode, list);
}
}
else
{
shared.Vessel.rootPart.GetConnectedResources(resourceDef.id, resourceDef.resourceFlowMode, list);
}
if (list.Count == 0)
{
return null;
}
double available = 0.0;
double capacity = 0.0;
foreach (var resource in list)
{
available += resource.amount;
capacity += resource.maxAmount;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hvacengi were you hoping to use this for something? it doesnt look like it is being used after the sum. If not you could write

 double available = list.Sum(resource => resource.amount);
 return Math.Round(available, 2);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah... I only did that while I was working on it because I thought it might be useful in the future. There's no reason to be doing the extra math, and I'm almost always in favor of more LINQ. Want me to make the change?

}
return Math.Round(available, 2);
}

public override string ToString()
Expand Down