Skip to content

Commit

Permalink
Merge pull request #77726 from ThePirate42/view_battery_chargers_cons…
Browse files Browse the repository at this point in the history
…umption

Show the power draw of battery chargers
  • Loading branch information
Maleclypse authored Nov 13, 2024
2 parents 4195527 + 2a80ffc commit c7b8e84
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 5 deletions.
26 changes: 24 additions & 2 deletions src/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5788,13 +5788,22 @@ static void process_vehicle_items( vehicle &cur_veh, int part )
int turns_elapsed = to_turns<int>( calendar::turn - recharge_part.last_charged );
recharge_part.last_charged = calendar::turn;

if( !recharge_part.removed && recharge_part.enabled && ( turns_elapsed > 0 ) ) {
if( !recharge_part.removed && recharge_part.enabled && ( turns_elapsed > 0 ) &&
cur_veh.is_battery_available() ) {

int dischargeable = turns_elapsed * recharge_part.info().bonus;
// Convert to kilojoule
dischargeable = ( dischargeable / 1000 ) + x_in_y( dischargeable % 1000, 1000 );

bool is_running = false;

for( item &n : cur_veh.get_items( vp ) ) {

if( dischargeable <= 0 ) {
// "dischargeable" could be 0 even if the battery charger is actually running,
// because of the rounding when the power is below a kilojoule. Before breaking,
// I need to know if there is at least a non-full battery, for the purpose of
// displaying the correct power draw.
if( is_running && dischargeable <= 0 ) {
break;
}

Expand All @@ -5811,6 +5820,13 @@ static void process_vehicle_items( vehicle &cur_veh, int part )
chargeable = n.ammo_capacity( ammo_battery ) - n.ammo_remaining();
}

if( !is_running && chargeable > 0 ) {
is_running = true;
if( dischargeable <= 0 ) {
break;
}
}

if( chargeable > 0 ) {

// Around 85% efficient; a few of the discharges don't actually recharge
Expand All @@ -5836,6 +5852,12 @@ static void process_vehicle_items( vehicle &cur_veh, int part )
}

}

if( is_running ) {
cur_veh.recharge_epower_this_turn -= units::from_watt( static_cast<std::int64_t>
( recharge_part.info().bonus ) );
}

}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/veh_appliance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,8 @@ void veh_app_interact::draw_info()
}

// Other power output
if( !veh->accessories.empty() ) {
units::power rate = veh->total_accessory_epower();
if( !veh->accessories.empty() || veh->has_part( "RECHARGE" ) ) {
units::power rate = veh->total_accessory_epower() + veh->recharge_epower_this_turn;
print_charge( _( "Appliance power consumption: " ), rate, row );
row++;
}
Expand Down
18 changes: 17 additions & 1 deletion src/vehicle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5415,7 +5415,8 @@ units::power vehicle::net_battery_charge_rate( bool include_reactors ) const
{
return total_engine_epower() + total_alternator_epower() + total_accessory_epower() +
total_solar_epower() + total_wind_epower() + total_water_wheel_epower() +
linked_item_epower_this_turn + ( include_reactors ? active_reactor_epower() : 0_W );
linked_item_epower_this_turn + recharge_epower_this_turn + ( include_reactors ?
active_reactor_epower() : 0_W );
}

units::power vehicle::active_reactor_epower() const
Expand Down Expand Up @@ -5793,6 +5794,20 @@ static void distribute_charge_evenly( const std::map<vpart_reference, float> &ba
}
}

bool vehicle::is_battery_available() const
{
for( const std::pair<const vehicle *const, float> &pair : search_connected_vehicles() ) {
const vehicle &veh = *pair.first;
for( const int part_idx : veh.batteries ) {
const vehicle_part &vp = veh.parts[part_idx];
if( vp.ammo_remaining() > 0 ) {
return true;
}
}
}
return false;
}

int64_t vehicle::battery_left( bool apply_loss ) const
{
int64_t ret = 0;
Expand Down Expand Up @@ -5953,6 +5968,7 @@ void vehicle::idle( bool on_map )
}

linked_item_epower_this_turn = 0_W;
recharge_epower_this_turn = 0_W;
smart_controller_handle_turn();

if( !on_map ) {
Expand Down
7 changes: 7 additions & 0 deletions src/vehicle.h
Original file line number Diff line number Diff line change
Expand Up @@ -1537,6 +1537,8 @@ class vehicle
units::power total_accessory_epower() const;
// Total power draw from all cable-connected devices. Is cleared every turn during idle().
units::power linked_item_epower_this_turn; // NOLINT(cata-serialize)
// Total power draw from all battery chargers. Is cleared every turn during idle().
units::power recharge_epower_this_turn; // NOLINT(cata-serialize)
// Net power draw or drain on batteries.
units::power net_battery_charge_rate( bool include_reactors ) const;
// Maximum available power available from all reactors. Power from
Expand All @@ -1554,6 +1556,11 @@ class vehicle
// Current and total battery power (kJ) level of all connected vehicles as a pair
std::pair<int, int> connected_battery_power_level() const;

/**
* @return true if at least one of the connected batteries has any charge left
*/
bool is_battery_available() const;

/**
* @param apply_loss if true apply wire loss when charge crosses vehicle power cables
* @return battery charge in kJ from all connected batteries
Expand Down

0 comments on commit c7b8e84

Please sign in to comment.