Skip to content

Commit

Permalink
Merge pull request #77093 from sparr/item_pocket_descriptions
Browse files Browse the repository at this point in the history
Numbered pockets in pocket description
  • Loading branch information
Maleclypse authored Oct 19, 2024
2 parents 8ffe156 + f465744 commit 50204d5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
23 changes: 18 additions & 5 deletions src/item_contents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2644,6 +2644,7 @@ void item_contents::info( std::vector<iteminfo> &info, const iteminfo_query *par
std::vector<iteminfo> contents_info;
std::vector<item_pocket> found_pockets;
std::map<int, int> pocket_num; // index, amount
const bool describe_contents = parts->test( iteminfo_parts::DESCRIPTION_CONTENTS );
for( const item_pocket &pocket : contents ) {
if( pocket.is_type( pocket_type::CONTAINER ) ) {
bool found = false;
Expand All @@ -2652,14 +2653,17 @@ void item_contents::info( std::vector<iteminfo> &info, const iteminfo_query *par
if( found_pocket == pocket ) {
found = true;
pocket_num[idx]++;
break;
}
idx++;
}
if( !found ) {
found_pockets.push_back( pocket );
pocket_num[idx]++;
}
pocket.contents_info( contents_info, pocket_number++, contents.size() != 1 );
if( describe_contents ) {
pocket.contents_info( contents_info, pocket_number++, contents.size() != 1 );
}
}
}
if( parts->test( iteminfo_parts::DESCRIPTION_POCKETS ) ) {
Expand Down Expand Up @@ -2687,30 +2691,39 @@ void item_contents::info( std::vector<iteminfo> &info, const iteminfo_query *par
}

int idx = 0;
int pocket_number = 1;
for( const item_pocket &pocket : found_pockets ) {
if( pocket.is_forbidden() ) {
continue;
}
insert_separation_line( info );
// If there are multiple similar pockets, show their capacity as a set
if( pocket_num[idx] > 1 ) {
info.emplace_back( "DESCRIPTION", string_format( _( "<bold>%d pockets</bold> with capacity:" ),
pocket_num[idx] ) );
std::vector<int> pocket_numbers( pocket_num[idx] );
std::iota( pocket_numbers.begin(), pocket_numbers.end(), pocket_number );
std::string pocket_numbers_enumeration = enumerate_as_string( pocket_numbers.begin(),
pocket_numbers.end(), []( int n ) {
// std::to_string is not addressable, can't be passed directly
return std::to_string( n );
} );
info.emplace_back( "DESCRIPTION", string_format( _( "<bold>Pockets %s</bold>" ),
pocket_numbers_enumeration ) );
} else {
// If this is the only pocket the item has, label it "Total capacity"
// Otherwise, give it a generic "Pocket" heading (is one of several pockets)
bool only_one_pocket = ( found_pockets.size() == 1 && pocket_num[0] == 1 );
if( only_one_pocket ) {
info.emplace_back( "DESCRIPTION", _( "<bold>Total capacity</bold>:" ) );
} else {
info.emplace_back( "DESCRIPTION", _( "<bold>Pocket</bold> with capacity:" ) );
info.emplace_back( "DESCRIPTION", string_format( _( "<bold>Pocket %d</bold>" ), pocket_number ) );
}
}
pocket_number += pocket_num[idx];
idx++;
pocket.general_info( info, idx, false );
}
}
if( parts->test( iteminfo_parts::DESCRIPTION_CONTENTS ) ) {
if( describe_contents ) {
info.insert( info.end(), contents_info.begin(), contents_info.end() );
}
}
Expand Down
27 changes: 21 additions & 6 deletions tests/iteminfo_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2810,26 +2810,41 @@ TEST_CASE( "pocket_info_for_a_multi-pocket_item", "[iteminfo][pocket][multiple]"
{
clear_avatar();

item test_belt( "test_tool_belt" );
item test_belt( "test_tool_belt_pocket_mix" );
std::vector<iteminfo_parts> pockets = { iteminfo_parts::DESCRIPTION_POCKETS };

override_option opt_vol( "VOLUME_UNITS", "l" );
override_option opt_weight( "USE_METRIC_WEIGHTS", "kg" );
override_option opt_dist( "DISTANCE_UNITS", "metric" );

// When two pockets have the same attributes, they are combined with a heading like:
// When multiple pockets have the same attributes, they are combined headings like:
//
// 2 Pockets with capacity:
// Pockets 1, 2, and 3
// Volume: ... Weight: ...
//
// The "Total capacity" indicates the sum Volume/Weight capacity of all pockets.
CHECK( item_info_str( test_belt, pockets ) ==
"--\n"
"<color_c_white>Total capacity</color>:\n"
"Volume: <color_c_yellow>6.00</color> L Weight: <color_c_yellow>4.80</color> kg\n"
"Volume: <color_c_yellow>7.00</color> L Weight: <color_c_yellow>9.00</color> kg\n"
"--\n"
"<color_c_white>4 pockets</color> with capacity:\n"
"Volume: <color_c_yellow>1.50</color> L Weight: <color_c_yellow>1.20</color> kg\n"
"<color_c_white>Pocket 1</color>\n"
"Volume: <color_c_yellow>1.00</color> L Weight: <color_c_yellow>1.50</color> kg\n"
"Item length: <color_c_yellow>0</color> cm to <color_c_yellow>40</color> cm\n"
"Base moves to remove item: <color_c_yellow>100</color>\n"
"--\n"
"<color_c_white>Pockets 2 and 3</color>\n"
"Volume: <color_c_yellow>1.50</color> L Weight: <color_c_yellow>1.50</color> kg\n"
"Item length: <color_c_yellow>0</color> cm to <color_c_yellow>70</color> cm\n"
"Minimum item volume: <color_c_yellow>0.050</color> L\n"
"Base moves to remove item: <color_c_yellow>50</color>\n"
"This is a <color_c_cyan>holster</color>, it only holds <color_c_cyan>one item at a time</color>.\n"
"<color_c_white>Restrictions</color>:\n"
"* Item must clip onto a belt loop\n"
"* <color_c_white>or</color> Item must fit in a sheath\n"
"--\n"
"<color_c_white>Pockets 4, 5, and 6</color>\n"
"Volume: <color_c_yellow>1.00</color> L Weight: <color_c_yellow>1.50</color> kg\n"
"Item length: <color_c_yellow>0</color> cm to <color_c_yellow>70</color> cm\n"
"Minimum item volume: <color_c_yellow>0.050</color> L\n"
"Base moves to remove item: <color_c_yellow>50</color>\n"
Expand Down

0 comments on commit 50204d5

Please sign in to comment.