Skip to content

Commit

Permalink
Support 0 volume tanks without bad things happening (#334)
Browse files Browse the repository at this point in the history
  • Loading branch information
siimav authored Feb 4, 2024
1 parent d244cb0 commit 7a4e85e
Showing 1 changed file with 28 additions and 24 deletions.
52 changes: 28 additions & 24 deletions Source/Tanks/ModuleFuelTanks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -686,37 +686,41 @@ protected void ChangeResources (double volumeRatio, bool propagate = false)

public void ChangeTotalVolume (double newTotalVolume, bool propagate = false)
{
double oldVolume = volume;
double newVolume = Math.Round (newTotalVolume * utilization * 0.01d, 4);
totalVolume = newTotalVolume;
volume = newVolume;

if (Double.IsInfinity(newVolume / volume))
if (oldVolume > 0) // Can't rescale resource amounts if previously the tank had 0 volume and thus also no resources
{
totalVolume = newTotalVolume;
volume = newVolume;
Debug.LogWarning("[ModularFuelTanks] caught DIV/0 in ChangeTotalVolume. Setting volume/totalVolume and exiting function");
return;
}
double volumeRatio = newVolume / volume;
double volumeRatio = newVolume / oldVolume;
bool doResources = false;
if (oldVolume > newVolume)
{
ChangeResources (volumeRatio, propagate);
}
else
{
doResources = true;
}

bool doResources = false;
if (propagate)
{
foreach (Part p in part.symmetryCounterparts)
{
// FIXME: Not safe, assumes only 1 MFT on the part.
ModuleFuelTanks m = p.FindModuleImplementing<ModuleFuelTanks>();
m.totalVolume = newTotalVolume;
m.volume = newVolume;
}
}

if (volume > newVolume) {
ChangeResources (volumeRatio, propagate);
} else {
doResources = true;
}
totalVolume = newTotalVolume;
volume = newVolume;
if (propagate) {
foreach (Part p in part.symmetryCounterparts) {
// FIXME: Not safe, assumes only 1 MFT on the part.
ModuleFuelTanks m = p.FindModuleImplementing<ModuleFuelTanks>();
m.totalVolume = newTotalVolume;
m.volume = newVolume;
if (doResources)
{
ChangeResources (volumeRatio, propagate);
}
}
if (doResources) {
ChangeResources (volumeRatio, propagate);
}

massDirty = true;
}

Expand Down

0 comments on commit 7a4e85e

Please sign in to comment.