From 49451f377906e0bb1134e4640d501c7c2e3af2d5 Mon Sep 17 00:00:00 2001 From: RenechCDDA <84619419+RenechCDDA@users.noreply.github.com> Date: Fri, 17 May 2024 15:03:10 +0100 Subject: [PATCH] Merge pull request #72780 from RenechCDDA/catch_bad_charges Throw debugmsg when setting charges for items that cannot have charges --- src/activity_handlers.cpp | 6 +++-- src/item.cpp | 6 ++++- src/mattack_actors.cpp | 1 - tests/food_fun_for_test.cpp | 2 +- tests/item_autopickup_test.cpp | 43 +++++++++++++++++----------------- 5 files changed, 32 insertions(+), 26 deletions(-) diff --git a/src/activity_handlers.cpp b/src/activity_handlers.cpp index 9c22e82b4ab69..97aa47d96dff4 100644 --- a/src/activity_handlers.cpp +++ b/src/activity_handlers.cpp @@ -1165,14 +1165,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 ); + } } } diff --git a/src/item.cpp b/src/item.cpp index 9258118025a04..7c69618c6cad7 100644 --- a/src/item.cpp +++ b/src/item.cpp @@ -308,7 +308,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 ); diff --git a/src/mattack_actors.cpp b/src/mattack_actors.cpp index 25d1cbd0715f8..c02a1f22559b1 100644 --- a/src/mattack_actors.cpp +++ b/src/mattack_actors.cpp @@ -1264,7 +1264,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() ); diff --git a/tests/food_fun_for_test.cpp b/tests/food_fun_for_test.cpp index 28590410f48bb..0ba49a9db4533 100644 --- a/tests/food_fun_for_test.cpp +++ b/tests/food_fun_for_test.cpp @@ -367,7 +367,7 @@ TEST_CASE( "fun_for_food_eaten_too_often", "[fun_for][food][monotony]" ) std::pair actual_fun; // A big box of tasty toast-ems - item toastem( "toastem", calendar::turn, 10 ); + item toastem( "toastem", calendar::turn ); REQUIRE( toastem.is_comestible() ); // Base fun value and monotony penalty for toast-em diff --git a/tests/item_autopickup_test.cpp b/tests/item_autopickup_test.cpp index 57070f9403b71..29ce14eb00162 100644 --- a/tests/item_autopickup_test.cpp +++ b/tests/item_autopickup_test.cpp @@ -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 ); @@ -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 @@ -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 } ); @@ -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 ); @@ -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 ); @@ -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 } ); @@ -524,7 +525,7 @@ 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 @@ -532,8 +533,8 @@ TEST_CASE( "auto_pickup_should_consider_item_ownership", "[autopickup][item]" ) 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 } ); @@ -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 );