Skip to content

Commit

Permalink
Aaalmost working
Browse files Browse the repository at this point in the history
  • Loading branch information
Coolthulhu committed Aug 14, 2022
1 parent 89db683 commit f9fd676
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 55 deletions.
14 changes: 3 additions & 11 deletions src/character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2479,9 +2479,9 @@ bool Character::i_add_or_drop( item &it, int qty )
return retval;
}

std::list<item *> Character::get_dependent_worn_items( const item &it ) const
std::list<const item *> Character::get_dependent_worn_items( const item &it ) const
{
std::list<item *> dependent;
std::list<const item *> dependent;
// Adds dependent worn items recursively
const std::function<void( const item &it )> add_dependent = [&]( const item & it ) {
for( const item &wit : worn ) {
Expand All @@ -2494,7 +2494,7 @@ std::list<item *> Character::get_dependent_worn_items( const item &it ) const
} );
if( iter == dependent.end() ) { // Not in the list yet
add_dependent( wit );
dependent.push_back( const_cast<item *>( & wit ) );
dependent.push_back( &wit );
}
}
};
Expand All @@ -2506,14 +2506,6 @@ std::list<item *> Character::get_dependent_worn_items( const item &it ) const
return dependent;
}

std::list<const item *> Character::get_dependent_worn_items( const item &it ) const
{
const std::list<item *> nonconst = const_cast<Character &>( *this ).get_dependent_worn_items( it );
std::list<const item *> ret;
std::copy( nonconst.begin(), nonconst.end(), ret.end() );
return ret;
}

void Character::drop( item_location loc, const tripoint &where )
{
item &oThisItem = *loc;
Expand Down
1 change: 0 additions & 1 deletion src/character.h
Original file line number Diff line number Diff line change
Expand Up @@ -1338,7 +1338,6 @@ class Character : public Creature, public visitable<Character>

void drop_invalid_inventory();
/** Returns all items that must be taken off before taking off this item */
std::list<item *> get_dependent_worn_items( const item &it ) const;
std::list<const item *> get_dependent_worn_items( const item &it ) const;
/** Drops an item to the specified location */
void drop( item_location loc, const tripoint &where );
Expand Down
86 changes: 46 additions & 40 deletions src/inventory_ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2162,7 +2162,8 @@ inventory_drop_selector::inventory_drop_selector( player &p,
ctxt.allow_text_entry = true;
#endif

rebuild();
caching_preset.set_implied_drops( implied_drops );
caching_preset.move_cost_sum = move_cost_sum;
}

inventory_drop_selector::~inventory_drop_selector() = default;
Expand Down Expand Up @@ -2194,7 +2195,7 @@ void inventory_drop_selector::process_selected( int count,

drop_locations inventory_drop_selector::execute()
{
rebuild( );
rebuild();

shared_ptr_fast<ui_adaptor> ui = create_or_get_ui_adaptor();

Expand Down Expand Up @@ -2362,19 +2363,20 @@ inventory_selector::stats inventory_drop_selector::get_raw_stats() const
void inventory_drop_selector::on_change( const inventory_entry &entry )
{
// TODO: Remove
implied_cache_dirty = true;
// implied_cache_dirty = true;
rebuild();
// TODO: Refresh only those which changed status
for( inventory_column *col : get_all_columns() ) {
col->clear_cell_cache();
}

for( auto &elem : get_all_columns() ) {
if( elem != &get_selection_column() ) {
elem->on_change( entry );
}
}
refresh_active_column();

// TODO: Refresh only those which changed status
for( inventory_column *col : get_all_columns() ) {
col->clear_cell_cache();
}
// Not using base::on_change, because we are not informing one of the column of changes
// TODO: Less ugly way of doing it
}
Expand Down Expand Up @@ -2413,6 +2415,8 @@ void inventory_drop_selector::rebuild()
return;
}

printf( "\nRebuilding\n" );

implied_cache_dirty = false;

const drop_locations &locations = convert_to_locations( u, dropping );
Expand All @@ -2426,14 +2430,16 @@ void inventory_drop_selector::rebuild()
move_cost_sum += ai.consumed_moves;
}

const decltype( implied_drops ) old_implied_drops = std::move( implied_drops );
implied_drops = decltype(implied_drops)();
implied_drops.clear();
// HACK! Manually clearing selection column because we want to populate it by hand
selection_column &sel_col = get_selection_column();
sel_col.clear();

std::unordered_map<const item *, const inventory_entry *> item_to_entry;
std::unordered_map<const item *, inventory_entry> item_to_entry;
auto all_columns = get_all_columns();
printf( "\n%lu columns\n", all_columns.size() );
printf( "%lu columns\n", all_columns.size() );
for( const inventory_column *ic : all_columns ) {
if( ic == &get_selection_column() ) {
if( ic == &sel_col ) {
continue;
}
const std::vector<inventory_entry *> entries_in_column =
Expand All @@ -2452,6 +2458,8 @@ void inventory_drop_selector::rebuild()
}
}

printf( "%s %d/%d\n", entry->any_item()->display_name().c_str(), matching_count, total_count );

if( matching_count > total_count ) {
debugmsg( "Dropped item count > total item count (somehow)" );
implied_cache_dirty = true;
Expand All @@ -2460,45 +2468,42 @@ void inventory_drop_selector::rebuild()
} else {
const item *it = &*entry->any_item();
implied_drops[it] = count_out_of( matching_count, total_count );
item_to_entry[it] = entry;
item_to_entry[it] = *entry;
}
}
}

