Skip to content

Commit

Permalink
Merge pull request #1 from NathanKell/master
Browse files Browse the repository at this point in the history
Updates
  • Loading branch information
RedAV8R committed Jun 7, 2014
2 parents fc3af82 + 46c8dff commit 20fba28
Showing 1 changed file with 33 additions and 98 deletions.
131 changes: 33 additions & 98 deletions Source/ModularFuelTanks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -339,16 +339,6 @@ public void Save(ConfigNode node)
node.AddValue("amount", amountExpression);
node.AddValue("maxAmount", maxAmountExpression);
}
else
{
// TODO: I don't think this is necicary anymore.

// You would think we want to do this only in the editor, but
// as it turns out, KSP is terrible about consistently setting
// up resources between the editor and the launchpad.
node.AddValue("amount", amount.ToString("G17"));
node.AddValue("maxAmount", maxAmount.ToString("G17"));
}
}

//------------------- Constructor
Expand All @@ -357,25 +347,15 @@ public FuelTank(ConfigNode node)
Load(node);
}

public FuelTank(ModuleFuelTanks module, ConfigNode node)
{
this.module = module;
Load(node);
}

public FuelTank CreateConcreteCopy(ModuleFuelTanks toModule)
public FuelTank CreateCopy(ModuleFuelTanks toModule, bool initializeAmounts)
{
FuelTank clone = (FuelTank)MemberwiseClone();
clone.module = toModule;
clone.InitializeAmounts();

return clone;
}
if(initializeAmounts)
clone.InitializeAmounts();
else
clone.amountExpression = clone.maxAmountExpression = null;

internal FuelTank CreateCopy(ModuleFuelTanks toModule)
{
FuelTank clone = (FuelTank)MemberwiseClone();
clone.module = toModule;
return clone;
}
}
Expand All @@ -391,25 +371,17 @@ public FuelTankList(ConfigNode node)
Load(node);
}

public FuelTankList(ModuleFuelTanks module, ConfigNode node)
{
foreach (ConfigNode tankNode in node.GetNodes("TANK"))
{
Add(new FuelTank(module, tankNode));
}
}

protected override string GetKeyForItem(FuelTank item)
{
return item.name;
}

public void CreateConcreteCopy(ModuleFuelTanks module, FuelTankList copyInto)
public void CreateCopy(ModuleFuelTanks module, FuelTankList copyInto, bool initializeAmounts)
{
foreach (FuelTank tank in this)
{
copyInto.Remove(tank.name);
copyInto.Add(tank.CreateConcreteCopy(module));
copyInto.Add(tank.CreateCopy(module, initializeAmounts));
}
}

Expand Down Expand Up @@ -499,37 +471,27 @@ public override void OnLoad(ConfigNode node)

typesAvailable = node.GetValues("typeAvailable");
}
else if (GameSceneFilter.Flight.IsLoaded())
else if (GameSceneFilter.AnyEditorOrFlight.IsLoaded())
{
// We're in flight, load the concrete tanks directly
LoadConcreteTankList(node);

// Ensure the old type matches so what's set up doesn't get clobbered.
oldType = type;
// The amounts initialized flag is there so that the tank type loading doesn't
// try to set up any resources. They'll get loaded directly from the save.
UpdateTankType(false);

// Destroy any resources still hanging around from the LOADING phase
for (int i = part.Resources.Count - 1; i >= 0; --i)
{
PartResource partResource = part.Resources[i];
if (!tankList.Contains(partResource.resourceName))
continue;
DestroyImmediate(partResource);
part.Resources.list.RemoveAt(i);
}
RaiseResourceListChanged();

// Setup the mass
part.mass = mass;
MassChanged(mass);
}
else if (GameSceneFilter.AnyEditor.IsLoaded())
{
// We've loaded a stored vessel / assembly in the editor.
// Initialize as per normal editor mode, but copy the fill fraction
// from the old data.
LoadConcreteTankList(node);

if (type != null)
{
FuelTankList loadedTanks = tankList;

UpdateTankType();

FuelTank newTank;
foreach (FuelTank oldTank in loadedTanks)
if(tankList.TryGet(oldTank.name, out newTank))
newTank.fillFraction = oldTank.fillFraction;
}
}
}
}
catch (Exception ex)
Expand All @@ -543,6 +505,8 @@ public override string GetInfo()
try
{
UpdateTankType();


if (dedicated)
return string.Empty;

Expand Down Expand Up @@ -573,10 +537,7 @@ public override void OnStart(StartState state)
// Basically do the guts of what UpdateTankType would do
// only copying everything from the source.
ModuleFuelTanks origTanks = original.GetComponent<ModuleFuelTanks>();
foreach (FuelTank origTank in origTanks.tankList)
{
tankList.Add(origTank.CreateCopy(this));
}
origTanks.tankList.CreateCopy(this, tankList, false);
pressurizedFuels = new Dictionary<string, bool>(origTanks.pressurizedFuels);

massDirty = true;
Expand All @@ -603,20 +564,6 @@ public override void OnSave (ConfigNode node)
base.OnSave(node);

node.AddValue("volume", volume.ToString("G17")); // no KSPField support for doubles

#if DEBUG
print ("========ModuleFuelTanks.OnSave called. Node is:=======");
print (node.ToString ());
#endif
foreach (FuelTank tank in tankList) {
ConfigNode subNode = new ConfigNode("TANK");
tank.Save (subNode);
#if DEBUG
print ("========ModuleFuelTanks.OnSave adding subNode:========");
print (subNode.ToString());
#endif
node.AddNode (subNode);
}
}
catch (Exception ex)
{
Expand Down Expand Up @@ -724,11 +671,6 @@ private void LoadTankListOverridesInEditor()
overrideList = new FuelTankList(overrideListNodes);
}

