private void OnVesselOffRails(Vessel v)
        {
            if (v is null || v.isEVA)
            {
                return;
            }

            RunVesselJointUpdateFunction(v);

            if (!vesselOffRails.Contains(v) && v.precalc.isEasingGravity)
            {
                Log.info("KJR easing {0}", v.vesselName);

                vesselOffRails.Add(v);

                for (int i = 0; i < v.Parts.Count; ++i)
                {
                    Part p = v.Parts[i];
                    p.crashTolerance = p.crashTolerance * 10000f;
                    if (p.attachJoint)
                    {
                        p.attachJoint.SetUnbreakable(true, false);
                    }

                    Joint[] partJoints = p.GetComponents <Joint>();

                    if (p.Modules.Contains <LaunchClamp>())
                    {
                        foreach (Joint j in partJoints)
                        {
                            if (j.connectedBody == null)
                            {
                                GameObject.Destroy(j);
                                KJRJointUtils.ConnectLaunchClampToGround(p);
                                break;
                            }
                        }
                    }
                }
            }
        }
        public void OnEaseStart(Vessel v)
        {
            if (KJRJointUtils.debug)
            {
                Debug.Log("KJR easing " + v.vesselName);
            }

            foreach (Part p in v.Parts)
            {
                if (KJRJointUtils.IsJointUnlockable(p))
                {
                    continue;                     // exclude those actions from joints that can be dynamically unlocked
                }
                p.crashTolerance = p.crashTolerance * 10000f;
                if (p.attachJoint)
                {
                    p.attachJoint.SetUnbreakable(true, false);
                }

                Joint[] partJoints = p.GetComponents <Joint>();

                if (p.Modules.Contains <LaunchClamp>())
                {
                    for (int j = 0; j < partJoints.Length; j++)
                    {
                        if (partJoints[j].connectedBody == null)
                        {
                            GameObject.Destroy(partJoints[j]);
                            KJRJointUtils.ConnectLaunchClampToGround(p);
                            break;
                        }
                    }
                }
            }

            easingVessels.Add(v);
        }
Ejemplo n.º 3
0
        private void OnVesselOffRails(Vessel v)
        {
            if ((object)v == null || v.isEVA)
            {
                return;
            }

            bool vesselHasLaunchClamps = false;

            RunVesselJointUpdateFunction(v);
            if (!vesselOffRails.Contains(v) && v.precalc.isEasingGravity)
            {
                Debug.Log("KJR easing " + v.vesselName);
                vesselOffRails.Add(v);
                List <Joint> jointList = new List <Joint>();
                for (int i = 0; i < v.Parts.Count; ++i)
                {
                    Part p = v.Parts[i];
                    p.crashTolerance = p.crashTolerance * 10000f;
                    if (p.attachJoint)
                    {
                        p.attachJoint.SetUnbreakable(true, false);
                    }

                    Joint[] partJoints = p.GetComponents <Joint>();

                    if (p.Modules.Contains <LaunchClamp>())
                    {
                        vesselHasLaunchClamps = true;
                        foreach (Joint j in partJoints)
                        {
                            if (j.connectedBody == null)
                            {
                                jointList.Remove(j);
                                GameObject.Destroy(j);
                                KJRJointUtils.ConnectLaunchClampToGround(p);
                                break;
                            }
                        }
                    }
                }
            }
            else
            {
                for (int i = 0; i < v.Parts.Count; ++i)
                {
                    Part p = v.Parts[i];

                    if (p.Modules.Contains <LaunchClamp>())
                    {
                        vesselHasLaunchClamps = true;
                    }
                }
            }
            // if we had launch clamps and our situation is not PRELAUNCH then assume some physics issue has bounced us into landed or flying situation.
            if (vesselHasLaunchClamps && v.situation != Vessel.Situations.PRELAUNCH)
            {
                Debug.Log("KJR: Vessel has launch clamps and is not PRELAUNCH: Moving back to PRELAUNCH");
                v.situation   = Vessel.Situations.PRELAUNCH;
                v.launchTime  = 0;
                v.missionTime = 0;

                if (Versioning.version_minor > 3)
                {
                    // The following field didn't exist pre KSP v1.4
                    var fInf = v.GetType().GetField("skipGroundPositioning");
                    fInf.SetValue(v, false);
                }
            }
        }
