From 5d7db143b47a3abc0a8f87653ca7f4381c9ad9fb Mon Sep 17 00:00:00 2001 From: andrei Date: Wed, 3 Nov 2021 19:58:13 +0200 Subject: [PATCH] npctrade: spill unwanted items --- src/npctrade.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/npctrade.cpp b/src/npctrade.cpp index 298989140eb64..51169bc3873d9 100644 --- a/src/npctrade.cpp +++ b/src/npctrade.cpp @@ -38,6 +38,33 @@ std::list npc_trading::transfer_items( trade_selector::select_t &stuff, Ch continue; } item gift = *ip.first.get_item(); + + npc const *npc = nullptr; + std::function f_wants; + if( giver.is_npc() ) { + npc = giver.as_npc(); + f_wants = [npc]( item const * it, int price, int market_price ) { + return npc->wants_to_sell( *it, price, market_price ); + }; + } else if( receiver.is_npc() ) { + npc = receiver.as_npc(); + f_wants = [npc]( item const * it, int price, int market_price ) { + return npc->wants_to_buy( *it, price, market_price ); + }; + } + // spill contained, unwanted items + if( f_wants and gift.is_container() ) { + for( item *it : gift.get_contents().all_items_top() ) { + int const price = + trading_price( giver, receiver, { item_location{ giver, it }, 1 } ); + int const market_price = it->price( true ); + if( !f_wants( it, price, market_price ) ) { + giver.i_add_or_drop( *it, 1, ip.first.get_item() ); + gift.remove_item( *it ); + } + } + } + gift.set_owner( receiver ); // Items are moving to escrow.