Skip to content

Commit

Permalink
Merge pull request CleverRaven#70029 from Kamayana/crafting_tests_plu…
Browse files Browse the repository at this point in the history
…g_in_tools

Plug in tools with cables during crafting tests
  • Loading branch information
Maleclypse authored Dec 7, 2023
2 parents 1e7d45b + 299e45a commit de95aef
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 19 deletions.
46 changes: 46 additions & 0 deletions data/mods/TEST_DATA/appliance.json
Original file line number Diff line number Diff line change
Expand Up @@ -140,5 +140,51 @@
"flags": [ "NO_INSTALL_HIDDEN", "UNMOUNT_ON_DAMAGE", "POWER_TRANSFER" ],
"breaks_into": [ { "item": "cable", "charges": [ 1, 10 ] }, { "item": "plastic_chunk", "count": [ 1, 2 ] } ],
"variants": [ { "symbols": "{", "symbols_broken": "*" } ]
},
{
"id": "test_storage_battery",
"type": "MAGAZINE",
"category": "veh_parts",
"name": { "str": "large storage battery", "str_pl": "large storage batteries" },
"description": "A large storage battery containing many lithium-ion cells. Could be installed into a storage battery case for easy removal from a vehicle, or just welded straight in.",
"weight": "75 kg",
"volume": "25 L",
"price": 200000,
"price_postapoc": 1500,
"to_hit": -2,
"material": [ "plastic", "steel" ],
"symbol": ":",
"color": "light_cyan",
"ammo_type": [ "battery" ],
"capacity": 1000000000,
"count": 1000000000,
"flags": [ "NO_SALVAGE", "NO_UNLOAD", "NO_RELOAD", "RECHARGE", "NO_REPAIR" ],
"pocket_data": [ { "pocket_type": "MAGAZINE", "rigid": true, "ammo_restriction": { "battery": 1000000000 } } ],
"melee_damage": { "bash": 20 }
},
{
"id": "ap_test_storage_battery",
"type": "vehicle_part",
"name": { "str": "large grid battery" },
"categories": [ "energy" ],
"item": "test_storage_battery",
"location": "fuel_source",
"fuel_type": "battery",
"color": "light_red",
"broken_color": "red",
"durability": 300,
"description": "A large battery to store electrical power in a static power grid.",
"damage_modifier": 80,
"breaks_into": [
{ "item": "steel_lump", "count": [ 5, 10 ] },
{ "item": "steel_chunk", "count": [ 5, 10 ] },
{ "item": "scrap", "count": [ 5, 10 ] },
{ "item": "medium_storage_battery", "count": [ 1, 2 ] }
],
"//": "29cm length battery, welded on at least two edges",
"requirements": { "removal": { "time": "6 m" } },
"damage_reduction": { "bash": 10 },
"flags": [ "OBSTACLE", "APPLIANCE" ],
"variants": [ { "symbols": "B", "symbols_broken": "#" } ]
}
]
41 changes: 30 additions & 11 deletions tests/crafting_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
#include "temp_crafting_inventory.h"
#include "type_id.h"
#include "value_ptr.h"
#include "veh_appliance.h"
#include "vehicle.h"

static const activity_id ACT_CRAFT( "ACT_CRAFT" );

Expand Down Expand Up @@ -109,6 +111,8 @@ static const skill_id skill_survival( "survival" );

static const trait_id trait_DEBUG_CNF( "DEBUG_CNF" );

static const vpart_id vpart_ap_test_storage_battery( "ap_test_storage_battery" );

TEST_CASE( "recipe_subset" )
{
recipe_subset subset;
Expand Down Expand Up @@ -361,7 +365,7 @@ TEST_CASE( "crafting_with_a_companion", "[.]" )
}
}

