Merge remote-tracking branch 'cybutek/experimentals' into exp
Merge remote-tracking branch 'cybutek/experimentals' into exp

--- a/EngineSim.cs
+++ b/EngineSim.cs
@@ -72,26 +72,33 @@
         }
 
         public static EngineSim New(PartSim theEngine,
-                         double atmosphere,
-                         float machNumber,
-                         float maxFuelFlow,
-                         float minFuelFlow,
-                         float thrustPercentage,
-                         Vector3 vecThrust,
-                         FloatCurve atmosphereCurve,
-                         bool atmChangeFlow,
-                         FloatCurve atmCurve,
-                         FloatCurve velCurve,
-                         float currentThrottle,
-                         float IspG,
-                         bool throttleLocked,
-                         List<Propellant> propellants,
-                         bool active,
-                         float resultingThrust,
-                         List<Transform> thrustTransforms,
-                        LogMsg log)
-        {
-            EngineSim engineSim = pool.Borrow();
+									ModuleEngines engineMod,
+									double atmosphere,
+									float machNumber,
+									bool vectoredThrust,
+									bool fullThrust,
+									LogMsg log)
+        {
+            float maxFuelFlow = engineMod.maxFuelFlow;
+            float minFuelFlow = engineMod.minFuelFlow;
+            float thrustPercentage = engineMod.thrustPercentage;
+            List<Transform> thrustTransforms = engineMod.thrustTransforms;
+            List<float> thrustTransformMultipliers = engineMod.thrustTransformMultipliers;
+            Vector3 vecThrust = CalculateThrustVector(vectoredThrust ? thrustTransforms : null,
+                                                        vectoredThrust ? thrustTransformMultipliers : null,
+                                                        log);
+            FloatCurve atmosphereCurve = engineMod.atmosphereCurve;
+            bool atmChangeFlow = engineMod.atmChangeFlow;
+            FloatCurve atmCurve = engineMod.useAtmCurve ? engineMod.atmCurve : null;
+            FloatCurve velCurve = engineMod.useVelCurve ? engineMod.velCurve : null;
+            float currentThrottle = engineMod.currentThrottle;
+            float IspG = engineMod.g;
+            bool throttleLocked = engineMod.throttleLocked || fullThrust;
+            List<Propellant> propellants = engineMod.propellants;
+            bool active = engineMod.isOperational;
+            float resultingThrust = engineMod.resultingThrust;
+			
+			EngineSim engineSim = pool.Borrow();
 
             engineSim.isp = 0.0;
             engineSim.maxMach = 0.0f;
@@ -120,7 +127,7 @@
                     log.buf.AppendFormat("actual  = {0:g6}\n", engineSim.actualThrust);
                 }
 
-                if (throttleLocked)
+				if (throttleLocked)
                 {
                     if (log != null) log.buf.AppendLine("throttleLocked is true, using thrust for flowRate");
                     flowRate = GetFlowRate(engineSim.thrust, engineSim.isp);
@@ -129,7 +136,8 @@
                 {
                     if (currentThrottle > 0.0f && engineSim.partSim.isLanded == false)
                     {
-                        if (log != null) log.buf.AppendLine("throttled up and not landed, using actualThrust for flowRate");
+						// TODO: This bit doesn't work for RF engines
+						if (log != null) log.buf.AppendLine("throttled up and not landed, using actualThrust for flowRate");
                         flowRate = GetFlowRate(engineSim.actualThrust, engineSim.isp);
                     }
                     else
@@ -190,19 +198,44 @@
                 engineSim.resourceFlowModes.Add(propellant.id, (double)propellant.GetFlowMode());
             }
 
-            double thrustPerThrustTransform = engineSim.thrust / thrustTransforms.Count;
             for (int i = 0; i < thrustTransforms.Count; i++)
             {
                 Transform thrustTransform = thrustTransforms[i];
                 Vector3 direction = thrustTransform.forward.normalized;
                 Vector3 position = thrustTransform.position;
 
-                AppliedForce appliedForce = AppliedForce.New(direction * (float)thrustPerThrustTransform, position);
+                AppliedForce appliedForce = AppliedForce.New(direction * engineSim.thrust * thrustTransformMultipliers[i], position);
                 engineSim.appliedForces.Add(appliedForce);
             }
 
             return engineSim;
         }
