Skip to content

Commit

Permalink
Cleanup fault types
Browse files Browse the repository at this point in the history
  • Loading branch information
irwiss committed May 15, 2024
1 parent 137967a commit 779b8fc
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 16 deletions.
6 changes: 6 additions & 0 deletions data/core/sentinels.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@
"legacy_enum_id": 0,
"intensity_levels": [ { "name": "nothing" } ]
},
{
"id": "null",
"type": "fault",
"name": { "str": "This is a bug" },
"description": "This fault id is a reserved sentinel."
},
{
"type": "SPELL",
"id": "null",
Expand Down
4 changes: 2 additions & 2 deletions src/avatar_action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1136,10 +1136,10 @@ void avatar_action::use_item( avatar &you, item_location &loc, std::string const
if( loc->wetness && loc->has_flag( flag_WATER_BREAK_ACTIVE ) ) {
if( query_yn( _( "This item is still wet and it will break if you turn it on. Proceed?" ) ) ) {
loc->deactivate();
loc.get_item()->set_fault( random_entry( faults::get_by_type( std::string( "wet" ) ) ) );
loc.get_item()->set_fault( faults::random_of_type( "wet" ) );
// An electronic item in water is also shorted.
if( loc->has_flag( flag_ELECTRONIC ) ) {
loc.get_item()->set_fault( random_entry( faults::get_by_type( std::string( "shorted" ) ) ) );
loc.get_item()->set_fault( faults::random_of_type( "shorted" ) );
}
} else {
return;
Expand Down
4 changes: 2 additions & 2 deletions src/explosion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,7 @@ void emp_blast( const tripoint &p )
!player_character.has_flag( json_flag_EMP_IMMUNE ) ) {
add_msg( m_bad, _( "The EMP blast fries your %s!" ), it->tname() );
it->deactivate();
it->faults.insert( random_entry( faults::get_by_type( "shorted" ) ) );
it->faults.insert( faults::random_of_type( "shorted" ) );
}
}
}
Expand All @@ -795,7 +795,7 @@ void emp_blast( const tripoint &p )
add_msg( _( "The EMP blast fries the %s!" ), it.tname() );
}
it.deactivate();
it.set_fault( random_entry( faults::get_by_type( "shorted" ) ) );
it.set_fault( faults::random_of_type( "shorted" ) );
}
}
// TODO: Drain NPC energy reserves
Expand Down
21 changes: 15 additions & 6 deletions src/fault.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <type_traits>
#include <utility>
#include <vector>

#include "debug.h"
#include "generic_factory.h"
Expand All @@ -17,13 +18,18 @@ generic_factory<fault_fix> fault_fixes_factory( "fault_fix", "id" );
std::multimap<fault_fix_id, std::pair<std::string, int>> reqs_temp_storage;

// Have a list of faults by type, the type right now is item prefix to avoid adding more JSON data
std::map<std::string, std::list<fault_id>> faults_by_type;
std::map<std::string, std::vector<fault_id>> faults_by_type;

} // namespace

const std::list<fault_id> &faults::get_by_type( const std::string &type )
const fault_id &faults::random_of_type( const std::string &type )
{
return faults_by_type.at( 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();
}
return random_entry_ref( typed->second );
}

void faults::load_fault( const JsonObject &jo, const std::string &src )
Expand All @@ -40,6 +46,7 @@ void faults::reset()
{
fault_factory.reset();
fault_fixes_factory.reset();
faults_by_type.clear();
}

void faults::finalize()
Expand All @@ -52,6 +59,11 @@ void faults::finalize()
fault_fix &fix = const_cast<fault_fix &>( const_fix );
fix.finalize();
}
for( const fault &f : fault_factory.get_all() ) {
if( !f.type().empty() ) {
faults_by_type[f.type()].emplace_back( f.id.str() );
}
}
reqs_temp_storage.clear();
}

Expand Down Expand Up @@ -133,9 +145,6 @@ void fault::load( const JsonObject &jo, std::string_view )
optional( jo, was_loaded, "fault_type", type_ );
optional( jo, was_loaded, "flags", flags );
optional( jo, was_loaded, "price_modifier", price_modifier, 1.0 );
if( !type_.empty() ) {
faults_by_type[ std::string( type_ ) ].push_back( id );
}
}

void fault::check() const
Expand Down
3 changes: 1 addition & 2 deletions src/fault.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#ifndef CATA_SRC_FAULT_H
#define CATA_SRC_FAULT_H

#include <list>
#include <map>
#include <memory>
#include <set>
Expand Down Expand Up @@ -30,7 +29,7 @@ void reset();
void finalize();
void check_consistency();

const std::list<fault_id> &get_by_type( const std::string &type );
const fault_id &random_of_type( const std::string &type );
} // namespace faults

class fault_fix
Expand Down
4 changes: 2 additions & 2 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11795,10 +11795,10 @@ void game::water_affect_items( Character &ch ) const
loc->deactivate();
// TODO: Maybe different types of wet faults? But I can't think of any.
// This just means it's still too wet to use.
loc->set_fault( random_entry( faults::get_by_type( std::string( "wet" ) ) ) );
loc->set_fault( faults::random_of_type( "wet" ) ) ;
// An electronic item in water is also shorted.
if( loc->has_flag( flag_ELECTRONIC ) ) {
loc->set_fault( random_entry( faults::get_by_type( std::string( "shorted" ) ) ) );
loc->set_fault( faults::random_of_type( "shorted" ) );
}
} else if( loc->has_flag( flag_WATER_BREAK_ACTIVE ) && !loc->is_broken()
&& !loc.protected_from_liquids() ) {
Expand Down
4 changes: 2 additions & 2 deletions src/item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14132,9 +14132,9 @@ bool item::process_internal( map &here, Character *carrier, const tripoint &pos,

if( wetness && has_flag( flag_WATER_BREAK ) ) {
deactivate();
set_fault( random_entry( faults::get_by_type( std::string( "wet" ) ) ) );
set_fault( faults::random_of_type( "wet" ) );
if( has_flag( flag_ELECTRONIC ) ) {
set_fault( random_entry( faults::get_by_type( std::string( "shorted" ) ) ) );
set_fault( faults::random_of_type( "shorted" ) );
}
}

Expand Down

0 comments on commit 779b8fc

Please sign in to comment.