1 #include <functional>
2 #include <iosfwd>
3 #include <string>
4 #include <vector>
5 
6 #include "calendar.h"
7 #include "catch/catch.hpp"
8 #include "character.h"
9 #include "inventory.h"
10 #include "item.h"
11 #include "item_pocket.h"
12 #include "map.h"
13 #include "map_helpers.h"
14 #include "player_helpers.h"
15 #include "point.h"
16 #include "requirements.h"
17 #include "ret_val.h"
18 #include "type_id.h"
19 #include "units.h"
20 #include "veh_type.h"
21 #include "vehicle.h"
22 
test_repair(const std::vector<item> & tools,bool expect_craftable)23 static void test_repair( const std::vector<item> &tools, bool expect_craftable )
24 {
25     clear_avatar();
26     clear_map();
27 
28     const tripoint test_origin( 60, 60, 0 );
29     Character &player_character = get_player_character();
30     player_character.setpos( test_origin );
31     const item backpack( "backpack" );
32     player_character.wear_item( backpack );
33     for( const item &gear : tools ) {
34         player_character.i_add( gear );
35     }
36 
37     const tripoint vehicle_origin = test_origin + tripoint_south_east;
38     vehicle *veh_ptr = get_map().add_vehicle( vproto_id( "bicycle" ), vehicle_origin, -90_degrees,
39                        0, 0 );
40     REQUIRE( veh_ptr != nullptr );
41     // Find the frame at the origin.
42     vehicle_part *origin_frame = nullptr;
43     for( vehicle_part *part : veh_ptr->get_parts_at( vehicle_origin, "", part_status_flag::any ) ) {
44         if( part->info().location == "structure" ) {
45             origin_frame = part;
46             break;
47         }
48     }
49     REQUIRE( origin_frame != nullptr );
50     REQUIRE( origin_frame->hp() == origin_frame->info().durability );
51     veh_ptr->mod_hp( *origin_frame, -100 );
52     REQUIRE( origin_frame->hp() < origin_frame->info().durability );
53 
54     const vpart_info &vp = origin_frame->info();
55     // Assertions about frame part?
56 
57     requirement_data reqs = vp.repair_requirements();
58     // Bust cache on crafting_inventory()
59     player_character.mod_moves( 1 );
60     inventory crafting_inv = player_character.crafting_inventory();
61     bool can_repair = vp.repair_requirements().can_make_with_inventory(
62                           player_character.crafting_inventory(),
63                           is_crafting_component );
64     CHECK( can_repair == expect_craftable );
65 }
66 
67 TEST_CASE( "repair_vehicle_part" )
68 {
69     SECTION( "welder" ) {
70         std::vector<item> tools;
71         tools.push_back( tool_with_ammo( "welder", 500 ) );
72         tools.emplace_back( "goggles_welding" );
73         test_repair( tools, true );
74     }
75     SECTION( "UPS_modded_welder" ) {
76         std::vector<item> tools;
77         item welder( "welder", calendar::turn_zero, 0 );
78         welder.put_in( item( "battery_ups" ), item_pocket::pocket_type::MOD );
79         tools.push_back( welder );
80         tools.emplace_back( "UPS_off", calendar::turn_zero, 500 );
81         tools.emplace_back( "goggles_welding" );
82         test_repair( tools, true );
83     }
84     SECTION( "welder_missing_goggles" ) {
85         std::vector<item> tools;
86         tools.push_back( tool_with_ammo( "welder", 500 ) );
87         test_repair( tools, false );
88     }
89     SECTION( "welder_missing_charge" ) {
90         std::vector<item> tools;
91         tools.push_back( tool_with_ammo( "welder", 5 ) );
92         tools.emplace_back( "goggles_welding" );
93         test_repair( tools, false );
94     }
95     SECTION( "UPS_modded_welder_missing_charges" ) {
96         std::vector<item> tools;
97         item welder( "welder", calendar::turn_zero, 0 );
98         welder.put_in( item( "battery_ups" ), item_pocket::pocket_type::MOD );
99         tools.push_back( welder );
100         tools.emplace_back( "UPS_off", calendar::turn_zero, 5 );
101         tools.emplace_back( "goggles_welding" );
102         test_repair( tools, false );
103     }
104 }
105