From 4c1b2908c9572c06bc0e4476149e1b84dace1534 Mon Sep 17 00:00:00 2001 From: Hirmuolio <22011552+Hirmuolio@users.noreply.github.com> Date: Thu, 17 Feb 2022 07:56:03 +0200 Subject: [PATCH] Display current firemode on sidebar (#55377) --- src/character.cpp | 40 +++++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/src/character.cpp b/src/character.cpp index b3fbff4f3a881..3354c928f5b75 100644 --- a/src/character.cpp +++ b/src/character.cpp @@ -7161,24 +7161,46 @@ bool Character::unwield() std::string Character::weapname() const { if( weapon.is_gun() ) { + gun_mode current_mode = weapon.gun_current_mode(); std::string gunmode; + std::string gun_name = current_mode->tname(); // only required for empty mags and empty guns std::string mag_ammo; - if( weapon.gun_all_modes().size() > 1 ) { - gunmode = weapon.gun_current_mode().tname(); + if( current_mode->gun_all_modes().size() > 1 ) { + gunmode = current_mode.tname() + " "; } - if( weapon.ammo_remaining() == 0 ) { - if( weapon.magazine_current() != nullptr ) { - const item *mag = weapon.magazine_current(); - mag_ammo = string_format( " (0/%d)", - mag->ammo_capacity( item( mag->ammo_default() ).ammo_type() ) ); - } else if( weapon.is_reloadable() ) { + if( current_mode->uses_magazine() || current_mode->magazine_integral() ) { + if( current_mode->uses_magazine() && !current_mode->magazine_current() ) { mag_ammo = _( " (empty)" ); + } else { + int cur_ammo = current_mode->ammo_remaining(); + int max_ammo; + if( cur_ammo == 0 ) { + max_ammo = current_mode->ammo_capacity( item( current_mode->ammo_default() ).ammo_type() ); + } else { + max_ammo = current_mode->ammo_capacity( current_mode->loaded_ammo().ammo_type() ); + } + + const double ratio = static_cast( cur_ammo ) / static_cast( max_ammo ); + nc_color charges_color; + if( cur_ammo == 0 ) { + charges_color = c_light_red; + } else if( cur_ammo == max_ammo ) { + charges_color = c_white; + } else if( ratio < 1.0 / 3.0 ) { + charges_color = c_red; + } else if( ratio < 2.0 / 3.0 ) { + charges_color = c_yellow; + } else { + charges_color = c_light_green; + } + mag_ammo = string_format( " (%s)", colorize( string_format( "%i/%i", cur_ammo, max_ammo ), + charges_color ) ); } } - return string_format( "%s%s%s", gunmode, weapon.display_name(), mag_ammo ); + return string_format( "%s%s%s", gunmode, gun_name, mag_ammo ); } else if( !is_armed() ) { return _( "fists" );