Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add units::power (watts) #62155

Merged
merged 2 commits into from
Nov 24, 2022
Merged

Add units::power (watts) #62155

merged 2 commits into from
Nov 24, 2022

Conversation

irwiss
Copy link
Contributor

@irwiss irwiss commented Nov 9, 2022

Summary

None

Purpose of change

Implements the power units from #32067 / #31079

Support power units for changes like #61950

Describe the solution

Implement units::power and the following operators:

Left hand Op Right hand Result
units::power * time_duration units::energy
time_duration * units::power units::energy
units::energy / time_duration units::power
units::energy / units::power time_duration

Describe alternatives you've considered

Testing

The patch adds some unit tests in units_test.cpp

Optionally apply the following patch to make a slight test on vehicle_part::epower and some minor deserialization test
  diff --git a/src/veh_interact.cpp b/src/veh_interact.cpp
  index 7e7e1699bf..2df018046c 100644
  --- a/src/veh_interact.cpp
  +++ b/src/veh_interact.cpp
  @@ -3018,11 +3018,11 @@ void veh_interact::display_details( const vpart_info *part )
                           whl ? whl->width : 0 );
       }
   
  -    if( part->epower != 0 ) {
  +    if( part->epower != 0_W ) {
           fold_and_print( w_details, point( col_2, line + 3 ), column_width, c_white,
                           "%s: <color_light_gray>%+4d</color>",
                           small_mode ? _( "Epwr" ) : _( "Electric Power" ),
  -                        part->epower );
  +                        units::to_watt<int64_t>( part->epower ) );
       }
   
       // line 4 [horizontal]: fuel_type (if applicable)
  diff --git a/src/veh_type.cpp b/src/veh_type.cpp
  index aefaaa9c88..2a0be18804 100644
  --- a/src/veh_type.cpp
  +++ b/src/veh_type.cpp
  @@ -947,7 +947,7 @@ void vpart_info::check()
                   debugmsg( "vehicle part %s has undefined tool quality %s", part.id.c_str(), q.first.c_str() );
               }
           }
  -        if( part.has_flag( VPFLAG_ENABLED_DRAINS_EPOWER ) && part.epower == 0 ) {
  +        if( part.has_flag( VPFLAG_ENABLED_DRAINS_EPOWER ) && part.epower == 0_W ) {
               debugmsg( "%s is set to drain epower, but has epower == 0", part.id.c_str() );
           }
           // Parts with non-zero epower must have a flag that affects epower usage
  @@ -957,7 +957,7 @@ void vpart_info::check()
                   "REACTOR", "WIND_TURBINE", "WATER_WHEEL"
               }
           };
  -        if( part.epower != 0 &&
  +        if( part.epower != 0_W &&
           std::none_of( handled.begin(), handled.end(), [&part]( const std::string & flag ) {
           return part.has_flag( flag );
           } ) ) {
  @@ -1020,14 +1020,15 @@ int vpart_info::format_description( std::string &msg, const nc_color &format_col
               continue;
           } else if( flagid == "ENABLED_DRAINS_EPOWER" ||
                      flagid == "ENGINE" ) { // ENGINEs get the same description
  -            if( epower < 0 ) {
  -                append_desc( string_format( json_flag::get( "ENABLED_DRAINS_EPOWER" ).info(), -epower ) );
  +            if( epower < 0_W ) {
  +                append_desc( string_format( json_flag::get( "ENABLED_DRAINS_EPOWER" ).info(),
  +                                            -units::to_watt<int64_t>( epower ) ) );
               }
           } else if( flagid == "ALTERNATOR" ||
                      flagid == "SOLAR_PANEL" ||
                      flagid == "WATER_WHEEL" ||
                      flagid == "WIND_TURBINE" ) {
  -            append_desc( string_format( json_flag::get( flagid ).info(), epower ) );
  +            append_desc( string_format( json_flag::get( flagid ).info(), units::to_watt<int64_t>( epower ) ) );
           } else {
               append_desc( json_flag::get( flagid ).info() );
           }
  diff --git a/src/veh_type.h b/src/veh_type.h
  index fcbb317813..f1e809fabd 100644
  --- a/src/veh_type.h
  +++ b/src/veh_type.h
  @@ -451,7 +451,7 @@ class vpart_info
            * Electrical power, flat rate (watts); positive for generation, negative for consumption
            * For motor consumption scaled with powertrain demand see @ref energy_consumption instead
            */
  -        int epower = 0;
  +        units::power epower = 0_W;
   
           /**
            * Energy consumed per second by engines and motors when delivering max @ref power
  diff --git a/src/vehicle.cpp b/src/vehicle.cpp
  index e9c6c952ae..16e9b9a6ce 100644
  --- a/src/vehicle.cpp
  +++ b/src/vehicle.cpp
  @@ -1105,7 +1105,7 @@ int vehicle::part_vpower_w( const int index, const bool at_full_hp ) const
   // for motor consumption see @ref vpart_info::energy_consumption instead
   int vehicle::part_epower_w( const int index ) const
   {
  -    int e = part_info( index ).epower;
  +    int e = units::to_watt<int>( part_info( index ).epower );
       if( e < 0 ) {
           return e; // Consumers always draw full power, even if broken
       }
  @@ -4723,7 +4723,7 @@ int vehicle::total_accessory_epower_w() const
       for( int part : accessories ) {
           const vehicle_part &vp = parts[part];
           if( vp.enabled ) {
  -            epower += vp.info().epower;
  +            epower += units::to_watt<int64_t>( vp.info().epower );
           }
       }
       return epower;
  @@ -5082,7 +5082,7 @@ void vehicle::power_parts()
   
           for( const vpart_reference &vp : get_enabled_parts( VPFLAG_ENABLED_DRAINS_EPOWER ) ) {
               vehicle_part &pt = vp.part();
  -            if( pt.info().epower < 0 ) {
  +            if( pt.info().epower < 0_W ) {
                   pt.enabled = false;
               }
           }
  @@ -5193,7 +5193,7 @@ int vehicle::traverse_vehicle_graph( Vehicle *start_veh, int amount, Func action
   
               // Add this connected vehicle to the queue of vehicles to search next,
               // but only if we haven't seen this one before (checked above)
  -            int target_loss = current_loss + current_veh->part_info( p ).epower;
  +            int target_loss = current_loss + units::to_watt( current_veh->part_info( p ).epower );
               connected_vehs.push_back( std::make_pair( target_veh, target_loss ) );
               // current_veh could be invalid after this point
   
  @@ -7415,7 +7415,7 @@ void vehicle::update_time( const time_point &update_to )
                   here.emit_field( exhaust_dest( exhaust_part ), e );
               }
           }
  -        discharge_battery( pt.info().epower );
  +        discharge_battery( units::to_watt( pt.info().epower ) );
       }
   
       if( sm_pos.z < 0 ) {
  diff --git a/src/vehicle_part.cpp b/src/vehicle_part.cpp
  index efa5969057..79b70e1c34 100644
  --- a/src/vehicle_part.cpp
  +++ b/src/vehicle_part.cpp
  @@ -689,7 +689,7 @@ bool vehicle::can_enable( const vehicle_part &pt, bool alert ) const
   
       // TODO: check fuel for combustion engines
   
  -    if( pt.info().epower < 0 && fuel_left( fuel_type_battery, true ) <= 0 ) {
  +    if( pt.info().epower < 0_W && fuel_left( fuel_type_battery, true ) <= 0 ) {
           if( alert ) {
               add_msg( m_bad, _( "Insufficient power to enable %s" ), pt.name() );
           }
  -- 
  2.36.1.windows.1
  
  

Additional context

Tagging @Hirmuolio in case you're interested in reviewing

@github-actions github-actions bot added [C++] Changes (can be) made in C++. Previously named `Code` Code: Tests Measurement, self-control, statistics, balancing. json-styled JSON lint passed, label assigned by github actions astyled astyled PR, label is assigned by github actions BasicBuildPassed This PR builds correctly, label assigned by github actions labels Nov 9, 2022
@irwiss irwiss changed the title Add power units (watts) Add units::power (watts) Nov 10, 2022
@irwiss irwiss marked this pull request as ready for review November 10, 2022 01:16
@Hirmuolio Hirmuolio mentioned this pull request Nov 24, 2022
12 tasks
@I-am-Erk I-am-Erk merged commit 43dbd7a into CleverRaven:master Nov 24, 2022
@irwiss irwiss deleted the watt-units branch November 24, 2022 17:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
astyled astyled PR, label is assigned by github actions BasicBuildPassed This PR builds correctly, label assigned by github actions [C++] Changes (can be) made in C++. Previously named `Code` Code: Tests Measurement, self-control, statistics, balancing. json-styled JSON lint passed, label assigned by github actions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants