Skip to content

Commit

Permalink
npctalk: fix transfering non-stackable items
Browse files Browse the repository at this point in the history
The logic to transfer non-stackable items was wrong and resulted
in people getting stacks of non-stackable items.  Fix the logic
and update the tests.
  • Loading branch information
mlangsdorf authored and kevingranade committed Jun 26, 2019
1 parent 609a6b8 commit f8e6357
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
22 changes: 18 additions & 4 deletions src/npctalk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1787,8 +1787,15 @@ void talk_effect_fun_t::set_u_buy_item( const std::string &item_name, int cost,
return;
}
if( container_name.empty() ) {
item new_item = item( item_name, calendar::turn, count );
u.i_add( new_item );
item new_item = item( item_name, calendar::turn, 1 );
if( new_item.count_by_charges() ) {
new_item.mod_charges( count - 1 );
u.i_add( new_item );
} else {
for( int i_cnt = 0; i_cnt < count; i_cnt++ ) {
u.i_add( new_item );
}
}
if( count == 1 ) {
//~ %1%s is the NPC name, %2$s is an item
popup( _( "%1$s gives you a %2$s." ), p.name, new_item.tname() );
Expand All @@ -1811,7 +1818,7 @@ void talk_effect_fun_t::set_u_sell_item( const std::string &item_name, int cost,
function = [item_name, cost, count]( const dialogue & d ) {
npc &p = *d.beta;
player &u = *d.alpha;
item old_item = item( item_name, calendar::turn, count );
item old_item = item( item_name, calendar::turn, 1 );
if( u.has_charges( item_name, count ) ) {
u.use_charges( item_name, count );
} else if( u.has_amount( item_name, count ) ) {
Expand All @@ -1821,7 +1828,14 @@ void talk_effect_fun_t::set_u_sell_item( const std::string &item_name, int cost,
popup( _( "You don't have a %1$s!" ), old_item.tname() );
return;
}
p.i_add( old_item );
if( old_item.count_by_charges() ) {
old_item.mod_charges( count - 1 );
p.i_add( old_item );
} else {
for( int i_cnt = 0; i_cnt < count; i_cnt++ ) {
p.i_add( old_item );
}
}

if( count == 1 ) {
//~ %1%s is the NPC name, %2$s is an item
Expand Down
9 changes: 7 additions & 2 deletions tests/npc_talk_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -415,8 +415,12 @@ TEST_CASE( "npc_talk_test" )
CHECK( d.responses[0].text == "This is a basic test response." );

const auto has_item = [&]( player & p, const std::string & id, int count ) {
return p.has_charges( itype_id( id ), count ) ||
p.has_amount( itype_id( id ), count );
item old_item = item( id );
if( old_item.count_by_charges() ) {
return p.has_charges( itype_id( id ), count );
} else {
return p.has_amount( itype_id( id ), count );
}
};
const auto has_beer_bottle = [&]( player & p, int count ) {
return has_item( p, "bottle_glass", 1 ) && has_item( p, "beer", count );
Expand Down Expand Up @@ -555,6 +559,7 @@ TEST_CASE( "npc_talk_test" )
CHECK( has_item( g->u, "beer", 1 ) );
effects = d.responses[17].success;
effects.apply( d );
CHECK( has_item( g->u, "beer", 0 ) );
CHECK( !has_item( g->u, "beer", 1 ) );

d.add_topic( "TALK_COMBAT_COMMANDS" );
Expand Down

0 comments on commit f8e6357

Please sign in to comment.