diff --git a/Source/DecayManager.cs b/Source/DecayManager.cs index c1b2da6..384da71 100644 --- a/Source/DecayManager.cs +++ b/Source/DecayManager.cs @@ -127,9 +127,12 @@ public void SetGUIToggledFalse(Part part) public void UpdateActiveVesselInformation(Vessel vessel) { - if (vessel == FlightGlobals.ActiveVessel) + if (HighLogic.LoadedScene != GameScenes.EDITOR) // 1.6.0 Fixes VAB Rightclick errors { - VesselData.UpdateActiveVesselData(vessel); + if (vessel == FlightGlobals.ActiveVessel) + { + VesselData.UpdateActiveVesselData(vessel); + } } } @@ -636,15 +639,48 @@ public static bool CheckReferenceBody(Vessel vessel) // 1.6.0 Body Checks } + public static bool CheckNBodyAltitude(Vessel vessel) + { + bool BeyondSafeArea = false; + + if (Math.Abs(vessel.orbitDriver.orbit.altitude) > (2.0 * vessel.orbitDriver.orbit.referenceBody.Radius)) + { + BeyondSafeArea = true; + } + + return BeyondSafeArea; + } + public static void RealisticDecaySimulator(Vessel vessel) // 1.4.0 Cleanup { Orbit orbit = vessel.orbitDriver.orbit; CelestialBody body = orbit.referenceBody; - if (CheckReferenceBody(vessel)) + if (Settings.ReadNB() && CheckNBodyAltitude(vessel)) { + if (vessel.situation == Vessel.Situations.ORBITING) + { + /* + print("Pos: " + vessel.orbitDriver.orbit.pos); + print("PosAtUT: " + vessel.orbitDriver.orbit.getPositionAtUT(HighLogic.CurrentGame.UniversalTime)); + print("PosAlternate: " + vessel.orbitDriver.orbit.getRelativePositionAtUT(HighLogic.CurrentGame.UniversalTime)); + print("DifferenceBetween Pos & PosAlt: " + (vessel.orbitDriver.orbit.pos - vessel.orbitDriver.orbit.getRelativePositionAtUT(HighLogic.CurrentGame.UniversalTime))); + + print("Vel: " + vessel.orbitDriver.orbit.vel.magnitude); + print("VelAt: " + vessel.orbitDriver.orbit.getOrbitalSpeedAt(HighLogic.CurrentGame.UniversalTime)); + print("VelAtAlt: " + vessel.orbitDriver.orbit.getOrbitalSpeedAtRelativePos(vessel.orbitDriver.orbit.getRelativePositionAtUT(HighLogic.CurrentGame.UniversalTime))); + print("Energy: " + vessel.orbitDriver.orbit.orbitalEnergy); + print("Energy Calculated: " + (((Math.Pow(vessel.orbit.vel.magnitude, 2.0)) / 2.0) - (vessel.orbitDriver.orbit.referenceBody.gravParameter / (vessel.orbitDriver.orbit.altitude + vessel.orbit.referenceBody.Radius)))); + */ + + NBodyManager.ManageVessel(vessel); // 1.6.0 N-Body master reference + } + } + + if (CheckReferenceBody(vessel)) + { RealisticGravitationalPertubationDecay(vessel); // 1.5.0 RealisticRadiationDragDecay(vessel); // 1.5.0 Happens everywhere now RealisticYarkovskyEffectDecay(vessel); // 1.5.0 // Partial, full for 1.6.0 @@ -886,6 +922,7 @@ public static void RealisticYarkovskyEffectDecay(Vessel vessel) // 1.5.0 VesselData.UpdateVesselSMA(vessel, VesselData.FetchSMA(vessel) - (-1.0 * YarkovskyEffect.FetchDeltaSMA(vessel))); } + #endregion #region Old Stock @@ -1043,7 +1080,9 @@ public static void ActiveDecayRealistic(Vessel vessel) // 1.4.0 Use R if ((p.physicalSignificance == Part.PhysicalSignificance.FULL) && (p.Rigidbody != null)) { + // NBody Active // p.Rigidbody.AddForce(Vector3d.back * (decayForce)); // 1.5.0 + } } @@ -1060,6 +1099,8 @@ public static void ActiveDecayRealistic(Vessel vessel) // 1.4.0 Use R { if (vessel.vesselType != VesselType.EVA) { + NBodyManager.ManageVessel(vessel); // 1.6.0 NBody + VesselData.UpdateVesselSMA(vessel, (VesselData.FetchSMA(vessel) - DecayValue)); CatchUpOrbit(vessel); } @@ -1329,6 +1370,16 @@ public static double DecayRateYarkovskyEffect(Vessel vessel) return DecayRate; } + public static double DecayRateNBodyPerturbation(Vessel vessel) + { + double decayRate = 0; + //decayRate = NBodyManager.CalculateSMA(vessel, vessel.orbitDriver.orbit.getOrbitalSpeedAtRelativePos(vessel.orbitDriver.orbit.getRelativePositionAtUT(HighLogic.CurrentGame.UniversalTime)), HighLogic.CurrentGame.UniversalTime, 1.0); + + // Work this out. + + return decayRate; + } + public static double DecayRateTotal(Vessel vessel) { double Total = DecayRateAtmosphericDrag(vessel) + DecayRateGravitationalPertubation(vessel) + DecayRateRadiationPressure(vessel) + DecayRateYarkovskyEffect(vessel); diff --git a/Source/NBodyConicsManager.cs b/Source/NBodyConicsManager.cs new file mode 100644 index 0000000..977230d --- /dev/null +++ b/Source/NBodyConicsManager.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using UnityEngine; +using KSP.IO; + +namespace WhitecatIndustries +{ + public static class NBodyConicsManager + { + public static void DrawPredictedVesselPath(Vessel vessel) + { + + } + + public static void DrawPredictedBodyPath(CelestialBody body) + { + + + } + + public static void DrawHillSphere(CelestialBody body) + { + + + } + + public static void DrawSphereOfInfluence(CelestialBody body) + { + + + } + } +} diff --git a/Source/NBodyManager.cs b/Source/NBodyManager.cs index b6cd206..45d81c9 100644 --- a/Source/NBodyManager.cs +++ b/Source/NBodyManager.cs @@ -5,72 +5,110 @@ using UnityEngine; using KSP.IO; -namespace WhitecatIndustries +namespace WhitecatIndustries { [KSPAddon(KSPAddon.Startup.EveryScene, false)] public class NBodyManager : MonoBehaviour // 1.6.0 NBody simulator { private float VariableUpdateInterval = 1.0f; private float lastUpdate = 0.0f; - public double GravitationalConstant = Math.Pow(6.67408 * 10, -11); - private bool CurrentProcess = false; + public static double GravitationalConstant = Math.Pow(6.67408 * 10, -11); + public static bool CurrentProcess = false; + public static bool PostTimewarpUpdateRequired = false; + public static double TimeAtTimewarpStart = 0; - List FutureRenderOrbits = new List(); - List CurrentMeshRenderers = new List(); - List CurrentLineGameObjects = new List(); - Material lineMaterial; + public static Dictionary VesselOrbitalPredictions = new Dictionary(); + public static Dictionary BodyOrbitalPredictions = new Dictionary(); + public static List VesselFutureRenderOrbits = new List(); + public static List CurrentMeshRenderers = new List(); + public static List CurrentLineGameObjects = new List(); + public static Material lineMaterial; - public bool ToggleHillSpheres = false; - public bool ToggleSphereOfInfluences = true; - public void FixedUpdate() + public static bool ToggleHillSpheres = false; + public static bool ToggleSphereOfInfluences = true; + + public void Start() { - if (!HighLogic.LoadedSceneHasPlanetarium) + GameEvents.onTimeWarpRateChanged.Add(TimewarpShift); + } + + public void TimewarpShift() + { + if (TimeWarp.CurrentRate < 2) { - ClearOrbitLines(); + TimeAtTimewarpStart = 0; } - if (CurrentProcess == false) + if (TimeWarp.CurrentRate > 2 && TimeAtTimewarpStart == 0) + { + TimeAtTimewarpStart = HighLogic.CurrentGame.UniversalTime; + } + } + + public void OnDestroy() + { + GameEvents.onTimeWarpRateChanged.Remove(TimewarpShift); + } + + public void FixedUpdate()// + { + + if (Settings.ReadNB() == true) { - if (HighLogic.LoadedSceneIsGame && (HighLogic.LoadedScene != GameScenes.LOADING && HighLogic.LoadedScene != GameScenes.LOADINGBUFFER && HighLogic.LoadedScene != GameScenes.MAINMENU)) + if (Time.timeSinceLevelLoad > 0.3) { - VariableUpdateInterval = 1.0f; + if (!HighLogic.LoadedSceneHasPlanetarium) + { + ClearOrbitLines(); + } - if (Time.timeSinceLevelLoad > 0.7) // Fit in here + if (VesselOrbitalPredictions.Keys.Count > 0) { - if ((Time.time - lastUpdate) > VariableUpdateInterval) + foreach (Vessel v in VesselOrbitalPredictions.Keys) { - lastUpdate = Time.time; - - foreach (CelestialBody body in FlightGlobals.Bodies) + if (!FlightGlobals.Vessels.Contains(v)) { - if (body != Sun.Instance.sun) - { - ManageBody(body); - } + VesselOrbitalPredictions.Remove(v); } + } + } + + if (CurrentProcess == false) + { + if (HighLogic.LoadedSceneIsGame && (HighLogic.LoadedScene != GameScenes.LOADING && HighLogic.LoadedScene != GameScenes.LOADINGBUFFER && HighLogic.LoadedScene != GameScenes.MAINMENU)) + { + VariableUpdateInterval = 1.0f; - foreach (Vessel vessel in FlightGlobals.Vessels) + if (Time.timeSinceLevelLoad > 0.7) // Fit in here { - if (vessel.vesselType != VesselType.Unknown && vessel.vesselType != VesselType.SpaceObject) // + if ((Time.time - lastUpdate) > VariableUpdateInterval) { - if (VesselData.FetchStationKeeping(vessel) == false) + lastUpdate = Time.time; + + foreach (Vessel v in FlightGlobals.Vessels) + { + if (DecayManager.CheckNBodyAltitude(v)) + { + ManageOrbitPredictionsVessel(v); + } + } + + foreach (CelestialBody b in FlightGlobals.Bodies) { - if (vessel == FlightGlobals.ActiveVessel) + if (b != Sun.Instance.sun) { - if (DecayManager.CheckVesselStateOrbEsc(vessel)) - { - ManageVessel(vessel); - } + ManageOrbitalPredictionsBody(b); } - else + } + + foreach (CelestialBody body in FlightGlobals.Bodies) + { + if (body != Sun.Instance.sun) { - if (DecayManager.CheckVesselStateOrbEsc(vessel)) - { - ManageVessel(vessel); - } + ManageBody(body); } } } @@ -82,7 +120,7 @@ public void FixedUpdate() } #region InfluencingBodyLists - public List InfluencingBodiesV(Vessel vessel) + public static List InfluencingBodiesV(Vessel vessel) { List InfluencingBodies = new List(); CelestialBody ReferenceBody = vessel.orbitDriver.orbit.referenceBody; @@ -98,7 +136,7 @@ public List InfluencingBodiesV(Vessel vessel) return InfluencingBodies; } - public List InfluencingBodiesB(CelestialBody body) + public static List InfluencingBodiesB(CelestialBody body) { List InfluencingBodies = new List(); CelestialBody ReferenceBody = body; @@ -113,11 +151,11 @@ public List InfluencingBodiesB(CelestialBody body) return InfluencingBodies; } - #endregion + #endregion #region InfluencingAccelerationLists - public List InfluencingAccelerationsB(Vessel vessel, double time) + public static List InfluencingAccelerationsB(Vessel vessel, double time) { List InfluencingAccelerations = new List(); @@ -125,17 +163,26 @@ public List InfluencingAccelerationsB(Vessel vessel, double time) return InfluencingAccelerations; } - public List InfluencingAccelerationsV(Vessel vessel, double time) + public static List InfluencingAccelerationsV(Vessel vessel, double time) { - List InfluencingAccelerations = new List(); + List InfluencingAccelerations = new List(); // Position at time foreach (CelestialBody Body in InfluencingBodiesV(vessel)) { - double VesselMass = vessel.GetTotalMass() * 1000.0; + double VesselMass = VesselData.FetchMass(vessel); double VesselMNA = vessel.orbitDriver.orbit.GetMeanAnomaly(vessel.orbitDriver.orbit.E, time); double InfluencingForce = 0; double BodyMass = Body.Mass; - double DistanceToVessel = Vector3d.Distance(Body.position, vessel.GetWorldPos3D()); + Vector3d BodyPosition = new Vector3d(); + if (vessel.orbitDriver.orbit.referenceBody == Body || Body == Sun.Instance.sun) + { + BodyPosition = new Vector3d(0, 0, 0); + } + else + { + BodyPosition = Body.orbit.getRelativePositionAtUT(time); + } + double DistanceToVessel = Vector3d.Distance(BodyPosition, vessel.orbitDriver.orbit.getRelativePositionAtUT(time)); // //print("Body " + Body.name + " distance to " + vessel.name + " : " + DistanceToVessel); double BodyMNA = 0; @@ -158,8 +205,16 @@ public List InfluencingAccelerationsV(Vessel vessel, double time) InfluencingForce = (GravitationalConstant * BodyMass * VesselMass) / (DistanceToVessel * DistanceToVessel); InfluencingForce = InfluencingForce * Math.Sin(MNADifference - 90.0); - Vector3d InfluencingAccelerationBodyDirectionVector = Body.position; - Vector3d VesselPositionVector = vessel.GetWorldPos3D(); + Vector3d InfluencingAccelerationBodyDirectionVector = new Vector3d(); + if (vessel.orbitDriver.orbit.referenceBody == Body || Body == Sun.Instance.sun) + { + InfluencingAccelerationBodyDirectionVector = new Vector3d(0, 0, 0); + } + else + { + InfluencingAccelerationBodyDirectionVector = Body.orbit.getRelativePositionAtUT(time); + } + Vector3d VesselPositionVector = vessel.orbitDriver.orbit.getRelativePositionAtUT(time); Vector3d InfluencingAccelerationVector = (new Vector3d(-InfluencingAccelerationBodyDirectionVector.x + VesselPositionVector.x, -InfluencingAccelerationBodyDirectionVector.y + VesselPositionVector.y, -InfluencingAccelerationBodyDirectionVector.z + VesselPositionVector.z)) * ((InfluencingForce / VesselMass)); InfluencingAccelerations.Add(InfluencingAccelerationVector); @@ -167,9 +222,9 @@ public List InfluencingAccelerationsV(Vessel vessel, double time) } return InfluencingAccelerations; } - - #endregion + + #endregion #region Calculations @@ -178,6 +233,7 @@ public static double CalculateHillSphere(Vessel vessel) double HillSphereRadius = 0; + // to do return HillSphereRadius; @@ -208,7 +264,7 @@ public static double DifferenceBetweenMNA(double VesselMNA, double BodyMNA) return Difference; } - public double GetMomentaryDeltaV(Vessel vessel, double time) + public static Vector3d GetMomentaryDeltaV(Vessel vessel, double time) { List InfluencingAccelerationVectors = InfluencingAccelerationsV(vessel, time); Vector3d FinalVelocityVector = new Vector3d(); @@ -218,19 +274,19 @@ public double GetMomentaryDeltaV(Vessel vessel, double time) FinalVelocityVector = FinalVelocityVector + (Acceleration); } - return FinalVelocityVector.magnitude; + return FinalVelocityVector; } -#endregion + #endregion #region ObjectManagement - public void ManageBody(CelestialBody body) + public static void ManageBody(CelestialBody body) { // Work out this! } - public void ManageVessel(Vessel vessel) + public static void ManageVessel(Vessel vessel) { CurrentProcess = true; @@ -240,17 +296,17 @@ public void ManageVessel(Vessel vessel) foreach (Vector3d Acceleration in InfluencingAccelerationVectors) { - FinalVelocityVector = FinalVelocityVector + (Acceleration * TimeWarp.CurrentRate); + FinalVelocityVector = FinalVelocityVector + (Acceleration); } SetOrbit(vessel, FinalVelocityVector); } - #endregion + #endregion #region PlanetariumManagement - public void ClearOrbitLines() + public static void ClearOrbitLines() { if (CurrentMeshRenderers.Count > 0) { @@ -272,9 +328,10 @@ public void ClearOrbitLines() } } - public void ManageConics(Vessel vessel, double time) + public static void ManageVesselConics(Vessel vessel, double time) { - FutureRenderOrbits.Clear(); + + VesselFutureRenderOrbits.Clear(); Orbit InitialOrbit = vessel.orbitDriver.orbit; double InitialTime = time; @@ -282,8 +339,8 @@ public void ManageConics(Vessel vessel, double time) double TimewarpRate = TimeWarp.CurrentRate; double NoOfSteps = Settings.ReadNBCC(); double TimeSnapshots = TimewarpRate / NoOfSteps; - + /* for (int i = 0; i < NoOfSteps; i++) { List InfluencingAccelerationVectors = InfluencingAccelerationsV(vessel, time + (TimeSnapshots * i)); @@ -295,20 +352,84 @@ public void ManageConics(Vessel vessel, double time) FinalVelocityVector = FinalVelocityVector + (Acceleration); } - FutureRenderOrbits.Add(NewCalculatedOrbit(vessel, FinalVelocityVector, (time + (TimeSnapshots * i)))); + VesselFutureRenderOrbits.Add(NewCalculatedOrbit(vessel, FinalVelocityVector, (time + (TimeSnapshots * i)))); + + if (VesselOrbitalPredictions.ContainsKey(vessel)) + { + VesselOrbitalPredictions.Remove(vessel); + VesselOrbitalPredictions.Add(vessel, NewCalculatedOrbit(vessel, FinalVelocityVector, (time + (TimeSnapshots * i)))); + } + else + { + VesselOrbitalPredictions.Add(vessel, NewCalculatedOrbit(vessel, FinalVelocityVector, (time + (TimeSnapshots * i)))); + } } + */ - foreach (Orbit orbit in FutureRenderOrbits) + foreach (Orbit orbit in VesselFutureRenderOrbits) { - Vector3 PositionAtStart = orbit.getTruePositionAtUT(time + (TimeSnapshots * FutureRenderOrbits.IndexOf(orbit))); - Vector3 PositionAt1stDegree = orbit.getTruePositionAtUT(time + (TimeSnapshots * (FutureRenderOrbits.IndexOf(orbit) + (1.0 / 6.0)))); - Vector3 PositionAt2ndDegree = orbit.getTruePositionAtUT(time + (TimeSnapshots * (FutureRenderOrbits.IndexOf(orbit) + (2.0 / 6.0)))); - Vector3 PositionAt3rdDegree = orbit.getTruePositionAtUT(time + (TimeSnapshots * (FutureRenderOrbits.IndexOf(orbit) + (3.0 / 6.0)))); - Vector3 PositionAt4thDegree = orbit.getTruePositionAtUT(time + (TimeSnapshots * (FutureRenderOrbits.IndexOf(orbit) + (4.0 / 6.0)))); - Vector3 PositionAt5thDegree = orbit.getTruePositionAtUT(time + (TimeSnapshots * (FutureRenderOrbits.IndexOf(orbit) + (5.0 / 6.0)))); - Vector3 PositionAtEnd = orbit.getTruePositionAtUT(time + (TimeSnapshots * (FutureRenderOrbits.IndexOf(orbit) + 1))); + /* + List PositionIncrements = new List(); + for (int i = 1; i < Math.Sqrt(TimewarpRate); i++ ) + { + + } + */ + LineRenderer line = null; + GameObject obj = new GameObject("Line"); + + line = obj.AddComponent(); + line.transform.parent = vessel.transform; + line.useWorldSpace = false; // ...and moving along with it (rather + // than staying in fixed world coordinates) + line.transform.localPosition = Vector3.zero; + line.transform.localEulerAngles = Vector3.zero; + + Vector3 PositionAtStart = orbit.getTruePositionAtUT(time + (TimeSnapshots * VesselFutureRenderOrbits.IndexOf(orbit))); + Vector3 PositionAtEnd = orbit.getTruePositionAtUT(time + (TimeSnapshots * (VesselFutureRenderOrbits.IndexOf(orbit) + 1))); + print(PositionAtStart); + print(PositionAtEnd); + + // Make it render a red to yellow triangle, 1 meter wide and 2 meters long + line.material = MapView.fetch.dottedLineMaterial; + line.SetColors(Color.red, Color.yellow); + line.SetWidth(100, 0); + line.SetVertexCount(50); + line.SetPosition(0, ScaledSpace.LocalToScaledSpace(PositionAtStart)); + line.SetPosition(1, ScaledSpace.LocalToScaledSpace(PositionAtEnd)); + + + + + + + + + + + + /* + Vector3 PositionAtStart = orbit.getTruePositionAtUT(time + (TimeSnapshots * VesselFutureRenderOrbits.IndexOf(orbit))); + Vector3 PositionAt1stDegree = orbit.getTruePositionAtUT(time + (TimeSnapshots * (VesselFutureRenderOrbits.IndexOf(orbit) + (1.0 / 6.0)))); + Vector3 PositionAt2ndDegree = orbit.getTruePositionAtUT(time + (TimeSnapshots * (VesselFutureRenderOrbits.IndexOf(orbit) + (2.0 / 6.0)))); + Vector3 PositionAt3rdDegree = orbit.getTruePositionAtUT(time + (TimeSnapshots * (VesselFutureRenderOrbits.IndexOf(orbit) + (3.0 / 6.0)))); + Vector3 PositionAt4thDegree = orbit.getTruePositionAtUT(time + (TimeSnapshots * (VesselFutureRenderOrbits.IndexOf(orbit) + (4.0 / 6.0)))); + Vector3 PositionAt5thDegree = orbit.getTruePositionAtUT(time + (TimeSnapshots * (VesselFutureRenderOrbits.IndexOf(orbit) + (5.0 / 6.0)))); + Vector3 PositionAtEnd = orbit.getTruePositionAtUT(time + (TimeSnapshots * (VesselFutureRenderOrbits.IndexOf(orbit) + 1))); // --- Dot to dot between each orbital segment to make a smooth curve --- // + + // Convert to scaled space + + PositionAtStart = ScaledSpace.LocalToScaledSpace(PositionAtStart); + PositionAt1stDegree = ScaledSpace.LocalToScaledSpace(PositionAt1stDegree); + PositionAt2ndDegree = ScaledSpace.LocalToScaledSpace(PositionAt2ndDegree); + PositionAt3rdDegree = ScaledSpace.LocalToScaledSpace(PositionAt3rdDegree); + PositionAt4thDegree = ScaledSpace.LocalToScaledSpace(PositionAt4thDegree); + PositionAt5thDegree = ScaledSpace.LocalToScaledSpace(PositionAt5thDegree); + PositionAtEnd = ScaledSpace.LocalToScaledSpace(PositionAtEnd); + + // //LineRenderer OrbitBezierRenderer = new LineRenderer(); @@ -348,8 +469,8 @@ public void ManageConics(Vessel vessel, double time) float LineWidth = 1.0f; var camera = PlanetariumCamera.Camera; - var start = camera.WorldToScreenPoint(ScaledSpace.LocalToScaledSpace(PositionAtStart)); - var end = camera.WorldToScreenPoint(ScaledSpace.LocalToScaledSpace(PositionAtEnd)); + var start = camera.WorldToScreenPoint((PositionAtStart)); + var end = camera.WorldToScreenPoint((PositionAtEnd)); var segment = new Vector3(end.y - start.y, start.x - end.x, 0).normalized * (LineWidth / 2); if (!MapView.Draw3DLines) @@ -488,59 +609,59 @@ public void ManageConics(Vessel vessel, double time) } - public void PlanetariumManager(Vessel vessel, double time) + public static void PlanetariumManager(Vessel vessel, double time) { #region Spheres if (ToggleSphereOfInfluences) - { - if (HighLogic.LoadedSceneHasPlanetarium) - { - foreach (CelestialBody body in FlightGlobals.Bodies) - { - - } - } - } - - else + { + if (HighLogic.LoadedSceneHasPlanetarium) { foreach (CelestialBody body in FlightGlobals.Bodies) { - // Remove Lines from view + // Add lines } } + } - if (ToggleHillSpheres) + else + { + foreach (CelestialBody body in FlightGlobals.Bodies) { - if (HighLogic.LoadedSceneHasPlanetarium) - { - foreach (CelestialBody body in FlightGlobals.Bodies) - { - - } - } + // Remove Lines from view } + } - else + if (ToggleHillSpheres) + { + if (HighLogic.LoadedSceneHasPlanetarium) { foreach (CelestialBody body in FlightGlobals.Bodies) { - // Remove Lines from view + // Add lines } } - #endregion + } - if (HighLogic.LoadedSceneHasPlanetarium) + else + { + foreach (CelestialBody body in FlightGlobals.Bodies) { + // Remove Lines from view + } + } + #endregion + + if (HighLogic.LoadedSceneHasPlanetarium) + { if (TimeWarp.CurrentRate > 1) { - ManageConics(vessel, time); + // ManageVesselConics(vessel, time); Planetarium.fetch.UpdateCBs(); vessel.orbitDriver.CancelInvoke("drawOrbit"); - vessel.orbitDriver.SetOrbitMode(OrbitDriver.UpdateMode.IDLE); - Planetarium.Orbits.Remove(vessel.orbitDriver); + //vessel.orbitDriver.SetOrbitMode(OrbitDriver.UpdateMode.IDLE); + //Planetarium.Orbits.Remove(vessel.orbitDriver); } else { @@ -548,29 +669,48 @@ public void PlanetariumManager(Vessel vessel, double time) { Planetarium.Orbits.Add(vessel.orbitDriver); } - Planetarium.fetch.UpdateCBs(); - vessel.orbitDriver.SetOrbitMode(OrbitDriver.UpdateMode.UPDATE); - vessel.orbitDriver.orbitColor = Color.red; + //Planetarium.fetch.UpdateCBs(); + //vessel.orbitDriver.SetOrbitMode(OrbitDriver.UpdateMode.UPDATE); } } } + #endregion + + + #region OrbitManagement - public Orbit NewCalculatedOrbit(Vessel vessel, Vector3d FinalVelocity, double time) + public static Orbit NewCalculatedOrbit(Vessel vessel, Orbit oldOrbit, Vector3d FinalVelocity, double time) { - - Orbit orbit = vessel.orbitDriver.orbit; + Orbit orbit = new Orbit(); CelestialBody oldBody = orbit.referenceBody; - // vessel.orbitDriver.orbit.pos needs a fix!!!! - - orbit.UpdateFromStateVectors(vessel.orbitDriver.orbit.pos, vessel.orbitDriver.orbit.vel + FinalVelocity, vessel.orbitDriver.orbit.referenceBody, time); - orbit.inclination = vessel.orbitDriver.orbit.inclination; - orbit.semiMajorAxis = vessel.orbitDriver.orbit.semiMajorAxis; - orbit.eccentricity = vessel.orbit.eccentricity; - orbit.LAN = vessel.orbit.LAN; - orbit.argumentOfPeriapsis = vessel.orbit.argumentOfPeriapsis; - orbit.meanAnomalyAtEpoch = vessel.orbit.meanAnomalyAtEpoch; - orbit.epoch = vessel.orbit.epoch; - orbit.referenceBody = vessel.orbit.referenceBody; + + if (VesselOrbitalPredictions.ContainsKey(vessel)) + { + VesselOrbitalPredictions.TryGetValue(vessel, out orbit); + } + else + { + orbit = oldOrbit; + VesselOrbitalPredictions.Add(vessel, oldOrbit); + } + + //orbit.UpdateFromStateVectors(orbit.getRelativePositionAtUT(time), orbit.vel, vessel.orbitDriver.orbit.referenceBody, time); + //orbit.UpdateFromStateVectors(oldOrbit.getRelativePositionAtUT(time), oldOrbit.vel + (FinalVelocity), vessel.orbitDriver.orbit.referenceBody, time); + + // Debuging // + #region Debug Values + print("DeltaV Vector: " + FinalVelocity); + print("DeltaV Scalar: " + FinalVelocity.magnitude); + print("OLDSMA: " + vessel.orbitDriver.orbit.semiMajorAxis); + print("NEWSMA: " + orbit.semiMajorAxis); //CalculateSMA(vessel, oldOrbit.getOrbitalSpeedAtRelativePos(oldOrbit.getRelativePositionAtUT(time)) * Vector3d.one + FinalVelocity, time, 1.0)); + print("OLDEcc: " + vessel.orbitDriver.orbit.eccentricity); + print("NEWEcc: " + orbit.eccentricity); //CalculateEccentricity(vessel, oldOrbit.getOrbitalSpeedAtRelativePos(oldOrbit.getRelativePositionAtUT(time)) * Vector3d.one + FinalVelocity, time, 1.0)); + print("OLDLAN: " + vessel.orbitDriver.orbit.LAN); + print("NEWLAN: " + orbit.LAN);//CalculateLPE(vessel, oldOrbit.getOrbitalSpeedAtRelativePos(oldOrbit.getRelativePositionAtUT(time)) * Vector3d.one + FinalVelocity, time, 1.0)); + print("OLDLpe: " + vessel.orbitDriver.orbit.argumentOfPeriapsis); + print("NEWLpe: " + orbit.argumentOfPeriapsis); // CalculateLPE(vessel, oldOrbit.getOrbitalSpeedAtRelativePos(oldOrbit.getRelativePositionAtUT(time)) * Vector3d.one + FinalVelocity, time, 1.0)); + # endregion + var newBody = vessel.orbitDriver.orbit.referenceBody; if (newBody != oldBody) @@ -580,14 +720,272 @@ public Orbit NewCalculatedOrbit(Vessel vessel, Vector3d FinalVelocity, double ti VesselData.UpdateBody(vessel, newBody); } + VesselOrbitalPredictions.Remove(vessel); + VesselOrbitalPredictions.Add(vessel, orbit); + return orbit; } + #region depreciated non state vectors + public static double CalculateInclination(Vessel vessel, Vector3d FinalVelocity, double time, double timeinterval) + { + double Inc = 0; + Orbit NextOrbit = new Orbit(); + NextOrbit.UpdateFromStateVectors(vessel.orbitDriver.orbit.getRelativePositionAtUT(time), FinalVelocity, vessel.orbitDriver.orbit.referenceBody, time); + + + + // Do this + + return Inc; + } + + public static double CalculateSMA(Vessel vessel, Vector3d FinalVelocity, double time, double timeinterval) + { + double SMA = 0; + double Altitude = vessel.orbitDriver.orbit.altitude + vessel.orbitDriver.orbit.referenceBody.Radius; + double CalculationEnergyError = (((Math.Pow(vessel.orbitDriver.orbit.vel.magnitude, 2.0)) / 2.0) - (vessel.orbitDriver.orbit.referenceBody.gravParameter / Altitude)) - vessel.orbitDriver.orbit.orbitalEnergy; + + double ForwardVelocity = FinalVelocity.magnitude; + double NewEnergy = (((Math.Pow(ForwardVelocity, 2.0)) / 2.0) - (vessel.orbitDriver.orbit.referenceBody.gravParameter / Altitude)) - CalculationEnergyError; + + SMA = ((-(vessel.orbitDriver.orbit.referenceBody.gravParameter) / NewEnergy) / 2.0); + + return Math.Abs(SMA); + } + + public static double CalculateEccentricity(Vessel vessel, Vector3d FinalVelocity, double time, double timeinterval) + { + double Eccentricity = 0; + + double ForwardVelocity = FinalVelocity.magnitude; + double Altitude = vessel.orbitDriver.orbit.altitude + vessel.orbitDriver.orbit.referenceBody.Radius; + double NewEnergy = ((Math.Pow(ForwardVelocity, 2.0)) / 2.0) - (vessel.orbitDriver.orbit.referenceBody.gravParameter / Altitude); + double CalculationEnergyError = (((Math.Pow(vessel.orbitDriver.orbit.vel.magnitude, 2.0)) / 2.0) - (vessel.orbitDriver.orbit.referenceBody.gravParameter / Altitude)) + vessel.orbitDriver.orbit.orbitalEnergy; + + + Orbit NextOrbit = new Orbit(); + NextOrbit.UpdateFromStateVectors(vessel.orbitDriver.orbit.getRelativePositionAtUT(time), FinalVelocity, vessel.orbitDriver.orbit.referenceBody, time); + + double MomentOfInertia = Math.Pow(vessel.orbitDriver.orbit.pos.magnitude, 2.0) * VesselData.FetchMass(vessel); + Vector3d AngularVelocity = Vector3d.Cross(vessel.orbitDriver.orbit.getRelativePositionAtUT(time), FinalVelocity) / (Math.Pow(vessel.orbitDriver.orbit.getRelativePositionAtUT(time).magnitude, 2.0)); + Vector3d AngularMomentumVector = AngularVelocity * MomentOfInertia; // Maybe this if not then calculate + + + Eccentricity = Math.Sqrt((1.0 + ((2.0 * (NewEnergy - CalculationEnergyError) * Math.Pow(AngularMomentumVector.magnitude, 2.0)) / (Math.Pow(vessel.orbitDriver.orbit.referenceBody.gravParameter, 2.0))))); + + return Eccentricity; + } + + public static double CalculateLAN(Vessel vessel, Vector3d FinalVelocity, double time, double timeinterval) + { + double LAN = 0; + + Orbit NextOrbit = new Orbit(); + NextOrbit.UpdateFromStateVectors(vessel.orbitDriver.orbit.pos, FinalVelocity, vessel.orbitDriver.orbit.referenceBody, time); + + double MomentOfInertia = Math.Pow(vessel.orbitDriver.orbit.getRelativePositionAtUT(time).magnitude, 2.0) * VesselData.FetchMass(vessel); + Vector3d AngularVelocity = Vector3d.Cross(vessel.orbitDriver.orbit.getRelativePositionAtUT(time), FinalVelocity) / (Math.Pow(vessel.orbitDriver.orbit.getRelativePositionAtUT(time).magnitude, 2.0)); + + Vector3d AngularMomentumVector = AngularVelocity * MomentOfInertia; // Maybe this if not then calculate + + Vector3d NeutralVector = new Vector3d(0, 0, 1); + + Vector3d AscendingNodeVector = Vector3d.Cross(AngularMomentumVector, NeutralVector); // Maybe this + + if (AscendingNodeVector.y < 0) + { + LAN = (2.0 * Math.PI) - Math.Acos(AscendingNodeVector.x / AscendingNodeVector.magnitude); + } + + else + { + LAN = Math.Acos(AscendingNodeVector.x / AscendingNodeVector.magnitude); + } + + LAN = UtilMath.RadiansToDegrees(LAN); + return LAN; + } + + public static double CalculateLPE(Vessel vessel, Vector3d FinalVelocity, double time, double timeinterval) + { + double LPE = 0; + + Orbit NextOrbit = new Orbit(); + NextOrbit.UpdateFromStateVectors(vessel.orbitDriver.orbit.getRelativePositionAtUT(time), FinalVelocity, vessel.orbitDriver.orbit.referenceBody, time); + + + double MomentOfInertia = Math.Pow(vessel.orbitDriver.orbit.getRelativePositionAtUT(time).magnitude, 2.0) * VesselData.FetchMass(vessel); + Vector3d AngularVelocity = Vector3d.Cross(vessel.orbitDriver.orbit.getRelativePositionAtUT(time), FinalVelocity) / (Math.Pow(vessel.orbitDriver.orbit.getRelativePositionAtUT(time).magnitude, 2.0)); + Vector3d AngularMomentumVector = AngularVelocity * MomentOfInertia; // Maybe this if not then calculate + + Vector3d NeutralVector = new Vector3d(0, 0, 1); + Vector3d AscendingNodeVector = Vector3d.Cross(AngularMomentumVector, NeutralVector); // Maybe this + Vector3d EccentricVector = NextOrbit.eccVec; + + LPE = Math.Acos((Vector3d.Cross(AscendingNodeVector, EccentricVector).magnitude) / (AscendingNodeVector.magnitude * EccentricVector.magnitude)); + + LPE = UtilMath.RadiansToDegrees(LPE); + return LPE; + } + #endregion + #endregion - public void SetOrbit(Vessel vessel, Vector3d FinalVelocity) + public static void ManageOrbitalPredictionsBody(CelestialBody body) + { + Orbit orbit = new Orbit(); + + if (BodyOrbitalPredictions.ContainsKey(body)) + { + BodyOrbitalPredictions.TryGetValue(body, out orbit); + } + + else + { + BodyOrbitalPredictions.Add(body, body.orbitDriver.orbit); + orbit = body.orbitDriver.orbit; + } + + + + + + } + + public static void ManageOrbitPredictionsVessel(Vessel vessel) // Called per second + { + Orbit orbit = new Orbit(); + + if (VesselOrbitalPredictions.ContainsKey(vessel)) + { + VesselOrbitalPredictions.TryGetValue(vessel, out orbit); + } + + else + { + VesselOrbitalPredictions.Add(vessel, vessel.orbitDriver.orbit); + orbit = vessel.orbitDriver.orbit; + } + + + Vector3d FinalVelVector = new Vector3d(); + Vector3d InitialVelVector = GetMomentaryDeltaV(vessel, HighLogic.CurrentGame.UniversalTime - TimeWarp.CurrentRate); + + + if (TimeWarp.CurrentRate > 100 && TimeWarp.CurrentRate <= 1000) // Anti Lag Method + { + for (int i = 0; i < TimeWarp.CurrentRate / 10.0; i++) + { + List InfluencingAccelerationVectors = InfluencingAccelerationsV(vessel, TimeAtTimewarpStart + i); + + Vector3d FinalVelocityVector = new Vector3d(); + + foreach (Vector3d Acceleration in InfluencingAccelerationVectors) + { + FinalVelocityVector = FinalVelocityVector + (Acceleration); + } + + FinalVelVector = FinalVelVector + FinalVelocityVector; + + // orbit = NewCalculatedOrbit(vessel, orbit, FinalVelocityVector , TimeAtTimewarpStart + i); + } + } + if (TimeWarp.CurrentRate > 1000 && TimeWarp.CurrentRate <= 10000) // Anti Lag Method + { + for (int i = 0; i < TimeWarp.CurrentRate / 100.0; i++) + { + List InfluencingAccelerationVectors = InfluencingAccelerationsV(vessel, TimeAtTimewarpStart + i); + + Vector3d FinalVelocityVector = new Vector3d(); + + foreach (Vector3d Acceleration in InfluencingAccelerationVectors) + { + FinalVelocityVector = FinalVelocityVector + (Acceleration); + } + + FinalVelVector = FinalVelVector + FinalVelocityVector; + + // orbit = NewCalculatedOrbit(vessel, orbit, FinalVelocityVector, TimeAtTimewarpStart + i); + } + } + + if (TimeWarp.CurrentRate > 10000 && TimeWarp.CurrentRate <= 100000) + { + for (int i = 0; i < TimeWarp.CurrentRate / 1000.0; i++) + { + List InfluencingAccelerationVectors = InfluencingAccelerationsV(vessel, TimeAtTimewarpStart + i); + + Vector3d FinalVelocityVector = new Vector3d(); + + foreach (Vector3d Acceleration in InfluencingAccelerationVectors) + { + FinalVelocityVector = FinalVelocityVector + (Acceleration); + } + + FinalVelVector = FinalVelVector + FinalVelocityVector; + + // orbit = NewCalculatedOrbit(vessel, orbit, FinalVelocityVector, TimeAtTimewarpStart + i); + } + } + + if (TimeWarp.CurrentRate > 100000) + { + for (int i = 0; i < TimeWarp.CurrentRate / 10000.0; i++) + { + List InfluencingAccelerationVectors = InfluencingAccelerationsV(vessel, TimeAtTimewarpStart + i); + + Vector3d FinalVelocityVector = new Vector3d(); + + foreach (Vector3d Acceleration in InfluencingAccelerationVectors) + { + FinalVelocityVector = FinalVelocityVector + (Acceleration); + } + + FinalVelVector = FinalVelVector + FinalVelocityVector; + + // orbit = NewCalculatedOrbit(vessel, orbit, FinalVelocityVector, TimeAtTimewarpStart + i); + } + } + + else + { + for (int i = 0; i < TimeWarp.CurrentRate; i++) + { + List InfluencingAccelerationVectors = InfluencingAccelerationsV(vessel, TimeAtTimewarpStart + i); + + Vector3d FinalVelocityVector = new Vector3d(); + + foreach (Vector3d Acceleration in InfluencingAccelerationVectors) + { + FinalVelocityVector = FinalVelocityVector + (Acceleration); + } + + + FinalVelVector = FinalVelVector + (FinalVelocityVector); + + //orbit = NewCalculatedOrbit(vessel, orbit, FinalVelocityVector, TimeAtTimewarpStart + i); + } + } + + FinalVelVector = InitialVelVector - FinalVelVector; + print("Change in delta V across timewarp duration in one second: " + FinalVelVector.magnitude); + + orbit = BuildOrbitFromStateVectors.BuildFromStateVectors(orbit.getRelativePositionAtUT(HighLogic.CurrentGame.UniversalTime), orbit.vel + FinalVelVector, orbit.referenceBody, HighLogic.CurrentGame.UniversalTime); + + // Is this actively updating the orbit? If So... why the bloody hell so?!!?!? + //orbit.UpdateFromStateVectors(orbit.getRelativePositionAtUT(HighLogic.CurrentGame.UniversalTime), orbit.vel + FinalVelVector, orbit.referenceBody, HighLogic.CurrentGame.UniversalTime); + + VesselOrbitalPredictions.Remove(vessel); + VesselOrbitalPredictions.Add(vessel, orbit); + + TimeAtTimewarpStart = HighLogic.CurrentGame.UniversalTime; + + } + + public static void SetOrbit(Vessel vessel, Vector3d FinalVelocity) { - PlanetariumManager(vessel, HighLogic.CurrentGame.UniversalTime); + //PlanetariumManager(vessel, HighLogic.CurrentGame.UniversalTime); #region DepreciatedMethods /* @@ -651,7 +1049,7 @@ public void SetOrbit(Vessel vessel, Vector3d FinalVelocity) vessel.orbitDriver.UpdateOrbit(); */ - // Priority Method: + // Priority Method: /* double DeltaOrbitalEnergy = -(Math.Pow((FinalVelocity.magnitude), 2.0) / 2.0) - (vessel.orbitDriver.orbit.referenceBody.gravParameter / (vessel.orbitDriver.orbit.altitude)); @@ -668,21 +1066,49 @@ public void SetOrbit(Vessel vessel, Vector3d FinalVelocity) */ #endregion - print("Delta V: " + FinalVelocity.magnitude); - Orbit orbit = NewCalculatedOrbit(vessel, FinalVelocity, HighLogic.CurrentGame.UniversalTime); + Orbit orbit = new Orbit(); + orbit = NewCalculatedOrbit(vessel, vessel.orbitDriver.orbit, FinalVelocity, HighLogic.CurrentGame.UniversalTime); + + if (TimeWarp.CurrentRate < 2) + { orbit.Init(); - orbit.UpdateFromUT(HighLogic.CurrentGame.UniversalTime); - vessel.orbitDriver.UpdateOrbit(); VesselData.UpdateVesselSMA(vessel, vessel.orbitDriver.orbit.semiMajorAxis); VesselData.UpdateVesselLPE(vessel, vessel.orbitDriver.orbit.argumentOfPeriapsis); VesselData.UpdateVesselLAN(vessel, vessel.orbitDriver.orbit.meanAnomaly); VesselData.UpdateVesselECC(vessel, vessel.orbitDriver.orbit.eccentricity); VesselData.UpdateVesselINC(vessel, vessel.orbitDriver.orbit.inclination); + } + //orbit.UpdateFromUT(HighLogic.CurrentGame.UniversalTime); + CurrentProcess = false; + } + } - CurrentProcess = false; + + + public static class BuildOrbitFromStateVectors + { + public static Orbit BuildFromStateVectors(Vector3d position, Vector3d velocity, CelestialBody body, double UniversalTime) + { + Orbit StateVectorBuiltOrbit = new Orbit(); + + + + + double NewSemiMajorAxis = 0; + double NewInclination = 0; + double NewEccentricity = 0; + double NewLAN = 0; + double NewLPE = 0; + double NewEPH = 0; + double NewMNA = 0; + + + return StateVectorBuiltOrbit; } } -} + + +} \ No newline at end of file diff --git a/Source/Settings.cs b/Source/Settings.cs index b13556c..5c85eee 100644 --- a/Source/Settings.cs +++ b/Source/Settings.cs @@ -77,6 +77,14 @@ public static void WriteNBodyConics(bool NBC) // 1.6.0 NBody SimSet.SetValue("NBodySimulationConics", NBC.ToString()); } + public static void WriteNBodyConicsPatches(double NBCC) // 1.6.0 NBody + { + ConfigNode Data = SettingData; + ConfigNode SimSet = Data.GetNode("SIMULATION"); + SimSet.SetValue("NBodySimulationConicsPatches", NBCC.ToString()); + } + + public static void Write24H(bool H24) { ConfigNode Data = SettingData; @@ -139,6 +147,13 @@ public static bool ReadNBC() // 1.6.0 NBody Conics return NBC; } + public static double ReadNBCC() // 1.6.0 NBody Conics + { + ConfigNode Data = SettingData; + ConfigNode SimSet = Data.GetNode("SIMULATION"); + double NBCC = double.Parse(SimSet.GetValue("NBodySimulationConicsPatches")); + return NBCC; + } public static bool Read24Hr() { diff --git a/Source/StationKeepingManager.cs b/Source/StationKeepingManager.cs index 1cf2bd5..f4939a4 100644 --- a/Source/StationKeepingManager.cs +++ b/Source/StationKeepingManager.cs @@ -126,7 +126,7 @@ public static void FuelManager(Vessel vessel) // Add Part Module Checks Here // Maybe 1.6.0 - if (ConnectionToVessel = false) + if (ConnectionToVessel == false) { SignalCheck = false; ScreenMessages.PostScreenMessage("Warning: " + vessel.vesselName + " has no connection to send Station Keeping command!"); diff --git a/Source/UserInterface.cs b/Source/UserInterface.cs index d524b8e..328479f 100644 --- a/Source/UserInterface.cs +++ b/Source/UserInterface.cs @@ -38,8 +38,9 @@ class UserInterface : MonoBehaviour { private static int currentTab = 0; private static string[] tabs = { "Vessels", "Settings" }; - private static Rect windowPosition = new Rect(0, 0, 300, 400); - private static Rect subwindowPosition = new Rect(0, 0, 450, 150); + private static Rect MainwindowPosition = new Rect(0, 0, 300, 400); + private static Rect DecayBreakdownwindowPosition = new Rect(0, 0, 450, 150); + private static Rect NBodyManagerwindowPosition = new Rect(0, 0, 450, 150); private static GUIStyle windowStyle = new GUIStyle(HighLogic.Skin.window); private static Color tabUnselectedColor = new Color(0.0f, 0.0f, 0.0f); private static Color tabSelectedColor = new Color(0.0f, 0.0f, 0.0f); @@ -50,7 +51,9 @@ class UserInterface : MonoBehaviour public static ApplicationLauncherButton ToolbarButton = null; public static bool Visible = false; - public static bool SubVisible = false; + public static bool DecayBreakdownVisible = false; + public static bool NBodyBreakdownVisible = false; + public static Texture launcher_icon = null; Vector2 scrollPosition1 = Vector2.zero; @@ -92,7 +95,8 @@ public void DestroyEvent() ApplicationLauncher.Instance.RemoveModApplication(ToolbarButton); ToolbarButton = null; Visible = false; - SubVisible = false; + DecayBreakdownVisible = false; + NBodyBreakdownVisible = false; } private void GuiOn() @@ -103,24 +107,29 @@ private void GuiOn() private void GuiOff() { Visible = false; - SubVisible = false; + DecayBreakdownVisible = false; + NBodyBreakdownVisible = false; } public void OnGUI() { if (Visible) { - windowPosition = GUILayout.Window(id, windowPosition, MainWindow, "Orbital Decay Manager", windowStyle); + MainwindowPosition = GUILayout.Window(id, MainwindowPosition, MainWindow, "Orbital Decay Manager", windowStyle); + } + if (DecayBreakdownVisible) + { + DecayBreakdownwindowPosition = GUILayout.Window(8989, DecayBreakdownwindowPosition, DecayBreakdownWindow, "Orbital Decay Breakdown Display", windowStyle); } - if (SubVisible) + if (NBodyBreakdownVisible) { - subwindowPosition = GUILayout.Window(8989, subwindowPosition, DecayBreakdownWindow, "Orbital Decay Breakdown Display", windowStyle); + NBodyManagerwindowPosition = GUILayout.Window(8988, NBodyManagerwindowPosition, NBodyManagerWindow, "Orbital Decay N-Body Manager", windowStyle); } } public void MainWindow(int windowID) { - if (GUI.Button(new Rect(windowPosition.width - 22, 3, 19, 19), "x")) + if (GUI.Button(new Rect(MainwindowPosition.width - 22, 3, 19, 19), "x")) { if (ToolbarButton != null) ToolbarButton.toggleButton.Value = false; @@ -153,8 +162,8 @@ public void MainWindow(int windowID) } GUILayout.EndVertical(); GUI.DragWindow(); - windowPosition.x = Mathf.Clamp(windowPosition.x, 0f, Screen.width - windowPosition.width); - windowPosition.y = Mathf.Clamp(windowPosition.y, 0f, Screen.height - windowPosition.height); + MainwindowPosition.x = Mathf.Clamp(MainwindowPosition.x, 0f, Screen.width - MainwindowPosition.width); + MainwindowPosition.y = Mathf.Clamp(MainwindowPosition.y, 0f, Screen.height - MainwindowPosition.height); } public void InformationTab() @@ -246,9 +255,18 @@ public void InformationTab() if (GUILayout.Button("Toggle Decay Rate Breakdown")) // Display a new window here? { subwindowVessel = vessel; - SubVisible = !SubVisible; + DecayBreakdownVisible = !DecayBreakdownVisible; } + if (Settings.ReadNB()) + { + GUILayout.Space(2); + if (GUILayout.Button("Toggle NBody Breakdown")) // New Window for NBody + { + subwindowVessel = vessel; + NBodyBreakdownVisible = !NBodyBreakdownVisible; + } + } GUILayout.Space(2); double TimeUntilDecayInUnits = 0.0; @@ -381,6 +399,20 @@ public void SettingsTab() var DecayDifficulty = Settings.ReadDecayDifficulty(); var ResourceDifficulty = Settings.ReadResourceRateDifficulty(); + var NBody = Settings.ReadNB(); + + var NBodyText = ""; // 1.6.0 N-Body + + if (Settings.ReadNB()) + { + NBodyText = "Disable N-Body Perturbations"; + } + + else + { + NBodyText = "Enable N-Body Perturbations"; + } + GUILayout.Space(2); if (GUILayout.Button("Toggle Kerbin Day (6 hour) / Earth Day (24 hour)")) @@ -446,41 +478,6 @@ public void SettingsTab() GUILayout.Space(2); GUILayout.Label("____________________________________"); GUILayout.Space(3); - - /*scrollPosition3 = GUILayout.BeginScrollView(scrollPosition3, GUILayout.Width(290), GUILayout.Height(100)); - for (int i = 0; i < PartResourceLibrary.Instance.resourceDefinitions.ToList().Count; i++) - { - string Resource = PartResourceLibrary.Instance.resourceDefinitions.ToList().ElementAt(i).name; - if (PartResourceLibrary.Instance.resourceDefinitions.ToList().ElementAt(i).resourceTransferMode != ResourceTransferMode.NONE && - PartResourceLibrary.Instance.resourceDefinitions.ToList().ElementAt(i).resourceFlowMode != ResourceFlowMode.ALL_VESSEL && - PartResourceLibrary.Instance.resourceDefinitions.ToList().ElementAt(i).resourceFlowMode != ResourceFlowMode.NO_FLOW && - (Resource != "EVA Propellant" && Resource != "Ore" && Resource != "ElectricCharge" && Resource != "IntakeAir")) - { - GUILayout.Label("Resource Name: " + Resource); - GUILayout.Space(3); - - if (GUILayout.Button("Set as Station Keeping Resource")) - { - Settings.WriteStatKeepResource(Resource); - for (int j = 0; j < FlightGlobals.Vessels.Count; j++) - { - Vessel vessel = FlightGlobals.Vessels.ElementAt(j); - if (vessel.situation == Vessel.Situations.ORBITING && vessel.vesselType != VesselType.SpaceObject && vessel.vesselType != VesselType.Unknown) - { - //VesselData.UpdateVesselFuel(vessel, ResourceManager.GetResources(vessel, Resource)); - } - } - ScreenMessages.PostScreenMessage("Station Keeping Resource set to: " + Resource); - } - GUILayout.Space(3); - } - } - GUILayout.EndScrollView(); - GUILayout.Label("Note: Changing resources requires switching to each Station Keeping vessel"); - GUILayout.Space(2); - GUILayout.Label("____________________________________"); - GUILayout.Space(3); - */ GUI.skin.label.alignment = TextAnchor.MiddleCenter; MultiplierValue2 = GUILayout.HorizontalSlider(MultiplierValue2, 0.5f, 50.0f); GUILayout.Space(2); @@ -495,15 +492,68 @@ public void SettingsTab() ScreenMessages.PostScreenMessage("Resource drain rate multiplier: " + ((MultiplierValue2 / 5).ToString("F1"))); } + GUILayout.Space(2); + + + if (GUILayout.Button(NBodyText)) + { + Settings.WriteNBody(!NBody); + } + + GUILayout.EndVertical(); } + + public void NBodyManagerWindow(int id) // 1.6.0 + { + if (GUI.Button(new Rect(NBodyManagerwindowPosition.width - 22, 3, 19, 19), "x")) + { + if (NBodyBreakdownVisible != null) + NBodyBreakdownVisible = false; + } + GUILayout.BeginVertical(); + GUILayout.Space(10); + try + { + Vessel vessel = subwindowVessel; + + GUILayout.Label("Vessel: " + vessel.GetName()); + GUILayout.Space(4); // Make new parsing here for vel formatting + GUILayout.Label("Total Change in velocity from external forces: " + NBodyManager.GetMomentaryDeltaV(vessel, HighLogic.CurrentGame.UniversalTime).magnitude.ToString("F9") + "m/s"); + GUILayout.Space(2); + GUILayout.Label("Change in velocity from external forces (x - axis): " + NBodyManager.GetMomentaryDeltaV(vessel, HighLogic.CurrentGame.UniversalTime).x.ToString("F9") + "m/s"); + GUILayout.Space(2); + GUILayout.Label("Change in velocity from external forces (y - axis): " + NBodyManager.GetMomentaryDeltaV(vessel, HighLogic.CurrentGame.UniversalTime).y.ToString("F9") + "m/s"); + GUILayout.Space(2); + GUILayout.Label("Change in velocity from external forces (z - axis): " + NBodyManager.GetMomentaryDeltaV(vessel, HighLogic.CurrentGame.UniversalTime).z.ToString("F9")+ "m/s"); + GUILayout.Space(2); + + + } + + catch (ArgumentNullException) + { + GUILayout.Label("WARNING: Error Detected"); + GUILayout.Space(2); + GUILayout.Label("Please post an error report in the Orbital Decay Forum. This is an N-Body problem."); + GUILayout.Space(2); + GUILayout.Label("Thanks, Whitecat106"); + } + GUILayout.EndVertical(); + GUI.DragWindow(); + //NBodyManagerwindowPosition.x = Mathf.Clamp(MainwindowPosition.x, 0f, Screen.width - MainwindowPosition.width); + //NBodyManagerwindowPosition.y = Mathf.Clamp(MainwindowPosition.y, 0f, Screen.height - MainwindowPosition.height); + + } + + public void DecayBreakdownWindow(int id) { - if (GUI.Button(new Rect(subwindowPosition.width - 22, 3, 19, 19), "x")) + if (GUI.Button(new Rect(DecayBreakdownwindowPosition.width - 22, 3, 19, 19), "x")) { - if (SubVisible != null) - SubVisible = false; + if (DecayBreakdownVisible != null) + DecayBreakdownVisible = false; } GUILayout.BeginVertical(); GUILayout.Space(10); @@ -545,8 +595,8 @@ public void DecayBreakdownWindow(int id) } GUILayout.EndVertical(); GUI.DragWindow(); - windowPosition.x = Mathf.Clamp(windowPosition.x, 0f, Screen.width - windowPosition.width); - windowPosition.y = Mathf.Clamp(windowPosition.y, 0f, Screen.height - windowPosition.height); + //MainwindowPosition.x = Mathf.Clamp(MainwindowPosition.x, 0f, Screen.width - MainwindowPosition.width); + //MainwindowPosition.y = Mathf.Clamp(MainwindowPosition.y, 0f, Screen.height - MainwindowPosition.height); } public string FormatDecayRateToString(double DecayRate)