From b12aa0d056c7e43e10a69f577bacc3eded4ccdc8 Mon Sep 17 00:00:00 2001 From: Ramza13 <52087122+Ramza13@users.noreply.github.com> Date: Tue, 7 Jan 2020 11:33:58 -0500 Subject: [PATCH 1/8] Added safe sound rule category --- src/safemode_ui.cpp | 101 +++++++++++++++++++++++++++++++------------- src/safemode_ui.h | 19 ++++++--- src/sounds.cpp | 4 +- 3 files changed, 88 insertions(+), 36 deletions(-) diff --git a/src/safemode_ui.cpp b/src/safemode_ui.cpp index d4b1fdbee119e..4eb08ebb353ca 100644 --- a/src/safemode_ui.cpp +++ b/src/safemode_ui.cpp @@ -60,13 +60,15 @@ void safemode::show( const std::string &custom_name_in, bool is_safemode_in ) COLUMN_ATTITUDE, COLUMN_PROXIMITY, COLUMN_WHITE_BLACKLIST, + COLUMN_CATEGORY }; std::map column_pos; column_pos[COLUMN_RULE] = 4; - column_pos[COLUMN_ATTITUDE] = 48; - column_pos[COLUMN_PROXIMITY] = 59; - column_pos[COLUMN_WHITE_BLACKLIST] = 66; + column_pos[COLUMN_ATTITUDE] = column_pos[COLUMN_RULE] + 38; + column_pos[COLUMN_PROXIMITY] = column_pos[COLUMN_ATTITUDE] + 10; + column_pos[COLUMN_WHITE_BLACKLIST] = column_pos[COLUMN_PROXIMITY] + 6; + column_pos[COLUMN_CATEGORY] = column_pos[COLUMN_WHITE_BLACKLIST] + 11; const int num_columns = column_pos.size(); @@ -128,6 +130,7 @@ void safemode::show( const std::string &custom_name_in, bool is_safemode_in ) mvwprintz( w_header, point( column_pos[COLUMN_ATTITUDE] + 2, 3 ), c_white, _( "Attitude" ) ); mvwprintz( w_header, point( column_pos[COLUMN_PROXIMITY] + 2, 3 ), c_white, _( "Dist" ) ); mvwprintz( w_header, point( column_pos[COLUMN_WHITE_BLACKLIST] + 2, 3 ), c_white, _( "B/W" ) ); + mvwprintz( w_header, point( column_pos[COLUMN_CATEGORY] + 2, 3 ), c_white, _( "Cat" ) ); wrefresh( w_header ); @@ -223,9 +226,15 @@ void safemode::show( const std::string &custom_name_in, bool is_safemode_in ) }; draw_column( COLUMN_RULE, ( rule.rule.empty() ) ? _( "" ) : rule.rule ); - draw_column( COLUMN_ATTITUDE, Creature::get_attitude_ui_data( rule.attitude ).first.translated() ); - draw_column( COLUMN_PROXIMITY, ( !rule.whitelist ) ? to_string( rule.proximity ) : "---" ); - draw_column( COLUMN_WHITE_BLACKLIST, ( rule.whitelist ) ? _( "Whitelist" ) : _( "Blacklist" ) ); + draw_column( COLUMN_ATTITUDE, ( rule.category == Categories::HOSTILE ) ? + Creature::get_attitude_ui_data( rule.attitude ).first.translated() : "---" ); + draw_column( COLUMN_PROXIMITY, ( ( rule.category == Categories::HOSTILE ) || + !rule.whitelist ) ? to_string( rule.proximity ) : "---" ); + draw_column( COLUMN_WHITE_BLACKLIST, + ( rule.category == Categories::HOSTILE ) ? ( rule.whitelist ) ? _( "Whitelist" ) : _( "Blacklist" ) + : "---" ); + draw_column( COLUMN_CATEGORY, ( rule.category == Categories::SOUND ) ? _( "Sound" ) : + _( "Hostile" ) ); } } @@ -261,12 +270,12 @@ void safemode::show( const std::string &custom_name_in, bool is_safemode_in ) } } else if( action == "ADD_DEFAULT_RULESET" ) { changes_made = true; - current_tab.push_back( rules_class( "*", true, false, Creature::A_HOSTILE, 0 ) ); + current_tab.push_back( rules_class( "*", true, false, Creature::A_HOSTILE, 0, HOSTILE ) ); line = current_tab.size() - 1; } else if( action == "ADD_RULE" ) { changes_made = true; current_tab.push_back( rules_class( "", true, false, Creature::A_HOSTILE, - get_option( "SAFEMODEPROXIMITY" ) ) ); + get_option( "SAFEMODEPROXIMITY" ), HOSTILE ) ); line = current_tab.size() - 1; } else if( action == "REMOVE_RULE" && !current_tab.empty() ) { changes_made = true; @@ -320,6 +329,12 @@ void safemode::show( const std::string &custom_name_in, bool is_safemode_in ) .query_string() ); } else if( column == COLUMN_WHITE_BLACKLIST ) { current_tab[line].whitelist = !current_tab[line].whitelist; + } else if( column == COLUMN_CATEGORY ) { + if( current_tab[line].category == HOSTILE ) { + current_tab[line].category = SOUND; + } else if( current_tab[line].category == SOUND ) { + current_tab[line].category = HOSTILE; + } } else if( column == COLUMN_ATTITUDE ) { auto &attitude = current_tab[line].attitude; switch( attitude ) { @@ -514,7 +529,7 @@ void safemode::add_rule( const std::string &rule_in, const Creature::Attitude at const rule_state state_in ) { character_rules.push_back( rules_class( rule_in, true, ( state_in == RULE_WHITELISTED ), - attitude_in, proximity_in ) ); + attitude_in, proximity_in, HOSTILE ) ); create_rules(); if( !get_option( "SAFEMODE" ) && @@ -557,8 +572,8 @@ bool safemode::empty() const void safemode::create_rules() { - safemode_rules.clear(); - + safemode_rules_hostile.clear(); + safemode_rules_sound.clear(); //process include/exclude in order of rules, global first, then character specific add_rules( global_rules ); add_rules( character_rules ); @@ -569,15 +584,19 @@ void safemode::add_rules( const std::vector &rules_in ) //if a specific monster is being added, all the rules need to be checked now //may have some performance issues since exclusion needs to check all monsters also for( auto &rule : rules_in ) { - if( !rule.whitelist ) { - //Check include patterns against all monster mtypes - for( const auto &mtype : MonsterGenerator::generator().get_all_mtypes() ) { - set_rule( rule, mtype.nname(), RULE_BLACKLISTED ); - } - } else { - //exclude monsters from the existing mapping - for( const auto &safemode_rule : safemode_rules ) { - set_rule( rule, safemode_rule.first, RULE_WHITELISTED ); + if( rule.category == SOUND ) { + set_rule( rule, rule.rule, RULE_WHITELISTED ); + } else if( rule.category == HOSTILE ) { + if( !rule.whitelist ) { + //Check include patterns against all monster mtypes + for( const auto &mtype : MonsterGenerator::generator().get_all_mtypes() ) { + set_rule( rule, mtype.nname(), RULE_BLACKLISTED ); + } + } else { + //exclude monsters from the existing mapping + for( const auto &safemode_rule : safemode_rules_hostile ) { + set_rule( rule, safemode_rule.first, RULE_WHITELISTED ); + } } } } @@ -585,25 +604,31 @@ void safemode::add_rules( const std::vector &rules_in ) void safemode::set_rule( const rules_class &rule_in, const std::string &name_in, rule_state rs_in ) { - static std::vector attitude_any = {{Creature::A_HOSTILE, Creature::A_NEUTRAL, Creature::A_FRIENDLY}}; + if( rule_in.category == HOSTILE ) { + static std::vector attitude_any = { {Creature::A_HOSTILE, Creature::A_NEUTRAL, Creature::A_FRIENDLY} }; - if( !rule_in.rule.empty() && rule_in.active && wildcard_match( name_in, rule_in.rule ) ) { - if( rule_in.attitude == Creature::A_ANY ) { - for( auto &att : attitude_any ) { - safemode_rules[ name_in ][ att ] = rule_state_class( rs_in, rule_in.proximity ); + if( !rule_in.rule.empty() && rule_in.active && wildcard_match( name_in, rule_in.rule ) ) { + if( rule_in.attitude == Creature::A_ANY ) { + for( auto &att : attitude_any ) { + safemode_rules_hostile[name_in][att] = rule_state_class( rs_in, rule_in.proximity, HOSTILE ); + } + } else { + safemode_rules_hostile[name_in][rule_in.attitude] = rule_state_class( rs_in, rule_in.proximity, + HOSTILE ); } - } else { - safemode_rules[ name_in ][ rule_in.attitude ] = rule_state_class( rs_in, rule_in.proximity ); } + } else if( rule_in.category == SOUND ) { + safemode_rules_sound[name_in] = rule_state_class( rs_in, rule_in.proximity, SOUND ); } + } rule_state safemode::check_monster( const std::string &creature_name_in, const Creature::Attitude attitude_in, const int proximity_in ) const { - const auto iter = safemode_rules.find( creature_name_in ); - if( iter != safemode_rules.end() ) { + const auto iter = safemode_rules_hostile.find( creature_name_in ); + if( iter != safemode_rules_hostile.end() ) { const auto &tmp = ( iter->second )[static_cast( attitude_in )]; if( tmp.state == RULE_BLACKLISTED ) { if( tmp.proximity == 0 || proximity_in <= tmp.proximity ) { @@ -618,6 +643,19 @@ rule_state safemode::check_monster( const std::string &creature_name_in, return RULE_NONE; } +bool safemode::check_sound( const std::string &sound_name_in, + const int proximity_in ) const +{ + for( auto &rule : safemode_rules_sound ) { + if( wildcard_match( sound_name_in, rule.first ) ) { + if( proximity_in >= rule.second.proximity ) { + return true; + } + } + }; + return false; +} + void safemode::clear_character_rules() { character_rules.clear(); @@ -703,6 +741,7 @@ void safemode::serialize( JsonOut &json ) const json.member( "whitelist", elem.whitelist ); json.member( "attitude", elem.attitude ); json.member( "proximity", elem.proximity ); + json.member( "category", elem.category ); json.end_object(); } @@ -724,9 +763,11 @@ void safemode::deserialize( JsonIn &jsin ) const bool whitelist = jo.get_bool( "whitelist" ); const Creature::Attitude attitude = static_cast( jo.get_int( "attitude" ) ); const int proximity = jo.get_int( "proximity" ); + const Categories cat = jo.has_member( "category" ) ? static_cast + ( jo.get_int( "category" ) ) : HOSTILE; temp_rules.push_back( - rules_class( rule, active, whitelist, attitude, proximity ) + rules_class( rule, active, whitelist, attitude, proximity, cat ) ); } } diff --git a/src/safemode_ui.h b/src/safemode_ui.h index 39841eed6ceda..250a1d18ae768 100644 --- a/src/safemode_ui.h +++ b/src/safemode_ui.h @@ -22,21 +22,27 @@ class safemode MAX_TAB }; + enum Categories : int { + HOSTILE, + SOUND + }; + class rules_class { public: std::string rule; bool active; bool whitelist; + Categories category; Creature::Attitude attitude; int proximity; rules_class() : active( false ), whitelist( false ), attitude( Creature::A_HOSTILE ), - proximity( 0 ) {} + proximity( 0 ), category( Categories::HOSTILE ) {} rules_class( const std::string &rule_in, bool active_in, bool whitelist_in, - Creature::Attitude attitude_in, int proximity_in ) : rule( rule_in ), + Creature::Attitude attitude_in, int proximity_in, Categories cat ) : rule( rule_in ), active( active_in ), whitelist( whitelist_in ), - attitude( attitude_in ), proximity( proximity_in ) {} + attitude( attitude_in ), proximity( proximity_in ), category( cat ) {} }; class rule_state_class @@ -46,7 +52,7 @@ class safemode int proximity; rule_state_class() : state( RULE_NONE ), proximity( 0 ) {} - rule_state_class( rule_state state_in, int proximity_in ) : state( state_in ), + rule_state_class( rule_state state_in, int proximity_in, Categories ) : state( state_in ), proximity( proximity_in ) {} }; @@ -57,7 +63,8 @@ class safemode * is added as the key, with RULE_WHITELISTED or RULE_BLACKLISTED as the values. * safemode_rules[ 'creature name' ][ 'attitude' ].rule_state_class('rule_state', 'proximity') */ - std::unordered_map < std::string, std::array < rule_state_class, 3 > > safemode_rules; + std::unordered_map < std::string, std::array < rule_state_class, 3 > > safemode_rules_hostile; + std::unordered_map < std::string, rule_state_class > safemode_rules_sound; /** * current rules for global and character tab @@ -87,6 +94,8 @@ class safemode rule_state check_monster( const std::string &creature_name_in, Creature::Attitude attitude_in, int proximity_in ) const; + bool check_sound( const std::string &sound_name_in, const int proximity_in ) const; + std::string npc_type_name(); void show(); diff --git a/src/sounds.cpp b/src/sounds.cpp index 937459ec5acd6..606ab9df4059d 100644 --- a/src/sounds.cpp +++ b/src/sounds.cpp @@ -43,6 +43,7 @@ #include "type_id.h" #include "point.h" #include "string_id.h" +#include "safemode_ui.h" #if defined(SDL_SOUND) # if defined(_MSC_VER) && defined(USE_VCPKG) @@ -431,7 +432,8 @@ void sounds::process_sound_markers( player *p ) // don't print our own noise or things without descriptions if( !sound.ambient && ( pos != p->pos() ) && !g->m.pl_sees( pos, distance_to_sound ) ) { - if( !p->activity.is_distraction_ignored( distraction_type::noise ) ) { + if( !p->activity.is_distraction_ignored( distraction_type::noise ) && + !get_safemode().check_sound( sound.description, distance_to_sound ) ) { const std::string query = string_format( _( "Heard %s!" ), description ); g->cancel_activity_or_ignore_query( distraction_type::noise, query ); } From e821e33bdb32036bdb84c0ecfc6e98b23ae2755f Mon Sep 17 00:00:00 2001 From: Ramza13 <52087122+Ramza13@users.noreply.github.com> Date: Tue, 7 Jan 2020 12:02:36 -0500 Subject: [PATCH 2/8] Hopefully fix build issue on server --- src/safemode_ui.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/safemode_ui.h b/src/safemode_ui.h index 250a1d18ae768..5a75bb63752a2 100644 --- a/src/safemode_ui.h +++ b/src/safemode_ui.h @@ -33,9 +33,9 @@ class safemode std::string rule; bool active; bool whitelist; - Categories category; - Creature::Attitude attitude; int proximity; + Categories category; + Creature::Attitude attitude; rules_class() : active( false ), whitelist( false ), attitude( Creature::A_HOSTILE ), proximity( 0 ), category( Categories::HOSTILE ) {} From 8532b6262fdab272e3f890e0908a225640c7a1b5 Mon Sep 17 00:00:00 2001 From: Ramza13 <52087122+Ramza13@users.noreply.github.com> Date: Tue, 7 Jan 2020 12:15:40 -0500 Subject: [PATCH 3/8] Properly fix build --- src/safemode_ui.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/safemode_ui.h b/src/safemode_ui.h index 5a75bb63752a2..fdb169a9f8eb1 100644 --- a/src/safemode_ui.h +++ b/src/safemode_ui.h @@ -33,9 +33,9 @@ class safemode std::string rule; bool active; bool whitelist; + Creature::Attitude attitude; int proximity; Categories category; - Creature::Attitude attitude; rules_class() : active( false ), whitelist( false ), attitude( Creature::A_HOSTILE ), proximity( 0 ), category( Categories::HOSTILE ) {} From c14299593966e079b7cf0be776dbfdf31cef9e43 Mon Sep 17 00:00:00 2001 From: Eric <52087122+Ramza13@users.noreply.github.com> Date: Tue, 7 Jan 2020 22:17:26 -0500 Subject: [PATCH 4/8] Update src/safemode_ui.cpp MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Jianxiang Wang (王健翔) --- src/safemode_ui.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/safemode_ui.cpp b/src/safemode_ui.cpp index 4eb08ebb353ca..4a69376ae9011 100644 --- a/src/safemode_ui.cpp +++ b/src/safemode_ui.cpp @@ -130,7 +130,7 @@ void safemode::show( const std::string &custom_name_in, bool is_safemode_in ) mvwprintz( w_header, point( column_pos[COLUMN_ATTITUDE] + 2, 3 ), c_white, _( "Attitude" ) ); mvwprintz( w_header, point( column_pos[COLUMN_PROXIMITY] + 2, 3 ), c_white, _( "Dist" ) ); mvwprintz( w_header, point( column_pos[COLUMN_WHITE_BLACKLIST] + 2, 3 ), c_white, _( "B/W" ) ); - mvwprintz( w_header, point( column_pos[COLUMN_CATEGORY] + 2, 3 ), c_white, _( "Cat" ) ); + mvwprintz( w_header, point( column_pos[COLUMN_CATEGORY] + 2, 3 ), c_white, pgettext( "category", "Cat" ) ); wrefresh( w_header ); From 3a4ba5997ac5cc60d83ff9dbe2edf132dd96b651 Mon Sep 17 00:00:00 2001 From: Ramza13 <52087122+Ramza13@users.noreply.github.com> Date: Sat, 11 Jan 2020 09:29:16 -0500 Subject: [PATCH 5/8] Update based on feedback --- src/safemode_ui.cpp | 149 ++++++++++++++++++++++++++------------------ src/safemode_ui.h | 8 +-- src/sounds.cpp | 2 +- 3 files changed, 95 insertions(+), 64 deletions(-) diff --git a/src/safemode_ui.cpp b/src/safemode_ui.cpp index 4eb08ebb353ca..9dfb12c7f1a1c 100644 --- a/src/safemode_ui.cpp +++ b/src/safemode_ui.cpp @@ -226,13 +226,11 @@ void safemode::show( const std::string &custom_name_in, bool is_safemode_in ) }; draw_column( COLUMN_RULE, ( rule.rule.empty() ) ? _( "" ) : rule.rule ); - draw_column( COLUMN_ATTITUDE, ( rule.category == Categories::HOSTILE ) ? + draw_column( COLUMN_ATTITUDE, ( rule.category == Categories::HOSTILE_SPOTTED ) ? Creature::get_attitude_ui_data( rule.attitude ).first.translated() : "---" ); - draw_column( COLUMN_PROXIMITY, ( ( rule.category == Categories::HOSTILE ) || + draw_column( COLUMN_PROXIMITY, ( ( rule.category == Categories::SOUND ) || !rule.whitelist ) ? to_string( rule.proximity ) : "---" ); - draw_column( COLUMN_WHITE_BLACKLIST, - ( rule.category == Categories::HOSTILE ) ? ( rule.whitelist ) ? _( "Whitelist" ) : _( "Blacklist" ) - : "---" ); + draw_column( COLUMN_WHITE_BLACKLIST, rule.whitelist ? _( "Whitelist" ) : _( "Blacklist" ) ); draw_column( COLUMN_CATEGORY, ( rule.category == Categories::SOUND ) ? _( "Sound" ) : _( "Hostile" ) ); } @@ -270,12 +268,13 @@ void safemode::show( const std::string &custom_name_in, bool is_safemode_in ) } } else if( action == "ADD_DEFAULT_RULESET" ) { changes_made = true; - current_tab.push_back( rules_class( "*", true, false, Creature::A_HOSTILE, 0, HOSTILE ) ); + current_tab.push_back( rules_class( "*", true, false, Creature::A_HOSTILE, 0, HOSTILE_SPOTTED ) ); + current_tab.push_back( rules_class( "*", true, true, Creature::A_HOSTILE, 5, SOUND ) ); line = current_tab.size() - 1; } else if( action == "ADD_RULE" ) { changes_made = true; current_tab.push_back( rules_class( "", true, false, Creature::A_HOSTILE, - get_option( "SAFEMODEPROXIMITY" ), HOSTILE ) ); + get_option( "SAFEMODEPROXIMITY" ), HOSTILE_SPOTTED ) ); line = current_tab.size() - 1; } else if( action == "REMOVE_RULE" && !current_tab.empty() ) { changes_made = true; @@ -307,19 +306,37 @@ void safemode::show( const std::string &custom_name_in, bool is_safemode_in ) } else if( action == "CONFIRM" && !current_tab.empty() ) { changes_made = true; if( column == COLUMN_RULE ) { - // NOLINTNEXTLINE(cata-use-named-point-constants) - fold_and_print( w_help, point( 1, 1 ), 999, c_white, - _( - "* is used as a Wildcard. A few Examples:\n" - "\n" - "human matches every NPC\n" - "zombie matches the monster name exactly\n" - "acidic zo* matches monsters beginning with 'acidic zo'\n" - "*mbie matches monsters ending with 'mbie'\n" - "*cid*zo*ie multiple * are allowed\n" - "AcI*zO*iE case insensitive search" ) - ); - + switch( current_tab[line].category ) { + case Categories::HOSTILE_SPOTTED: + // NOLINTNEXTLINE(cata-use-named-point-constants) + fold_and_print( w_help, point( 1, 1 ), 999, c_white, + _( + "* is used as a Wildcard. A few Examples:\n" + "\n" + "human matches every NPC\n" + "zombie matches the monster name exactly\n" + "acidic zo* matches monsters beginning with 'acidic zo'\n" + "*mbie matches monsters ending with 'mbie'\n" + "*cid*zo*ie multiple * are allowed\n" + "AcI*zO*iE case insensitive search" ) + ); + break; + case Categories::SOUND: + // NOLINTNEXTLINE(cata-use-named-point-constants) + fold_and_print( w_help, point( 1, 1 ), 999, c_white, + _( + "* is used as a Wildcard. A few Examples:\n" + "\n" + "footsteps matches the sound name exactly\n" + "a loud ba* matches sounds beginning with 'a loud ba'\n" + "*losion! matches sounds ending with 'losion!'\n" + "a *oud*ba* multiple * are allowed\n" + "*LoU*bA* case insensitive search" ) + ); + break; + default: + break; + } draw_border( w_help ); wrefresh( w_help ); current_tab[line].rule = wildcard_trim_rule( string_input_popup() @@ -330,10 +347,10 @@ void safemode::show( const std::string &custom_name_in, bool is_safemode_in ) } else if( column == COLUMN_WHITE_BLACKLIST ) { current_tab[line].whitelist = !current_tab[line].whitelist; } else if( column == COLUMN_CATEGORY ) { - if( current_tab[line].category == HOSTILE ) { + if( current_tab[line].category == HOSTILE_SPOTTED ) { current_tab[line].category = SOUND; } else if( current_tab[line].category == SOUND ) { - current_tab[line].category = HOSTILE; + current_tab[line].category = HOSTILE_SPOTTED; } } else if( column == COLUMN_ATTITUDE ) { auto &attitude = current_tab[line].attitude; @@ -350,7 +367,8 @@ void safemode::show( const std::string &custom_name_in, bool is_safemode_in ) case Creature::A_ANY: attitude = Creature::A_HOSTILE; } - } else if( column == COLUMN_PROXIMITY && !current_tab[line].whitelist ) { + } else if( column == COLUMN_PROXIMITY && ( current_tab[line].category == SOUND || + !current_tab[line].whitelist ) ) { const auto text = string_input_popup() .title( _( "Proximity Distance (0=max view distance)" ) ) .width( 4 ) @@ -529,7 +547,7 @@ void safemode::add_rule( const std::string &rule_in, const Creature::Attitude at const rule_state state_in ) { character_rules.push_back( rules_class( rule_in, true, ( state_in == RULE_WHITELISTED ), - attitude_in, proximity_in, HOSTILE ) ); + attitude_in, proximity_in, HOSTILE_SPOTTED ) ); create_rules(); if( !get_option( "SAFEMODE" ) && @@ -584,43 +602,52 @@ void safemode::add_rules( const std::vector &rules_in ) //if a specific monster is being added, all the rules need to be checked now //may have some performance issues since exclusion needs to check all monsters also for( auto &rule : rules_in ) { - if( rule.category == SOUND ) { - set_rule( rule, rule.rule, RULE_WHITELISTED ); - } else if( rule.category == HOSTILE ) { - if( !rule.whitelist ) { - //Check include patterns against all monster mtypes - for( const auto &mtype : MonsterGenerator::generator().get_all_mtypes() ) { - set_rule( rule, mtype.nname(), RULE_BLACKLISTED ); - } - } else { - //exclude monsters from the existing mapping - for( const auto &safemode_rule : safemode_rules_hostile ) { - set_rule( rule, safemode_rule.first, RULE_WHITELISTED ); + switch( rule.category ) { + case HOSTILE_SPOTTED: + if( !rule.whitelist ) { + //Check include patterns against all monster mtypes + for( const auto &mtype : MonsterGenerator::generator().get_all_mtypes() ) { + set_rule( rule, mtype.nname(), RULE_BLACKLISTED ); + } + } else { + //exclude monsters from the existing mapping + for( const auto &safemode_rule : safemode_rules_hostile ) { + set_rule( rule, safemode_rule.first, RULE_WHITELISTED ); + } } - } + break; + case SOUND: + set_rule( rule, rule.rule, rule.whitelist ? RULE_WHITELISTED : RULE_BLACKLISTED ); + break; + default: + break; } } } void safemode::set_rule( const rules_class &rule_in, const std::string &name_in, rule_state rs_in ) { - if( rule_in.category == HOSTILE ) { - static std::vector attitude_any = { {Creature::A_HOSTILE, Creature::A_NEUTRAL, Creature::A_FRIENDLY} }; - - if( !rule_in.rule.empty() && rule_in.active && wildcard_match( name_in, rule_in.rule ) ) { - if( rule_in.attitude == Creature::A_ANY ) { - for( auto &att : attitude_any ) { - safemode_rules_hostile[name_in][att] = rule_state_class( rs_in, rule_in.proximity, HOSTILE ); + static std::vector attitude_any = { {Creature::A_HOSTILE, Creature::A_NEUTRAL, Creature::A_FRIENDLY} }; + switch( rule_in.category ) { + case HOSTILE_SPOTTED: + if( !rule_in.rule.empty() && rule_in.active && wildcard_match( name_in, rule_in.rule ) ) { + if( rule_in.attitude == Creature::A_ANY ) { + for( auto &att : attitude_any ) { + safemode_rules_hostile[name_in][att] = rule_state_class( rs_in, rule_in.proximity, + HOSTILE_SPOTTED ); + } + } else { + safemode_rules_hostile[name_in][rule_in.attitude] = rule_state_class( rs_in, rule_in.proximity, + HOSTILE_SPOTTED ); } - } else { - safemode_rules_hostile[name_in][rule_in.attitude] = rule_state_class( rs_in, rule_in.proximity, - HOSTILE ); } - } - } else if( rule_in.category == SOUND ) { - safemode_rules_sound[name_in] = rule_state_class( rs_in, rule_in.proximity, SOUND ); + break; + case SOUND: + safemode_rules_sound.push_back( rule_in ); + break; + default: + break; } - } rule_state safemode::check_monster( const std::string &creature_name_in, @@ -643,17 +670,21 @@ rule_state safemode::check_monster( const std::string &creature_name_in, return RULE_NONE; } -bool safemode::check_sound( const std::string &sound_name_in, - const int proximity_in ) const +bool safemode::is_sound_safe( const std::string &sound_name_in, + const int proximity_in ) const { - for( auto &rule : safemode_rules_sound ) { - if( wildcard_match( sound_name_in, rule.first ) ) { - if( proximity_in >= rule.second.proximity ) { - return true; + bool sound_safe = false; + for( int i = 0; i < safemode_rules_sound.size(); i++ ) { + if( wildcard_match( sound_name_in, safemode_rules_sound[i].rule ) && + proximity_in >= safemode_rules_sound[i].proximity ) { + if( safemode_rules_sound[i].whitelist ) { + sound_safe = true; + } else { + return false; } } }; - return false; + return sound_safe; } void safemode::clear_character_rules() @@ -764,7 +795,7 @@ void safemode::deserialize( JsonIn &jsin ) const Creature::Attitude attitude = static_cast( jo.get_int( "attitude" ) ); const int proximity = jo.get_int( "proximity" ); const Categories cat = jo.has_member( "category" ) ? static_cast - ( jo.get_int( "category" ) ) : HOSTILE; + ( jo.get_int( "category" ) ) : HOSTILE_SPOTTED; temp_rules.push_back( rules_class( rule, active, whitelist, attitude, proximity, cat ) diff --git a/src/safemode_ui.h b/src/safemode_ui.h index fdb169a9f8eb1..c50f149cbce83 100644 --- a/src/safemode_ui.h +++ b/src/safemode_ui.h @@ -23,7 +23,7 @@ class safemode }; enum Categories : int { - HOSTILE, + HOSTILE_SPOTTED, SOUND }; @@ -38,7 +38,7 @@ class safemode Categories category; rules_class() : active( false ), whitelist( false ), attitude( Creature::A_HOSTILE ), - proximity( 0 ), category( Categories::HOSTILE ) {} + proximity( 0 ), category( Categories::HOSTILE_SPOTTED ) {} rules_class( const std::string &rule_in, bool active_in, bool whitelist_in, Creature::Attitude attitude_in, int proximity_in, Categories cat ) : rule( rule_in ), active( active_in ), whitelist( whitelist_in ), @@ -64,7 +64,7 @@ class safemode * safemode_rules[ 'creature name' ][ 'attitude' ].rule_state_class('rule_state', 'proximity') */ std::unordered_map < std::string, std::array < rule_state_class, 3 > > safemode_rules_hostile; - std::unordered_map < std::string, rule_state_class > safemode_rules_sound; + std::vector < rules_class > safemode_rules_sound; /** * current rules for global and character tab @@ -94,7 +94,7 @@ class safemode rule_state check_monster( const std::string &creature_name_in, Creature::Attitude attitude_in, int proximity_in ) const; - bool check_sound( const std::string &sound_name_in, const int proximity_in ) const; + bool is_sound_safe( const std::string &sound_name_in, const int proximity_in ) const; std::string npc_type_name(); diff --git a/src/sounds.cpp b/src/sounds.cpp index 606ab9df4059d..2985a57f5b2ca 100644 --- a/src/sounds.cpp +++ b/src/sounds.cpp @@ -433,7 +433,7 @@ void sounds::process_sound_markers( player *p ) // don't print our own noise or things without descriptions if( !sound.ambient && ( pos != p->pos() ) && !g->m.pl_sees( pos, distance_to_sound ) ) { if( !p->activity.is_distraction_ignored( distraction_type::noise ) && - !get_safemode().check_sound( sound.description, distance_to_sound ) ) { + !get_safemode().is_sound_safe( sound.description, distance_to_sound ) ) { const std::string query = string_format( _( "Heard %s!" ), description ); g->cancel_activity_or_ignore_query( distraction_type::noise, query ); } From ae9bd56de2214017031ec273d741aa9892603a05 Mon Sep 17 00:00:00 2001 From: Ramza13 <52087122+Ramza13@users.noreply.github.com> Date: Sat, 11 Jan 2020 10:09:08 -0500 Subject: [PATCH 6/8] appease build gods --- src/safemode_ui.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/safemode_ui.cpp b/src/safemode_ui.cpp index ef496863704bb..7d6cd37667441 100644 --- a/src/safemode_ui.cpp +++ b/src/safemode_ui.cpp @@ -674,7 +674,7 @@ bool safemode::is_sound_safe( const std::string &sound_name_in, const int proximity_in ) const { bool sound_safe = false; - for( int i = 0; i < safemode_rules_sound.size(); i++ ) { + for( unsigned int i = 0; i < safemode_rules_sound.size(); i++ ) { if( wildcard_match( sound_name_in, safemode_rules_sound[i].rule ) && proximity_in >= safemode_rules_sound[i].proximity ) { if( safemode_rules_sound[i].whitelist ) { From 4c3996b4ff99298bfd44f59acb2fb1ce7ea82fa9 Mon Sep 17 00:00:00 2001 From: Ramza13 <52087122+Ramza13@users.noreply.github.com> Date: Sat, 11 Jan 2020 13:15:01 -0500 Subject: [PATCH 7/8] astyle --- src/safemode_ui.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/safemode_ui.cpp b/src/safemode_ui.cpp index 7d6cd37667441..c61d9e58e9842 100644 --- a/src/safemode_ui.cpp +++ b/src/safemode_ui.cpp @@ -130,7 +130,8 @@ void safemode::show( const std::string &custom_name_in, bool is_safemode_in ) mvwprintz( w_header, point( column_pos[COLUMN_ATTITUDE] + 2, 3 ), c_white, _( "Attitude" ) ); mvwprintz( w_header, point( column_pos[COLUMN_PROXIMITY] + 2, 3 ), c_white, _( "Dist" ) ); mvwprintz( w_header, point( column_pos[COLUMN_WHITE_BLACKLIST] + 2, 3 ), c_white, _( "B/W" ) ); - mvwprintz( w_header, point( column_pos[COLUMN_CATEGORY] + 2, 3 ), c_white, pgettext( "category", "Cat" ) ); + mvwprintz( w_header, point( column_pos[COLUMN_CATEGORY] + 2, 3 ), c_white, pgettext( "category", + "Cat" ) ); wrefresh( w_header ); From 973182677168d4902e1affad09d342fa279d0fdd Mon Sep 17 00:00:00 2001 From: ZhilkinSerg Date: Thu, 23 Jan 2020 21:49:02 +0300 Subject: [PATCH 8/8] Astyle --- src/safemode_ui.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/safemode_ui.cpp b/src/safemode_ui.cpp index d8c32ce19d819..0041d5c17fa85 100644 --- a/src/safemode_ui.cpp +++ b/src/safemode_ui.cpp @@ -136,8 +136,8 @@ void safemode::show( const std::string &custom_name_in, bool is_safemode_in ) mvwprintz( w_header, point( column_pos[COLUMN_PROXIMITY] + 2, 3 ), c_white, _( "Dist" ) ); mvwprintz( w_header, point( column_pos[COLUMN_WHITE_BLACKLIST] + 2, 3 ), c_white, _( "B/W" ) ); mvwprintz( w_header, point( column_pos[COLUMN_CATEGORY] + 2, 3 ), c_white, pgettext( "category", - "Cat" ) ); - + "Cat" ) ); + wrefresh( w_header ); }; @@ -280,9 +280,9 @@ void safemode::show( const std::string &custom_name_in, bool is_safemode_in ) } } else if( action == "ADD_DEFAULT_RULESET" ) { changes_made = true; - current_tab.push_back( rules_class( "*", true, false, Creature::A_HOSTILE, - get_option( "SAFEMODEPROXIMITY" ) - , HOSTILE_SPOTTED ) ); + current_tab.push_back( rules_class( "*", true, false, Creature::A_HOSTILE, + get_option( "SAFEMODEPROXIMITY" ) + , HOSTILE_SPOTTED ) ); current_tab.push_back( rules_class( "*", true, true, Creature::A_HOSTILE, 5, SOUND ) ); line = current_tab.size() - 1; } else if( action == "ADD_RULE" ) {