Skip to content

Commit

Permalink
Make electric motors go in reverse better
Browse files Browse the repository at this point in the history
Due to the downscaling of reverse power/speed for vehicle engines, it
can happen that a vehicle is so heavy, it can move forward but not
backward.

Because battery-powered electric motors can turn in either direction
with equal speed and torque, vehicles with electric motors should be
exempt from the power-reduction for reverse speeds.

This commit adds a new `vehicle::max_reverse_velocity` function as the
counterpart to `vehicle::max_velocity`, where an exception is made for
battery-powered motors. Here I suppose is where the maximum reverse
speed multiplier for horses, bicycles, and jet turbines would eventually
be made; for now it just checks for battery-powered motors.

Note, how *much* the motor is contributing to overall torque is not
calculated; the simple presence of any electric motor in the drivetrain
(no matter how small) ought to negate the reverse-power penalty.

Fixed #38165
  • Loading branch information
wapcaplet committed Feb 21, 2020
1 parent 142295b commit ddd9370
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
12 changes: 12 additions & 0 deletions src/vehicle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3691,6 +3691,18 @@ int vehicle::max_velocity( const bool fueled ) const
return is_watercraft() ? max_water_velocity( fueled ) : max_ground_velocity( fueled );
}

int vehicle::max_reverse_velocity( const bool fueled ) const
{
int max_vel = max_velocity( fueled );
if( has_engine_type( fuel_type_battery, true ) ) {
// Electric motors can go in reverse as well as forward
return -max_vel;
} else {
// All other motive powers do poorly in reverse
return -max_vel / 4;
}
}

// the same physics as max_ground_velocity, but with a smaller engine power
int vehicle::safe_ground_velocity( const bool fueled ) const
{
Expand Down
2 changes: 2 additions & 0 deletions src/vehicle.h
Original file line number Diff line number Diff line change
Expand Up @@ -1191,6 +1191,8 @@ class vehicle
int max_water_velocity( bool fueled = true ) const;
// Get maximum velocity for the current movement mode
int max_velocity( bool fueled = true ) const;
// Get maximum reverse velocity for the current movement mode
int max_reverse_velocity( bool fueled = true ) const;

// Get safe ground velocity gained by combined power of all engines.
// If fueled == true, then only the engines which the vehicle has fuel for are included
Expand Down
8 changes: 4 additions & 4 deletions src/vehicle_move.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ void vehicle::thrust( int thd )

//pos or neg if accelerator or brake
int vel_inc = ( ( thrusting ) ? accel : brk ) * thd;
if( thd == -1 && thrusting ) {
//accelerate 60% if going backward
// Reverse is only 60% acceleration, unless an electric motor is in use
if( thd == -1 && thrusting && !has_engine_type( fuel_type_battery, true ) ) {
vel_inc = .6 * vel_inc;
}

Expand Down Expand Up @@ -213,7 +213,7 @@ void vehicle::thrust( int thd )
stop();
} else {
// Increase velocity up to max_vel or min_vel, but not above.
const int min_vel = -max_vel / 4;
const int min_vel = max_reverse_velocity();
if( vel_inc > 0 ) {
// Don't allow braking by accelerating (could happen with damaged engines)
velocity = std::max( velocity, std::min( velocity + vel_inc, max_vel ) );
Expand Down Expand Up @@ -245,7 +245,7 @@ void vehicle::cruise_thrust( int amount )
}
int safe_vel = safe_velocity();
int max_vel = max_velocity();
int max_rev_vel = -max_vel / 4;
int max_rev_vel = max_reverse_velocity();

//if the safe velocity is between the cruise velocity and its next value, set to safe velocity
if( ( cruise_velocity < safe_vel && safe_vel < ( cruise_velocity + amount ) ) ||
Expand Down

0 comments on commit ddd9370

Please sign in to comment.