From 3ee10f45f9bc4eb4f48e1ac87c274d7e23b68949 Mon Sep 17 00:00:00 2001 From: wwkk222208 <373132166@qq.com> Date: Thu, 16 Dec 2021 13:42:03 +0800 Subject: [PATCH] Allow players to decide whether magazines are hidden --- src/game.cpp | 27 +++++++++++++++++++++------ src/inventory_ui.cpp | 5 +++++ src/item.cpp | 16 ++++++++++++++++ src/item.h | 3 +++ src/item_contents.cpp | 36 ++++++++++++++++++++++++++++++++++++ src/item_contents.h | 4 ++++ 6 files changed, 85 insertions(+), 6 deletions(-) diff --git a/src/game.cpp b/src/game.cpp index da060becf69c9..b020a8a9e0129 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -1617,18 +1617,29 @@ static hint_rating rate_action_collapse( const item &it ) } } return hint_rating::cant; + } else if( it.is_gun() ) { + for( const item_pocket *magazine_well : it.get_all_magazine_wells().value() ) { + if( !magazine_well->settings.is_collapsed() ) { + return hint_rating::good; + } + } } return hint_rating::cant; } static hint_rating rate_action_expand( const item &it ) { - if( !it.is_container() ) { - return hint_rating::cant; - } - for( const item_pocket *pocket : it.get_all_contained_pockets().value() ) { - if( pocket->settings.is_collapsed() ) { - return hint_rating::good; + if( it.is_container() ) { + for( const item_pocket *pocket : it.get_all_contained_pockets().value() ) { + if( pocket->settings.is_collapsed() ) { + return hint_rating::good; + } + } + } else if( it.is_gun() ) { + for( const item_pocket *magazine_well : it.get_all_magazine_wells().value() ) { + if( magazine_well->settings.is_collapsed() ) { + return hint_rating::good; + } } } return hint_rating::cant; @@ -2036,6 +2047,10 @@ int game::inventory_item_menu( item_location locThisItem, for( item_pocket *pocket : oThisItem.get_all_contained_pockets().value() ) { pocket->settings.set_collapse( cMenu == '>' ); } + } else if( oThisItem.is_gun() ) { + for( item_pocket *magazine_well : oThisItem.get_all_magazine_wells().value() ) { + magazine_well->settings.set_collapse( cMenu == '>' ); + } } break; default: diff --git a/src/inventory_ui.cpp b/src/inventory_ui.cpp index b9ec030a1daea..ef4848fb1575b 100644 --- a/src/inventory_ui.cpp +++ b/src/inventory_ui.cpp @@ -797,6 +797,11 @@ void inventory_column::set_collapsed( inventory_entry &entry, const bool collaps pocket->settings.set_collapse( collapse ); collapsed = true; } + } else if( loc.get_item()->is_gun() ) { + for( item_pocket *magazine_well : loc->get_all_magazine_wells().value() ) { + magazine_well->settings.set_collapse( collapse ); + collapsed = true; + } } } diff --git a/src/item.cpp b/src/item.cpp index d4736e8287545..d22a40087e7e4 100644 --- a/src/item.cpp +++ b/src/item.cpp @@ -316,6 +316,12 @@ item::item( const itype *type, time_point turn, int qty ) : type( type ), bday( } } + if( is_gun() ) { + for( item_pocket *pocket : get_all_magazine_wells().value() ) { + pocket->settings.set_collapse( true ); + } + } + if( has_flag( flag_NANOFAB_TEMPLATE ) ) { itype_id nanofab_recipe = item_group::item_from( type->nanofab_template_group ).typeId(); @@ -8128,6 +8134,16 @@ ret_val> item::get_all_contained_pockets() return contents.get_all_contained_pockets(); } +ret_val> item::get_all_magazine_wells() const +{ + return contents.get_all_magazine_wells(); +} + +ret_val> item::get_all_magazine_wells() +{ + return contents.get_all_magazine_wells(); +} + item_pocket *item::contained_where( const item &contained ) { return contents.contained_where( contained ); diff --git a/src/item.h b/src/item.h index cd5efaa7272cb..a20d905693340 100644 --- a/src/item.h +++ b/src/item.h @@ -773,6 +773,9 @@ class item : public visitable ret_val> get_all_contained_pockets() const; ret_val> get_all_contained_pockets(); + // gets all magazine well in this item + ret_val> get_all_magazine_wells() const; + ret_val> get_all_magazine_wells(); /** * Updates the pockets of this item to be correct based on the mods that are installed. * Pockets which are modified that contain an item will be spilled diff --git a/src/item_contents.cpp b/src/item_contents.cpp index c1b0247caa942..77e76181a6f5c 100644 --- a/src/item_contents.cpp +++ b/src/item_contents.cpp @@ -1434,6 +1434,42 @@ ret_val> item_contents::get_all_contained_pockets() } } +ret_val> item_contents::get_all_magazine_wells() const +{ + std::vector magazine_wells; + bool found = false; + + for( const item_pocket &magazine_well : contents ) { + if( magazine_well.is_type( item_pocket::pocket_type::MAGAZINE_WELL ) ) { + found = true; + magazine_wells.push_back( &magazine_well ); + } + } + if( found ) { + return ret_val>::make_success( magazine_wells ); + } else { + return ret_val>::make_failure( magazine_wells ); + } +} + +ret_val> item_contents::get_all_magazine_wells() +{ + std::vector magazine_wells; + bool found = false; + + for( item_pocket &magazine_well : contents ) { + if( magazine_well.is_type( item_pocket::pocket_type::MAGAZINE_WELL ) ) { + found = true; + magazine_wells.push_back( &magazine_well ); + } + } + if( found ) { + return ret_val>::make_success( magazine_wells ); + } else { + return ret_val>::make_failure( magazine_wells ); + } +} + std::vector item_contents::get_added_pockets() const { std::vector items_added; diff --git a/src/item_contents.h b/src/item_contents.h index fd7785c1daa37..74ff783011c27 100644 --- a/src/item_contents.h +++ b/src/item_contents.h @@ -159,6 +159,10 @@ class item_contents ret_val> get_all_contained_pockets() const; ret_val> get_all_contained_pockets(); + // gets all magazine well in this item + ret_val> get_all_magazine_wells() const; + ret_val> get_all_magazine_wells(); + // called when adding an item as pockets // to a molle item void add_pocket( const item &pocket );