static void give_tools( const std::vector<item> &tools )
static void give_tools( const std::vector<item> &tools, const bool plug_in )
{
Character &player_character = get_player_character();
player_character.clear_worn();
Expand All @@ -374,7 +378,18 @@ static void give_tools( const std::vector<item> &tools )
std::vector<item> boil;
for( const item &gear : tools ) {
if( gear.get_quality( qual_BOIL, false ) == 0 ) {
REQUIRE( player_character.i_add( gear ) );
item_location added_tool = player_character.i_add( gear );
REQUIRE( added_tool );
if( plug_in && added_tool->can_link_up() ) {
added_tool->link = cata::make_value<item::link_data>();
added_tool->link->t_state = link_state::vehicle_port;
added_tool->link->t_abs_pos = get_map().getglobal( player_character.pos() + tripoint_north );
added_tool->link->last_processed = calendar::turn;
added_tool->link->t_veh_safe = get_map().veh_at( player_character.pos() +
tripoint_north )->vehicle().get_safe_reference();
added_tool->set_link_traits();
REQUIRE( added_tool->link->t_veh_safe );
}
} else {
boil.emplace_back( gear );
}
Expand Down Expand Up @@ -413,7 +428,7 @@ static void grant_profs_to_character( Character &you, const recipe &r )
}

static void prep_craft( const recipe_id &rid, const std::vector<item> &tools,
bool expect_craftable, int offset = 0, bool grant_profs = false )
bool expect_craftable, int offset = 0, bool grant_profs = false, bool plug_in_tools = true )
{
clear_avatar();
clear_map();
Expand All @@ -428,7 +443,11 @@ static void prep_craft( const recipe_id &rid, const std::vector<item> &tools,
grant_profs_to_character( player_character, r );
}

give_tools( tools );
const tripoint battery_pos = test_origin + tripoint_north;
std::optional<item> battery_item( "test_storage_battery" );
place_appliance( battery_pos, vpart_ap_test_storage_battery, battery_item );

give_tools( tools, plug_in_tools );
const inventory &crafting_inv = player_character.crafting_inventory();

bool can_craft_with_crafting_inv = r.deduped_requirements()
Expand Down Expand Up @@ -541,7 +560,7 @@ TEST_CASE( "proficiency_gain_short_crafts", "[crafting][proficiency]" )

do {
turns_taken += test_craft_for_prof( rec, proficiency_prof_carving, 1.0f );
give_tools( tools );
give_tools( tools, true );

// Escape door to avoid infinite loop if there is no progress
REQUIRE( turns_taken < max_turns );
Expand Down Expand Up @@ -840,7 +859,7 @@ TEST_CASE( "tools_use_charge_to_craft", "[crafting][charge]" )
tools.push_back( plastic_molding );

THEN( "crafting succeeds, and uses charges from each tool" ) {
prep_craft( recipe_carver_off, tools, true );
prep_craft( recipe_carver_off, tools, true, 0, false, false );
int turns = actually_test_craft( recipe_carver_off, INT_MAX );
CAPTURE( turns );
CHECK( get_remaining_charges( "hotplate_induction" ) == 0 );
Expand All @@ -854,7 +873,7 @@ TEST_CASE( "tools_use_charge_to_craft", "[crafting][charge]" )
tools.insert( tools.end(), 1, tool_with_ammo( "vac_mold", 4 ) );

THEN( "crafting succeeds, and uses charges from multiple tools" ) {
prep_craft( recipe_carver_off, tools, true );
prep_craft( recipe_carver_off, tools, true, 0, false, false );
actually_test_craft( recipe_carver_off, INT_MAX );
CHECK( get_remaining_charges( "hotplate_induction" ) == 0 );
CHECK( get_remaining_charges( "soldering_iron" ) == 0 );
Expand All @@ -876,7 +895,7 @@ TEST_CASE( "tools_use_charge_to_craft", "[crafting][charge]" )
tools.push_back( tool_with_ammo( "vac_mold", 4 ) );

THEN( "crafting succeeds, and uses charges from the UPS" ) {
prep_craft( recipe_carver_off, tools, true );
prep_craft( recipe_carver_off, tools, true, 0, false, false );
actually_test_craft( recipe_carver_off, INT_MAX );
CHECK( get_remaining_charges( "hotplate" ) == 0 );
CHECK( get_remaining_charges( "soldering_iron" ) == 0 );
Expand All @@ -899,7 +918,7 @@ TEST_CASE( "tools_use_charge_to_craft", "[crafting][charge]" )
tools.push_back( ups );

THEN( "crafting fails, and no charges are used" ) {
prep_craft( recipe_carver_off, tools, false );
prep_craft( recipe_carver_off, tools, false, 0, false, false );
CHECK( get_remaining_charges( "UPS_off" ) == 10 );
}
}
Expand Down Expand Up @@ -1171,7 +1190,7 @@ static void test_skill_progression( const recipe_id &test_recipe, int expected_t
REQUIRE( previous_knowledge < new_knowledge );
previous_knowledge = new_knowledge;
}
give_tools( tools );
give_tools( tools, true );
} while( static_cast<int>( you.get_skill_level( skill_used ) ) == starting_skill_level );
CAPTURE( test_recipe.str() );
CAPTURE( expected_turns_taken );
Expand Down Expand Up @@ -1335,7 +1354,7 @@ TEST_CASE( "book_proficiency_mitigation", "[crafting][proficiency]" )
WHEN( "player has a book mitigating lack of proficiency" ) {
std::vector<item> books;
books.emplace_back( "manual_tailor" );
give_tools( books );
give_tools( books, true );
get_player_character().invalidate_crafting_inventory();
int mitigated_time_taken = test_recipe.batch_time( get_player_character(), 1, 1, 0 );
THEN( "it takes less time to craft the recipe" ) {
Expand Down
34 changes: 26 additions & 8 deletions tests/vehicle_interact_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@
#include "ret_val.h"
#include "type_id.h"
#include "units.h"
#include "veh_appliance.h"
#include "veh_type.h"
#include "vehicle.h"

static const skill_id skill_mechanics( "mechanics" );

static const vpart_id vpart_ap_test_storage_battery( "ap_test_storage_battery" );

static const vproto_id vehicle_prototype_car( "car" );

static void test_repair( const std::vector<item> &tools, bool expect_craftable )
static void test_repair( const std::vector<item> &tools, bool plug_in_tools, bool expect_craftable )
{
clear_avatar();
clear_map();
Expand All @@ -34,8 +37,23 @@ static void test_repair( const std::vector<item> &tools, bool expect_craftable )
player_character.setpos( test_origin );
const item debug_backpack( "debug_backpack" );
player_character.wear_item( debug_backpack );

const tripoint battery_pos = test_origin + tripoint_north_west;
std::optional<item> battery_item( "test_storage_battery" );
place_appliance( battery_pos, vpart_ap_test_storage_battery, battery_item );

for( const item &gear : tools ) {
player_character.i_add( gear );
item_location added_tool = player_character.i_add( gear );
if( plug_in_tools && added_tool->can_link_up() ) {
added_tool->link = cata::make_value<item::link_data>();
added_tool->link->t_state = link_state::vehicle_port;
added_tool->link->t_abs_pos = get_map().getglobal( player_character.pos() + tripoint_north_west );
added_tool->link->last_processed = calendar::turn;
added_tool->link->t_veh_safe = get_map().veh_at( player_character.pos() +
tripoint_north_west )->vehicle().get_safe_reference();
added_tool->set_link_traits();
REQUIRE( added_tool->link->t_veh_safe );
}
}
player_character.set_skill_level( skill_mechanics, 10 );

Expand Down Expand Up @@ -87,7 +105,7 @@ TEST_CASE( "repair_vehicle_part", "[vehicle]" )
tools.emplace_back( "hammer" );
tools.insert( tools.end(), 20, item( "steel_chunk" ) );
tools.insert( tools.end(), 200, item( "welding_wire_steel" ) );
test_repair( tools, true );
test_repair( tools, true, true );
}
SECTION( "UPS_modded_welder" ) {
std::vector<item> tools;
Expand All @@ -105,7 +123,7 @@ TEST_CASE( "repair_vehicle_part", "[vehicle]" )
tools.emplace_back( "hammer" );
tools.insert( tools.end(), 5, item( "steel_chunk" ) );
tools.insert( tools.end(), 50, item( "welding_wire_steel" ) );
test_repair( tools, true );
test_repair( tools, false, true );
}
SECTION( "welder_missing_goggles" ) {
std::vector<item> tools;
Expand All @@ -119,7 +137,7 @@ TEST_CASE( "repair_vehicle_part", "[vehicle]" )
tools.emplace_back( "hammer" );
tools.insert( tools.end(), 5, item( "steel_chunk" ) );
tools.insert( tools.end(), 50, item( "welding_wire_steel" ) );
test_repair( tools, false );
test_repair( tools, true, false );
}
SECTION( "welder_missing_charge" ) {
std::vector<item> tools;
Expand All @@ -134,7 +152,7 @@ TEST_CASE( "repair_vehicle_part", "[vehicle]" )
tools.emplace_back( "hammer" );
tools.insert( tools.end(), 5, item( "steel_chunk" ) );
tools.insert( tools.end(), 50, item( "welding_wire_steel" ) );
test_repair( tools, false );
test_repair( tools, false, false );
}
SECTION( "UPS_modded_welder_missing_charges" ) {
std::vector<item> tools;
Expand All @@ -151,7 +169,7 @@ TEST_CASE( "repair_vehicle_part", "[vehicle]" )
tools.emplace_back( "goggles_welding" );
tools.insert( tools.end(), 5, item( "steel_chunk" ) );
tools.insert( tools.end(), 50, item( "welding_wire_steel" ) );
test_repair( tools, false );
test_repair( tools, false, false );
}
SECTION( "welder_missing_consumables" ) {
std::vector<item> tools;
Expand All @@ -163,6 +181,6 @@ TEST_CASE( "repair_vehicle_part", "[vehicle]" )
tools.push_back( welder );

tools.emplace_back( "goggles_welding" );
test_repair( tools, false );
test_repair( tools, true, false );
}
}

0 comments on commit de95aef

Please sign in to comment.