From 9f6ba0c1f3b70f1cf4291b16af7ec148ce0e0705 Mon Sep 17 00:00:00 2001 From: Katie M Date: Sun, 31 Mar 2024 22:01:47 -0400 Subject: [PATCH] Modified f_bulk_trade_accept to automatically send sealed cans containing the target item with the item asked for --- src/npctalk.cpp | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/src/npctalk.cpp b/src/npctalk.cpp index 9dc0545ff9fb0..91aa41e118184 100644 --- a/src/npctalk.cpp +++ b/src/npctalk.cpp @@ -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 seller_cans; + auto is_canned_item = [&tmp]( const item & e ) { + std::vector 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 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; @@ -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 ); }; }