Skip to content

Commit

Permalink
Merge pull request #72780 from RenechCDDA/catch_bad_charges
Browse files Browse the repository at this point in the history
Throw debugmsg when setting charges for items that cannot have charges
  • Loading branch information
akrieger authored Apr 7, 2024
2 parents 7fece1f + 29f4429 commit 84dae0b
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 26 deletions.
6 changes: 4 additions & 2 deletions src/activity_handlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1167,14 +1167,16 @@ static bool butchery_drops_harvest( item *corpse_item, const mtype &mt, Characte
const int item_charges = monster_weight_remaining / to_gram( item::find_type(
leftover_id )->weight );
if( item_charges > 0 ) {
item ruined_parts( leftover_id, calendar::turn, item_charges );
item ruined_parts( leftover_id, calendar::turn );
ruined_parts.set_mtype( &mt );
ruined_parts.set_item_temperature( corpse_item->temperature );
ruined_parts.set_rot( corpse_item->get_rot() );
if( !you.backlog.empty() && you.backlog.front().id() == ACT_MULTIPLE_BUTCHER ) {
ruined_parts.set_var( "activity_var", you.name );
}
here.add_item_or_charges( you.pos(), ruined_parts );
for( int i = 0; i < item_charges; ++i ) {
here.add_item_or_charges( you.pos(), ruined_parts );
}
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,11 @@ item::item( const itype *type, time_point turn, int qty ) : type( type ), bday(
}

if( qty >= 0 ) {
charges = qty;
if( type->can_have_charges() ) {
charges = qty;
} else if( qty > 1 ) {
debugmsg( "Tried to set charges for item %s that could not have them!", tname() );
}
} else {
if( type->tool && type->tool->rand_charges.size() > 1 ) {
const int charge_roll = rng( 1, type->tool->rand_charges.size() - 1 );
Expand Down
1 change: 0 additions & 1 deletion src/mattack_actors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1273,7 +1273,6 @@ void gun_actor::shoot( monster &z, const tripoint &target, const gun_mode_id &mo
}

tmp.set_wielded_item( gun );
tmp.i_add( item( "UPS_off", calendar::turn, 1000 ) );

add_msg_if_player_sees( z, m_warning, description.translated(), z.name(),
tmp.get_wielded_item()->tname() );
Expand Down
2 changes: 1 addition & 1 deletion tests/food_fun_for_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ TEST_CASE( "fun_for_food_eaten_too_often", "[fun_for][food][monotony]" )
std::pair<int, int> actual_fun;

// A big box of tasty toast-ems
item toastem( "toastem_test", calendar::turn, 10 );
item toastem( "toastem_test", calendar::turn );
REQUIRE( toastem.is_comestible() );

// Base fun value and monotony penalty for toast-em
Expand Down
43 changes: 22 additions & 21 deletions tests/item_autopickup_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,12 +212,12 @@ TEST_CASE( "auto_pickup_should_recognize_container_content", "[autopickup][item]
REQUIRE( here.i_at( ground ).empty() );

// add random items to the tile on the ground
here.add_item( ground, item( itype_marble, calendar::turn, 10 ) );
here.add_item( ground, item( itype_pebble, calendar::turn, 15 ) );
here.add_item( ground, item( itype_marble, calendar::turn ) );
here.add_item( ground, item( itype_pebble, calendar::turn ) );

// codeine (20)(WL)
// codeine (WL)
WHEN( "there is an item on the ground whitelisted in auto-pickup rules" ) {
unique_item item_codeine = unique_item( itype_codeine, 20 );
unique_item item_codeine = unique_item( itype_codeine );
REQUIRE( item_codeine.spawn_item( ground ) );
add_autopickup_rule( item_codeine.get(), true );

Expand All @@ -226,9 +226,9 @@ TEST_CASE( "auto_pickup_should_recognize_container_content", "[autopickup][item]
expect_to_find( backpack, { &item_codeine } );
}
}
// plastic prescription bottle > (WL)aspirin (12)
// plastic prescription bottle > (WL)aspirin
WHEN( "there is a container on the ground containing only items whitelisted in auto-pickup rules" ) {
unique_item item_aspirin = unique_item( itype_aspirin, 12 );
unique_item item_aspirin = unique_item( itype_aspirin );
unique_item item_prescription_bottle = unique_item(
itype_bottle_plastic_pill_prescription, {
&item_aspirin
Expand All @@ -244,10 +244,10 @@ TEST_CASE( "auto_pickup_should_recognize_container_content", "[autopickup][item]
} );
}
}
// plastic bag > paper (5), paper wrapper (WL) > chocolate candy (3)
// plastic bag > paper, paper wrapper (WL) > chocolate candy
WHEN( "there is a container on the ground with a deeply nested item whitelisted in auto-pickup rules" ) {
const unique_item item_paper = unique_item( itype_paper, 5 );
const unique_item item_chocolate_candy = unique_item( itype_candy2, 3 );
const unique_item item_paper = unique_item( itype_paper );
const unique_item item_chocolate_candy = unique_item( itype_candy2 );
const unique_item item_paper_wrapper = unique_item( itype_wrapper, {
&item_chocolate_candy
} );
Expand Down Expand Up @@ -387,8 +387,8 @@ TEST_CASE( "auto_pickup_should_consider_item_rigidness_and_seal", "[autopickup][
// small tin can (sealed) > canned tuna fish (WL), canned meat
WHEN( "there is a sealed container on the ground containing items whitelisted in auto-pickup rules" ) {
item item_medium_tin_can = item( itype_can_medium );
unique_item item_canned_tuna = unique_item( itype_can_tuna, 1, true );
unique_item item_canned_meat = unique_item( itype_meat_canned, 1, true );
unique_item item_canned_tuna = unique_item( itype_can_tuna, true );
unique_item item_canned_meat = unique_item( itype_meat_canned, true );

// insert items inside can and seal it
item_medium_tin_can.force_insert_item( *item_canned_tuna.get(), pocket_type_container );
Expand All @@ -410,8 +410,8 @@ TEST_CASE( "auto_pickup_should_consider_item_rigidness_and_seal", "[autopickup][
WHEN( "there is a sealed container on the ground containing no whitelisted items" ) {
item item_medium_tin_can = item( itype_can_medium );
item item_bottle_plastic = item( itype_bottle_plastic );
unique_item item_canned_tuna = unique_item( itype_can_tuna, 1, true );
unique_item item_canned_meat = unique_item( itype_meat_canned, 1, true );
unique_item item_canned_tuna = unique_item( itype_can_tuna, true );
unique_item item_canned_meat = unique_item( itype_meat_canned, true );

// insert items inside can and seal it
item_medium_tin_can.force_insert_item( *item_canned_tuna.get(), pocket_type_container );
Expand Down Expand Up @@ -447,15 +447,16 @@ TEST_CASE( "auto_pickup_should_respect_volume_and_weight_limits", "[autopickup][
item &backpack = *backpack_iter;
REQUIRE( they.has_item( backpack ) );

// backpack > lump of steel (5)(WL), cigrarette (3)(WL), paper (10)(WL)
// backpack > lump of steel (WL), cigrarette (WL), paper (WL)
GIVEN( "there is a container with some items that exceed volume or weight limit" ) {
options_manager &options = get_options();
options_manager::cOpt &ap_weight_limit = options.get_option( "AUTO_PICKUP_WEIGHT_LIMIT" );
options_manager::cOpt &ap_volume_limit = options.get_option( "AUTO_PICKUP_VOLUME_LIMIT" );

// Note: This relies on charge-spawning behavior for steel lumps
unique_item item_lump_of_steel = unique_item( itype_steel_lump, 5 );
unique_item item_cigarette = unique_item( itype_cig, 3 );
unique_item item_paper = unique_item( itype_paper, 10 );
unique_item item_cigarette = unique_item( itype_cig );
unique_item item_paper = unique_item( itype_paper );
unique_item item_backpack = unique_item( itype_backpack, {
&item_lump_of_steel, &item_cigarette, &item_paper
} );
Expand Down Expand Up @@ -524,16 +525,16 @@ TEST_CASE( "auto_pickup_should_consider_item_ownership", "[autopickup][item]" )
REQUIRE( they.has_item( backpack ) );

// candy cigarette(WL)
// pack(WL) > cigarette (2)(WL), rolling paper (10)(WL)
// pack(WL) > cigarette (WL), rolling paper (WL)
GIVEN( "there is a container with some items and an item outside it on the ground" ) {
options_manager::cOpt &autopickup_owned = get_options().get_option( "AUTO_PICKUP_OWNED" );
// make sure the autopickup owned option is disabled
if( autopickup_owned.value_as<bool>() ) {
autopickup_owned.setValue( "false" );
}
unique_item item_candy_cigarette = unique_item( itype_candycigarette );
unique_item item_cigarette = unique_item( itype_cig, 2 );
unique_item item_rolling_paper = unique_item( itype_rolling_paper, 10 );
unique_item item_cigarette = unique_item( itype_cig );
unique_item item_rolling_paper = unique_item( itype_rolling_paper );
unique_item item_pack = unique_item( itype_box_cigarette, {
&item_cigarette, &item_rolling_paper
} );
Expand Down Expand Up @@ -601,8 +602,8 @@ TEST_CASE( "auto_pickup_should_not_implicitly_pickup_corpses", "[autopickup][ite
expect_to_find( *body_bag, {} );
}
WHEN( "the corpse contains whitelisted items" ) {
unique_item item_cigarette = unique_item( itype_cig, 5 );
unique_item item_rolling_paper = unique_item( itype_rolling_paper, 10 );
unique_item item_cigarette = unique_item( itype_cig );
unique_item item_rolling_paper = unique_item( itype_rolling_paper );

item *found = item_corpse.find_on_ground( ground );
found->force_insert_item( *item_cigarette.get(), pocket_type_container );
Expand Down

0 comments on commit 84dae0b

Please sign in to comment.