+
+		private static Vector3 CalculateThrustVector(List<Transform> thrustTransforms, List<float> thrustTransformMultipliers, LogMsg log)
+		{
+			if (thrustTransforms == null)
+			{
+				return Vector3.forward;
+			}
+
+			Vector3 thrustvec = Vector3.zero;
+			for (int i = 0; i < thrustTransforms.Count; ++i)
+			{
+				Transform trans = thrustTransforms[i];
+
+				if (log != null) log.buf.AppendFormat("Transform = ({0:g6}, {1:g6}, {2:g6})   length = {3:g6}\n", trans.forward.x, trans.forward.y, trans.forward.z, trans.forward.magnitude);
+
+				thrustvec -= (trans.forward * thrustTransformMultipliers[i]);
+			}
+
+			if (log != null) log.buf.AppendFormat("ThrustVec  = ({0:g6}, {1:g6}, {2:g6})   length = {3:g6}\n", thrustvec.x, thrustvec.y, thrustvec.z, thrustvec.magnitude);
+
+			thrustvec.Normalize();
+
+			if (log != null) log.buf.AppendFormat("ThrustVecN = ({0:g6}, {1:g6}, {2:g6})   length = {3:g6}\n", thrustvec.x, thrustvec.y, thrustvec.z, thrustvec.magnitude);
+
+			return thrustvec;
+		}
 
         public ResourceContainer ResourceConsumptions
         {

--- a/PartExtensions.cs
+++ b/PartExtensions.cs
@@ -1,4 +1,4 @@
-// 
+// 
 //     Kerbal Engineer Redux
 // 
 //     Copyright (C) 2014 CYBUTEK
@@ -17,12 +17,12 @@
 //     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 // 
 
-using System;
-using System.Collections.Generic;
-using System.Linq;
-
 namespace KerbalEngineer.Extensions
 {
+    using System;
+    using System.Collections.Generic;
+    using CompoundParts;
+
     public static class PartExtensions
     {
         //private static Part cachePart;
@@ -42,27 +42,37 @@
         /// </summary>
         public static bool ContainsResources(this Part part)
         {
-            return part.Resources.list.Count(p => p.amount > 0d) > 0;
+            for (int i = 0; i < part.Resources.list.Count; ++i)
+            {
+                if (part.Resources.list[i].amount > 0.0)
+                {
+                    return true;
+                }
+            }
+            return false;
         }
 
         /// <summary>
         ///     Gets whether the part has fuel.
         /// </summary>
+        /* not used
         public static bool EngineHasFuel(this Part part)
         {
             PartModule cachePartModule = GetModule<ModuleEngines>(part);
             if (cachePartModule != null)
             {
-                return part.GetModuleEngines().getFlameoutState;
-            }
-            if (part.HasModule<MultiModeEngine>())
-            {
-                return part.GetModuleMultiModeEngine().getFlameoutState;
+                return (cachePartModule as ModuleEngines).getFlameoutState;
+            }
+
+            cachePartModule = GetModuleMultiModeEngine(part);
+            if (cachePartModule != null)
+            {
+                return (cachePartModule as ModuleEnginesFX).getFlameoutState;
             }
 
             return false;
         }
-
+        */
         /// <summary>
         ///     Gets the cost of the part excluding resources.
         /// </summary>
@@ -90,7 +100,7 @@
             {
                 PartModule pm = part.Modules[i];
                 if (pm is IPartCostModifier)
-                    cost += (pm as IPartCostModifier).GetModuleCost(defaultCost);
+                    cost += (pm as IPartCostModifier).GetModuleCost(defaultCost, ModifierStagingSituation.CURRENT);
             }
             return cost;
         }
@@ -114,24 +124,24 @@
         /// <summary>
         ///     Gets the maximum thrust of the part if it's an engine.
         /// </summary>
+        /* not used
         public static double GetMaxThrust(this Part part)
         {
             PartModule cachePartModule = GetModule<ModuleEngines>(part);
             if (cachePartModule != null)
             {
-                return part.GetModuleEngines().maxThrust;
-            }
-            if (part.HasModule<MultiModeEngine>())
-            {
-                return part.GetModuleMultiModeEngine().maxThrust;
-            }
-            if (part.HasModule<ModuleEnginesFX>())
-            {
-                return part.GetModuleEnginesFx().maxThrust;
-            }
-
-            return 0d;
-        }
+                return (cachePartModule as ModuleEngines).maxThrust;
+            }
+
+            cachePartModule = GetModuleMultiModeEngine(part) ?? GetModule<ModuleEnginesFX>(part);
+            if (cachePartModule != null)
+            {
+                return (cachePartModule as ModuleEnginesFX).maxThrust;
+            }
+
+            return 0.0;
+        }
+        */
 
         /// <summary>
         ///     Gets the first typed PartModule in the part's module list.
@@ -152,7 +162,7 @@
         /// </summary>
         public static T GetModule<T>(this Part part, string className) where T : PartModule
         {
-            return (T)Convert.ChangeType(part.Modules[className], typeof(T));
+            return part.Modules[className] as T;
         }
 
         /// <summary>
@@ -160,7 +170,7 @@
         /// </summary>
         public static T GetModule<T>(this Part part, int classId) where T : PartModule
         {
-            return (T)Convert.ChangeType(part.Modules[classId], typeof(T));
+            return part.Modules[classId] as T;
         }
 
         /// <summary>
@@ -168,7 +178,7 @@
         /// </summary>
         public static ModuleAlternator GetModuleAlternator(this Part part)
         {
-            return part.GetModule<ModuleAlternator>();
+            return GetModule<ModuleAlternator>(part);
         }
 
         /// <summary>
@@ -176,7 +186,7 @@
         /// </summary>
         public static ModuleDeployableSolarPanel GetModuleDeployableSolarPanel(this Part part)
         {
-            return part.GetModule<ModuleDeployableSolarPanel>();
+            return GetModule<ModuleDeployableSolarPanel>(part);
         }
 
         /// <summary>
@@ -184,20 +194,20 @@
         /// </summary>
         public static ModuleEngines GetModuleEngines(this Part part)
         {
-            return part.GetModule<ModuleEngines>();
-        }
-
-        public static ModuleEnginesFX GetModuleEnginesFx(this Part part)
-        {
-            return part.GetModule<ModuleEnginesFX>();
-        }
+            return GetModule<ModuleEngines>(part);
+        }
+
+/*        public static ModuleEnginesFX GetModuleEnginesFx(this Part part)
+        {
+            return GetModule<ModuleEnginesFX>(part);
+        }*/
 
         /// <summary>
         ///     Gets a ModuleGenerator typed PartModule.
         /// </summary>
         public static ModuleGenerator GetModuleGenerator(this Part part)
         {
-            return part.GetModule<ModuleGenerator>();
+            return GetModule<ModuleGenerator>(part);
         }
 
         /// <summary>
@@ -205,16 +215,29 @@
         /// </summary>
         public static ModuleGimbal GetModuleGimbal(this Part part)
         {
-            return part.GetModule<ModuleGimbal>();
+            return GetModule<ModuleGimbal>(part);
         }
 
         /// <summary>
         ///     Gets the current selected ModuleEnginesFX.
         /// </summary>
-        public static ModuleEnginesFX GetModuleMultiModeEngine(this Part part)
-        {
-            var mode = part.GetModule<MultiModeEngine>().mode;
-            return part.Modules.OfType<ModuleEnginesFX>().FirstOrDefault(engine => engine.engineID == mode);
+        public static ModuleEngines GetModuleMultiModeEngine(this Part part)
+        {
+            ModuleEngines moduleEngines;
+            MultiModeEngine multiMod = GetModule<MultiModeEngine>(part);
+            if (multiMod != null)
+            {
+                string mode = multiMod.mode;
+                for (int i = 0; i < part.Modules.Count; ++i)
+                {
+                    moduleEngines = part.Modules[i] as ModuleEngines;
+                    if (moduleEngines != null && moduleEngines.engineID == mode)
+                    {
+                        return moduleEngines;
+                    }
+                }
+            }
+            return null;
         }
 
         /// <summary>
@@ -222,12 +245,12 @@
         /// </summary>
         public static ModuleParachute GetModuleParachute(this Part part)
         {
-            return part.GetModule<ModuleParachute>();
+            return GetModule<ModuleParachute>(part);
         }
 
         public static ModuleRCS GetModuleRcs(this Part part)
         {
-            return part.GetModule<ModuleRCS>();
+            return GetModule<ModuleRCS>(part);
         }
 
         /// <summary>
@@ -235,7 +258,16 @@
         /// </summary>
         public static List<T> GetModules<T>(this Part part) where T : PartModule
         {
-            return part.Modules.OfType<T>().ToList();
+            List<T> list = new List<T>();
+            for (int i = 0; i < part.Modules.Count; ++i)
+            {
+                T module = part.Modules[i] as T;
+                if (module != null)
+                {
+                    list.Add(module);
+                }
+            }
+            return list;
         }
 
         public static ProtoModuleDecoupler GetProtoModuleDecoupler(this Part part)
@@ -243,12 +275,13 @@
             PartModule cachePartModule = GetModule<ModuleDecouple>(part);
             if (cachePartModule == null)
             {
-                return new ProtoModuleDecoupler(GetModule<ModuleDecouple>(part));
-            }
-            if (HasModule<ModuleAnchoredDecoupler>(part))
-            {
-                return new ProtoModuleDecoupler(GetModule<ModuleAnchoredDecoupler>(part));
-            }
+                cachePartModule = GetModule<ModuleAnchoredDecoupler>(part);
+            }
+            if (cachePartModule != null)
+            {
+                return new ProtoModuleDecoupler(cachePartModule);
+            }
+
             return null;
         }
 
