diff --git a/src/character.cpp b/src/character.cpp index 55665c6aae775..9cd7a3ff4f49b 100644 --- a/src/character.cpp +++ b/src/character.cpp @@ -485,7 +485,7 @@ Character::Character() : move_mode = move_mode_id( "walk" ); next_expected_position = cata::nullopt; - cached_time = calendar::before_time_starts; + crafting_cache.time = calendar::before_time_starts; } // *INDENT-ON* diff --git a/src/character.h b/src/character.h index 02e563b0d3fa8..19766afd20790 100644 --- a/src/character.h +++ b/src/character.h @@ -2922,11 +2922,14 @@ class Character : public Creature, public visitable struct weighted_int_list melee_miss_reasons; - /* crafting inventory cached time */ - mutable time_point cached_time; - mutable int cached_moves; - mutable tripoint cached_position; - mutable pimpl cached_crafting_inventory; + struct crafting_cache_type { + time_point time; + int moves; + tripoint position; + int radius; + pimpl crafting_inventory; + }; + mutable crafting_cache_type crafting_cache; protected: /** Subset of learned recipes. Needs to be mutable for lazy initialization. */ diff --git a/src/crafting.cpp b/src/crafting.cpp index bf066b71ca49a..b6b2a39e5f5f7 100644 --- a/src/crafting.cpp +++ b/src/crafting.cpp @@ -541,17 +541,15 @@ const inventory &Character::crafting_inventory( const tripoint &src_pos, int rad if( src_pos == tripoint_zero ) { inv_pos = pos(); } - static int radius_mem = radius; - if( cached_moves == moves - && radius_mem == radius - && cached_time == calendar::turn - && cached_position == inv_pos ) { - return *cached_crafting_inventory; - } - radius_mem = radius; - cached_crafting_inventory->clear(); + if( moves == crafting_cache.moves + && radius == crafting_cache.radius + && calendar::turn == crafting_cache.time + && inv_pos == crafting_cache.position) { + return *crafting_cache.crafting_inventory; + } + crafting_cache.crafting_inventory->clear(); if( radius >= 0 ) { - cached_crafting_inventory->form_from_map( inv_pos, radius, this, false, clear_path ); + crafting_cache.crafting_inventory->form_from_map( inv_pos, radius, this, false, clear_path ); } for( const item_location &it : const_cast( this )->all_items_loc() ) { @@ -559,31 +557,32 @@ const inventory &Character::crafting_inventory( const tripoint &src_pos, int rad if( !it->contents.empty_container() ) { continue; } - cached_crafting_inventory->add_item( *it ); + crafting_cache.crafting_inventory->add_item( *it ); } for( const bionic &bio : *my_bionics ) { const bionic_data &bio_data = bio.info(); if( ( !bio_data.activated || bio.powered ) && !bio_data.fake_item.is_empty() ) { - *cached_crafting_inventory += item( bio.info().fake_item, + *crafting_cache.crafting_inventory += item( bio.info().fake_item, calendar::turn, units::to_kilojoule( get_power_level() ) ); } } if( has_trait( trait_BURROW ) ) { - *cached_crafting_inventory += item( "pickaxe", calendar::turn ); - *cached_crafting_inventory += item( "shovel", calendar::turn ); + *crafting_cache.crafting_inventory += item( "pickaxe", calendar::turn ); + *crafting_cache.crafting_inventory += item( "shovel", calendar::turn ); } - cached_moves = moves; - cached_time = calendar::turn; - cached_position = inv_pos; - return *cached_crafting_inventory; + crafting_cache.moves = moves; + crafting_cache.time = calendar::turn; + crafting_cache.position = inv_pos; + crafting_cache.radius = radius; + return *crafting_cache.crafting_inventory; } void Character::invalidate_crafting_inventory() { - cached_time = calendar::before_time_starts; + crafting_cache.time = calendar::before_time_starts; } void Character::make_craft( const recipe_id &id_to_make, int batch_size,