Skip to content

Commit

Permalink
Add command to pet menu for removing individual items from storage (c…
Browse files Browse the repository at this point in the history
…ataclysmbnteam#2868)

* Add command to pet menu for removing individual items from storage

* Update monexamine.cpp

* refactor: use `const` where possible

the value `pet_name` does not get mutated, however reader has to read through the entire function to figure it out. using [const](https://learn.microsoft.com/en-us/cpp/cpp/const-cpp?view=msvc-170#const-values) will clarify it out and prevent possible accidental modification in the future.

* refactor: use alias pet_name defined earlier

using alias pet_name we used earlier

* refactor: use references instead of copies

i suspect you used `z.inv.erase` at the end of function after experiencing that `monster_inv.erase` did not remove item from monster's inventory.

this is because `monster_inv` was a **copy** of `z.inv`. so while item from `monster_inv` was erased, it monsters inventory was kept intact.

if you're using `monster_inv` as alias to `z.inv`, you should use [references](https://learn.microsoft.com/en-us/cpp/cpp/references-cpp?view=msvc-170) to ensure that
- it does not get copied
- mutating `monster_inv` are the same as mutating `z.inv`

* refactor: group related usages together

1. adding comment to clarify why we need to remove 1 from index.
2. declaring `selection` to prevent making off-by-1 error.
3. grouped related mutation ('pop' an element from vector).
4. grouped you and its usage close together.

---------

Co-authored-by: scarf <[email protected]>
  • Loading branch information
chaosvolt and scarf005 authored May 26, 2023
1 parent 4bdbc0d commit 5b815d9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/monexamine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ bool monexamine::pet_menu( monster &z )
remove_bag,
drop_all,
give_items,
take_items,
mon_armor_add,
mon_harness_remove,
mon_armor_remove,
Expand Down Expand Up @@ -124,6 +125,7 @@ bool monexamine::pet_menu( monster &z )
amenu.addentry( give_items, true, 'g', _( "Place items into bag" ) );
amenu.addentry( remove_bag, true, 'b', _( "Remove bag from %s" ), pet_name );
if( !z.inv.empty() ) {
amenu.addentry( take_items, true, 'G', _( "Take items from bag" ) );
amenu.addentry( drop_all, true, 'd', _( "Remove all items from bag" ) );
}
} else if( !z.has_flag( MF_RIDEABLE_MECH ) ) {
Expand Down Expand Up @@ -276,6 +278,9 @@ bool monexamine::pet_menu( monster &z )
break;
case give_items:
return give_items_to( z );
case take_items:
take_items_from( z );
break;
case mon_armor_add:
return add_armor( z );
case mon_harness_remove:
Expand Down Expand Up @@ -732,6 +737,40 @@ bool monexamine::give_items_to( monster &z )
return false;
}

void monexamine::take_items_from( monster &z )
{
const std::string pet_name = z.get_name();
std::vector<item> &monster_inv = z.inv;
if( monster_inv.empty() ) {
return;
}

int i = 0;
uilist selection_menu;
selection_menu.text = string_format( _( "Select an item to remove from the %s." ), pet_name );
selection_menu.addentry( i++, true, MENU_AUTOASSIGN, _( "Cancel" ) );
for( auto iter : monster_inv ) {
selection_menu.addentry( i++, true, MENU_AUTOASSIGN, _( "Retrieve %s" ), iter.tname() );
}
selection_menu.selected = 1;
selection_menu.query();
const int index = selection_menu.ret;
if( index == 0 || index == UILIST_CANCEL || index < 0 ||
index > static_cast<int>( monster_inv.size() ) ) {
return;
}

// because the first entry is the cancel option
const int selection = index - 1;
item retrieved_item = monster_inv[selection];
monster_inv.erase( monster_inv.begin() + selection );

add_msg( _( "You remove the %1$s from the %2$s's bag." ), retrieved_item.tname(), pet_name );

avatar &you = get_avatar();
you.i_add( retrieved_item );
}

bool monexamine::add_armor( monster &z )
{
std::string pet_name = z.get_name();
Expand Down
1 change: 1 addition & 0 deletions src/monexamine.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ void attach_bag_to( monster &z );
void remove_bag_from( monster &z );
void dump_items( monster &z );
bool give_items_to( monster &z );
void take_items_from( monster &z );
bool add_armor( monster &z );
void remove_armor( monster &z );
void remove_harness( monster &z );
Expand Down

0 comments on commit 5b815d9

Please sign in to comment.