private void LoadConcreteTankList(ConfigNode node)
{
tankList = new FuelTankList(this, node);
}

private void InitializeTankType()
{
if (typesAvailable == null || typesAvailable.Length <= 1)
Expand All @@ -744,7 +686,7 @@ private void InitializeTankType()
UpdateTankType();
}

private void UpdateTankType()
private void UpdateTankType(bool initializeAmounts = true)
{
if (oldType == type || type == null)
return;
Expand All @@ -767,10 +709,10 @@ private void UpdateTankType()
FuelTankList oldList = tankList;

tankList = new FuelTankList();
def.tankList.CreateConcreteCopy(this, tankList);
def.tankList.CreateCopy(this, tankList, initializeAmounts);

LoadTankListOverridesInEditor();
overrideList.CreateConcreteCopy(this, tankList);
overrideList.CreateCopy(this, tankList, initializeAmounts);

// Destroy resources that are in either the new or the old type.
bool needsMesage = false;
Expand Down Expand Up @@ -1499,18 +1441,15 @@ public FuelInfo(Part engine, ModuleFuelTanks tank)
ratioFactor = 0.0;
efficiency = 0.0;

propellants = new List<Propellant>();
if (engine.Modules.Contains("ModuleEnginesFX"))
{
ModuleEnginesFX e = (ModuleEnginesFX)engine.Modules["ModuleEnginesFX"];
foreach (Propellant p in e.propellants)
propellants.Add(p);
propellants = e.propellants;
}
else if (engine.Modules.Contains("ModuleEngines"))
{
ModuleEngines e = (ModuleEngines)engine.Modules["ModuleEngines"];
foreach (Propellant p in e.propellants)
propellants.Add(p);
propellants = e.propellants;
}

foreach (Propellant tfuel in propellants)
Expand All @@ -1521,16 +1460,12 @@ public FuelInfo(Part engine, ModuleFuelTanks tank)
ratioFactor = 0.0;
break;
}
if (PartResourceLibrary.Instance.GetDefinition(tfuel.name).resourceTransferMode == ResourceTransferMode.NONE)
{
//ignore this propellant, since it isn't serviced by fuel tanks
}
else
if (tank.dedicated || PartResourceLibrary.Instance.GetDefinition(tfuel.name).resourceTransferMode != ResourceTransferMode.NONE)
{
FuelTank t;
if (tank.tankList.TryGet(tfuel.name, out t))
{
efficiency += tfuel.ratio / t.utilization;
efficiency += tfuel.ratio/t.utilization;
ratioFactor += tfuel.ratio;
}
else if (!IgnoreFuel(tfuel.name))
Expand All @@ -1557,7 +1492,7 @@ private void ConfigureFor(FuelInfo fi)
double availableVolume = AvailableVolume;
foreach (Propellant tfuel in fi.propellants)
{
if (PartResourceLibrary.Instance.GetDefinition(tfuel.name).resourceTransferMode != ResourceTransferMode.NONE)
if (dedicated || PartResourceLibrary.Instance.GetDefinition(tfuel.name).resourceTransferMode != ResourceTransferMode.NONE)
{
FuelTank tank;
if (tankList.TryGet(tfuel.name, out tank))
Expand Down

0 comments on commit 20fba28

Please sign in to comment.