Merge remote-tracking branch 'cybutek/master'
Merge remote-tracking branch 'cybutek/master'

--- a/AttachNodeSim.cs
+++ b/AttachNodeSim.cs
@@ -51,7 +51,10 @@
             return nodeSim;
         }
 
-        static private void Reset(AttachNodeSim attachNodeSim) { }
+        static private void Reset(AttachNodeSim attachNodeSim)
+        {
+            attachNodeSim.attachedPartSim = null;
+        }
 
 
         public void Release()

--- a/EngineSim.cs
+++ b/EngineSim.cs
@@ -39,6 +39,7 @@
         public PartSim partSim;
         public List<AppliedForce> appliedForces = new List<AppliedForce>();
         public float maxMach;
+        public bool isFlamedOut;
 
         public double thrust = 0;
 
@@ -54,6 +55,7 @@
         {
             engineSim.resourceConsumptions.Reset();
             engineSim.resourceFlowModes.Reset();
+            engineSim.partSim = null;
             engineSim.actualThrust = 0;
             engineSim.isActive = false;
             engineSim.isp = 0;
@@ -64,6 +66,7 @@
             engineSim.appliedForces.Clear();
             engineSim.thrust = 0;
             engineSim.maxMach = 0f;
+            engineSim.isFlamedOut = false;
         }
 
         public void Release()
@@ -97,6 +100,7 @@
             List<Propellant> propellants = engineMod.propellants;
             bool active = engineMod.isOperational;
             float resultingThrust = engineMod.resultingThrust;
+            bool isFlamedOut = engineMod.flameout;
 			
 			EngineSim engineSim = pool.Borrow();
 
@@ -106,6 +110,7 @@
             engineSim.partSim = theEngine;
             engineSim.isActive = active;
             engineSim.thrustVec = vecThrust;
+            engineSim.isFlamedOut = isFlamedOut;
             engineSim.resourceConsumptions.Reset();
             engineSim.resourceFlowModes.Reset();
             engineSim.appliedForces.Clear();

file:a/PartSim.cs -> file:b/PartSim.cs
--- a/PartSim.cs
+++ b/PartSim.cs
@@ -86,9 +86,11 @@
             }
             partSim.attachNodes.Clear();
             partSim.fuelTargets.Clear();
+            partSim.surfaceMountFuelTargets.Clear();
             partSim.resourceDrains.Reset();
             partSim.resourceFlowStates.Reset();
             partSim.resources.Reset();
+            partSim.parent = null;
             partSim.baseCost = 0d;
             partSim.baseMass = 0d;
             partSim.baseMassForCoM = 0d;
@@ -626,6 +628,17 @@
                     this.fuelTargets[i] = null;
                 }
             }
+
+            // Loop through the surface attached fuel targets (surface attached parts for new flow modes)
+            for (int i = 0; i < this.surfaceMountFuelTargets.Count; i++)
+            {
+                PartSim fuelTargetSim = this.surfaceMountFuelTargets[i];
+                // If the part is in the set then "remove" it by clearing the PartSim reference
+                if (fuelTargetSim != null && partSims.Contains(fuelTargetSim))
+                {
+                    this.surfaceMountFuelTargets[i] = null;
+                }
+            }
         }
 
         public void SetupAttachNodes(Dictionary<Part, PartSim> partSimLookup, LogMsg log)
@@ -764,7 +777,8 @@
 
         private bool IsDecoupler(Part thePart)
         {
-            return thePart.GetProtoModuleDecoupler()?.IsStageEnabled ?? false;
+            PartExtensions.ProtoModuleDecoupler protoDecoupler = thePart.GetProtoModuleDecoupler();
+            return protoDecoupler != null && protoDecoupler.IsStageEnabled;
         }
 
         private bool IsSepratron()

--- a/Simulation.cs
+++ b/Simulation.cs
@@ -30,7 +30,6 @@
 
 namespace KerbalEngineer.VesselSimulator
 {
-    using System.ComponentModel;
     using CompoundParts;
     using Extensions;
     using Helpers;
@@ -793,7 +792,7 @@
             for (int i = 0; i < allEngines.Count; ++i)
             {
                 EngineSim engine = allEngines[i];
-                if (engine.isActive)
+                if (engine.isActive && engine.isFlamedOut == false)
                 {
                     this.activeEngines.Add(engine);
                 }