private StabilityMode GetStabilityMode(Vessel vessel) { if (vessel.IsControllable == false || RemoteTechWrapper.Controllable(vessel) == false) { return(StabilityMode.OFF); /* Vessel is uncontrollable */ } else if (RemoteTechWrapper.GetMode(vessel) != RemoteTechWrapper.ACFlightMode.Off) { return(StabilityMode.ABSOLUTE); } else if (MechJebWrapper.Active(vessel)) { return(StabilityMode.AUTOPILOT); /* MechJeb is commanding the vessel */ } else if (vessel.ActionGroups[KSPActionGroup.SAS] && data.FindPRVessel(vessel).mjMode == MechJebWrapper.SATarget.OFF && MechJebWrapper.GetMode(vessel) == MechJebWrapper.SATarget.OFF) { /* Only stock SAS is enabled */ if (vessel.Autopilot.Mode == VesselAutopilot.AutopilotMode.StabilityAssist) { return(StabilityMode.RELATIVE); } else if (vessel.Autopilot.Mode == VesselAutopilot.AutopilotMode.Prograde) { return(StabilityMode.PROGRADE); } else { return(StabilityMode.ABSOLUTE); } } else if (!vessel.ActionGroups[KSPActionGroup.SAS] && (data.FindPRVessel(vessel).mjMode != MechJebWrapper.SATarget.OFF || MechJebWrapper.GetMode(vessel) != MechJebWrapper.SATarget.OFF)) { return(StabilityMode.ABSOLUTE); /* Only SmartA.S.S. is enabled */ } else if (!vessel.ActionGroups[KSPActionGroup.SAS] && data.FindPRVessel(vessel).mjMode == MechJebWrapper.SATarget.OFF && MechJebWrapper.GetMode(vessel) == MechJebWrapper.SATarget.OFF) { return(StabilityMode.OFF); /* Nothing is enabled */ } else { return(StabilityMode.OFF); } }
private void FixedUpdate() { if (activeVessel != FlightGlobals.ActiveVessel) { activeVessel = FlightGlobals.ActiveVessel; Interface.instance.desiredRPMstr = data.FindPRVessel(activeVessel).desiredRPM.ToString(); } foreach (Data.PRVessel v in data.PRVessels) { v.processed = false; } if (RigidBodyMaxAngularVelocityPairs.Count > 0) { List <Rigidbody> toremove = null; foreach (KeyValuePair <Rigidbody, RigidBodyMaxAngularVelocitySaveData> kp in RigidBodyMaxAngularVelocityPairs) { kp.Value.FrameCounter--; if (kp.Value.FrameCounter == 0) { try { kp.Key.maxAngularVelocity = kp.Value.OriginalValue; } catch { } if (toremove == null) { toremove = new List <Rigidbody>(); } toremove.Add(kp.Key); } } if (toremove != null) { for (int i = 0; i < toremove.Count; ++i) { RigidBodyMaxAngularVelocityPairs.Remove(toremove[i]); } } } #region ### Cycle through all vessels ### foreach (Vessel vessel in FlightGlobals.Vessels) { Data.PRVessel v = data.FindPRVessel(vessel); v.processed = true; if (v.dynamicReference) { if (v.reference == null || (v.reference.GetType() != typeof(CelestialBody) || v.reference.GetName() != vessel.mainBody.GetName())) //Main body mode; continuous update of reference to mainBody { Debug.Log("[PR] Updated the reference of " + v.vessel.vesselName + " from " + (v.reference != null ? v.reference.GetName() : "Null") + " to " + vessel.mainBody.name); v.reference = vessel.mainBody; v.direction = (v.reference.GetTransform().position - vessel.transform.position).normalized; v.rotation = vessel.transform.rotation; v.planetariumRight = Planetarium.right; v.lastActive = false; } } if (vessel.packed) { #region ### PACKED ### if (vessel.loaded) //is okay, rotation doesnt need to be persistent when rotating { var currentStabilityMode = GetStabilityMode(vessel); //Debug.Log("[PR] - currentStabilityMode: " + Enum.GetName(typeof(StabilityMode), currentStabilityMode)); if (currentStabilityMode == StabilityMode.PROGRADE) { var rotation = Quaternion.FromToRotation(vessel.transform.up.normalized, vessel.obt_velocity.normalized); vessel.transform.Rotate(rotation.eulerAngles, Space.World); v.vessel.SetRotation(vessel.transform.rotation); } else if (v.storedAngularMomentum.magnitude >= threshold) { if (currentStabilityMode != StabilityMode.AUTOPILOT) { PackedSpin(v); } } else if (currentStabilityMode != StabilityMode.ABSOLUTE) { if (currentStabilityMode == StabilityMode.RELATIVE && v.rotationModeActive && !v.momentumModeActive && v.storedAngularMomentum.magnitude < threshold) { if (v.rotationModeActive == true && v.reference != null) { if (v.reference == v.lastReference) { PackedRotation(v); } } } else { PackedSpin(v); } } } v.lastActive = false; #endregion } else { #region ### UNPACKED ### //Did this vessel just go off rails? if (v.GoingOffRailsFrameCounter != -1) { --v.GoingOffRailsFrameCounter; if (v.GoingOffRailsFrameCounter == 0) { ApplyMomentumNow(v); v.GoingOffRailsFrameCounter = -1; } } else { //Update Momentum when unpacked if (GetStabilityMode(vessel) != StabilityMode.OFF && !v.momentumModeActive && vessel.angularVelocity.magnitude < threshold) //C1 { v.storedAngularMomentum = Vector3.zero; } else { v.storedAngularMomentum = vessel.angularMomentum; //KSPLog.print(string.Format("SAVE angular vel: {0}, Momentum: {1}, MOI: {2}", vessel.angularVelocity, vessel.angularMomentum, vessel.MOI)); } } //Update mjMode when unpacked v.mjMode = MechJebWrapper.GetMode(vessel); v.rtMode = RemoteTechWrapper.GetMode(vessel); //Apply Momentum to activeVessel using Fly-By-Wire if (GetStabilityMode(vessel) == StabilityMode.RELATIVE && v.momentumModeActive) //C1 \ IsControllable { float desiredRPM = (vessel.angularVelocity.magnitude * 60f * (1f / Time.fixedDeltaTime)) / 360f; if (v.desiredRPM >= 0) { vessel.ctrlState.roll = Mathf.Clamp((v.desiredRPM - desiredRPM), -1f, +1f); } else { vessel.ctrlState.roll = -Mathf.Clamp((-v.desiredRPM - desiredRPM), -1f, +1f); } } //Update rotation v.rotation = vessel.transform.rotation; v.planetariumRight = Planetarium.right; //Adjust SAS for Relative Rotation if (v.rotationModeActive && v.reference != null) //C2 { //Update direction v.direction = (v.reference.GetTransform().position - vessel.transform.position).normalized; if (GetStabilityMode(vessel) == StabilityMode.RELATIVE && !v.momentumModeActive) { if (v.lastActive && v.reference == v.lastReference) { AdjustSAS(v); } v.lastActive = true; } else { v.lastActive = false; } v.lastPosition = (Vector3d)v.lastTransform.position - v.reference.GetTransform().position; } else { v.direction = Vector3.zero; v.lastPosition = Vector3.zero; v.lastActive = false; } #endregion } v.lastTransform = vessel.ReferenceTransform; v.lastReference = v.reference; } #endregion data.PRVessels.RemoveAll(v => v.processed == false); }
private void FixedUpdate() { if (activeVessel != FlightGlobals.ActiveVessel) { activeVessel = FlightGlobals.ActiveVessel; Interface.instance.desiredRPMstr = data.FindPRVessel(activeVessel).desiredRPM.ToString(); } foreach (Data.PRVessel v in data.PRVessels) { v.processed = false; } #region ### Cycle through all vessels ### foreach (Vessel vessel in FlightGlobals.Vessels) { Data.PRVessel v = data.FindPRVessel(vessel); v.processed = true; if (v.dynamicReference) { if (v.reference == null || (v.reference.GetType() != typeof(CelestialBody) || v.reference.GetName() != vessel.mainBody.GetName())) //Main body mode; continuous update of reference to mainBody { Debug.Log("[PR] Updated the reference of " + v.vessel.vesselName + " from " + (v.reference != null ? v.reference.GetName() : "Null") + " to " + vessel.mainBody.name); v.reference = vessel.mainBody; v.direction = (v.reference.GetTransform().position - vessel.transform.position).normalized; v.rotation = vessel.transform.rotation; v.planetariumRight = Planetarium.right; v.lastActive = false; } } if (vessel.packed) { #region ### PACKED ### if (vessel.loaded) //is okay, rotation doesnt need to be persistent when rotating { if (v.momentum.magnitude >= threshold) { PackedSpin(v); } else if (GetStabilityMode(vessel) != StabilityMode.ABSOLUTE) { if (GetStabilityMode(vessel) == StabilityMode.RELATIVE && v.rotationModeActive && !v.momentumModeActive && v.momentum.magnitude < threshold) { if (v.rotationModeActive == true && v.reference != null) { if (v.reference == v.lastReference) { PackedRotation(v); } } } else { PackedSpin(v); } } } v.lastActive = false; #endregion } else { #region ### UNPACKED ### //Update Momentum when unpacked if (GetStabilityMode(vessel) != StabilityMode.OFF && !v.momentumModeActive && vessel.angularVelocity.magnitude < threshold) //C1 { v.momentum = Vector3.zero; } else { v.momentum = vessel.angularVelocity; } //Update mjMode when unpacked v.mjMode = MechJebWrapper.GetMode(vessel); v.rtMode = RemoteTechWrapper.GetMode(vessel); //Apply Momentum to activeVessel using Fly-By-Wire if (GetStabilityMode(vessel) == StabilityMode.RELATIVE && v.momentumModeActive) //C1 \ IsControllable { float desiredRPM = (vessel.angularVelocity.magnitude * 60f * (1f / Time.fixedDeltaTime)) / 360f; if (v.desiredRPM >= 0) { vessel.ctrlState.roll = Mathf.Clamp((v.desiredRPM - desiredRPM), -1f, +1f); } else { vessel.ctrlState.roll = -Mathf.Clamp((-v.desiredRPM - desiredRPM), -1f, +1f); } } //Update rotation v.rotation = vessel.transform.rotation; v.planetariumRight = Planetarium.right; //Adjust SAS for Relative Rotation if (v.rotationModeActive && v.reference != null) //C2 { //Update direction v.direction = (v.reference.GetTransform().position - vessel.transform.position).normalized; if (GetStabilityMode(vessel) == StabilityMode.RELATIVE && !v.momentumModeActive) { if (v.lastActive && v.reference == v.lastReference) { AdjustSAS(v); } v.lastActive = true; } else { v.lastActive = false; } v.lastPosition = (Vector3d)v.lastTransform.position - v.reference.GetTransform().position; } else { v.direction = Vector3.zero; v.lastPosition = Vector3.zero; v.lastActive = false; } #endregion } v.lastTransform = vessel.ReferenceTransform; v.lastReference = v.reference; } #endregion data.PRVessels.RemoveAll(v => v.processed == false); }