@@ -260,16 +293,15 @@
             PartModule cachePartModule = GetModule<ModuleEngines>(part);
             if (cachePartModule != null)
             {
-                return new ProtoModuleEngine(GetModule<ModuleEngines>(part));
-            }
-            if (HasModule<MultiModeEngine>(part))
-            {
-                return new ProtoModuleEngine(GetModuleMultiModeEngine(part));
-            }
-            if (HasModule<ModuleEnginesFX>(part))
-            {
-                return new ProtoModuleEngine(GetModule<ModuleEnginesFX>(part));
-            }
+                return new ProtoModuleEngine(cachePartModule);
+            }
+
+            cachePartModule = GetModuleMultiModeEngine(part) ?? GetModule<ModuleEnginesFX>(part);
+            if (cachePartModule != null)
+            {
+                return new ProtoModuleEngine(cachePartModule);
+            }
+
             return null;
         }
 
@@ -318,24 +350,24 @@
         /// <summary>
         ///     Gets the current specific impulse for the engine.
         /// </summary>
+        /* not used
         public static double GetSpecificImpulse(this Part part, float atmosphere)
         {
             PartModule cachePartModule = GetModule<ModuleEngines>(part);
             if (cachePartModule != null)
             {
-                return part.GetModuleEngines().atmosphereCurve.Evaluate(atmosphere);
-            }
-            if (part.HasModule<MultiModeEngine>())
-            {
-                return part.GetModuleMultiModeEngine().atmosphereCurve.Evaluate(atmosphere);
-            }
-            if (part.HasModule<ModuleEnginesFX>())
-            {
-                return part.GetModuleEnginesFx().atmosphereCurve.Evaluate(atmosphere);
-            }
-
-            return 0d;
-        }
+                return (cachePartModule as ModuleEngines).atmosphereCurve.Evaluate(atmosphere);
+            }
+
+            cachePartModule = GetModuleMultiModeEngine(part) ?? GetModule<ModuleEnginesFX>(part);
+            if (cachePartModule != null)
+            {
+                return (cachePartModule as ModuleEnginesFX).atmosphereCurve.Evaluate(atmosphere);
+            }
+
+            return 0.0;
+        }
+        */
 
         /// <summary>
         ///     Gets the total mass of the part including resources.
@@ -402,7 +434,7 @@
         /// </summary>
         public static bool IsCommandModule(this Part part)
         {
-            return part.HasModule<ModuleCommand>();
+            return HasModule<ModuleCommand>(part);
         }
 
         /// <summary>
@@ -410,7 +442,7 @@
         /// </summary>
         public static bool IsDecoupledInStage(this Part part, int stage)
         {
-            if ((part.IsDecoupler() || part.IsLaunchClamp()) && part.inverseStage == stage)
+            if ((IsDecoupler(part) || IsLaunchClamp(part)) && part.inverseStage == stage)
             {
                 return true;
             }
@@ -418,7 +450,7 @@
             {
                 return false;
             }
-            return part.parent.IsDecoupledInStage(stage);
+            return IsDecoupledInStage(part.parent, stage);
         }
 
         /// <summary>
@@ -426,7 +458,7 @@
         /// </summary>
         public static bool IsDecoupler(this Part part)
         {
-            return part.HasModule<ModuleDecouple>() || part.HasModule<ModuleAnchoredDecoupler>();
+            return HasModule<ModuleDecouple>(part) || HasModule<ModuleAnchoredDecoupler>(part);
         }
 
         /// <summary>
@@ -434,7 +466,7 @@
         /// </summary>
         public static bool IsEngine(this Part part)
         {
-            return part.HasModule<ModuleEngines>() || part.HasModule<ModuleEnginesFX>();
+            return HasModule<ModuleEngines>(part);
         }
 
         /// <summary>
