From e553d807b0d35ae4cff725b20ac3762cd282882c Mon Sep 17 00:00:00 2001 From: Alexey Kim Date: Tue, 14 Mar 2023 01:20:55 +0200 Subject: [PATCH] Special case batteries in vehicle::fuel_capacity --- src/vehicle.cpp | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/src/vehicle.cpp b/src/vehicle.cpp index 6d5a6791dfcc5..4b51b913f8862 100644 --- a/src/vehicle.cpp +++ b/src/vehicle.cpp @@ -3375,13 +3375,24 @@ int vehicle::engine_fuel_left( const vehicle_part &vp ) const int vehicle::fuel_capacity( const itype_id &ftype ) const { - vehicle_part_range vpr = get_all_parts(); - return std::accumulate( vpr.begin(), vpr.end(), 0, [&ftype]( const int &lhs, - const vpart_reference & rhs ) { - cata::value_ptr a_val = item::find_type( ftype )->ammo; - return lhs + ( rhs.part().ammo_current() == ftype ? - rhs.part().ammo_capacity( !!a_val ? a_val->type : ammotype::NULL_ID() ) : - 0 ); + if( ftype == fuel_type_battery ) { // batteries get special treatment due to power cables + int64_t capacity = 0; + for( const std::pair &pair : search_connected_vehicles() ) { + const vehicle &veh = *pair.first; + for( const int part_idx : veh.batteries ) { + const vehicle_part &vp = veh.parts[part_idx]; + capacity += vp.ammo_capacity( fuel_type_battery->ammo->type ); + } + } + return capacity; + } + const vehicle_part_range vpr = get_all_parts(); + return std::accumulate( vpr.begin(), vpr.end(), int64_t { 0 }, + [&ftype]( const int64_t &lhs, const vpart_reference & rhs ) { + if( rhs.part().ammo_current() == ftype && ftype->ammo ) { + return lhs + rhs.part().ammo_capacity( ftype->ammo->type ); + } + return lhs; } ); }