From 4a2d04b5219633349af6356d75f365a71bf11051 Mon Sep 17 00:00:00 2001 From: Anton Simakov <67688115+GuardianDll@users.noreply.github.com> Date: Fri, 30 Aug 2024 22:09:58 +0200 Subject: [PATCH] backport #71875 and #75815 --- src/inventory_ui.cpp | 16 ++++++++++++---- tests/reloading_test.cpp | 17 +++++++++++++++-- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/inventory_ui.cpp b/src/inventory_ui.cpp index 75585ebbe1723..9481b968bb6f5 100644 --- a/src/inventory_ui.cpp +++ b/src/inventory_ui.cpp @@ -2893,7 +2893,7 @@ drop_location inventory_selector::get_only_choice() const for( const inventory_column *col : columns ) { const std::vector ent = col->get_entries( return_item, true ); if( !ent.empty() ) { - return { ent.front()->any_item(), ent.front()->get_available_count() }; + return { ent.front()->any_item(), static_cast( ent.front()->chosen_count ) }; } } @@ -3358,9 +3358,14 @@ void ammo_inventory_selector::set_all_entries_chosen_count() for( inventory_column *col : columns ) { for( inventory_entry *entry : col->get_entries( return_item, true ) ) { 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() ); + item_location it = entry->any_item(); + if( loc->can_reload_with( *it, true ) ) { + item::reload_option tmp_opt( &u, loc, it ); + int count = entry->get_available_count(); + if( it->has_flag( flag_SPEEDLOADER ) || it->has_flag( flag_SPEEDLOADER_CLIP ) ) { + count = it->ammo_remaining(); + } + tmp_opt.qty( count ); entry->chosen_count = tmp_opt.qty(); break; } @@ -3371,6 +3376,9 @@ void ammo_inventory_selector::set_all_entries_chosen_count() void ammo_inventory_selector::mod_chosen_count( inventory_entry &entry, int value ) { + if( !entry.any_item()->is_ammo() ) { + return; + } 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() ); diff --git a/tests/reloading_test.cpp b/tests/reloading_test.cpp index 6dd22f8d0e846..09509a8a01e3b 100644 --- a/tests/reloading_test.cpp +++ b/tests/reloading_test.cpp @@ -4,6 +4,7 @@ #include #include +#include "activity_actor_definitions.h" #include "avatar.h" #include "calendar.h" #include "cata_catch.h" @@ -801,6 +802,8 @@ TEST_CASE( "reload_gun_with_integral_magazine_using_speedloader", "[reload],[gun item_location ammo = dummy.i_add( item( "38_special", calendar::turn_zero, item::default_charges_tag{} ) ); item_location speedloader = dummy.i_add( item( "38_speedloader", calendar::turn_zero, false ) ); + item_location ammo = dummy.i_add( item( "38_special", calendar::turn_zero, + speedloader->remaining_ammo_capacity() ) ); item_location gun = dummy.i_add( item( "sw_619", calendar::turn_zero, false ) ); REQUIRE( dummy.has_item( *ammo ) ); @@ -815,9 +818,19 @@ TEST_CASE( "reload_gun_with_integral_magazine_using_speedloader", "[reload],[gun REQUIRE( speedloader_success ); REQUIRE( speedloader->remaining_ammo_capacity() == 0 ); - bool success = gun->reload( dummy, speedloader, speedloader->ammo_remaining() ); + // This automatically selects the speedloader as ammo + // as long as dummy has nothing else available. + // If there are multiple options, it will crash from opening a ui. + item::reload_option opt = dummy.select_ammo( gun ); - REQUIRE( success ); + REQUIRE( opt ); + + dummy.assign_activity( reload_activity_actor( std::move( opt ) ) ); + if( !!dummy.activity ) { + process_activity( dummy ); + } + + //REQUIRE( success ); REQUIRE( gun->remaining_ammo_capacity() == 0 ); // Speedloader is still in inventory. REQUIRE( dummy.has_item( *speedloader ) );