-
Notifications
You must be signed in to change notification settings - Fork 280
/
encumbrance_test.cpp
152 lines (137 loc) · 4.39 KB
/
encumbrance_test.cpp
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
#include "catch/catch.hpp"
#include <array>
#include <functional>
#include <list>
#include <memory>
#include <string>
#include <vector>
#include "avatar.h"
#include "bodypart.h"
#include "character.h"
#include "character_encumbrance.h"
#include "game.h"
#include "item.h"
#include "material.h"
#include "npc.h"
#include "player.h"
#include "state_helpers.h"
#include "type_id.h"
static void test_encumbrance_on(
player &p,
std::vector<detached_ptr<item>> &clothing,
const std::string &body_part,
int expected_encumbrance,
const std::function<void( player & )> &tweak_player = {}
)
{
CAPTURE( body_part );
p.set_body();
p.clear_mutations();
p.worn.clear();
if( tweak_player ) {
tweak_player( p );
}
for( detached_ptr<item> &i : clothing ) {
p.worn.push_back( std::move( i ) );
}
p.reset_encumbrance();
encumbrance_data enc = p.get_encumbrance().elems[ get_body_part_token( body_part ) ];
CHECK( enc.encumbrance == expected_encumbrance );
}
static void test_encumbrance_items(
std::vector<detached_ptr<item>> &clothing,
const std::string &body_part,
const int expected_encumbrance,
const std::function<void( player & )> &tweak_player = {}
)
{
// Test NPC first because NPC code can accidentally end up using properties
// of g->u, and such bugs are hidden if we test the other way around.
SECTION( "testing on npc" ) {
npc example_npc;
test_encumbrance_on( example_npc, clothing, body_part, expected_encumbrance, tweak_player );
}
SECTION( "testing on player" ) {
test_encumbrance_on( get_avatar(), clothing, body_part, expected_encumbrance, tweak_player );
}
}
static void test_encumbrance(
const std::vector<std::string> &clothing_types,
const std::string &body_part,
const int expected_encumbrance
)
{
CAPTURE( clothing_types );
std::vector<detached_ptr<item>> clothing;
for( const std::string &type : clothing_types ) {
clothing.push_back( item::spawn( type ) );
}
test_encumbrance_items( clothing, body_part, expected_encumbrance );
}
struct add_trait {
add_trait( const std::string &t ) : trait( t ) {}
add_trait( const trait_id &t ) : trait( t ) {}
void operator()( player &p ) {
p.toggle_trait( trait );
}
trait_id trait;
};
static constexpr int karate_gi_e = 0;
static constexpr int vest_e = 5;
static constexpr int greatcoat_e = 23;
TEST_CASE( "regular_clothing_encumbrance", "[encumbrance]" )
{
clear_all_state();
test_encumbrance( { "karate_gi" }, "TORSO", karate_gi_e );
test_encumbrance( { "vest" }, "TORSO", vest_e );
test_encumbrance( { "greatcoat" }, "TORSO", greatcoat_e );
}
TEST_CASE( "separate_layer_encumbrance", "[encumbrance]" )
{
clear_all_state();
test_encumbrance( { "vest", "greatcoat" }, "TORSO", vest_e + greatcoat_e );
}
TEST_CASE( "out_of_order_encumbrance", "[encumbrance]" )
{
clear_all_state();
test_encumbrance( { "greatcoat", "vest" }, "TORSO", vest_e * 2 + greatcoat_e );
}
TEST_CASE( "same_layer_encumbrance", "[encumbrance]" )
{
clear_all_state();
// When stacking within a layer, encumbrance for additional items is
// counted twice
test_encumbrance( { "vest", "vest" }, "TORSO", vest_e * 2 + vest_e );
// ... with a minimum of 2
test_encumbrance( { "karate_gi", "karate_gi" }, "TORSO", karate_gi_e * 2 + 2 );
// ... and a maximum of 10
test_encumbrance( { "greatcoat", "greatcoat" }, "TORSO", greatcoat_e * 2 + 10 );
}
TEST_CASE( "tiny_clothing", "[encumbrance]" )
{
clear_all_state();
detached_ptr<item> i = item::spawn( "vest" );
i->set_flag( flag_id( "UNDERSIZE" ) );
std::vector<detached_ptr<item>> items;
items.push_back( std::move( i ) );
test_encumbrance_items( items, "TORSO",
vest_e * 3 );
}
TEST_CASE( "tiny_character", "[encumbrance]" )
{
clear_all_state();
detached_ptr<item> i = item::spawn( "vest" );
item &obj = *i;
std::vector<detached_ptr<item>> items;
items.push_back( std::move( i ) );
SECTION( "regular shirt" ) {
test_encumbrance_items( items, "TORSO",
vest_e * 2,
add_trait( "SMALL2" ) );
}
SECTION( "undersize shrt" ) {
obj.set_flag( flag_id( "UNDERSIZE" ) );
test_encumbrance_items( items, "TORSO", vest_e,
add_trait( "SMALL2" ) );
}
}