Skip to content

Commit

Permalink
Fixed gain/loss of resource in transfers
Browse files Browse the repository at this point in the history
Fixed gain/loss or resource in multitank to multitank transfers.
Also:
  - better comments!
  - variable name changes in order to reflect their purpose
  - creation of intermediate list in order not to duplicate skip
  conditions (if they change in only one loop where doomed)
  • Loading branch information
arivaldh committed Feb 12, 2018
1 parent ca84830 commit 47f5b95
Showing 1 changed file with 34 additions and 41 deletions.
75 changes: 34 additions & 41 deletions ShipManifest/Process/TransferPump.cs
Expand Up @@ -411,6 +411,7 @@ internal void RunPumpCycle(double cycleAmount)
if (cycleAmount > maxAmount) cycleAmount = maxAmount;
}

SmUtils.LogMessage("RunPumpCycle: cycleAmount = " + cycleAmount, SmUtils.LogType.Error, true);
// From Parts. used by Dump and Transfer.
DrainParts(cycleAmount);

Expand Down Expand Up @@ -438,49 +439,46 @@ internal void FillParts(double cycleAmount)
double cycleBalance = cycleAmount;
while (cycleBalance > SMSettings.Tolerance)
{
// calc average of parts in list.
double toPartAvgAmt = cycleAmount/ToParts.Count;

// reduce to the smallest container.
double minAmt = toPartAvgAmt;
int remainingPartsCount = 0;
// find minimum but positive remaining capacity or single tank!
double minAmt = Double.MaxValue;
List<Part>.Enumerator theseParts = ToParts.GetEnumerator();
List<Part> nonFullParts = new List<Part>();
while (theseParts.MoveNext())
{
if (theseParts.Current == null) continue;
double thisPartCap = PartRemainingCapacity(theseParts.Current, Resource);
Part part = theseParts.Current;
if (part == null) continue;
double thisPartCap = PartRemainingCapacity(part, Resource);
if (thisPartCap <= SMSettings.Tolerance) continue;
if (thisPartCap >= minAmt)
if (thisPartCap < minAmt)
{
remainingPartsCount++;
continue;
minAmt = thisPartCap;
}
minAmt = thisPartCap;
remainingPartsCount++;
nonFullParts.Add(part);
}
theseParts.Dispose();

//Utilities.LogMessage(string.Format("Inside: TransferPump.FillParts: toPartAmt = {0}, minAmt = {1}, PartsLeft = {2}, cycleBalance = {3}", toPartAmt, minAmt[0], toPartCount, cycleBalance), Utilities.LogType.Info, SMSettings.VerboseLogging);
// Calculate pump amounts for each To part and Increment.
if (remainingPartsCount > 0)
if (nonFullParts.Count > 0)
{
List<Part>.Enumerator toParts = ToParts.GetEnumerator();
double toTransfer = Math.Min(cycleBalance / nonFullParts.Count, minAmt);
List<Part>.Enumerator toParts = nonFullParts.GetEnumerator();
while (toParts.MoveNext())
{
if (toParts.Current == null) continue;
if (PartRemainingCapacity(toParts.Current, Resource) <= SMSettings.Tolerance) continue;
Part part = toParts.Current;
part.Resources[Resource].amount += minAmt;
cycleBalance -= minAmt;
part.Resources[Resource].amount += toTransfer;
cycleBalance -= toTransfer;

// Ensure part is capped and does not contain more than allowed.
if (part.Resources[Resource].amount > part.Resources[Resource].maxAmount)
{
part.Resources[Resource].amount = part.Resources[Resource].maxAmount;
}
}
toParts.Dispose();
}
//Utilities.LogMessage(string.Format("Inside: TransferPump.FillParts: toPartAmt = {0}, minAmt = {1}, PartsLeft = {2}, cycleBalance = {3}", toPartAmt, minAmt[0], toPartCount, cycleBalance), Utilities.LogType.Info, SMSettings.VerboseLogging);
if (remainingPartsCount == 0 && cycleBalance > SMSettings.Tolerance) cycleBalance = 0;
if (nonFullParts.Count == 0 && cycleBalance > SMSettings.Tolerance) cycleBalance = 0;
}
}

Expand All @@ -494,47 +492,42 @@ internal void DrainParts(double cycleAmount)
double cycleBalance = cycleAmount;
while (cycleBalance > SMSettings.Tolerance)
{
// calc average of parts in list.
double fromPartAvgAmt = cycleAmount / FromParts.Count;

// reduce to the smallest container.
double minAmt = fromPartAvgAmt;
int remainingPartsCount = 0;
// find least but positive amount of resource in single tank!
double minAmt = Double.MaxValue;
List<Part>.Enumerator theseParts = FromParts.GetEnumerator();
List<Part> nonEmptyParts = new List<Part>();
while (theseParts.MoveNext())
{
if (theseParts.Current == null) continue;
if (theseParts.Current.Resources[Resource].amount <= SMSettings.Tolerance) continue;
if (theseParts.Current.Resources[Resource].amount >= minAmt)
Part part = theseParts.Current;
if (part == null) continue;
if (part.Resources[Resource].amount <= SMSettings.Tolerance) continue;
if (part.Resources[Resource].amount < minAmt)
{
remainingPartsCount++;
continue;
minAmt = part.Resources[Resource].amount;
}
minAmt = theseParts.Current.Resources[Resource].amount;
remainingPartsCount++;
nonEmptyParts.Add(theseParts.Current);
}
theseParts.Dispose();

//Utilities.LogMessage(string.Format("Inside: TransferPump.DrainParts: fromPartAmt = {0}, minAmt = {1}, PartsLeft = {2}, cycleBalance = {3}", fromPartAmt, minAmt, fromPartCount, cycleBalance), Utilities.LogType.Info, SMSettings.VerboseLogging);
// Decrement.
if (remainingPartsCount > 0)
if (nonEmptyParts.Count > 0)
{
List<Part>.Enumerator fromParts = FromParts.GetEnumerator();
double toTransfer = Math.Min(cycleBalance / nonEmptyParts.Count, minAmt);
List<Part>.Enumerator fromParts = nonEmptyParts.GetEnumerator();
while (fromParts.MoveNext())
{
if (fromParts.Current == null) continue;
if (fromParts.Current.Resources[Resource].amount <= SMSettings.Tolerance) continue;
Part part = fromParts.Current;
part.Resources[Resource].amount -= minAmt;
cycleBalance -= minAmt;
AmtPumped += minAmt;
part.Resources[Resource].amount -= toTransfer;
cycleBalance -= toTransfer;
AmtPumped += toTransfer;

// Ensure part is empty and does not contain less than 0.
if (part.Resources[Resource].amount <= SMSettings.Tolerance) part.Resources[Resource].amount = 0;
}
fromParts.Dispose();
}
if (remainingPartsCount == 0 && cycleBalance > SMSettings.Tolerance) cycleBalance = 0;
if (nonEmptyParts.Count == 0 && cycleBalance > SMSettings.Tolerance) cycleBalance = 0;
//Utilities.LogMessage(string.Format("Inside: TransferPump.DrainParts: fromPartAmt = {0}, minAmt = {1}, PartsLeft = {2}, cycleBalance = {3}", fromPartAmt, minAmt, fromPartCount, cycleBalance), Utilities.LogType.Info, SMSettings.VerboseLogging);
}
}
Expand Down

0 comments on commit 47f5b95

Please sign in to comment.