Skip to content

Commit

Permalink
feat(ai): Auto-pilot now stops thrusting upon reaching 99% of max spe…
Browse files Browse the repository at this point in the history
…ed to save energy (endless-sky#8489)
  • Loading branch information
samrocketman authored May 18, 2024
1 parent 025d06b commit 6ebacb8
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
9 changes: 8 additions & 1 deletion source/AI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
5 changes: 3 additions & 2 deletions source/Ship.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}


Expand Down
2 changes: 1 addition & 1 deletion source/Ship.h
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ class Ship : public Body, public std::enable_shared_from_this<Ship> {
double InertialMass() const;
double TurnRate() const;
double Acceleration() const;
double MaxVelocity() const;
double MaxVelocity(bool withAfterburner = false) const;
double ReverseAcceleration() const;
double MaxReverseVelocity() const;

Expand Down

0 comments on commit 6ebacb8

Please sign in to comment.