forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
new_character_test.cpp
171 lines (155 loc) · 6.22 KB
/
new_character_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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include "catch/catch.hpp"
#include "game.h"
#include "item.h"
#include "player.h"
#include "profession.h"
#include "scenario.h"
#include "mutation.h"
#include "string_id.h"
#include <vector>
#include <string>
#include <algorithm>
#include <unordered_map>
std::ostream &operator<<( std::ostream &s, const std::vector<trait_id> &v )
{
for( const auto &e : v ) {
s << e.c_str() << " ";
}
return s;
}
static std::vector<trait_id> next_subset( const std::vector<trait_id> &set )
{
// Doing it this way conveniently returns a vector containing solely set[foo] before
// it returns any other vectors with set[foo] in it
static unsigned bitset = 0;
std::vector<trait_id> ret;
++bitset;
// Check each bit position for a match
for( unsigned idx = 0; idx < set.size(); idx++ ) {
if( bitset & ( 1 << idx ) ) {
ret.push_back( set[idx] );
}
}
return ret;
}
static bool try_set_traits( const std::vector<trait_id> &traits )
{
g->u.empty_traits();
g->u.add_traits(); // mandatory prof/scen traits
for( const trait_id &tr : traits ) {
if( g->u.has_conflicting_trait( tr ) || !g->scen->traitquery( tr ) ) {
return false;
} else if( !g->u.has_trait( tr ) ) {
g->u.set_mutation( tr );
}
}
return true;
}
static player get_sanitized_player()
{
// You'd think that this hp stuff would be in the c'tor...
player ret = player();
ret.recalc_hp();
for( int i = 0; i < num_hp_parts; i++ ) {
ret.hp_cur[i] = ret.hp_max[i];
}
// Set these insanely high so can_eat doesn't return TOO_FULL
ret.set_hunger( 10000 );
ret.set_thirst( 10000 );
return ret;
}
// TODO: According to profiling (interrupt, backtrace, wait a few seconds, repeat) with a sample
// size of 20, 70% of the time is due to the call to Character::set_mutation in try_set_traits.
// When the mutation stuff isn't commented out, the test takes 110 minutes (not a typo)!
TEST_CASE( "starting_items" ) {
// Every starting trait that interferes with food/clothing
const std::vector<trait_id> mutations = {
trait_id( "ANTIFRUIT" ),
trait_id( "ANTIJUNK" ),
trait_id( "ANTIWHEAT" ),
//trait_id( "ARM_TENTACLES" ),
//trait_id( "BEAK" ),
trait_id( "CANNIBAL" ),
//trait_id( "CARNIVORE" ),
//trait_id( "HERBIVORE" ),
//trait_id( "HOOVES" ),
trait_id( "LACTOSE" ),
//trait_id( "LEG_TENTACLES" ),
trait_id( "MEATARIAN" ),
//trait_id( "RAP_TALONS" ),
//trait_id( "TAIL_FLUFFY" ),
//trait_id( "TAIL_LONG" ),
trait_id( "VEGETARIAN" ),
trait_id( "WOOLALLERGY" )
};
// Prof/scen combinations that need to be checked.
std::unordered_map<const scenario *, std::vector<string_id<profession>>> scen_prof_combos;
for( const auto &id : scenario::generic()->permitted_professions() ) {
scen_prof_combos[scenario::generic()].push_back( id );
}
/*for( const scenario &scen : scenario::get_all() ) {
const bool special = std::any_of( mutation_branch::get_all().begin(), mutation_branch::get_all().end(),
[&scen]( const std::pair<trait_id, mutation_branch> &elem ) {
return !elem.second.startingtrait && scen.traitquery( elem.first );
} );
if( !special && &scen != scenario::generic() ) {
// The only scenarios that need checked are the ones that give access to mutation traits, and
// the generic scenario
continue;
}
for( const auto &id : scen.permitted_professions() ) {
scen_prof_combos[&scen].push_back( id );
}
}*/
struct failure {
string_id<profession> prof;
std::vector<trait_id> mut;
itype_id item_name;
std::string reason;
};
std::vector<failure> failures;
auto add_failure = [&]( const profession &prof, const std::vector<trait_id> &traits,
const std::string &item_name, const std::string &reason ) {
if( !std::any_of( failures.begin(), failures.end(), [&item_name]( const failure &f ) {
return f.item_name == item_name;
} ) ) {
failures.push_back( failure{ prof.ident(), traits, item_name, reason } );
}
};
g->u = get_sanitized_player();
const player control = get_sanitized_player(); // Avoid false positives from ingredients like salt and cornmeal
std::vector<trait_id> traits = next_subset( mutations );
for( ; !traits.empty(); traits = next_subset( mutations ) ) {
for( const auto &pair : scen_prof_combos ) {
g->scen = pair.first;
for( const string_id<profession> &prof : pair.second ) {
g->u.prof = &prof.obj();
if( !try_set_traits( traits ) ) {
continue; // Trait conflict: this prof/scen/trait combo is impossible to attain
}
for( int i = 0; i < 2; i++ ) {
g->u.worn.clear();
g->u.reset_encumbrance();
g->u.male = i == 0;
std::list<item> items = prof->items( g->u.male, traits );
for( const item &it : items ) {
items.insert( items.begin(), it.contents.begin(), it.contents.end() );
}
for( const item &it : items ) {
// Seeds don't count- they're for growing things, not eating
if( !it.is_seed() && it.is_food() && g->u.can_eat( it, false, false ) != EDIBLE &&
control.can_eat( it, false, false ) == EDIBLE ) {
add_failure( *prof, g->u.get_mutations(), it.typeId(), "Couldn't eat it" );
} else if( it.is_armor() && !g->u.wear_item( it, false ) ) {
add_failure( *prof, g->u.get_mutations(), it.typeId(), "Couldn't wear it" );
}
}
} // all genders
} // all profs
} // all scens
}
for( const failure &f : failures ) {
std::cout << f.prof.c_str() << " " << f.mut << " " << f.item_name << ": " << f.reason << "\n";
}
REQUIRE( failures.empty() );
}