Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

backport #71875 and #75815 #76066

Merged
merged 1 commit into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/inventory_ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2893,7 +2893,7 @@ drop_location inventory_selector::get_only_choice() const
for( const inventory_column *col : columns ) {
const std::vector<inventory_entry *> 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<int>( ent.front()->chosen_count ) };
}
}

Expand Down Expand Up @@ -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;
}
Expand All @@ -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() );
Expand Down
17 changes: 15 additions & 2 deletions tests/reloading_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <set>
#include <vector>

#include "activity_actor_definitions.h"
#include "avatar.h"
#include "calendar.h"
#include "cata_catch.h"
Expand Down Expand Up @@ -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 ) );
Expand All @@ -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 ) );
Expand Down
Loading