Skip to content

Commit

Permalink
tidy up fault code a bit (#76314)
Browse files Browse the repository at this point in the history
* tidy up fault code

* Astyle

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* auto -> const auto

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
GuardianDll and github-actions[bot] authored Sep 16, 2024
1 parent 83ecacd commit e186f1b
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 21 deletions.
17 changes: 0 additions & 17 deletions src/fault.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
2 changes: 0 additions & 2 deletions src/fault.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions src/item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9882,6 +9882,28 @@ std::set<fault_id> item::faults_potential() const
return res;
}

bool item::can_have_fault_type( const std::string &fault_type ) const
{
std::set<fault_id> res;
for( const auto &some_fault : type->faults ) {
if( some_fault->type() == fault_type ) {
return true;
}
}
return false;
}

std::set<fault_id> item::faults_potential_of_type( const std::string &fault_type ) const
{
std::set<fault_id> 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;
Expand Down
4 changes: 4 additions & 0 deletions src/item.h
Original file line number Diff line number Diff line change
Expand Up @@ -1685,6 +1685,10 @@ class item : public visitable
/** What faults can potentially occur with this item? */
std::set<fault_id> faults_potential() const;

bool can_have_fault_type( const std::string &fault_type ) const;

std::set<fault_id> 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;

Expand Down
4 changes: 2 additions & 2 deletions src/ranged.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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!" ),
_( "<npcname>'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'.
Expand Down

0 comments on commit e186f1b

Please sign in to comment.