Skip to content

Commit

Permalink
Refactor item 'you could craft' section
Browse files Browse the repository at this point in the history
Some failed test cases in `tests/iteminfo_test.cpp` made me realize my
previous commit should have kept the `DESCRIPTION_APPLICABLE_RECIPES`
check, since that is a flag indicating whether the section is applicable
to that kind of item, and should be shown at all.

Doing that check first made it clear that the preceding block,
initializing `tid` and `known_recipes`, could move inside the `if` and
avoid being needlessly calculated in some cases.

Due to indentation, this looks like a larger diff than it really is, but
it's all the same logic as before - if no recipes are known, the section
will display "You don't know anything you could craft with it."

Verified that all tests in `make check` are passing now, and confirmed
in-game that messages are shown as expected.
  • Loading branch information
wapcaplet committed Feb 3, 2020
1 parent e0fd2e7 commit f45a1e7
Showing 1 changed file with 34 additions and 31 deletions.
65 changes: 34 additions & 31 deletions src/item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3295,43 +3295,46 @@ void item::final_info( std::vector<iteminfo> &info, const iteminfo_query *parts,
}

// list recipes you could use it in
itype_id tid;
if( contents.empty() ) { // use this item
tid = typeId();
} else { // use the contained item
tid = contents.front().typeId();
}
const std::set<const recipe *> &known_recipes = g->u.get_learned_recipes().of_component( tid );
if( known_recipes.empty() ) {
insert_separation_line( info );
info.push_back( iteminfo( "DESCRIPTION",
_( "You don't know anything you could craft with it." ) ) );
} else if( parts->test( iteminfo_parts::DESCRIPTION_APPLICABLE_RECIPES ) ) {
const inventory &inv = g->u.crafting_inventory();
if( parts->test( iteminfo_parts::DESCRIPTION_APPLICABLE_RECIPES ) ) {
itype_id tid;
if( contents.empty() ) { // use this item
tid = typeId();
} else { // use the contained item
tid = contents.front().typeId();
}
const std::set<const recipe *> &known_recipes = g->u.get_learned_recipes().of_component( tid );

if( known_recipes.size() > 24 ) {
insert_separation_line( info );
info.push_back( iteminfo( "DESCRIPTION",
_( "You know dozens of things you could craft with it." ) ) );
} else if( known_recipes.size() > 12 ) {
if( known_recipes.empty() ) {
insert_separation_line( info );
info.push_back( iteminfo( "DESCRIPTION",
_( "You could use it to craft various other things." ) ) );
_( "You don't know anything you could craft with it." ) ) );
} else {
const std::string recipes = enumerate_as_string( known_recipes.begin(), known_recipes.end(),
[ &inv ]( const recipe * r ) {
if( r->deduped_requirements().can_make_with_inventory(
inv, r->get_component_filter() ) ) {
return r->result_name();
} else {
return string_format( "<dark>%s</dark>", r->result_name() );
}
} );
if( !recipes.empty() ) {
const inventory &inv = g->u.crafting_inventory();

if( known_recipes.size() > 24 ) {
insert_separation_line( info );
info.push_back( iteminfo( "DESCRIPTION",
string_format( _( "You could use it to craft: %s" ),
recipes ) ) );
_( "You know dozens of things you could craft with it." ) ) );
} else if( known_recipes.size() > 12 ) {
insert_separation_line( info );
info.push_back( iteminfo( "DESCRIPTION",
_( "You could use it to craft various other things." ) ) );
} else {
const std::string recipes = enumerate_as_string( known_recipes.begin(), known_recipes.end(),
[ &inv ]( const recipe * r ) {
if( r->deduped_requirements().can_make_with_inventory(
inv, r->get_component_filter() ) ) {
return r->result_name();
} else {
return string_format( "<dark>%s</dark>", r->result_name() );
}
} );
if( !recipes.empty() ) {
insert_separation_line( info );
info.push_back( iteminfo( "DESCRIPTION",
string_format( _( "You could use it to craft: %s" ),
recipes ) ) );
}
}
}
}
Expand Down

0 comments on commit f45a1e7

Please sign in to comment.