Skip to content

Commit

Permalink
Track NPC storable volume when trading (CleverRaven#52261)
Browse files Browse the repository at this point in the history
* Track NPC storable volume when trading
* Exclude shopkeepers from pocket check
  • Loading branch information
dseguin authored Oct 17, 2021
1 parent 8c4046a commit b870101
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/npctrade.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,9 @@ bool trading_window::perform_trade( npc &np, const std::string &deal )
if( np.mission == NPC_MISSION_SHOPKEEP ) {
volume_left = 5'000_liter;
weight_left = 5'000_kilogram;
} else {
volume_left = np.volume_capacity() - np.volume_carried();
weight_left = np.weight_capacity() - np.weight_carried();
}

input_context ctxt( "NPC_TRADE" );
Expand Down Expand Up @@ -706,6 +709,8 @@ bool trading_window::perform_trade( npc &np, const std::string &deal )
} else if( volume_left < 0_ml || weight_left < 0_gram ) {
// Make sure NPC doesn't go over allowed volume
popup( _( "%s can't carry all that." ), np.get_name() );
} else if( np.mission != NPC_MISSION_SHOPKEEP && !npc_can_fit_items( np ) ) {
popup( _( "%s doesn't have the appropriate pockets to accept that." ), np.get_name() );
} else if( calc_npc_owes_you( np ) < your_balance ) {
// NPC is happy with the trade, but isn't willing to remember the whole debt.
const bool trade_ok = query_yn(
Expand Down Expand Up @@ -855,3 +860,37 @@ bool trading_window::npc_will_accept_trade( const npc &np ) const
{
return np.will_exchange_items_freely() || your_balance + np.max_credit_extended() >= 0;
}

bool trading_window::npc_can_fit_items( const npc &np ) const
{
std::vector<item> to_store;
std::vector<item> avail_pockets;
for( const item_pricing &ip : yours ) {
if( ip.selected ) {
to_store.push_back( *ip.loc );
}
}
for( const item &it : np.worn ) {
if( it.is_container() || it.is_holster() ) {
avail_pockets.push_back( it );
}
}
if( avail_pockets.empty() ) {
return false;
}
for( item &i : to_store ) {
bool item_stored = false;
for( item &pkt : avail_pockets ) {
const units::volume pvol = pkt.max_containable_volume();
if( pkt.can_holster( i ) || ( pkt.can_contain( i ).success() && pvol > i.volume() ) ) {
pkt.put_in( i, item_pocket::pocket_type::CONTAINER );
item_stored = true;
break;
}
}
if( !item_stored ) {
return false;
}
}
return true;
}
1 change: 1 addition & 0 deletions src/npctrade.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class trading_window
item_pricing &ip, bool max = false );
int get_var_trade( const item &it, int total_count );
bool npc_will_accept_trade( const npc &np ) const;
bool npc_can_fit_items( const npc &np ) const;
int calc_npc_owes_you( const npc &np ) const;
};

Expand Down

0 comments on commit b870101

Please sign in to comment.