Skip to content

Commit

Permalink
Fix uneven distribution of battery drain from motors (cataclysmbnteam…
Browse files Browse the repository at this point in the history
…#1601)

Earlier, when riding a car with electric engines and multiple batteries, batteries discharged one after one.
However, they should discharge by similar percent (this is how connected electric grids work).
Made fuel consumption reuse existing code for utility discharges.

Tested by playing locally and looking at battery changes.
  • Loading branch information
AngelicosPhosphoros authored Jun 10, 2022
1 parent 8d77cdd commit fe7444e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
14 changes: 13 additions & 1 deletion src/vehicle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4382,13 +4382,25 @@ std::map<itype_id, int> vehicle::fuel_usage() const

double vehicle::drain_energy( const itype_id &ftype, double energy_j )
{
// Consumption of battery power is done differently.
// From all batteries at once and doesn't change mass.
if( ftype == fuel_type_battery ) {
// Batteries stored in kilojoules
const int total_kj_to_drain = static_cast<int>( energy_j / 1000.0 );
if( total_kj_to_drain <= 0 ) {
return 0.0;
}
const int not_fulfilled = discharge_battery( total_kj_to_drain );
return static_cast<double>( total_kj_to_drain - not_fulfilled ) * 1000.0;
}

double drained = 0.0f;
for( auto &p : parts ) {
if( energy_j <= 0.0f ) {
break;
}

double consumed = p.consume_energy( ftype, energy_j );
const double consumed = p.consume_energy( ftype, energy_j );
drained += consumed;
energy_j -= consumed;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/vehicle_efficiency_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ TEST_CASE( "vehicle_efficiency", "[vehicle] [engine]" )
test_vehicle( "beetle", 815669, 431300, 338700, 95610, 68060 );
test_vehicle( "car", 1120618, 617500, 386100, 52730, 25170 );
test_vehicle( "car_sports", 1154214, 352600, 267600, 36790, 22350 );
test_vehicle( "electric_car", 1126087, 132700, 72290, 8160, 3390 );
test_vehicle( "electric_car", 1126087, 132700, 72290, 8240, 3390 );
test_vehicle( "suv", 1320286, 1163000, 630000, 85540, 30810 );
test_vehicle( "motorcycle", 163085, 120300, 99920, 63320, 50810 );
test_vehicle( "quad_bike", 265345, 116100, 116100, 46770, 46770 );
Expand Down

0 comments on commit fe7444e

Please sign in to comment.