Skip to content

Commit

Permalink
enable reloading attached gunmods by reloading the gun
Browse files Browse the repository at this point in the history
  • Loading branch information
mqrause committed Jan 14, 2024
1 parent b91c55d commit e441c35
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 21 deletions.
37 changes: 22 additions & 15 deletions src/game_inventory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -2845,20 +2857,7 @@ class select_ammo_inventory_preset : public inventory_selector_preset
return false;
}

std::vector<item_location> opts;
opts.emplace_back( target );

if( target->magazine_current() ) {
opts.emplace_back( target, const_cast<item *>( target->magazine_current() ) );
}

for( const item *mod : target->gunmods() ) {
item_location mod_loc( target, const_cast<item *>( mod ) );
opts.emplace_back( mod_loc );
if( mod->magazine_current() ) {
opts.emplace_back( mod_loc, const_cast<item *>( mod->magazine_current() ) );
}
}
std::vector<item_location> opts = get_possible_reload_targets( target );

for( item_location &p : opts ) {
if( ( loc->has_flag( flag_SPEEDLOADER ) && p->allows_speedloader( loc->typeId() ) &&
Expand Down Expand Up @@ -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;
Expand Down
42 changes: 36 additions & 6 deletions src/inventory_ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3332,23 +3332,53 @@ ammo_inventory_selector::ammo_inventory_selector( Character &you,
force_single_column = true;
}

std::vector<item_location> get_possible_reload_targets( const item_location &target )
{
std::vector<item_location> opts;
opts.emplace_back( target );

if( target->magazine_current() ) {
opts.emplace_back( target, const_cast<item *>( target->magazine_current() ) );
}

for( const item *mod : target->gunmods() ) {
item_location mod_loc( target, const_cast<item *>( mod ) );
opts.emplace_back( mod_loc );
if( mod->magazine_current() ) {
opts.emplace_back( mod_loc, const_cast<item *>( 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 );
Expand Down
1 change: 1 addition & 0 deletions src/inventory_ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,7 @@ class container_inventory_selector : public inventory_pick_selector
item_location loc;
};

std::vector<item_location> get_possible_reload_targets( const item_location &target );
class ammo_inventory_selector : public inventory_selector
{
public:
Expand Down

0 comments on commit e441c35

Please sign in to comment.