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

Code optimizations reported by static code analysis (2020-05-29) #40961

Merged
merged 5 commits into from
Jun 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ std::vector<char> keys_bound_to( action_id act, const bool restrict_to_printable
action_id action_from_key( char ch )
{
input_context ctxt = get_default_mode_input_context();
const input_event event( ch, CATA_INPUT_KEYBOARD );
const input_event event( ch, input_event_t::keyboard );
const std::string &action = ctxt.input_to_action( event );
return look_up_action( action );
}
Expand Down
24 changes: 14 additions & 10 deletions src/activity_handlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@
#include "vpart_position.h"
#include "weather.h"

enum class creature_size : int;

static const efftype_id effect_sheared( "sheared" );

#define dbg(x) DebugLog((x),D_GAME) << __FILE__ << ":" << __LINE__ << ": "
Expand Down Expand Up @@ -615,7 +617,7 @@ static void set_up_butchery( player_activity &act, player &u, butcher_type actio
u.has_amount( itype_vine_30, 1 ) ||
u.has_amount( itype_grapnel, 1 ) ||
u.has_amount( itype_chain, 1 );
const bool big_corpse = corpse.size >= MS_MEDIUM;
const bool big_corpse = corpse.size >= creature_size::medium;

if( big_corpse ) {
if( has_rope && !has_tree_nearby && !b_rack_present ) {
Expand Down Expand Up @@ -667,7 +669,7 @@ static void set_up_butchery( player_activity &act, player &u, butcher_type actio
}

if( action == butcher_type::QUARTER ) {
if( corpse.size == MS_TINY ) {
if( corpse.size == creature_size::tiny ) {
u.add_msg_if_player( m_bad, _( "This corpse is too small to quarter without damaging." ),
corpse.nname() );
act.targets.pop_back();
Expand Down Expand Up @@ -734,19 +736,19 @@ int butcher_time_to_cut( const player &u, const item &corpse_item, const butcher
int time_to_cut = 0;
switch( corpse.size ) {
// Time (roughly) in turns to cut up the corpse
case MS_TINY:
case creature_size::tiny:
time_to_cut = 150;
break;
case MS_SMALL:
case creature_size::small:
time_to_cut = 300;
break;
case MS_MEDIUM:
case creature_size::medium:
time_to_cut = 450;
break;
case MS_LARGE:
case creature_size::large:
time_to_cut = 600;
break;
case MS_HUGE:
case creature_size::huge:
time_to_cut = 1800;
break;
}
Expand Down Expand Up @@ -972,7 +974,7 @@ static void butchery_drops_harvest( item *corpse_item, const mtype &mt, player &
roll = roll / 4;
} else if( entry.type == "bone" ) {
roll /= 2;
} else if( corpse_item->get_mtype()->size >= MS_MEDIUM && ( entry.type == "skin" ) ) {
} else if( corpse_item->get_mtype()->size >= creature_size::medium && ( entry.type == "skin" ) ) {
roll /= 2;
} else if( entry.type == "offal" ) {
roll /= 5;
Expand Down Expand Up @@ -1161,9 +1163,11 @@ static void butchery_drops_harvest( item *corpse_item, const mtype &mt, player &
}

if( action == butcher_type::DISSECT ) {
p.practice( skill_firstaid, std::max( 0, practice ), std::max( mt.size - MS_MEDIUM, 0 ) + 4 );
p.practice( skill_firstaid, std::max( 0, practice ), std::max( mt.size - creature_size::medium,
0 ) + 4 );
} else {
p.practice( skill_survival, std::max( 0, practice ), std::max( mt.size - MS_MEDIUM, 0 ) + 4 );
p.practice( skill_survival, std::max( 0, practice ), std::max( mt.size - creature_size::medium,
0 ) + 4 );
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/activity_item_handling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1262,7 +1262,7 @@ static activity_reason_info can_do_activity_there( const activity_id &act, playe
// make sure nobody else is working on that corpse right now
if( i.is_corpse() && !i.has_var( "activity_var" ) ) {
const mtype corpse = *i.get_mtype();
if( corpse.size >= MS_MEDIUM ) {
if( corpse.size >= creature_size::medium ) {
big_count += 1;
} else {
small_count += 1;
Expand Down Expand Up @@ -1888,7 +1888,7 @@ static bool butcher_corpse_activity( player &p, const tripoint &src_loc,
for( auto &elem : items ) {
if( elem.is_corpse() && !elem.has_var( "activity_var" ) ) {
const mtype corpse = *elem.get_mtype();
if( corpse.size >= MS_MEDIUM && reason != do_activity_reason::NEEDS_BIG_BUTCHERING ) {
if( corpse.size >= creature_size::medium && reason != do_activity_reason::NEEDS_BIG_BUTCHERING ) {
continue;
}
elem.set_var( "activity_var", p.name );
Expand Down
6 changes: 3 additions & 3 deletions src/ballistics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ static void drop_or_embed_projectile( const dealt_projectile_attack &attack )
bool embed = mon_there && effects.count( "NO_EMBED" ) == 0 && effects.count( "TANGLE" ) == 0;
// Don't embed in small creatures
if( embed ) {
const m_size critter_size = mon->get_size();
const creature_size critter_size = mon->get_size();
const units::volume vol = dropped_item.volume();
embed = embed && ( critter_size > MS_TINY || vol < 250_ml );
embed = embed && ( critter_size > MS_SMALL || vol < 500_ml );
embed = embed && ( critter_size > creature_size::tiny || vol < 250_ml );
embed = embed && ( critter_size > creature_size::small || vol < 500_ml );
// And if we deal enough damage
// Item volume bumps up the required damage too
embed = embed &&
Expand Down
4 changes: 2 additions & 2 deletions src/basecamp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,7 @@ bool basecamp_action_components::choose_components()
comp_selection<item_comp> is =
g->u.select_item_component( it, batch_size_, base_._inv, true, filter,
!base_.by_radio );
if( is.use_from == cancel ) {
if( is.use_from == usage_from::cancel ) {
return false;
}
item_selections_.push_back( is );
Expand All @@ -734,7 +734,7 @@ bool basecamp_action_components::choose_components()
comp_selection<tool_comp> ts =
g->u.select_tool_component( it, batch_size_, base_._inv, DEFAULT_HOTKEYS, true,
!base_.by_radio );
if( ts.use_from == cancel ) {
if( ts.use_from == usage_from::cancel ) {
return false;
}
tool_selections_.push_back( ts );
Expand Down
14 changes: 7 additions & 7 deletions src/character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ int Character::get_fat_to_hp() const
return mut_fat_hp * ( get_bmi() - character_weight_category::normal );
}

m_size Character::get_size() const
creature_size Character::get_size() const
{
return size_class;
}
Expand Down Expand Up @@ -1123,7 +1123,7 @@ bool Character::check_mount_is_spooked()
// / 2 if horse has full tack and saddle.
// Monster in spear reach monster and average stat (8) player on saddled horse, 14% -2% -0.8% / 2 = ~5%
if( mounted_creature && mounted_creature->type->has_fear_trigger( mon_trigger::HOSTILE_CLOSE ) ) {
const m_size mount_size = mounted_creature->get_size();
const creature_size mount_size = mounted_creature->get_size();
const bool saddled = mounted_creature->has_effect( effect_monster_saddled );
for( const monster &critter : g->all_monsters() ) {
double chance = 1.0;
Expand Down Expand Up @@ -7155,15 +7155,15 @@ std::string Character::height_string() const
int Character::height() const
{
switch( get_size() ) {
case MS_TINY:
case creature_size::tiny:
return init_height - 100;
case MS_SMALL:
case creature_size::small:
return init_height - 50;
case MS_MEDIUM:
case creature_size::medium:
return init_height;
case MS_LARGE:
case creature_size::large:
return init_height + 50;
case MS_HUGE:
case creature_size::huge:
return init_height + 100;
}

Expand Down
6 changes: 3 additions & 3 deletions src/character.h
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ class Character : public Creature, public visitable<Character>
int get_fat_to_hp() const;

/** Get size class of character **/
m_size get_size() const override;
creature_size get_size() const override;

/** Returns either "you" or the player's name. capitalize_first assumes
that the character's name is already upper case and uses it only for
Expand Down Expand Up @@ -2246,7 +2246,7 @@ class Character : public Creature, public visitable<Character>
/**height at character creation*/
int init_height = 175;
/** Size class of character. */
m_size size_class = MS_MEDIUM;
creature_size size_class = creature_size::medium;

// the player's activity level for metabolism calculations
float activity_level = NO_EXERCISE;
Expand Down Expand Up @@ -2391,7 +2391,7 @@ class Character : public Creature, public visitable<Character>
};

// Little size helper, exposed for use in deserialization code.
m_size calculate_size( const Character &c );
creature_size calculate_size( const Character &c );

template<>
struct enum_traits<Character::stat> {
Expand Down
68 changes: 34 additions & 34 deletions src/craft_command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ template<typename CompType>
std::string comp_selection<CompType>::nname() const
{
switch( use_from ) {
case use_from_map:
case usage_from::map:
return item::nname( comp.type, comp.count ) + _( " (nearby)" );
case use_from_both:
case usage_from::both:
return item::nname( comp.type, comp.count ) + _( " (person & nearby)" );
case use_from_player:
case usage_from::player:
// Is the same as the default return;
case use_from_none:
case cancel:
case num_usages:
case usage_from::none:
case usage_from::cancel:
case usage_from::num_usages_from:
break;
}

Expand All @@ -46,17 +46,17 @@ namespace io
{

template<>
std::string enum_to_string<usage>( usage data )
std::string enum_to_string<usage_from>( usage_from data )
{
switch( data ) {
// *INDENT-OFF*
case usage::use_from_map: return "map";
case usage::use_from_player: return "player";
case usage::use_from_both: return "both";
case usage::use_from_none: return "none";
case usage::cancel: return "cancel";
case usage_from::map: return "map";
case usage_from::player: return "player";
case usage_from::both: return "both";
case usage_from::none: return "none";
case usage_from::cancel: return "cancel";
// *INDENT-ON*
case usage::num_usages:
case usage_from::num_usages_from:
break;
}
debugmsg( "Invalid usage" );
Expand Down Expand Up @@ -84,7 +84,7 @@ void comp_selection<CompType>::deserialize( JsonIn &jsin )

std::string use_from_str;
data.read( "use_from", use_from_str );
use_from = io::string_to_enum<usage>( use_from_str );
use_from = io::string_to_enum<usage_from>( use_from_str );
data.read( "type", comp.type );
data.read( "count", comp.count );
}
Expand Down Expand Up @@ -154,7 +154,7 @@ void craft_command::execute( const tripoint &new_loc )
for( const auto &it : needs->get_components() ) {
comp_selection<item_comp> is =
crafter->select_item_component( it, batch_size, map_inv, true, filter );
if( is.use_from == cancel ) {
if( is.use_from == usage_from::cancel ) {
return;
}
item_selections.push_back( is );
Expand All @@ -166,7 +166,7 @@ void craft_command::execute( const tripoint &new_loc )
it, batch_size, map_inv, DEFAULT_HOTKEYS, true, true, []( int charges ) {
return charges / 20 + charges % 20;
} );
if( ts.use_from == cancel ) {
if( ts.use_from == usage_from::cancel ) {
return;
}
tool_selections.push_back( ts );
Expand Down Expand Up @@ -296,49 +296,49 @@ std::vector<comp_selection<item_comp>> craft_command::check_item_components_miss

if( item::count_by_charges( type ) && count > 0 ) {
switch( item_sel.use_from ) {
case use_from_player:
case usage_from::player:
if( !crafter->has_charges( type, count, filter ) ) {
missing.push_back( item_sel );
}
break;
case use_from_map:
case usage_from::map:
if( !map_inv.has_charges( type, count, filter ) ) {
missing.push_back( item_sel );
}
break;
case use_from_both:
case usage_from::both:
if( !( crafter->charges_of( type, INT_MAX, filter ) +
map_inv.charges_of( type, INT_MAX, filter ) >= count ) ) {
missing.push_back( item_sel );
}
break;
case use_from_none:
case cancel:
case num_usages:
case usage_from::none:
case usage_from::cancel:
case usage_from::num_usages_from:
break;
}
} else {
// Counting by units, not charges.
switch( item_sel.use_from ) {
case use_from_player:
case usage_from::player:
if( !crafter->has_amount( type, count, false, filter ) ) {
missing.push_back( item_sel );
}
break;
case use_from_map:
case usage_from::map:
if( !map_inv.has_components( type, count, filter ) ) {
missing.push_back( item_sel );
}
break;
case use_from_both:
case usage_from::both:
if( !( crafter->amount_of( type, false, std::numeric_limits<int>::max(), filter ) +
map_inv.amount_of( type, false, std::numeric_limits<int>::max(), filter ) >= count ) ) {
missing.push_back( item_sel );
}
break;
case use_from_none:
case cancel:
case num_usages:
case usage_from::none:
case usage_from::cancel:
case usage_from::num_usages_from:
break;
}
}
Expand All @@ -357,20 +357,20 @@ std::vector<comp_selection<tool_comp>> craft_command::check_tool_components_miss
if( tool_sel.comp.count > 0 ) {
const int count = tool_sel.comp.count * batch_size;
switch( tool_sel.use_from ) {
case use_from_player:
case usage_from::player:
if( !crafter->has_charges( type, count ) ) {
missing.push_back( tool_sel );
}
break;
case use_from_map:
case usage_from::map:
if( !map_inv.has_charges( type, count ) ) {
missing.push_back( tool_sel );
}
break;
case use_from_both:
case use_from_none:
case cancel:
case num_usages:
case usage_from::both:
case usage_from::none:
case usage_from::cancel:
case usage_from::num_usages_from:
break;
}
} else if( !crafter->has_amount( type, 1 ) && !map_inv.has_tools( type, 1 ) ) {
Expand Down