for( const auto &maybe_removed : old_implied_drops ) {
const auto entry_in_new = implied_drops.find( maybe_removed.first );
if( entry_in_new == implied_drops.end() ||
entry_in_new->second.selected != maybe_removed.second.selected ) {
printf( "removed %s\n", maybe_removed.first->display_name().c_str() );
auto iter = item_to_entry.find( maybe_removed.first );
if( iter == item_to_entry.end() ) {
debugmsg( "WTF" );
return;
}
const inventory_entry &ie = *item_to_entry.at( maybe_removed.first );
on_change( ie );
}
}
for( const auto &maybe_added : implied_drops ) {
const auto entry_in_old = old_implied_drops.find( maybe_added.first );
if( entry_in_old == implied_drops.end() ||
entry_in_old->second.selected != maybe_added.second.selected ) {
printf( "added %s\n", maybe_added.first->display_name().c_str() );
auto iter = item_to_entry.find( maybe_added.first );
if( iter == item_to_entry.end() ) {
debugmsg( "WTF" );
return;
}
const inventory_entry &ie = *item_to_entry.at( maybe_added.first );
on_change( ie );
printf( "added %s\n", maybe_added.first->display_name().c_str() );
auto iter = item_to_entry.find( maybe_added.first );
if( iter == item_to_entry.end() ) {
debugmsg( "WTF" );
return;
}
const inventory_entry &ie = item_to_entry.at( maybe_added.first );
// sel_col.on_change( ie );
// on_change( ie );

// This is a copypaste of on_change
for( auto &elem : get_all_columns() ) {
elem->on_change( ie );
}
inventory_entry ie_modified = ie;
ie_modified.chosen_count = implied_drops.at( maybe_added.first ).selected;
sel_col.on_change( ie_modified );
refresh_active_column();

// TODO: Refresh only those which changed status
for( inventory_column *col : get_all_columns() ) {
col->clear_cell_cache();
}
}


// If our cache is empty but inventory isn't, something went wrong
if( implied_drops.empty() && !u.weapon.is_null() && !u.worn.empty() && u.inv.size() != 0 ) {
if( implied_drops.empty() && ( !u.weapon.is_null() || !u.worn.empty() || u.inv.size() != 0 ) ) {
// TODO: Prevent this from happening
debugmsg( "AAAAA!" );
debugmsg( "Shouldn't be empty, but is!" );
implied_cache_dirty = true;
}

Expand All @@ -2510,6 +2515,7 @@ nc_color caching_drop_preset::get_color( const inventory_entry &entry ) const
{
if( implied_drops == nullptr ) {
debugmsg( "Data not set" );
return c_blue_red;
}
if( !entry.is_item() ) {
return inventory_selector_preset::get_color( entry );
Expand Down
9 changes: 6 additions & 3 deletions src/inventory_ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -674,11 +674,14 @@ class inventory_multiselector : public inventory_selector
const std::unique_ptr<inventory_column> selection_col );
protected:
void rearrange_columns( size_t client_width ) override;
const inventory_column &get_selection_column() {
selection_column &get_selection_column() {
return *selection_col;
}
const selection_column &get_selection_column() const {
return *selection_col;
}
private:
std::unique_ptr<inventory_column> selection_col;
std::unique_ptr<selection_column> selection_col;
};

class inventory_compare_selector : public inventory_multiselector
Expand Down Expand Up @@ -775,7 +778,7 @@ class inventory_drop_selector : public inventory_multiselector

mutable std::unordered_map<const item *, count_out_of> implied_drops;
mutable int move_cost_sum = -1;
mutable bool implied_cache_dirty = true;
mutable bool implied_cache_dirty = false;

};

Expand Down

0 comments on commit f9fd676

Please sign in to comment.