diff --git a/KerbalJointReinforcement/KerbalJointReinforcement/KJRJointUtils.cs b/KerbalJointReinforcement/KerbalJointReinforcement/KJRJointUtils.cs index 9657981..b4e47d4 100644 --- a/KerbalJointReinforcement/KerbalJointReinforcement/KJRJointUtils.cs +++ b/KerbalJointReinforcement/KerbalJointReinforcement/KJRJointUtils.cs @@ -104,14 +104,11 @@ public static List GetDecouplerPartStiffeningList(Part p, bool childrenNot { if (childrenNotParent) { - if (p.children != null) + foreach (Part q in p.children) { - foreach (Part q in p.children) + if (q != null && q.parent == p) { - if (q != null && q.parent == p) - { - newAdditions.AddRange(GetDecouplerPartStiffeningList(q, childrenNotParent, onlyAddLastPart)); - } + newAdditions.AddRange(GetDecouplerPartStiffeningList(q, childrenNotParent, onlyAddLastPart)); } } } @@ -122,16 +119,16 @@ public static List GetDecouplerPartStiffeningList(Part p, bool childrenNot } else { - float thisPartMaxMass = MaximumPossiblePartMass(p); - if (childrenNotParent) + double thisPartMaxMass = p.physicsMass; + if (thisPartMaxMass > 0) { - if (p.children != null) + if (childrenNotParent) { foreach (Part q in p.children) { if (q != null && q.parent == p) { - float massRatio = MaximumPossiblePartMass(q) / thisPartMaxMass; + double massRatio = q.physicsMass / thisPartMaxMass; //if (massRatio < 1) // massRatio = 1 / massRatio; @@ -144,18 +141,18 @@ public static List GetDecouplerPartStiffeningList(Part p, bool childrenNot } } } - } - else if (p.parent) - { - float massRatio = MaximumPossiblePartMass(p.parent) / thisPartMaxMass; - //if (massRatio < 1) - // massRatio = 1 / massRatio; - - if (massRatio > settings.stiffeningExtensionMassRatioThreshold) + else if (p.parent) { - newAdditions.Add(p.parent); - if (settings.debug) - Debug.Log($"[KJR] Part {p.parent.partInfo.title} added to list due to mass ratio difference"); + double massRatio = p.parent.physicsMass / thisPartMaxMass; + //if (massRatio < 1) + // massRatio = 1 / massRatio; + + if (massRatio > settings.stiffeningExtensionMassRatioThreshold) + { + newAdditions.Add(p.parent); + if (settings.debug) + Debug.Log($"[KJR] Part {p.parent.partInfo.title} added to list due to mass ratio difference"); + } } } } @@ -188,18 +185,6 @@ public static void ConnectLaunchClampToGround(Part clamp) //newJoint.angularXMotion = newJoint.angularYMotion = newJoint.angularZMotion = ConfigurableJointMotion.Locked; } - public static float MaximumPossiblePartMass(Part p) - { - float maxMass = p.mass; - foreach (PartResource r in p.Resources) - { - maxMass += (float)(r.info.density * r.maxAmount); - } - if (settings.debug) - Debug.Log($"[KJR] Maximum mass for part {p.partInfo.title} is {maxMass}"); - return maxMass; - } - public static void AddLaunchClampReinforcementModule(Part p) { var pm = (KJRLaunchClampReinforcementModule)p.AddModule(nameof(KJRLaunchClampReinforcementModule)); diff --git a/KerbalJointReinforcement/KerbalJointReinforcement/KJRManager.cs b/KerbalJointReinforcement/KerbalJointReinforcement/KJRManager.cs index e265882..440ec86 100644 --- a/KerbalJointReinforcement/KerbalJointReinforcement/KJRManager.cs +++ b/KerbalJointReinforcement/KerbalJointReinforcement/KJRManager.cs @@ -305,7 +305,7 @@ private void RunVesselJointUpdateFunction(Vessel v) { p.breakingForce = Mathf.Infinity; p.breakingTorque = Mathf.Infinity; - p.mass = Mathf.Max(p.mass, (p.parent.mass + p.parent.GetResourceMass()) * 0.01f); //We do this to make sure that there is a mass ratio of 100:1 between the clamp and what it's connected to. This helps counteract some of the wobbliness simply, but also allows some give and springiness to absorb the initial physics kick + p.mass = (float)Math.Max(p.physicsMass, p.parent.physicsMass * 0.01); //We do this to make sure that there is a mass ratio of 100:1 between the clamp and what it's connected to. This helps counteract some of the wobbliness simply, but also allows some give and springiness to absorb the initial physics kick if (KJRJointUtils.settings.debug) Debug.Log("[KJR] Launch Clamp Break Force / Torque increased"); @@ -413,7 +413,7 @@ private void UpdatePartJoint(Part p) //addAdditionalJointToParent &= !(p.Modules.Contains("LaunchClamp") || (p.parent.Modules.Contains("ModuleDecouple") || p.parent.Modules.Contains("ModuleAnchoredDecoupler"))); addAdditionalJointToParent &= !p.Modules.Contains(); - float partMass = p.mass + p.GetResourceMass(); + double partMass = p.physicsMass; for (int i = 0; i < jointList.Count; i++) { ConfigurableJoint j = jointList[i]; @@ -424,7 +424,7 @@ private void UpdatePartJoint(Part p) Rigidbody connectedBody = j.connectedBody; Part connectedPart = connectedBody.GetComponent() ?? p.parent; - float parentMass = connectedPart.mass + connectedPart.GetResourceMass(); + double parentMass = connectedPart.physicsMass; if (partMass < KJRJointUtils.settings.massForAdjustment || parentMass < KJRJointUtils.settings.massForAdjustment) { @@ -677,7 +677,7 @@ private void UpdatePartJoint(Part p) bool massRatioBelowThreshold = false; int numPartsFurther = 0; - float partMaxMass = KJRJointUtils.MaximumPossiblePartMass(p); + double partMaxMass = p.physicsMass; List partsCrossed = new List(); List possiblePartsCrossed = new List(); @@ -689,12 +689,12 @@ private void UpdatePartJoint(Part p) do { - float massRat1, massRat2; - massRat1 = partMaxMass / newConnectedPart.mass; + double massRat1, massRat2; + massRat1 = partMaxMass / newConnectedPart.physicsMass; if (massRat1 < 1) massRat1 = 1 / massRat1; - massRat2 = p.mass / KJRJointUtils.MaximumPossiblePartMass(newConnectedPart); + massRat2 = p.physicsMass / newConnectedPart.physicsMass; if (massRat2 < 1) massRat2 = 1 / massRat2; @@ -965,13 +965,10 @@ public void MultiPartJointTreeChildren(Vessel v) for (int i = 0; i < v.Parts.Count; ++i) { Part p = v.Parts[i]; - if (p.children.Count == 0 && !p.Modules.Contains("LaunchClamp") && - KJRJointUtils.MaximumPossiblePartMass(p) > KJRJointUtils.settings.massForAdjustment) + if (p.rb != null && + p.children.Count == 0 && !p.Modules.Contains("LaunchClamp") && + p.physicsMass > KJRJointUtils.settings.massForAdjustment) { - if (p.rb == null && p.Rigidbody != null) - { - p = p.RigidBodyPart; - } childPartsToConnect.Add(p); } }