Skip to content

Commit

Permalink
use a struct to encapsulate the crafting inventory cache
Browse files Browse the repository at this point in the history
  • Loading branch information
eltank committed May 31, 2021
1 parent f46c907 commit dbc8e65
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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*

Expand Down
13 changes: 8 additions & 5 deletions src/character.h
Original file line number Diff line number Diff line change
Expand Up @@ -2922,11 +2922,14 @@ class Character : public Creature, public visitable

struct weighted_int_list<std::string> melee_miss_reasons;

/* crafting inventory cached time */
mutable time_point cached_time;
mutable int cached_moves;
mutable tripoint cached_position;
mutable pimpl<inventory> cached_crafting_inventory;
struct crafting_cache_type {
time_point time;
int moves;
tripoint position;
int radius;
pimpl<inventory> crafting_inventory;
};
mutable crafting_cache_type crafting_cache;

protected:
/** Subset of learned recipes. Needs to be mutable for lazy initialization. */
Expand Down
37 changes: 18 additions & 19 deletions src/crafting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -541,49 +541,48 @@ 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<Character *>( this )->all_items_loc() ) {
// can't craft with containers that have items in them
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,
Expand Down

0 comments on commit dbc8e65

Please sign in to comment.