@@ -442,7 +474,7 @@
         /// </summary>
         public static bool IsFuelLine(this Part part)
         {
-            return (part is FuelLine);
+            return HasModule<CModuleFuelLine>(part);
         }
 
         /// <summary>
@@ -450,7 +482,7 @@
         /// </summary>
         public static bool IsGenerator(this Part part)
         {
-            return part.HasModule<ModuleGenerator>();
+            return HasModule<ModuleGenerator>(part);
         }
 
         /// <summary>
@@ -458,7 +490,7 @@
         /// </summary>
         public static bool IsLaunchClamp(this Part part)
         {
-            return part.HasModule<LaunchClamp>();
+            return HasModule<LaunchClamp>(part);
         }
 
         /// <summary>
@@ -466,7 +498,7 @@
         /// </summary>
         public static bool IsParachute(this Part part)
         {
-            return part.HasModule<ModuleParachute>();
+            return HasModule<ModuleParachute>(part);
         }
 
         /// <summary>
@@ -493,7 +525,7 @@
 
         public static bool IsRcsModule(this Part part)
         {
-            return part.HasModule<ModuleRCS>();
+            return HasModule<ModuleRCS>(part);
         }
 
         /// <summary>
@@ -501,7 +533,7 @@
         /// </summary>
         public static bool IsSepratron(this Part part)
         {
-            return (part.IsSolidRocket() && part.ActivatesEvenIfDisconnected && part.IsDecoupledInStage(part.inverseStage));
+            return IsSolidRocket(part) && part.ActivatesEvenIfDisconnected && IsDecoupledInStage(part, part.inverseStage);
         }
 
         /// <summary>
@@ -509,7 +541,7 @@
         /// </summary>
         public static bool IsSolarPanel(this Part part)
         {
-            return part.HasModule<ModuleDeployableSolarPanel>();
+            return HasModule<ModuleDeployableSolarPanel>(part);
         }
 
         /// <summary>
@@ -517,7 +549,7 @@
         /// </summary>
         public static bool IsSolidRocket(this Part part)
         {
-            return (part.HasModule<ModuleEngines>() && part.GetModuleEngines().throttleLocked) || (part.HasModule<ModuleEnginesFX>() && part.GetModuleEnginesFx().throttleLocked);
+            return (part.HasModule<ModuleEngines>() && part.GetModuleEngines().throttleLocked);
         }
 
         public class ProtoModuleDecoupler
@@ -530,41 +562,50 @@
 
                 if (this.module is ModuleDecouple)
                 {
-                    this.SetModuleDecouple();
+                    SetModuleDecouple();
                 }
                 else if (this.module is ModuleAnchoredDecoupler)
                 {
-                    this.SetModuleAnchoredDecoupler();
+                    SetModuleAnchoredDecoupler();
                 }
             }
 
             public double EjectionForce { get; private set; }
             public bool IsOmniDecoupler { get; private set; }
