diff --git a/source/AI.cpp b/source/AI.cpp index 9aad7d5bbc0b..74de5a2ea2b5 100644 --- a/source/AI.cpp +++ b/source/AI.cpp @@ -2204,7 +2204,14 @@ bool AI::MoveTo(Ship &ship, Command &command, const Point &targetPosition, bool isFacing = (dp.Unit().Dot(angle.Unit()) > .95); if(!isClose || (!isFacing && !shouldReverse)) command.SetTurn(TurnToward(ship, dp)); - if(isFacing) + // Drag is not applied when not thrusting, so stop thrusting when close to max speed + // to save energy. Work with a slightly lower maximum velocity to avoid border cases. + // In order for a ship to use their afterburner, they must also have the forward + // command active. Therefore, if this ship should use its afterburner, use the + // max velocity with afterburner thrust included. + double maxVelocity = ship.MaxVelocity(ShouldUseAfterburner(ship)) * .99; + if(isFacing && (velocity.LengthSquared() <= maxVelocity * maxVelocity + || dp.Unit().Dot(velocity.Unit()) < .95)) command |= Command::FORWARD; else if(shouldReverse) { diff --git a/source/Ship.cpp b/source/Ship.cpp index 56220ee0eee0..fcab78dda27e 100644 --- a/source/Ship.cpp +++ b/source/Ship.cpp @@ -2972,13 +2972,14 @@ double Ship::Acceleration() const -double Ship::MaxVelocity() const +double Ship::MaxVelocity(bool withAfterburner) const { // v * drag / mass == thrust / mass // v * drag == thrust // v = thrust / drag double thrust = attributes.Get("thrust"); - return (thrust ? thrust : attributes.Get("afterburner thrust")) / Drag(); + double afterburnerThrust = attributes.Get("afterburner thrust"); + return (thrust ? thrust + afterburnerThrust * withAfterburner: afterburnerThrust) / Drag(); } diff --git a/source/Ship.h b/source/Ship.h index d706748b2371..1ef099798de2 100644 --- a/source/Ship.h +++ b/source/Ship.h @@ -384,7 +384,7 @@ class Ship : public Body, public std::enable_shared_from_this { double InertialMass() const; double TurnRate() const; double Acceleration() const; - double MaxVelocity() const; + double MaxVelocity(bool withAfterburner = false) const; double ReverseAcceleration() const; double MaxReverseVelocity() const;