From 05475d6acfbd4eea00069e9cdc594dbadf0ee76d Mon Sep 17 00:00:00 2001 From: Lamont Granquist Date: Mon, 20 Nov 2023 21:13:31 -0800 Subject: [PATCH] Nuke more of the old gravity turner code somehow this never got cleaned up Signed-off-by: Lamont Granquist --- MechJeb2/MechJeb2.csproj | 1 - MechJeb2/MechJebModuleAscentGTAutopilot.cs | 240 --------------------- MechJeb2/MechJebModuleAscentMenu.cs | 27 +-- MechJeb2/MechJebModuleAscentSettings.cs | 17 +- 4 files changed, 7 insertions(+), 278 deletions(-) delete mode 100644 MechJeb2/MechJebModuleAscentGTAutopilot.cs diff --git a/MechJeb2/MechJeb2.csproj b/MechJeb2/MechJeb2.csproj index 2932d2bd..60b46450 100644 --- a/MechJeb2/MechJeb2.csproj +++ b/MechJeb2/MechJeb2.csproj @@ -100,7 +100,6 @@ - diff --git a/MechJeb2/MechJebModuleAscentGTAutopilot.cs b/MechJeb2/MechJebModuleAscentGTAutopilot.cs deleted file mode 100644 index 5e5e6e41..00000000 --- a/MechJeb2/MechJebModuleAscentGTAutopilot.cs +++ /dev/null @@ -1,240 +0,0 @@ -extern alias JetBrainsAnnotations; -using System; -using KSP.Localization; - -/* - * NOTE: THAT THIS IS NOT INTENDED TO BE A PERFECTLY FAITHFUL REIMPLEMENTATION OF - * THE GRAVITY TURN MOD FOR KSP. - */ - -namespace MuMech -{ - public class MechJebModuleAscentGTAutopilot : MechJebModuleAscentBaseAutopilot - { - public MechJebModuleAscentGTAutopilot(MechJebCore core) : base(core) { } - - protected override void OnModuleEnabled() - { - base.OnModuleEnabled(); - _maxholdAPTime = 0.0F; - _mode = AscentMode.VERTICAL_ASCENT; - } - - private bool IsVerticalAscent(double altitude, double velocity) - { - if (altitude < AscentSettings.TurnStartAltitude && velocity < AscentSettings.TurnStartVelocity) - { - return true; - } - - return false; - } - - private enum AscentMode { VERTICAL_ASCENT, INITIATE_TURN, GRAVITY_TURN, HOLD_AP, COAST_TO_APOAPSIS, EXIT } - - private AscentMode _mode; - - protected override bool DriveAscent2() - { - switch (_mode) - { - case AscentMode.VERTICAL_ASCENT: - DriveVerticalAscent(); - break; - - case AscentMode.INITIATE_TURN: - DriveInitiateTurn(); - break; - - case AscentMode.GRAVITY_TURN: - DriveGravityTurn(); - break; - - case AscentMode.HOLD_AP: - DriveHoldAP(); - break; - - case AscentMode.COAST_TO_APOAPSIS: - DriveCoastToApoapsis(); - break; - - case AscentMode.EXIT: - return false; - - default: - throw new ArgumentOutOfRangeException(); - } - - return true; - } - - private void DriveVerticalAscent() - { - if (!IsVerticalAscent(VesselState.altitudeTrue, VesselState.speedSurface)) _mode = AscentMode.INITIATE_TURN; - if (Orbit.ApA > AscentSettings.IntermediateAltitude) _mode = AscentMode.GRAVITY_TURN; - - //during the vertical ascent we just thrust straight up at max throttle - AttitudeTo(90); - - bool liftedOff = Vessel.LiftedOff() && !Vessel.Landed; - - Core.Attitude.SetAxisControl(liftedOff, liftedOff, liftedOff && VesselState.altitudeBottom > AscentSettings.RollAltitude); - - Core.Thrust.TargetThrottle = 1.0F; - - if (!Vessel.LiftedOff() || Vessel.Landed) Status = Localizer.Format("#MechJeb_Ascent_status6"); //"Awaiting liftoff" - else Status = Localizer.Format("#MechJeb_Ascent_status18"); //"Vertical ascent" - } - - private void DriveInitiateTurn() - { - //stop the intermediate "burn" when our apoapsis reaches the desired altitude - if (Orbit.ApA > AscentSettings.DesiredOrbitAltitude) - { - _mode = AscentMode.COAST_TO_APOAPSIS; - return; - } - - if (90 - AscentSettings.TurnStartPitch >= SrfvelPitch()) - { - _mode = AscentMode.GRAVITY_TURN; - return; - } - - if (Orbit.ApA > AscentSettings.IntermediateAltitude) - { - _mode = AscentMode.GRAVITY_TURN; - return; - } - - //if we've fallen below the turn start altitude, go back to vertical ascent - if (IsVerticalAscent(VesselState.altitudeTrue, VesselState.speedSurface)) - { - _mode = AscentMode.VERTICAL_ASCENT; - return; - } - - AttitudeTo(90 - AscentSettings.TurnStartPitch); - - - Core.Thrust.TargetThrottle = ThrottleToRaiseApoapsis(Orbit.ApR, AscentSettings.IntermediateAltitude + MainBody.Radius); - if (Core.Thrust.TargetThrottle < 1.0F) - { - Status = Localizer.Format("#MechJeb_Ascent_status19"); //"Fine tuning intermediate altitude" - return; - } - - - Status = Localizer.Format("#MechJeb_Ascent_status20"); //"Initiate gravity turn" - } - - private double FixedTimeToAp() - { - if (Vessel.orbit.timeToPe < Vessel.orbit.timeToAp) - return Vessel.orbit.timeToAp - Vessel.orbit.period; - return Vessel.orbit.timeToAp; - } - - private double _maxholdAPTime; - - private void DriveGravityTurn() - { - //stop the intermediate "burn" when our apoapsis reaches the desired altitude - if (Orbit.ApA > AscentSettings.DesiredOrbitAltitude) - { - _mode = AscentMode.COAST_TO_APOAPSIS; - return; - } - - - if (FixedTimeToAp() < AscentSettings.HoldAPTime && _maxholdAPTime > AscentSettings.HoldAPTime) - { - _mode = AscentMode.HOLD_AP; - return; - } - - _maxholdAPTime = Math.Max(_maxholdAPTime, FixedTimeToAp()); - - // fade pitch from AoA at 90% of intermediateAltitude to 0 at 95% of intermediateAltitude - double pitchfade = MuUtils.Clamp(-20 * VesselState.altitudeASL / AscentSettings.IntermediateAltitude + 19, 0.0, 1.0); - - // srfvelPitch == zero AoA - AttitudeTo(SrfvelPitch() * pitchfade); - - Core.Thrust.TargetThrottle = ThrottleToRaiseApoapsis(Orbit.ApR, AscentSettings.IntermediateAltitude + MainBody.Radius); - if (Core.Thrust.TargetThrottle < 1.0F) - { - Status = Localizer.Format("#MechJeb_Ascent_status19"); //"Fine tuning intermediate altitude" - return; - } - - - Status = Localizer.Format("#MechJeb_Ascent_status22"); //"Gravity turn" - } - - private void DriveHoldAP() - { - //stop the intermediate "burn" when our apoapsis reaches the desired altitude - if (Orbit.ApA > AscentSettings.DesiredOrbitAltitude) - { - _mode = AscentMode.COAST_TO_APOAPSIS; - return; - } - - // generally this is in response to typing numbers into the intermediate altitude text box and - // an accidental transition to later in the state machine - if (Orbit.ApA < 0.98 * AscentSettings.IntermediateAltitude) - { - _mode = AscentMode.GRAVITY_TURN; - return; - } - - AttitudeTo(0); /* FIXME: corrective steering */ - - Core.Thrust.TargetThrottle = FixedTimeToAp() < AscentSettings.HoldAPTime ? 1.0F : 0.1F; - - Status = Localizer.Format("#MechJeb_Ascent_status24"); //"Holding AP" - } - - private void DriveCoastToApoapsis() - { - Core.Thrust.TargetThrottle = 0; - - if (VesselState.altitudeASL > MainBody.RealMaxAtmosphereAltitude()) - { - _mode = AscentMode.EXIT; - Core.Warp.MinimumWarp(); - return; - } - - //if our apoapsis has fallen too far, resume the gravity turn - if (Orbit.ApA < AscentSettings.DesiredOrbitAltitude - 1000.0) - { - _mode = AscentMode.HOLD_AP; - Core.Warp.MinimumWarp(); - return; - } - - Core.Thrust.TargetThrottle = 0; - - // follow surface velocity to reduce flipping - AttitudeTo(SrfvelPitch()); - - if (Orbit.ApA < AscentSettings.DesiredOrbitAltitude) - { - Core.Warp.WarpPhysicsAtRate(1); - Core.Thrust.TargetThrottle = ThrottleToRaiseApoapsis(Orbit.ApR, AscentSettings.DesiredOrbitAltitude + MainBody.Radius); - } - else - { - if (Core.Node.Autowarp) - { - //warp at x2 physical warp: - Core.Warp.WarpPhysicsAtRate(2); - } - } - - Status = Localizer.Format("#MechJeb_Ascent_status23"); //"Coasting to edge of atmosphere" - } - } -} diff --git a/MechJeb2/MechJebModuleAscentMenu.cs b/MechJeb2/MechJebModuleAscentMenu.cs index 0e91bdbc..0fbcf7f7 100644 --- a/MechJeb2/MechJebModuleAscentMenu.cs +++ b/MechJeb2/MechJebModuleAscentMenu.cs @@ -175,31 +175,6 @@ private void ShowTargetingGUIElements() Profiler.EndSample(); } - private void ShowGuidanceSettingsGUIElements() - { - Profiler.BeginSample("MJ.GUIWindow.ShowGuidanceSettings"); - - - if (_ascentSettings.AscentType == AscentType.GRAVITYTURN) - { - GUILayout.BeginVertical(GUI.skin.box); - - GuiUtils.SimpleTextBox(CachedLocalizer.Instance.MechJebAscentLabel8, _ascentSettings.TurnStartAltitude, - "km"); //Turn start altitude: - GuiUtils.SimpleTextBox(CachedLocalizer.Instance.MechJebAscentLabel9, _ascentSettings.TurnStartVelocity, - "m/s"); //Turn start velocity: - GuiUtils.SimpleTextBox(CachedLocalizer.Instance.MechJebAscentLabel10, _ascentSettings.TurnStartPitch, "deg"); //Turn start pitch: - GuiUtils.SimpleTextBox(CachedLocalizer.Instance.MechJebAscentLabel11, _ascentSettings.IntermediateAltitude, - "km"); //Intermediate altitude: - GuiUtils.SimpleTextBox(CachedLocalizer.Instance.MechJebAscentLabel12, _ascentSettings.HoldAPTime, "s"); //Hold AP Time: - GUILayout.EndVertical(); - } - - _ascentSettings.LimitQaEnabled = _ascentSettings.AscentType == AscentType.PVG; // this is mandatory for PVG - - Profiler.EndSample(); - } - private void ShowStatusGUIElements() { Profiler.BeginSample("MJ.GUIWindow.ShowStatus"); @@ -413,8 +388,8 @@ protected override void WindowGUI(int windowID) //PerformanceTestGUIElements(); VisibleSectionsGUIElements(); ShowTargetingGUIElements(); - ShowGuidanceSettingsGUIElements(); + _ascentSettings.LimitQaEnabled = _ascentSettings.AscentType == AscentType.PVG; // this is mandatory for PVG GUILayout.BeginVertical(GUI.skin.box); GUILayout.BeginHorizontal(); diff --git a/MechJeb2/MechJebModuleAscentSettings.cs b/MechJeb2/MechJebModuleAscentSettings.cs index 1314943a..44617b14 100644 --- a/MechJeb2/MechJebModuleAscentSettings.cs +++ b/MechJeb2/MechJebModuleAscentSettings.cs @@ -6,7 +6,7 @@ namespace MuMech { - public enum AscentType { CLASSIC, GRAVITYTURN, PVG } + public enum AscentType { CLASSIC, PVG } public class MechJebModuleAscentSettings : ComputerModule { @@ -287,17 +287,12 @@ public bool Autostage private MechJebModuleAscentBaseAutopilot GetAscentModule(AscentType type) { - switch (type) + return type switch { - case AscentType.CLASSIC: - return Core.GetComputerModule(); - case AscentType.GRAVITYTURN: - return Core.GetComputerModule(); - case AscentType.PVG: - return Core.GetComputerModule(); - default: - return Core.GetComputerModule(); - } + AscentType.CLASSIC => Core.GetComputerModule(), + AscentType.PVG => Core.GetComputerModule(), + _ => Core.GetComputerModule() + }; } private void DisableAscentModules()