+            public bool IsStageEnabled { get; private set; }
 
             private void SetModuleAnchoredDecoupler()
             {
-                var decoupler = this.module as ModuleAnchoredDecoupler;
+                ModuleAnchoredDecoupler decoupler = module as ModuleAnchoredDecoupler;
                 if (decoupler == null)
                 {
                     return;
                 }
 
-                this.EjectionForce = decoupler.ejectionForce;
+                EjectionForce = decoupler.ejectionForce;
+                IsStageEnabled = decoupler.stagingEnabled;
             }
 
             private void SetModuleDecouple()
             {
-                var decoupler = this.module as ModuleDecouple;
+                ModuleDecouple decoupler = module as ModuleDecouple;
                 if (decoupler == null)
                 {
                     return;
                 }
 
-                this.EjectionForce = decoupler.ejectionForce;
-                this.IsOmniDecoupler = decoupler.isOmniDecoupler;
-            }
-        }
-
+                EjectionForce = decoupler.ejectionForce;
+                IsOmniDecoupler = decoupler.isOmniDecoupler;
+                IsStageEnabled = decoupler.stagingEnabled;
+            }
+        }
+
+        // This needs updating to handle multi-mode engines and engines with multiple ModuleEngines correctly.
+        // It currently just shows the stats of the currently active module for multi-mode engines and just 
+        // the first ModuleEngines for engines with multiple modules.
+        // It should really show all the modes for multi-mode engines as separate sections.
+        // For other engines with multiple ModuleEngines it should combine the separate modules into a single set of data
+        // The constructor should be changed to take the Part itself.  It can be called if HasModule<ModuleEngines>() is true.
         public class ProtoModuleEngine
         {
             private readonly PartModule module;
@@ -575,11 +616,7 @@
 
                 if (module is ModuleEngines)
                 {
-                    this.SetModuleEngines();
-                }
-                else if (module is ModuleEnginesFX)
-                {
-                    this.SetModuleEnginesFx();
+                    SetModuleEngines();
                 }
             }
 
@@ -589,41 +626,24 @@
 
             public float GetSpecificImpulse(float atmosphere)
             {
-                if (this.module is ModuleEngines)
-                {
-                    return (this.module as ModuleEngines).atmosphereCurve.Evaluate(atmosphere);
-                }
-                if (this.module is ModuleEnginesFX)
-                {
-                    return (this.module as ModuleEnginesFX).atmosphereCurve.Evaluate(atmosphere);
+                if (module is ModuleEngines)
+                {
+                    return (module as ModuleEngines).atmosphereCurve.Evaluate(atmosphere);
                 }
                 return 0.0f;
             }
 
             private void SetModuleEngines()
             {
-                var engine = this.module as ModuleEngines;
+                ModuleEngines engine = module as ModuleEngines;
                 if (engine == null)
                 {
                     return;
                 }
 
-                this.MaximumThrust = engine.maxThrust * (engine.thrustPercentage * 0.01);
-                this.MinimumThrust = engine.minThrust;
-                this.Propellants = engine.propellants;
-            }
-
-            private void SetModuleEnginesFx()
-            {
-                var engine = this.module as ModuleEnginesFX;
-                if (engine == null)
-                {
-                    return;
-                }
-
-                this.MaximumThrust = engine.maxThrust * (engine.thrustPercentage * 0.01);
-                this.MinimumThrust = engine.minThrust;
-                this.Propellants = engine.propellants;
+                MaximumThrust = engine.maxThrust * (engine.thrustPercentage * 0.01);
+                MinimumThrust = engine.minThrust;
+                Propellants = engine.propellants;
             }
         }
     }

file:a/PartSim.cs -> file:b/PartSim.cs
--- a/PartSim.cs
+++ b/PartSim.cs
@@ -119,7 +119,6 @@
             partSim.isFuelLine = partSim.part.HasModule<CModuleFuelLine>();
             partSim.isFuelTank = partSim.part is FuelTank;
             partSim.isSepratron = partSim.IsSepratron();
-            partSim.isFairing = partSim.IsFairing(partSim.part);
             partSim.inverseStage = partSim.part.inverseStage;
             //MonoBehaviour.print("inverseStage = " + inverseStage);
 