/*        public void LateUpdate()
 *      {
 *          if (!FlightDriver.Pause && FlightGlobals.fetch && FlightGlobals.Vessels != null)
 *          {
 *              foreach (Vessel v in FlightGlobals.Vessels)
 *              {
 *                  if (!v.loaded)
 *                      continue;
 *
 * //                    int tick = 0;
 * //                    float scalingFactor = 1;
 *                  //This scales up the inertia tensor over a few frames rather than trying to initialize with massive inertia tensors
 * //                    if (vesselOffRailsTick.TryGetValue(v, out tick))
 * //                    {
 * //                        scalingFactor = 1 - physicsEasingCurve.Evaluate(tick);
 * //                    }
 *
 *                  //ScreenMessages.PostScreenMessage("Scaling Factor: " + scalingFactor, TimeWarp.deltaTime, ScreenMessageStyle.UPPER_LEFT);
 *
 *                  foreach (Part p in v.Parts)
 *                  {
 *                      //if (p.Modules.Contains("LaunchClamp"))
 *                      //    continue;
 *                      // This relies on KSP resetting the tensors to a constant in Update
 *                      if (p.started && p.State != PartStates.DEAD && p.rb)
 *                      {
 *                          float mass = p.rb.mass;// *scalingFactor;
 *
 *                          if (mass > 1f)
 *                              p.rb.inertiaTensor *= mass;
 *                      }
 *                  }
 *              }
 *          }
 *      }*/

        public void FixedUpdate()
        {
            if (FlightGlobals.ready && FlightGlobals.Vessels != null)
            {
                for (int i = 0; i < updatedVessels.Count; ++i)
                {
                    Vessel v = updatedVessels[i];
                    if (v == null || !vesselOffRailsTick.ContainsKey(v))
                    {
                        continue;
                    }

                    int tick = vesselOffRailsTick[v];
                    if (tick > 0)
                    {
                        float physicsScalingFactor = physicsEasingCurve.Evaluate(tick);
                        if (tick >= numTicksForEasing)
                        {
                            List <Joint> jointList = new List <Joint>();
                            foreach (Part p in v.Parts)
                            {
                                p.crashTolerance = p.crashTolerance * 10000f;
                                if (p.attachJoint)
                                {
                                    p.attachJoint.SetUnbreakable(true);
                                }

                                Joint[] partJoints = p.GetComponents <Joint>();

/*                                foreach (Joint j in partJoints)
 *                              {
 * //                                    j.breakForce *= 1000000000000000000;
 * //                                    j.breakTorque *= 1000000000000000000;
 *                                  jointList.Add(j);
 *                                  Debug.Log("Part: " + p.partInfo.title + " BreakForce = " + j.breakForce + " BreakTorque = " + j.breakTorque);
 *                              }*/
                                if (p.Modules.Contains <LaunchClamp>())
                                {
                                    foreach (Joint j in partJoints)
                                    {
                                        if (j.connectedBody == null)
                                        {
                                            jointList.Remove(j);
                                            GameObject.Destroy(j);
                                            KJRJointUtils.ConnectLaunchClampToGround(p);
                                            break;
                                        }
                                    }
                                }
                            }
                            //vesselJointStrengthened.Add(v, jointList);
                        }
                        bool easing = false;
                        if (v.situation == Vessel.Situations.PRELAUNCH || v.situation == Vessel.Situations.LANDED || v.situation == Vessel.Situations.SPLASHED)
                        {
                            easing = true;
                        }
                        else
                        {
                            foreach (Part p in v.Parts)
                            {
                                if (p.Modules.Contains <LaunchClamp>())
                                {
                                    easing = true;
                                    break;
                                }
                            }
                        }
                        if (easing)
                        {
                            Vector3d vesselPos          = v.GetWorldPos3D();
                            Vector3d vesselVel          = v.obt_velocity;
                            Vector3d vesselAcceleration = FlightGlobals.getGeeForceAtPosition(vesselPos) + FlightGlobals.getCentrifugalAcc(vesselPos, FlightGlobals.currentMainBody) + FlightGlobals.getCoriolisAcc(vesselVel, FlightGlobals.currentMainBody);
                            vesselAcceleration *= physicsScalingFactor;
                            foreach (Part p in v.Parts)
                            {
                                if (p.rb)
                                {
                                    p.rb.AddForce(-vesselAcceleration * p.rb.mass);
                                }
                            }
                        }
                        if (v == FlightGlobals.ActiveVessel)
                        {
                            if (InputLockManager.GetControlLock("KJRLoadLock") != ControlTypes.ALL_SHIP_CONTROLS)
                            {
                                InputLockManager.SetControlLock(ControlTypes.ALL_SHIP_CONTROLS, "KJRLoadLock");
                            }
                            ScreenMessages.PostScreenMessage("KJR stabilizing physics load...", TimeWarp.fixedDeltaTime, ScreenMessageStyle.UPPER_RIGHT);
                        }
                        else
                        if (InputLockManager.GetControlLock("KJRLoadLock") == ControlTypes.ALL_SHIP_CONTROLS)
                        {
                            InputLockManager.RemoveControlLock("KJRLoadLock");
                        }

                        tick--;
                        vesselOffRailsTick[v] = tick;
                    }
                    else if (tick == 0)
                    {
                        foreach (Part p in v.Parts)
                        {
                            p.crashTolerance = p.crashTolerance / 10000f;
                            if (p.attachJoint)
                            {
                                p.attachJoint.SetUnbreakable(false);
                            }
                        }

                        vesselOffRailsTick.Remove(v);
                        if (InputLockManager.GetControlLock("KJRLoadLock") == ControlTypes.ALL_SHIP_CONTROLS)
                        {
                            InputLockManager.RemoveControlLock("KJRLoadLock");
                        }
                    }
                    else
                    {
                        foreach (Part p in v.Parts)
                        {
                            p.crashTolerance = p.crashTolerance / 10000f;
                            if (p.attachJoint)
                            {
                                p.attachJoint.SetUnbreakable(false);
                            }
                        }

                        vesselOffRailsTick.Remove(v);
                        if (InputLockManager.GetControlLock("KJRLoadLock") == ControlTypes.ALL_SHIP_CONTROLS)
                        {
                            InputLockManager.RemoveControlLock("KJRLoadLock");
                        }
                    }
                }
            }
        }