forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bodypart.cpp
245 lines (204 loc) · 6.43 KB
/
bodypart.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#include "bodypart.h"
#include "translations.h"
#include "rng.h"
#include "debug.h"
#include "generic_factory.h"
#include <map>
#include <unordered_map>
int body_part_struct::size_sum = 0;
side opposite_side( side s )
{
switch( s ) {
case side::BOTH:
return side::BOTH;
case side::LEFT:
return side::RIGHT;
case side::RIGHT:
return side::LEFT;
}
return s;
}
namespace io
{
static const std::map<std::string, side> side_map = {{
{ "left", side::LEFT },
{ "right", side::RIGHT },
{ "both", side::BOTH }
}
};
template<>
side string_to_enum<side>( const std::string &data )
{
return string_to_enum_look_up( side_map, data );
}
}
static std::array < body_part_struct, num_bp + 1 > human_anatomy;
static std::unordered_map<std::string, body_part> body_parts_map;
namespace
{
const std::unordered_map<std::string, body_part> &get_body_parts_map()
{
return body_parts_map;
}
} // namespace
body_part legacy_id_to_enum( const std::string &legacy_id )
{
static const std::unordered_map<std::string, body_part> body_parts = {
{ "TORSO", bp_torso },
{ "HEAD", bp_head },
{ "EYES", bp_eyes },
{ "MOUTH", bp_mouth },
{ "ARM_L", bp_arm_l },
{ "ARM_R", bp_arm_r },
{ "HAND_L", bp_hand_l },
{ "HAND_R", bp_hand_r },
{ "LEG_L", bp_leg_l },
{ "LEG_R", bp_leg_r },
{ "FOOT_L", bp_foot_l },
{ "FOOT_R", bp_foot_r },
{ "NUM_BP", num_bp },
};
const auto &iter = body_parts.find( legacy_id );
if( iter == body_parts.end() ) {
debugmsg( "Invalid body part id %s", legacy_id.c_str() );
return num_bp;
}
return iter->second;
}
body_part get_body_part_token( const std::string &id )
{
return legacy_id_to_enum( id );
}
const body_part_struct &get_bp( body_part bp )
{
return human_anatomy[ bp ];
}
const body_part_struct &get_bp( const std::string &id )
{
const auto &bmap = get_body_parts_map();
const auto &iter = bmap.find( id );
if( iter == bmap.end() ) {
return human_anatomy[ num_bp ];
}
return human_anatomy[ iter->second ];
}
void body_part_struct::load( JsonObject &jo, const std::string &src )
{
body_part_struct new_part;
new_part.load_this( jo, src );
human_anatomy[ new_part.id ] = new_part;
body_parts_map[ new_part.id_s ] = new_part.id;
}
void body_part_struct::load_this( JsonObject &jo, const std::string & )
{
mandatory( jo, was_loaded, "id", id_s );
mandatory( jo, was_loaded, "name", name );
mandatory( jo, was_loaded, "heading_singular", name_as_heading_singular );
mandatory( jo, was_loaded, "heading_plural", name_as_heading_multiple );
mandatory( jo, was_loaded, "encumbrance_text", encumb_text );
mandatory( jo, was_loaded, "hit_size", hit_size );
mandatory( jo, was_loaded, "legacy_id", legacy_id );
id = legacy_id_to_enum( legacy_id );
mandatory( jo, was_loaded, "main_part", main_part_string );
mandatory( jo, was_loaded, "opposite_part", opposite_part_string );
part_side = jo.get_enum_value<side>( "side" );
}
void body_part_struct::reset()
{
for( auto &part : human_anatomy ) {
// This will cause warnings if data isn't loaded before next check_consistency()
part.id = num_bp;
}
body_parts_map.clear();
}
void body_part_struct::finalize()
{
size_sum = 0;
for( auto &part : human_anatomy ) {
size_sum += part.hit_size;
part.main_part = get_bp( part.main_part_string ).id;
part.opposite_part = get_bp( part.opposite_part_string ).id;
}
}
// @todo get_function_with_better_name
const body_part_struct &body_part_struct::get_part_with_cumulative_hit_size( int size )
{
for( auto &part : human_anatomy ) {
size -= part.hit_size;
if( size <= 0 ) {
return part;
}
}
return human_anatomy[ num_bp ];
}
void body_part_struct::check_consistency()
{
for( int i = 0; i < num_bp; i++ ) {
body_part bp = static_cast<body_part>( i );
const auto &part = human_anatomy[ i ];
if( part.id != bp ) {
debugmsg( "Body part %s has invalid id %d (should be %d)", part.id_s.c_str(), part.id, i );
}
body_part mapped = get_bp( part.id_s ).id;
if( part.id != num_bp && mapped == num_bp ) {
debugmsg( "Body part %s is not in the map", part.id_s.c_str() );
} else if( mapped != part.id ) {
debugmsg( "Body part map has mapped %s to id %d (should be %d)", part.id_s.c_str(), mapped,
part.id );
}
if( part.id != num_bp && part.main_part == num_bp ) {
debugmsg( "Body part %s has unset main part", part.id_s.c_str() );
}
if( part.id != num_bp && part.opposite_part == num_bp ) {
debugmsg( "Body part %s has unset opposite part", part.id_s.c_str() );
}
// @todo Get rid of bp_aiOther
if( static_cast<body_part>( bp_aiOther[ i ] ) != part.opposite_part ) {
debugmsg( "Body part %s has invalid opposite_part %d (should be %d)", part.id_s.c_str(),
part.opposite_part, bp_aiOther[ i ] );
}
}
if( get_part_with_cumulative_hit_size( size_sum ).id == num_bp ) {
debugmsg( "Invalid size_sum calculation" );
}
}
std::string body_part_name( body_part bp )
{
return _( get_bp( bp ).name.c_str() );
}
std::string body_part_name_accusative( body_part bp )
{
return pgettext( "bodypart_accusative", get_bp( bp ).name.c_str() );
}
std::string body_part_name_as_heading( body_part bp, int number )
{
const auto &bdy = get_bp( bp );
return ngettext( bdy.name_as_heading_singular.c_str(), bdy.name_as_heading_multiple.c_str(),
number );
}
std::string encumb_text( body_part bp )
{
const std::string &txt = get_bp( bp ).encumb_text;
return !txt.empty() ? _( txt.c_str() ) : txt;
}
const body_part_struct &body_part_struct::random_part()
{
return get_part_with_cumulative_hit_size( rng( 1, size_sum ) );
}
body_part random_body_part( bool main_parts_only )
{
const auto &part = body_part_struct::random_part();
return main_parts_only ? part.main_part : part.id;
}
body_part mutate_to_main_part( body_part bp )
{
return get_bp( bp ).main_part;
}
body_part opposite_body_part( body_part bp )
{
return get_bp( bp ).opposite_part;
}
std::string get_body_part_id( body_part bp )
{
return get_bp( bp ).legacy_id;
}