From 9ef9fe31c6160b121f8d5df984137b1e7d6ef209 Mon Sep 17 00:00:00 2001 From: RoyBerube Date: Sun, 25 Apr 2021 19:14:55 -0600 Subject: [PATCH 1/2] Change filter without crash when washing items --- src/inventory_ui.cpp | 25 +++++++++++++++++++++++-- src/inventory_ui.h | 1 + 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/inventory_ui.cpp b/src/inventory_ui.cpp index 93e2ff665115b..9b321c54f5e07 100644 --- a/src/inventory_ui.cpp +++ b/src/inventory_ui.cpp @@ -2447,6 +2447,18 @@ drop_locations inventory_iuse_selector::execute() { shared_ptr_fast ui = create_or_get_ui_adaptor(); + auto is_entry = []( const inventory_entry & elem ) { + return elem.is_selectable(); + }; + for( inventory_column *col : get_all_columns() ) { + if( col->allows_selecting() ) { + for( inventory_entry *ie : col->get_entries( is_entry ) ) { + for( item_location x : ie->locations ) { + usable_locs.push_back( x ); + } + } + } + } int count = 0; while( true ) { ui_manager::redraw(); @@ -2510,6 +2522,7 @@ drop_locations inventory_iuse_selector::execute() void inventory_iuse_selector::set_chosen_count( inventory_entry &entry, size_t count ) { const item_location &it = entry.any_item(); + std::map temp_use; if( count == 0 ) { entry.chosen_count = 0; @@ -2519,17 +2532,25 @@ void inventory_iuse_selector::set_chosen_count( inventory_entry &entry, size_t c } else { entry.chosen_count = std::min( std::min( count, max_chosen_count ), entry.get_available_count() ); if( it->count_by_charges() ) { - to_use[&it] = entry.chosen_count; + temp_use[&it] = entry.chosen_count; } else { for( const item_location &loc : entry.locations ) { if( count == 0 ) { break; } - to_use[&loc] = 1; + temp_use[&loc] = 1; count--; } } } + // Optimisation to reduce the scale of looping if otherwise done in the preceeding code. + for( auto iter : temp_use ) { + for( item_location &x : usable_locs ) { + if( x == *iter.first ) { + to_use[&x] = iter.second; + } + } + } on_change( entry ); } diff --git a/src/inventory_ui.h b/src/inventory_ui.h index 1bd4b06afa567..e08ba4e4b6fd4 100644 --- a/src/inventory_ui.h +++ b/src/inventory_ui.h @@ -770,6 +770,7 @@ class inventory_iuse_selector : public inventory_multiselector private: GetStats get_stats; std::map to_use; + std::vector usable_locs; size_t max_chosen_count; }; From 2350fbe1c0efc6b6b3f2cbd56bcfdbfec2e7e59e Mon Sep 17 00:00:00 2001 From: RoyBerube Date: Sun, 25 Apr 2021 21:21:09 -0600 Subject: [PATCH 2/2] appease clang --- src/inventory_ui.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/inventory_ui.cpp b/src/inventory_ui.cpp index 9b321c54f5e07..bd2e88f7e3134 100644 --- a/src/inventory_ui.cpp +++ b/src/inventory_ui.cpp @@ -2453,7 +2453,7 @@ drop_locations inventory_iuse_selector::execute() for( inventory_column *col : get_all_columns() ) { if( col->allows_selecting() ) { for( inventory_entry *ie : col->get_entries( is_entry ) ) { - for( item_location x : ie->locations ) { + for( item_location const &x : ie->locations ) { usable_locs.push_back( x ); } }