diff --git a/src/fault.cpp b/src/fault.cpp index ce89c757ce3e1..8a4c9ce69d5ce 100644 --- a/src/fault.cpp +++ b/src/fault.cpp @@ -50,23 +50,6 @@ const fault_id &faults::random_of_type_item_has( const item &it, const std::stri return fault_id::NULL_ID(); } -const fault_id &faults::get_random_of_type_item_can_have( const item &it, const std::string &type ) -{ - const auto &typed = faults_by_type.find( type ); - if( typed == faults_by_type.end() ) { - debugmsg( "there are no faults with type '%s'", type ); - return fault_id::NULL_ID(); - } - - for( const fault_id &fid : typed->second ) { - if( it.faults_potential().count( fid ) ) { - return fid; - } - } - - return fault_id::NULL_ID(); -} - void faults::load_fault( const JsonObject &jo, const std::string &src ) { fault_factory.load( jo, src ); diff --git a/src/fault.h b/src/fault.h index dd0ccacacc380..703b97b6e8e8f 100644 --- a/src/fault.h +++ b/src/fault.h @@ -29,8 +29,6 @@ void reset(); void finalize(); void check_consistency(); -const fault_id &get_random_of_type_item_can_have( const item &it, const std::string &type ); - const fault_id &random_of_type( const std::string &type ); const fault_id &random_of_type_item_has( const item &it, const std::string &type ); } // namespace faults diff --git a/src/item.cpp b/src/item.cpp index 4cae629721a53..2643dd5b3ee12 100644 --- a/src/item.cpp +++ b/src/item.cpp @@ -9882,6 +9882,28 @@ std::set item::faults_potential() const return res; } +bool item::can_have_fault_type( const std::string &fault_type ) const +{ + std::set res; + for( const auto &some_fault : type->faults ) { + if( some_fault->type() == fault_type ) { + return true; + } + } + return false; +} + +std::set item::faults_potential_of_type( const std::string &fault_type ) const +{ + std::set res; + for( const auto &some_fault : type->faults ) { + if( some_fault->type() == fault_type ) { + res.emplace( some_fault ); + } + } + return res; +} + int item::wheel_area() const { return is_wheel() ? type->wheel->diameter * type->wheel->width : 0; diff --git a/src/item.h b/src/item.h index 14daa129babdb..6f5e788b34d48 100644 --- a/src/item.h +++ b/src/item.h @@ -1685,6 +1685,10 @@ class item : public visitable /** What faults can potentially occur with this item? */ std::set faults_potential() const; + bool can_have_fault_type( const std::string &fault_type ) const; + + std::set faults_potential_of_type( const std::string &fault_type ) const; + /** Returns the total area of this wheel or 0 if it isn't one. */ int wheel_area() const; diff --git a/src/ranged.cpp b/src/ranged.cpp index 816532f567b4d..28a5152f6c24a 100644 --- a/src/ranged.cpp +++ b/src/ranged.cpp @@ -753,11 +753,11 @@ bool Character::handle_gun_damage( item &it ) // Chance for the weapon to suffer a failure, caused by the magazine size, quality, or condition } else if( x_in_y( jam_chance, 1 ) && !it.has_var( "u_know_round_in_chamber" ) && - faults::get_random_of_type_item_can_have( it, gun_mechanical_simple ) != fault_id::NULL_ID() ) { + it.can_have_fault_type( gun_mechanical_simple ) ) { add_msg_player_or_npc( m_bad, _( "Your %s malfunctions!" ), _( "'s %s malfunctions!" ), it.tname() ); - it.faults.insert( faults::get_random_of_type_item_can_have( it, gun_mechanical_simple ) ); + it.faults.insert( random_entry( it.faults_potential_of_type( gun_mechanical_simple ) ) ); return false; // Here we check for a chance for attached mods to get damaged if they are flagged as 'CONSUMABLE'.