Skip to content

Commit

Permalink
Merge pull request CleverRaven#68118 from Brambor/view-byproduct
Browse files Browse the repository at this point in the history
view recipe: fix nested and or more results
  • Loading branch information
Rivet-the-Zombie authored Sep 13, 2023
2 parents f12487c + 2a3d4f1 commit 0997bfc
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 28 deletions.
43 changes: 31 additions & 12 deletions src/activity_handlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2689,27 +2689,46 @@ void activity_handlers::view_recipe_do_turn( player_activity *act, Character *yo

recipe_id id( act->name );
std::string itname;
if( act->index == 0 ) {
// act->name is itype_id
itype_id it( act->name );
itname = it->nname( 1U );
} else {
const inventory &inven = you->crafting_inventory();
const std::vector<npc *> &helpers = you->get_crafting_helpers();
if( act->index != 0 ) {
// act->name is recipe_id
itname = id->result_name();
if( !you->get_available_recipes( inven, &helpers ).contains( &id.obj() ) ) {
add_msg( m_info, _( "You don't know how to craft the %s!" ), itname );
return;
}
you->craft( std::nullopt, id );
return;
}
// act->name is itype_id
itype_id item( act->name );
itname = item->nname( 1U );

bool is_byproduct = false; // product or byproduct
bool can_craft = false;
// Does a recipe for the item exist?
for( auto it = recipe_dict.begin(); it != recipe_dict.end(); ++it ) {
const recipe &r = ( *it ).second;
if( !r.obsolete && ( item == r.result() || r.in_byproducts( item ) ) ) {
is_byproduct = true;
// If if exists, do I know it?
if( you->get_available_recipes( inven, &helpers ).contains( &r ) ) {
can_craft = true;
break;
}
}
}
if( id.is_null() || !id.is_valid() ) {
if( !is_byproduct ) {
add_msg( m_info, _( "You wonder if it's even possible to craft the %s…" ), itname );
return;
}

const inventory &inven = you->crafting_inventory();
const std::vector<npc *> &helpers = you->get_crafting_helpers();
if( !you->get_available_recipes( inven, &helpers ).contains( &id.obj() ) ) {
} else if( !can_craft ) {
add_msg( m_info, _( "You don't know how to craft the %s!" ), itname );
return;
}

you->craft( std::nullopt, id );
std::string filterstring = string_format( "r:%s", itname );
you->craft( std::nullopt, recipe_id(), filterstring );
}

void activity_handlers::move_loot_do_turn( player_activity *act, Character *you )
Expand Down
2 changes: 1 addition & 1 deletion src/character.h
Original file line number Diff line number Diff line change
Expand Up @@ -3424,7 +3424,7 @@ class Character : public Creature, public visitable
* @param goto_recipe the recipe to display initially. A null recipe_id opens the default crafting screen.
*/
void craft( const std::optional<tripoint> &loc = std::nullopt,
const recipe_id &goto_recipe = recipe_id() );
const recipe_id &goto_recipe = recipe_id(), const std::string &filterstring = "" );
void recraft( const std::optional<tripoint> &loc = std::nullopt );
void long_craft( const std::optional<tripoint> &loc = std::nullopt,
const recipe_id &goto_recipe = recipe_id() );
Expand Down
5 changes: 3 additions & 2 deletions src/crafting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -352,10 +352,11 @@ bool Character::has_morale_to_craft() const
return get_morale_level() >= -50;
}

void Character::craft( const std::optional<tripoint> &loc, const recipe_id &goto_recipe )
void Character::craft( const std::optional<tripoint> &loc, const recipe_id &goto_recipe,
const std::string &filterstring )
{
int batch_size = 0;
const recipe *rec = select_crafting_recipe( batch_size, goto_recipe, *this );
const recipe *rec = select_crafting_recipe( batch_size, goto_recipe, *this, filterstring );
if( rec ) {
std::string reason;
if( is_npc() && !rec->npc_can_craft( reason ) ) {
Expand Down
3 changes: 1 addition & 2 deletions src/crafting_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1125,7 +1125,7 @@ static bool selection_ok( const std::vector<const recipe *> &list, const int cur
}

const recipe *select_crafting_recipe( int &batch_size_out, const recipe_id &goto_recipe,
Character &crafter )
Character &crafter, std::string filterstring )
{
recipe_result_info_cache result_info( crafter );
recipe_info_cache r_info_cache;
Expand Down Expand Up @@ -1246,7 +1246,6 @@ const recipe *select_crafting_recipe( int &batch_size_out, const recipe_id &goto

const inventory &crafting_inv = crafter.crafting_inventory();
const std::vector<npc *> helpers = crafter.get_crafting_helpers();
std::string filterstring;

const recipe_subset &available_recipes = crafter.get_available_recipes( crafting_inv,
&helpers );
Expand Down
2 changes: 1 addition & 1 deletion src/crafting_gui.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class JsonObject;
class recipe;

const recipe *select_crafting_recipe( int &batch_size_out, const recipe_id &goto_recipe,
Character &crafter );
Character &crafter, std::string filterstring = "" );

void load_recipe_category( const JsonObject &jsobj );
void reset_recipe_categories();
Expand Down
39 changes: 29 additions & 10 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1808,19 +1808,38 @@ static hint_rating rate_action_disassemble( avatar &you, const item &it )

static hint_rating rate_action_view_recipe( avatar &you, const item &it )
{
const recipe &craft_recipe = it.is_craft() ? it.get_making() :
recipe_dictionary::get_craft( it.typeId() );
if( craft_recipe.is_null() || !craft_recipe.ident().is_valid() ) {
return hint_rating::cant;
}
const inventory &inven = you.crafting_inventory();
const std::vector<npc *> helpers = you.get_crafting_helpers();
if( you.get_available_recipes( inven, &helpers ).contains( &craft_recipe ) ) {
return hint_rating::good;
} else if( craft_recipe.ident().is_valid() ) {
return hint_rating::iffy;
if( it.is_craft() ) {
const recipe &craft_recipe = it.get_making();
if( craft_recipe.is_null() || !craft_recipe.ident().is_valid() ) {
return hint_rating::cant;
} else if( you.get_available_recipes( inven, &helpers ).contains( &craft_recipe ) ) {
return hint_rating::good;
}
} else {
itype_id item = it.typeId();
bool is_byproduct = false; // product or byproduct
bool can_craft = false;
// Does a recipe for the item exist?
for( auto it = recipe_dict.begin(); it != recipe_dict.end(); ++it ) {
const recipe &r = ( *it ).second;
if( !r.obsolete && ( item == r.result() || r.in_byproducts( item ) ) ) {
is_byproduct = true;
// If if exists, do I know it?
if( you.get_available_recipes( inven, &helpers ).contains( &r ) ) {
can_craft = true;
break;
}
}
}
if( !is_byproduct ) {
return hint_rating::cant;
} else if( can_craft ) {
return hint_rating::good;
}
}
return hint_rating::cant;
return hint_rating::iffy;
}

static hint_rating rate_action_eat( const avatar &you, const item &it )
Expand Down

0 comments on commit 0997bfc

Please sign in to comment.