diff --git a/src/character.h b/src/character.h index a5ef82c25026c..a51ca935a1402 100644 --- a/src/character.h +++ b/src/character.h @@ -3517,7 +3517,7 @@ class Character : public Creature, public visitable /** * Return all available recipes for any member of `this` crafter's group. Using `this` inventory. */ - recipe_subset get_group_available_recipes() const; + recipe_subset &get_group_available_recipes() const; /** * Returns the set of book types in crafting_inv that provide the * given recipe. @@ -4123,6 +4123,11 @@ class Character : public Creature, public visitable // a cache of all active enchantment values. // is recalculated every turn in Character::recalculate_enchantment_cache pimpl enchantment_cache; + + private: + /* cached recipes, which are invalidated if the turn changes */ + mutable time_point cached_recipe_turn; + pimpl cached_recipe_subset; }; Character &get_player_character(); diff --git a/src/character_crafting.cpp b/src/character_crafting.cpp index 4c0cebbedea6d..e89148a805cf1 100644 --- a/src/character_crafting.cpp +++ b/src/character_crafting.cpp @@ -6,6 +6,7 @@ #include #include +#include "cached_options.h" #include "character.h" #include "inventory.h" #include "item.h" @@ -181,13 +182,20 @@ recipe_subset Character::get_available_recipes( const inventory &crafting_inv, return res; } -recipe_subset Character::get_group_available_recipes() const +recipe_subset &Character::get_group_available_recipes() const { - recipe_subset res; + if( !test_mode && calendar::turn == cached_recipe_turn && cached_recipe_subset->size() > 0 ) { + return *cached_recipe_subset; + } + + cached_recipe_turn = calendar::turn; + cached_recipe_subset->clear(); + for( const Character *guy : get_crafting_group() ) { - res.include( guy->get_available_recipes( crafting_inventory() ) ); + cached_recipe_subset->include( guy->get_available_recipes( crafting_inventory() ) ); } - return res; + + return *cached_recipe_subset; } std::set Character::get_books_for_recipe( const inventory &crafting_inv,