From af50aa38304a1ee7bbcd0cd7d2838f7ada0dfcca Mon Sep 17 00:00:00 2001 From: cakepie Date: Fri, 6 Jan 2023 20:53:33 +0800 Subject: [PATCH 1/3] fix flawed can_crush_frozen_liquid() logic --- src/character.cpp | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/character.cpp b/src/character.cpp index 1ceba436a6347..6164eac7f2bb6 100644 --- a/src/character.cpp +++ b/src/character.cpp @@ -9056,24 +9056,26 @@ ret_val Character::can_crush_frozen_liquid( item_location loc ) if( has_quality( qual_DRILL ) ) { enough_quality = true; } else if( has_quality( qual_HAMMER ) && has_quality( qual_SCREW ) ) { - std::list screw_tools; - std::vector hammer_tools; + int num_hammer_tools = 0, num_screw_tools = 0, num_both_tools = 0; for( item_location &tool : const_cast( this )->all_items_loc() ) { + bool can_hammer = false, can_screw = false; if( tool->has_quality( qual_HAMMER ) ) { - hammer_tools.emplace_back( tool ); + can_hammer = true; + num_hammer_tools++; } if( tool->has_quality( qual_SCREW ) ) { - screw_tools.emplace_back( tool ); + can_screw = true; + num_screw_tools++; } - } - if( !screw_tools.empty() && !hammer_tools.empty() ) { - for( item_location &hammer : hammer_tools ) { - screw_tools.remove( hammer ); - } - if( !screw_tools.empty() ) { - enough_quality = true; + if( can_hammer && can_screw ) { + num_both_tools++; } } + if( num_hammer_tools > 0 && num_screw_tools > 0 && + // Only one tool that fulfils both requirements: can't hammer itself + !( num_hammer_tools == 1 && num_screw_tools == 1 && num_both_tools == 1 ) ) { + enough_quality = true; + } } success = enough_quality; } From 070aa549e9ed983da809bf7d581de95b71901074 Mon Sep 17 00:00:00 2001 From: cakepie Date: Sat, 7 Jan 2023 11:28:17 +0800 Subject: [PATCH 2/3] fix flawed fix for flawed can_crush_frozen_liquid() logic --- src/character.cpp | 4 ++-- src/item.cpp | 22 +++++++++++++++++++++- src/item.h | 15 +++++++++++++++ 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/character.cpp b/src/character.cpp index 6164eac7f2bb6..2904a82675fce 100644 --- a/src/character.cpp +++ b/src/character.cpp @@ -9059,11 +9059,11 @@ ret_val Character::can_crush_frozen_liquid( item_location loc ) int num_hammer_tools = 0, num_screw_tools = 0, num_both_tools = 0; for( item_location &tool : const_cast( this )->all_items_loc() ) { bool can_hammer = false, can_screw = false; - if( tool->has_quality( qual_HAMMER ) ) { + if( tool->has_quality_nonrecursive( qual_HAMMER ) ) { can_hammer = true; num_hammer_tools++; } - if( tool->has_quality( qual_SCREW ) ) { + if( tool->has_quality_nonrecursive( qual_SCREW ) ) { can_screw = true; num_screw_tools++; } diff --git a/src/item.cpp b/src/item.cpp index a4234bc8c4c80..7916f094db0de 100644 --- a/src/item.cpp +++ b/src/item.cpp @@ -7374,7 +7374,12 @@ int64_t item::get_property_int64_t( const std::string &prop, int64_t def ) const return def; } -int item::get_quality( const quality_id &id, const bool strict_boiling ) const +bool item::has_quality_nonrecursive( const quality_id &qual, int level ) const +{ + return get_quality_nonrecursive( qual ) >= level; +} + +int item::get_quality_nonrecursive( const quality_id &id, const bool strict_boiling ) const { /** * EXCEPTION: Items with quality BOIL only count as such if they are empty. @@ -7403,6 +7408,21 @@ int item::get_quality( const quality_id &id, const bool strict_boiling ) const } } + return return_quality; +} + +int item::get_quality( const quality_id &id, const bool strict_boiling ) const +{ + /** + * EXCEPTION: Items with quality BOIL only count as such if they are empty. + */ + if( strict_boiling && id == qual_BOIL && !contents.empty_container() ) { + return INT_MIN; + } + + // First check the item itself + int return_quality = get_quality_nonrecursive( id, false ); + // If any contained item has a better quality, use that instead return_quality = std::max( return_quality, contents.best_quality( id ) ); diff --git a/src/item.h b/src/item.h index f3c6a61aafd8f..39095c195c2b3 100644 --- a/src/item.h +++ b/src/item.h @@ -933,9 +933,24 @@ class item : public visitable void add_rain_to_container( bool acid, int charges = 1 ); /*@}*/ + /** + * Returns true if this item itself is of at least quality level + * This version is non-recursive and does not check the item contents. + */ + bool has_quality_nonrecursive( const quality_id &qual, int level = 1 ) const; + + /** + * Return the level of a given quality the tool may have, or INT_MIN if it + * does not have that quality, or lacks enough charges to have that quality. + * This version is non-recursive and does not check the item contents. + * @param strict_boiling True if containers must be empty to have BOIL quality + */ + int get_quality_nonrecursive( const quality_id &id, bool strict_boiling = true ) const; + /** * Return the level of a given quality the tool may have, or INT_MIN if it * does not have that quality, or lacks enough charges to have that quality. + * This version is recursive and will check the item contents as well. * @param strict_boiling True if containers must be empty to have BOIL quality */ int get_quality( const quality_id &id, bool strict_boiling = true ) const; From 5c3a2c1652af997e58dbbc772fad637825c5d98b Mon Sep 17 00:00:00 2001 From: cakepie Date: Sat, 7 Jan 2023 20:24:03 +0800 Subject: [PATCH 3/3] pacify clang-tidy --- src/character.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/character.cpp b/src/character.cpp index 2904a82675fce..7e3e51d0f3245 100644 --- a/src/character.cpp +++ b/src/character.cpp @@ -9056,9 +9056,12 @@ ret_val Character::can_crush_frozen_liquid( item_location loc ) if( has_quality( qual_DRILL ) ) { enough_quality = true; } else if( has_quality( qual_HAMMER ) && has_quality( qual_SCREW ) ) { - int num_hammer_tools = 0, num_screw_tools = 0, num_both_tools = 0; + int num_hammer_tools = 0; + int num_screw_tools = 0; + int num_both_tools = 0; for( item_location &tool : const_cast( this )->all_items_loc() ) { - bool can_hammer = false, can_screw = false; + bool can_hammer = false; + bool can_screw = false; if( tool->has_quality_nonrecursive( qual_HAMMER ) ) { can_hammer = true; num_hammer_tools++;