@@ -145,13 +144,13 @@
             else
             {
                 partSim.realMass = partSim.part.mass;
-                if (log != null) log.buf.AppendLine("Using part.mass of " + partSim.part.mass);
-            }
-
-            if (partSim.isFairing)
-            {
-                partSim.fairingMass = partSim.part.GetModuleMass((float)partSim.realMass);
-            }
+                if (log != null)
+                {
+                    log.buf.AppendLine("Using part.mass" + partSim.part.mass);
+                }
+            }
+
+            partSim.fairingMass = partSim.part.GetModule<ModuleProceduralFairing>()?.GetModuleMass(partSim.part.mass) ?? 0.0f;
 
             for (int i = 0; i < partSim.part.Resources.Count; i++)
             {
@@ -234,27 +233,13 @@
                     {
                         if (log != null) log.buf.AppendLine("Module: " + engine.moduleName);
 
-                        Vector3 thrustvec = this.CalculateThrustVector(vectoredThrust ? engine.thrustTransforms : null, log);
-
                         EngineSim engineSim = EngineSim.New(
                             this,
-                            atmosphere,
+							engine,
+							atmosphere,
                             (float)mach,
-                            engine.maxFuelFlow,
-                            engine.minFuelFlow,
-                            engine.thrustPercentage,
-                            thrustvec,
-                            engine.atmosphereCurve,
-                            engine.atmChangeFlow,
-                            engine.useAtmCurve ? engine.atmCurve : null,
-                            engine.useVelCurve ? engine.velCurve : null,
-                            engine.currentThrottle,
-                            engine.g,
-                            engine.throttleLocked || fullThrust,
-                            engine.propellants,
-                            engine.isOperational,
-                            engine.resultingThrust,
-                            engine.thrustTransforms,
+							vectoredThrust,
+							fullThrust,
                             log);
                         allEngines.Add(engineSim);
                     }
@@ -268,28 +253,14 @@
                     ModuleEngines engine = engines[i];
                     if (log != null) log.buf.AppendLine("Module: " + engine.moduleName);
 
-                    Vector3 thrustvec = this.CalculateThrustVector(vectoredThrust ? engine.thrustTransforms : null, log);
-
                     EngineSim engineSim = EngineSim.New(
                         this,
+						engine,
                         atmosphere,
                         (float)mach,
-                        engine.maxFuelFlow,
-                        engine.minFuelFlow,
-                        engine.thrustPercentage,
-                        thrustvec,
-                        engine.atmosphereCurve,
-                        engine.atmChangeFlow,
-                        engine.useAtmCurve ? engine.atmCurve : null,
-                        engine.useVelCurve ? engine.velCurve : null,
-                        engine.currentThrottle,
-                        engine.g,
-                        engine.throttleLocked || fullThrust,
-                        engine.propellants,
-                        engine.isOperational,
-                        engine.resultingThrust,
-                        engine.thrustTransforms,
-                        log);
+						vectoredThrust,
+						fullThrust,
+						log);
                     allEngines.Add(engineSim);
                 }
             }
@@ -417,7 +388,7 @@
                 mass += resources.GetResourceMass(resources.Types[i]);
             }
 
-            if (isFairing && currentStage <= inverseStage)
+            if (fairingMass > 0.0 && currentStage <= inverseStage)
             {
                 mass -= fairingMass;
             }
@@ -777,13 +748,7 @@
 
         private bool IsDecoupler(Part thePart)
         {
-            return thePart.HasModule<ModuleDecouple>() ||
-                   thePart.HasModule<ModuleAnchoredDecoupler>();
-        }
-
-        private bool IsFairing(Part thePart)
-        {
-            return thePart.HasModule<ModuleProceduralFairing>();
+            return thePart.GetProtoModuleDecoupler()?.IsStageEnabled ?? false;
         }
 
         private bool IsSepratron()

--- a/Simulation.cs
+++ b/Simulation.cs
@@ -34,6 +34,7 @@
     using CompoundParts;
     using Extensions;
     using Helpers;
+    using KSP.UI.Screens;
 
     public class Simulation
     {
@@ -147,7 +148,7 @@
             this.gravity = theGravity;
             this.atmosphere = theAtmosphere;
             this.mach = theMach;
-            this.lastStage = Staging.lastStage;
+            this.lastStage = StageManager.LastStage;
             this.maxMach = 1.0f;
             //MonoBehaviour.print("lastStage = " + lastStage);