Skip to content

Commit

Permalink
Merge pull request #72759 from katemonster33/traders_respect_cans
Browse files Browse the repository at this point in the history
NPCs taking canned food donations no longer remove the food from the can
  • Loading branch information
I-am-Erk authored Apr 4, 2024
2 parents 9bb37c4 + 9f6ba0c commit bb46136
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions src/npctalk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3996,12 +3996,31 @@ talk_effect_fun_t::func f_bulk_trade_accept( const JsonObject &jo, std::string_v
item tmp( d.cur_item );
int quantity = dov_quantity.evaluate( d );
int seller_has = 0;
int seller_has_loose = 0;
std::vector<item> seller_cans;
auto is_canned_item = [&tmp]( const item & e ) {
std::vector<const item_pocket *> pockets = e.get_all_contained_pockets();
return pockets.size() == 1 &&
pockets[0]->size() == 1 &&
pockets[0]->sealed() &&
pockets[0]->front().type == tmp.type;
};
if( tmp.count_by_charges() ) {
seller_has = seller->charges_of( d.cur_item );
} else {
seller_has = seller->items_with( [&tmp]( const item & e ) {
return tmp.type == e.type;
std::vector<item *> cans_tmp = seller->items_with( is_canned_item );
for( item *it : cans_tmp ) {
seller_cans.emplace_back( *it );
}
seller_has_loose = seller->items_with( [&tmp,
&cans_tmp]( const item & e ) {
return tmp.type == e.type &&
!std::any_of( cans_tmp.begin(), cans_tmp.end(), [&e]( const item * n ) {
return &n->get_all_contained_pockets()[0]->front() == &e;
} );
} ).size();
seller_has = cans_tmp.size() + seller_has_loose;

}
seller_has = ( quantity == -1 ) ? seller_has : std::min( seller_has, quantity );
tmp.charges = seller_has;
Expand Down Expand Up @@ -4047,9 +4066,15 @@ talk_effect_fun_t::func f_bulk_trade_accept( const JsonObject &jo, std::string_v
if( tmp.count_by_charges() ) {
seller->use_charges( d.cur_item, seller_has );
} else {
seller->use_amount( d.cur_item, seller_has );
seller->use_amount( d.cur_item, seller_has_loose );
seller->remove_items_with( is_canned_item );
}
if( seller_cans.size() != size_t( seller_has ) ) {
buyer->i_add( tmp );
}
for( const item &it : seller_cans ) {
buyer->i_add( it );
}
buyer->i_add( tmp );
};
}

Expand Down

0 comments on commit bb46136

Please sign in to comment.