diff --git a/src/game_inventory.cpp b/src/game_inventory.cpp index 2116d0c47ddcb..3dc2903a8e281 100644 --- a/src/game_inventory.cpp +++ b/src/game_inventory.cpp @@ -2791,6 +2791,18 @@ class select_ammo_inventory_preset : public inventory_selector_preset return loc.describe( &player_character ); }, _( "LOCATION" ) ); + append_cell( [&you, target]( const item_location & loc ) { + for( const item_location &opt : get_possible_reload_targets( target ) ) { + if( opt->can_reload_with( *loc, true ) ) { + if( opt == target ) { + return std::string(); + } + return string_format( _( "%s, %s" ), opt->type_name(), opt.describe( &you ) ); + } + } + return std::string(); + }, _( "DESTINATION" ) ); + append_cell( []( const inventory_entry & entry ) { if( entry.any_item()->is_ammo() ) { return std::to_string( entry.chosen_count ); @@ -2845,20 +2857,7 @@ class select_ammo_inventory_preset : public inventory_selector_preset return false; } - std::vector opts; - opts.emplace_back( target ); - - if( target->magazine_current() ) { - opts.emplace_back( target, const_cast( target->magazine_current() ) ); - } - - for( const item *mod : target->gunmods() ) { - item_location mod_loc( target, const_cast( mod ) ); - opts.emplace_back( mod_loc ); - if( mod->magazine_current() ) { - opts.emplace_back( mod_loc, const_cast( mod->magazine_current() ) ); - } - } + std::vector opts = get_possible_reload_targets( target ); for( item_location &p : opts ) { if( ( loc->has_flag( flag_SPEEDLOADER ) && p->allows_speedloader( loc->typeId() ) && @@ -2933,7 +2932,15 @@ item::reload_option game_menus::inv::select_ammo( Character &you, const item_loc return item::reload_option(); } - item::reload_option opt( &you, loc, selected.first ); + item_location target_loc; + for( const item_location &opt : get_possible_reload_targets( loc ) ) { + if( opt->can_reload_with( *selected.first, true ) ) { + target_loc = opt; + break; + } + } + + item::reload_option opt( &you, target_loc, selected.first ); opt.qty( selected.second ); return opt; diff --git a/src/inventory_ui.cpp b/src/inventory_ui.cpp index 5301f76594bf6..75585ebbe1723 100644 --- a/src/inventory_ui.cpp +++ b/src/inventory_ui.cpp @@ -3332,23 +3332,53 @@ ammo_inventory_selector::ammo_inventory_selector( Character &you, force_single_column = true; } +std::vector get_possible_reload_targets( const item_location &target ) +{ + std::vector opts; + opts.emplace_back( target ); + + if( target->magazine_current() ) { + opts.emplace_back( target, const_cast( target->magazine_current() ) ); + } + + for( const item *mod : target->gunmods() ) { + item_location mod_loc( target, const_cast( mod ) ); + opts.emplace_back( mod_loc ); + if( mod->magazine_current() ) { + opts.emplace_back( mod_loc, const_cast( mod->magazine_current() ) ); + } + } + + return opts; +} + // todo: this should happen when the entries are created, but that's a different refactoring void ammo_inventory_selector::set_all_entries_chosen_count() { for( inventory_column *col : columns ) { for( inventory_entry *entry : col->get_entries( return_item, true ) ) { - item::reload_option tmp_opt( &u, reload_loc, entry->any_item() ); - tmp_opt.qty( entry->get_available_count() ); - entry->chosen_count = tmp_opt.qty(); + for( const item_location &loc : get_possible_reload_targets( reload_loc ) ) { + if( loc->can_reload_with( *entry->any_item(), true ) ) { + item::reload_option tmp_opt( &u, loc, entry->any_item() ); + tmp_opt.qty( entry->get_available_count() ); + entry->chosen_count = tmp_opt.qty(); + break; + } + } } } } void ammo_inventory_selector::mod_chosen_count( inventory_entry &entry, int value ) { - item::reload_option tmp_opt( &u, reload_loc, entry.any_item() ); - tmp_opt.qty( entry.chosen_count + value ); - entry.chosen_count = tmp_opt.qty(); + for( const item_location &loc : get_possible_reload_targets( reload_loc ) ) { + if( loc->can_reload_with( *entry.any_item(), true ) ) { + item::reload_option tmp_opt( &u, loc, entry.any_item() ); + tmp_opt.qty( entry.chosen_count + value ); + entry.chosen_count = tmp_opt.qty(); + break; + } + } entry.make_entry_cell_cache( preset ); on_change( entry ); diff --git a/src/inventory_ui.h b/src/inventory_ui.h index ae3e0ba422ab6..bbe8ca99bcce4 100644 --- a/src/inventory_ui.h +++ b/src/inventory_ui.h @@ -948,6 +948,7 @@ class container_inventory_selector : public inventory_pick_selector item_location loc; }; +std::vector get_possible_reload_targets( const item_location &target ); class ammo_inventory_selector : public inventory_selector { public: