Skip to content

Commit

Permalink
Possibly breaking cleanup of MechJebCore
Browse files Browse the repository at this point in the history
The renaming here is intended to be breaking, mods will need to
update.  I tried to keep public APIs that looked deliberately
public to be [UsedImplicitly] and visible, if the visibility
changed of something that should be made public that is a bug
and it can be reverted back to public.

Signed-off-by: Lamont Granquist <lamont@scriptkiddie.org>
  • Loading branch information
lamont-granquist committed Jun 19, 2023
1 parent 4652221 commit 1bcbef0
Show file tree
Hide file tree
Showing 59 changed files with 1,071 additions and 1,086 deletions.
2 changes: 1 addition & 1 deletion MechJeb2/AutopilotModule.cs
Expand Up @@ -60,7 +60,7 @@ public class AutopilotStep
protected readonly MechJebCore Core;

//conveniences:
protected VesselState VesselState => Core.vesselState;
protected VesselState VesselState => Core.VesselState;
protected Vessel Vessel => Core.part.vessel;
protected CelestialBody MainBody => Core.part.vessel.mainBody;
protected Orbit Orbit => Core.part.vessel.orbit;
Expand Down
2 changes: 1 addition & 1 deletion MechJeb2/ComputerModule.cs
Expand Up @@ -72,7 +72,7 @@ public ComputerModule(MechJebCore core)
{
this.core = core;
part = core.part;
vesselState = core.vesselState;
vesselState = core.VesselState;
profilerName = GetType().Name;

users = new UserPool(this);
Expand Down
4 changes: 2 additions & 2 deletions MechJeb2/DisplayModule.cs
Expand Up @@ -153,7 +153,7 @@ protected void WindowGUI(int windowID, bool draggable)
// }

bool allowDrag = !locked;
if (!locked && !isOverlay && core.settings.useTitlebarDragging)
if (!locked && !isOverlay && core.Settings.useTitlebarDragging)
{
float x = Mouse.screenPos.x / GuiUtils.scale;
float y = Mouse.screenPos.y / GuiUtils.scale;
Expand Down Expand Up @@ -284,7 +284,7 @@ public override void OnLoad(ConfigNode local, ConfigNode type, ConfigNode global
public virtual bool isActive()
{
if (makesActive == null)
makesActive = new ComputerModule[] { core.attitude, core.thrust, core.rover, core.node, core.rcs, core.rcsbal };
makesActive = new ComputerModule[] { core.Attitude, core.Thrust, core.Rover, core.Node, core.RCS, core.Rcsbal };

bool active = false;
for (int i = 0; i < makesActive.Length; i++)
Expand Down
70 changes: 35 additions & 35 deletions MechJeb2/LandingAutopilot/CoastToDeceleration.cs
Expand Up @@ -15,20 +15,20 @@ public CoastToDeceleration(MechJebCore core) : base(core)

public override AutopilotStep Drive(FlightCtrlState s)
{
if (!Core.landing.PredictionReady)
if (!Core.Landing.PredictionReady)
return this;

Vector3d deltaV = Core.landing.ComputeCourseCorrection(true);
Vector3d deltaV = Core.Landing.ComputeCourseCorrection(true);

if (!Core.landing.RCSAdjustment) return this;
if (!Core.Landing.RCSAdjustment) return this;

if (deltaV.magnitude > 3)
Core.rcs.enabled = true;
Core.RCS.enabled = true;
else if (deltaV.magnitude < 0.01)
Core.rcs.enabled = false;
Core.RCS.enabled = false;

if (Core.rcs.enabled)
Core.rcs.SetWorldVelocityError(deltaV);
if (Core.RCS.enabled)
Core.RCS.SetWorldVelocityError(deltaV);

return this;
}
Expand All @@ -37,93 +37,93 @@ public override AutopilotStep Drive(FlightCtrlState s)

public override AutopilotStep OnFixedUpdate()
{
Core.thrust.targetThrottle = 0;
Core.Thrust.targetThrottle = 0;

// If the atmospheric drag is has started to act on the vessel then we are in a position to start considering when to deploy the parachutes.
if (Core.landing.DeployChutes)
if (Core.Landing.DeployChutes)
{
if (Core.landing.ParachutesDeployable())
if (Core.Landing.ParachutesDeployable())
{
Core.landing.ControlParachutes();
Core.Landing.ControlParachutes();
}
}

double maxAllowedSpeed = Core.landing.MaxAllowedSpeed();
double maxAllowedSpeed = Core.Landing.MaxAllowedSpeed();
if (VesselState.speedSurface > 0.9 * maxAllowedSpeed)
{
Core.warp.MinimumWarp();
if (Core.landing.RCSAdjustment)
Core.rcs.enabled = false;
Core.Warp.MinimumWarp();
if (Core.Landing.RCSAdjustment)
Core.RCS.enabled = false;
return new DecelerationBurn(Core);
}

Status = Localizer.Format("#MechJeb_LandingGuidance_Status1"); //"Coasting toward deceleration burn"

if (Core.landing.LandAtTarget)
if (Core.Landing.LandAtTarget)
{
double currentError = Vector3d.Distance(Core.target.GetPositionTargetPosition(), Core.landing.LandingSite);
double currentError = Vector3d.Distance(Core.Target.GetPositionTargetPosition(), Core.Landing.LandingSite);
if (currentError > 1000)
{
if (!VesselState.parachuteDeployed &&
VesselState.drag <=
0.1) // However if there is already a parachute deployed or drag is high, then do not bother trying to correct the course as we will not have any attitude control anyway.
{
Core.warp.MinimumWarp();
if (Core.landing.RCSAdjustment)
Core.rcs.enabled = false;
Core.Warp.MinimumWarp();
if (Core.Landing.RCSAdjustment)
Core.RCS.enabled = false;
return new CourseCorrection(Core);
}
}
else
{
Vector3d deltaV = Core.landing.ComputeCourseCorrection(true);
Vector3d deltaV = Core.Landing.ComputeCourseCorrection(true);
Status += "\n" + Localizer.Format("#MechJeb_LandingGuidance_Status2",
deltaV.magnitude.ToString("F3")); //"Course correction DV: " + + " m/s"
}
}

// If we're already low, skip directly to the Deceleration burn
if (VesselState.altitudeASL < Core.landing.DecelerationEndAltitude() + 5)
if (VesselState.altitudeASL < Core.Landing.DecelerationEndAltitude() + 5)
{
Core.warp.MinimumWarp();
if (Core.landing.RCSAdjustment)
Core.rcs.enabled = false;
Core.Warp.MinimumWarp();
if (Core.Landing.RCSAdjustment)
Core.RCS.enabled = false;
return new DecelerationBurn(Core);
}

if (Core.attitude.attitudeAngleFromTarget() < 1) { _warpReady = true; } // less warp start warp stop jumping
if (Core.Attitude.attitudeAngleFromTarget() < 1) { _warpReady = true; } // less warp start warp stop jumping

if (Core.attitude.attitudeAngleFromTarget() > 5) { _warpReady = false; } // hopefully
if (Core.Attitude.attitudeAngleFromTarget() > 5) { _warpReady = false; } // hopefully

if (Core.landing.PredictionReady)
if (Core.Landing.PredictionReady)
{
if (VesselState.drag < 0.01)
{
double decelerationStartTime = Core.landing.Prediction.Trajectory.Any()
? Core.landing.Prediction.Trajectory.First().UT
double decelerationStartTime = Core.Landing.Prediction.Trajectory.Any()
? Core.Landing.Prediction.Trajectory.First().UT
: VesselState.time;
Vector3d decelerationStartAttitude = -Orbit.WorldOrbitalVelocityAtUT(decelerationStartTime);
decelerationStartAttitude += MainBody.getRFrmVel(Orbit.WorldPositionAtUT(decelerationStartTime));
decelerationStartAttitude = decelerationStartAttitude.normalized;
Core.attitude.attitudeTo(decelerationStartAttitude, AttitudeReference.INERTIAL, Core.landing);
Core.Attitude.attitudeTo(decelerationStartAttitude, AttitudeReference.INERTIAL, Core.Landing);
}
else
{
Core.attitude.attitudeTo(Vector3.back, AttitudeReference.SURFACE_VELOCITY, Core.landing);
Core.Attitude.attitudeTo(Vector3.back, AttitudeReference.SURFACE_VELOCITY, Core.Landing);
}
}

//Warp at a rate no higher than the rate that would have us impacting the ground 10 seconds from now:
if (_warpReady && Core.node.autowarp)
if (_warpReady && Core.Node.autowarp)
{
// Make sure if we're hovering that we don't go straight into too fast of a warp
// (g * 5 is average velocity falling for 10 seconds from a hover)
double velocityGuess = Math.Max(Math.Abs(VesselState.speedVertical), VesselState.localg * 5);
Core.warp.WarpRegularAtRate((float)(VesselState.altitudeASL / (10 * velocityGuess)));
Core.Warp.WarpRegularAtRate((float)(VesselState.altitudeASL / (10 * velocityGuess)));
}
else
{
Core.warp.MinimumWarp();
Core.Warp.MinimumWarp();
}

return this;
Expand Down
32 changes: 16 additions & 16 deletions MechJeb2/LandingAutopilot/CourseCorrection.cs
Expand Up @@ -14,30 +14,30 @@ public CourseCorrection(MechJebCore core) : base(core)

public override AutopilotStep Drive(FlightCtrlState s)
{
if (!Core.landing.PredictionReady)
if (!Core.Landing.PredictionReady)
return this;

// If the atomospheric drag is at least 100mm/s2 then start trying to target the overshoot using the parachutes
if (Core.landing.DeployChutes)
if (Core.Landing.DeployChutes)
{
if (Core.landing.ParachutesDeployable())
if (Core.Landing.ParachutesDeployable())
{
Core.landing.ControlParachutes();
Core.Landing.ControlParachutes();
}
}

double currentError = Vector3d.Distance(Core.target.GetPositionTargetPosition(), Core.landing.LandingSite);
double currentError = Vector3d.Distance(Core.Target.GetPositionTargetPosition(), Core.Landing.LandingSite);

if (currentError < 150)
{
Core.thrust.targetThrottle = 0;
if (Core.landing.RCSAdjustment)
Core.rcs.enabled = true;
Core.Thrust.targetThrottle = 0;
if (Core.Landing.RCSAdjustment)
Core.RCS.enabled = true;
return new CoastToDeceleration(Core);
}

// If we're off course, but already too low, skip the course correction
if (VesselState.altitudeASL < Core.landing.DecelerationEndAltitude() + 5)
if (VesselState.altitudeASL < Core.Landing.DecelerationEndAltitude() + 5)
{
return new DecelerationBurn(Core);
}
Expand All @@ -46,7 +46,7 @@ public override AutopilotStep Drive(FlightCtrlState s)
// If a parachute has already been deployed then we will not be able to control attitude anyway, so move back to the coast to deceleration step.
if (VesselState.parachuteDeployed)
{
Core.thrust.targetThrottle = 0;
Core.Thrust.targetThrottle = 0;
return new CoastToDeceleration(Core);
}

Expand All @@ -56,26 +56,26 @@ public override AutopilotStep Drive(FlightCtrlState s)
return new CoastToDeceleration(Core);
}

Vector3d deltaV = Core.landing.ComputeCourseCorrection(true);
Vector3d deltaV = Core.Landing.ComputeCourseCorrection(true);

Status = Localizer.Format("#MechJeb_LandingGuidance_Status3",
deltaV.magnitude.ToString("F1")); //"Performing course correction of about " + + " m/s"

Core.attitude.attitudeTo(deltaV.normalized, AttitudeReference.INERTIAL, Core.landing);
Core.Attitude.attitudeTo(deltaV.normalized, AttitudeReference.INERTIAL, Core.Landing);

if (Core.attitude.attitudeAngleFromTarget() < 2)
if (Core.Attitude.attitudeAngleFromTarget() < 2)
_courseCorrectionBurning = true;
else if (Core.attitude.attitudeAngleFromTarget() > 30)
else if (Core.Attitude.attitudeAngleFromTarget() > 30)
_courseCorrectionBurning = false;

if (_courseCorrectionBurning)
{
const double TIME_CONSTANT = 2.0;
Core.thrust.ThrustForDV(deltaV.magnitude, TIME_CONSTANT);
Core.Thrust.ThrustForDV(deltaV.magnitude, TIME_CONSTANT);
}
else
{
Core.thrust.targetThrottle = 0;
Core.Thrust.targetThrottle = 0;
}

return this;
Expand Down
34 changes: 17 additions & 17 deletions MechJeb2/LandingAutopilot/DecelerationBurn.cs
Expand Up @@ -15,71 +15,71 @@ public DecelerationBurn(MechJebCore core) : base(core)

public override AutopilotStep OnFixedUpdate()
{
if (VesselState.altitudeASL < Core.landing.DecelerationEndAltitude() + 5)
if (VesselState.altitudeASL < Core.Landing.DecelerationEndAltitude() + 5)
{
Core.warp.MinimumWarp();
Core.Warp.MinimumWarp();

if (Core.landing.UseAtmosphereToBrake())
if (Core.Landing.UseAtmosphereToBrake())
return new FinalDescent(Core);
return new KillHorizontalVelocity(Core);
}

double decelerationStartTime =
Core.landing.Prediction.Trajectory.Any() ? Core.landing.Prediction.Trajectory.First().UT : VesselState.time;
Core.Landing.Prediction.Trajectory.Any() ? Core.Landing.Prediction.Trajectory.First().UT : VesselState.time;
if (decelerationStartTime - VesselState.time > 5)
{
Core.thrust.targetThrottle = 0;
Core.Thrust.targetThrottle = 0;

Status = Localizer.Format("#MechJeb_LandingGuidance_Status4"); //"Warping to start of braking burn."

//warp to deceleration start
Vector3d decelerationStartAttitude = -Orbit.WorldOrbitalVelocityAtUT(decelerationStartTime);
decelerationStartAttitude += MainBody.getRFrmVel(Orbit.WorldPositionAtUT(decelerationStartTime));
decelerationStartAttitude = decelerationStartAttitude.normalized;
Core.attitude.attitudeTo(decelerationStartAttitude, AttitudeReference.INERTIAL, Core.landing);
bool warpReady = Core.attitude.attitudeAngleFromTarget() < 5;
Core.Attitude.attitudeTo(decelerationStartAttitude, AttitudeReference.INERTIAL, Core.Landing);
bool warpReady = Core.Attitude.attitudeAngleFromTarget() < 5;

if (warpReady && Core.node.autowarp)
Core.warp.WarpToUT(decelerationStartTime - 5);
if (warpReady && Core.Node.autowarp)
Core.Warp.WarpToUT(decelerationStartTime - 5);
else if (!MuUtils.PhysicsRunning())
Core.warp.MinimumWarp();
Core.Warp.MinimumWarp();
return this;
}

Vector3d desiredThrustVector = -VesselState.surfaceVelocity.normalized;

Vector3d courseCorrection = Core.landing.ComputeCourseCorrection(false);
Vector3d courseCorrection = Core.Landing.ComputeCourseCorrection(false);
double correctionAngle = courseCorrection.magnitude / (2.0 * VesselState.limitedMaxThrustAccel);
correctionAngle = Math.Min(0.1, correctionAngle);
desiredThrustVector = (desiredThrustVector + correctionAngle * courseCorrection.normalized).normalized;

if (Vector3d.Dot(VesselState.surfaceVelocity, VesselState.up) > 0
|| Vector3d.Dot(VesselState.forward, desiredThrustVector) < 0.75)
{
Core.thrust.targetThrottle = 0;
Core.Thrust.targetThrottle = 0;
Status = Localizer.Format("#MechJeb_LandingGuidance_Status5"); //"Braking"
}
else
{
double controlledSpeed =
VesselState.speedSurface *
Math.Sign(Vector3d.Dot(VesselState.surfaceVelocity, VesselState.up)); //positive if we are ascending, negative if descending
double desiredSpeed = -Core.landing.MaxAllowedSpeed();
double desiredSpeedAfterDt = -Core.landing.MaxAllowedSpeedAfterDt(VesselState.deltaT);
double desiredSpeed = -Core.Landing.MaxAllowedSpeed();
double desiredSpeedAfterDt = -Core.Landing.MaxAllowedSpeedAfterDt(VesselState.deltaT);
double minAccel = -VesselState.localg * Math.Abs(Vector3d.Dot(VesselState.surfaceVelocity.normalized, VesselState.up));
double maxAccel = VesselState.maxThrustAccel * Vector3d.Dot(VesselState.forward, -VesselState.surfaceVelocity.normalized) -
VesselState.localg * Math.Abs(Vector3d.Dot(VesselState.surfaceVelocity.normalized, VesselState.up));
const double SPEED_CORRECTION_TIME_CONSTANT = 0.3;
double speedError = desiredSpeed - controlledSpeed;
double desiredAccel = speedError / SPEED_CORRECTION_TIME_CONSTANT + (desiredSpeedAfterDt - desiredSpeed) / VesselState.deltaT;
if (maxAccel - minAccel > 0)
Core.thrust.targetThrottle = Mathf.Clamp((float)((desiredAccel - minAccel) / (maxAccel - minAccel)), 0.0F, 1.0F);
else Core.thrust.targetThrottle = 0;
Core.Thrust.targetThrottle = Mathf.Clamp((float)((desiredAccel - minAccel) / (maxAccel - minAccel)), 0.0F, 1.0F);
else Core.Thrust.targetThrottle = 0;
Status = Localizer.Format("#MechJeb_LandingGuidance_Status6",
desiredSpeed >= double.MaxValue ? "" : Math.Abs(desiredSpeed).ToString("F1")); //"Braking: target speed = " + + " m/s"
}

Core.attitude.attitudeTo(desiredThrustVector, AttitudeReference.INERTIAL, Core.landing);
Core.Attitude.attitudeTo(desiredThrustVector, AttitudeReference.INERTIAL, Core.Landing);

return this;
}
Expand Down

0 comments on commit 1bcbef0

Please sign in to comment.