File: vehicle_interact_test.cpp

package info (click to toggle)
cataclysm-dda 0.H-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 710,808 kB
  • sloc: cpp: 524,019; python: 11,580; sh: 1,228; makefile: 1,169; xml: 507; javascript: 150; sql: 56; exp: 41; perl: 37
file content (173 lines) | stat: -rw-r--r-- 6,426 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <functional>
#include <iosfwd>
#include <string>
#include <vector>

#include "calendar.h"
#include "cata_catch.h"
#include "character.h"
#include "inventory.h"
#include "item.h"
#include "map.h"
#include "map_helpers.h"
#include "player_helpers.h"
#include "pocket_type.h"
#include "point.h"
#include "requirements.h"
#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 plug_in_tools, bool expect_craftable )
{
    clear_avatar();
    clear_map();

    const tripoint test_origin( 60, 60, 0 );
    Character &player_character = get_player_character();
    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 ) {
        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 );

    const tripoint vehicle_origin = test_origin + tripoint_south_east;
    vehicle *veh_ptr = get_map().add_vehicle( vehicle_prototype_car, vehicle_origin, -90_degrees, 0,
                       0 );

    REQUIRE( veh_ptr != nullptr );
    // Find the frame at the origin.
    vehicle_part *origin_frame = nullptr;
    for( vehicle_part *part : veh_ptr->get_parts_at( vehicle_origin, "", part_status_flag::any ) ) {
        if( part->info().location == "structure" ) {
            origin_frame = part;
            break;
        }
    }
    REQUIRE( origin_frame != nullptr );
    REQUIRE( origin_frame->hp() == origin_frame->info().durability );
    veh_ptr->mod_hp( *origin_frame, -50 );
    REQUIRE( origin_frame->hp() < origin_frame->info().durability );
    // for a steel frame, one quadrant of damage takes 1000 kJ, 5 chunks of steel and 50 welding wires/rods to fix. (it has 400 max hp)

    const vpart_info &vp = origin_frame->info();
    // Assertions about frame part?

    requirement_data reqs = vp.repair_requirements();
    // Bust cache on crafting_inventory()
    player_character.mod_moves( 1 );
    inventory crafting_inv = player_character.crafting_inventory();
    bool can_repair = vp.repair_requirements().can_make_with_inventory(
                          player_character.crafting_inventory(),
                          is_crafting_component );
    CHECK( can_repair == expect_craftable );
}

TEST_CASE( "repair_vehicle_part", "[vehicle]" )
{
    SECTION( "welder" ) {
        std::vector<item> tools;

        item welder( "welder" );
        tools.push_back( welder );

        tools.emplace_back( "goggles_welding" );
        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, true );
    }
    SECTION( "UPS_modded_welder" ) {
        std::vector<item> tools;
        item welder( "welder", calendar::turn_zero, 0 );
        welder.put_in( item( "battery_ups" ), pocket_type::MOD );
        tools.push_back( welder );

        item ups( "UPS_ON" );
        item ups_mag( ups.magazine_default() );
        ups_mag.ammo_set( ups_mag.ammo_default(), 1000 );
        ups.put_in( ups_mag, pocket_type::MAGAZINE_WELL );
        tools.push_back( ups );

        tools.emplace_back( "goggles_welding" );
        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, true );
    }
    SECTION( "welder_missing_goggles" ) {
        std::vector<item> tools;

        item welder( "welder" );
        tools.push_back( welder );

        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, false );
    }
    SECTION( "welder_missing_charge" ) {
        std::vector<item> tools;

        item welder( "welder" );
        tools.push_back( welder );

        tools.emplace_back( "goggles_welding" );
        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, false );
    }
    SECTION( "UPS_modded_welder_missing_charges" ) {
        std::vector<item> tools;
        item welder( "welder", calendar::turn_zero, 0 );
        welder.put_in( item( "battery_ups" ), pocket_type::MOD );
        tools.push_back( welder );

        item ups( "UPS_ON" );
        item ups_mag( ups.magazine_default() );
        ups_mag.ammo_set( ups_mag.ammo_default(), 500 );
        ups.put_in( ups_mag, pocket_type::MAGAZINE_WELL );
        tools.push_back( ups );

        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, false );
    }
    SECTION( "welder_missing_consumables" ) {
        std::vector<item> tools;

        item welder( "welder" );
        tools.push_back( welder );

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