forked from cataclysmbnteam/Cataclysm-BN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bodypart.cpp
491 lines (411 loc) · 12.4 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
#include "bodypart.h"
#include <cstdlib>
#include <set>
#include <unordered_map>
#include <utility>
#include <vector>
#include "anatomy.h"
#include "debug.h"
#include "enum_conversions.h"
#include "generic_factory.h"
#include "json.h"
#include "pldata.h"
#include "type_id.h"
const bodypart_str_id body_part_head( "head" );
const bodypart_str_id body_part_eyes( "eyes" );
const bodypart_str_id body_part_mouth( "mouth" );
const bodypart_str_id body_part_torso( "torso" );
const bodypart_str_id body_part_arm_l( "arm_l" );
const bodypart_str_id body_part_arm_r( "arm_r" );
const bodypart_str_id body_part_hand_l( "hand_l" );
const bodypart_str_id body_part_hand_r( "hand_r" );
const bodypart_str_id body_part_leg_l( "leg_l" );
const bodypart_str_id body_part_foot_l( "foot_l" );
const bodypart_str_id body_part_leg_r( "leg_r" );
const bodypart_str_id body_part_foot_r( "foot_r" );
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;
case side::num_sides:
debugmsg( "invalid side %d", static_cast<int>( s ) );
break;
}
return s;
}
namespace io
{
template<>
std::string enum_to_string<side>( side data )
{
switch( data ) {
// *INDENT-OFF*
case side::LEFT: return "left";
case side::RIGHT: return "right";
case side::BOTH: return "both";
// *INDENT-ON*
case side::num_sides:
break;
}
debugmsg( "Invalid side" );
abort();
}
template<>
std::string enum_to_string<hp_part>( hp_part data )
{
switch( data ) {
// *INDENT-OFF*
case hp_part::hp_head: return "head";
case hp_part::hp_torso: return "torso";
case hp_part::hp_arm_l: return "arm_l";
case hp_part::hp_arm_r: return "arm_r";
case hp_part::hp_leg_l: return "leg_l";
case hp_part::hp_leg_r: return "leg_r";
// *INDENT-ON*
case hp_part::num_hp_parts:
break;
}
debugmsg( "Invalid hp_part" );
abort();
}
} // namespace io
namespace
{
generic_factory<body_part_type> body_part_factory( "body part" );
} // namespace
static 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 },
// Also try the new ones, just in case
{ "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 legacy id %s", legacy_id.c_str() );
return num_bp;
}
return iter->second;
}
/**@relates string_id*/
template<>
bool string_id<body_part_type>::is_valid() const
{
return body_part_factory.is_valid( *this );
}
/** @relates int_id */
template<>
bool int_id<body_part_type>::is_valid() const
{
return body_part_factory.is_valid( *this );
}
/**@relates string_id*/
template<>
const body_part_type &string_id<body_part_type>::obj() const
{
return body_part_factory.obj( *this );
}
/** @relates int_id */
template<>
const body_part_type &int_id<body_part_type>::obj() const
{
return body_part_factory.obj( *this );
}
/** @relates int_id */
template<>
const bodypart_str_id &int_id<body_part_type>::id() const
{
return body_part_factory.convert( *this );
}
/**@relates string_id*/
template<>
bodypart_id string_id<body_part_type>::id() const
{
return body_part_factory.convert( *this, bodypart_id( 0 ) );
}
/** @relates int_id */
template<>
int_id<body_part_type>::int_id( const string_id<body_part_type> &id ) : _id( id.id() ) {}
body_part get_body_part_token( const std::string &id )
{
return legacy_id_to_enum( id );
}
const bodypart_str_id &convert_bp( body_part bp )
{
static const std::vector<bodypart_str_id> body_parts = {
bodypart_str_id( "torso" ),
bodypart_str_id( "head" ),
bodypart_str_id( "eyes" ),
bodypart_str_id( "mouth" ),
bodypart_str_id( "arm_l" ),
bodypart_str_id( "arm_r" ),
bodypart_str_id( "hand_l" ),
bodypart_str_id( "hand_r" ),
bodypart_str_id( "leg_l" ),
bodypart_str_id( "leg_r" ),
bodypart_str_id( "foot_l" ),
bodypart_str_id( "foot_r" ),
bodypart_str_id::NULL_ID()
};
if( bp > num_bp || bp < bp_torso ) {
debugmsg( "Invalid body part token %d", bp );
return body_parts[ num_bp ];
}
return body_parts[static_cast<size_t>( bp )];
}
static const body_part_type &get_bp( body_part bp )
{
return convert_bp( bp ).obj();
}
void body_part_type::load_bp( const JsonObject &jo, const std::string &src )
{
body_part_factory.load( jo, src );
}
void body_part_type::load( const JsonObject &jo, const std::string & )
{
mandatory( jo, was_loaded, "id", id );
mandatory( jo, was_loaded, "name", name );
// This is NOT the plural of `name`; it's a name refering to the pair of
// bodyparts which this bodypart belongs to, and thus should not be implemented
// using "vgettext" or "translation::make_plural". Otherwise, in languages
// without plural forms, translation of this string would indicate it
// to be a left or right part, while it is not.
optional( jo, was_loaded, "name_multiple", name_multiple );
mandatory( jo, was_loaded, "accusative", accusative );
// same as the above comment
optional( jo, was_loaded, "accusative_multiple", accusative_multiple );
mandatory( jo, was_loaded, "heading", name_as_heading );
// Same as the above comment
optional( jo, was_loaded, "heading_multiple", name_as_heading_multiple, name_as_heading );
optional( jo, was_loaded, "hp_bar_ui_text", hp_bar_ui_text );
optional( jo, was_loaded, "encumbrance_text", encumb_text );
assign( jo, "hit_size", hit_size, true );
assign( jo, "hit_difficulty", hit_difficulty, true );
assign( jo, "hit_size_relative", hit_size_relative, true );
assign( jo, "base_hp", base_hp, true );
assign( jo, "legacy_id", legacy_id );
token = legacy_id_to_enum( legacy_id );
optional( jo, was_loaded, "main_part", main_part, id );
optional( jo, was_loaded, "opposite_part", opposite_part, id );
optional( jo, was_loaded, "essential", essential, false );
optional( jo, was_loaded, "hot_morale_mod", hot_morale_mod, 0.0 );
optional( jo, was_loaded, "cold_morale_mod", cold_morale_mod, 0.0 );
optional( jo, was_loaded, "stylish_bonus", stylish_bonus, 0 );
optional( jo, was_loaded, "squeamish_penalty", squeamish_penalty, 0 );
optional( jo, was_loaded, "bionic_slots", bionic_slots_, 0 );
const auto side_reader = enum_flags_reader<side> { "side" };
optional( jo, was_loaded, "side", part_side, side_reader, side::BOTH );
}
void body_part_type::reset()
{
body_part_factory.reset();
}
void body_part_type::finalize_all()
{
body_part_factory.finalize();
}
void body_part_type::finalize()
{
}
void body_part_type::check_consistency()
{
for( const body_part bp : all_body_parts ) {
const auto &legacy_bp = convert_bp( bp );
if( !legacy_bp.is_valid() ) {
debugmsg( "Mandatory body part %s was not loaded", legacy_bp.c_str() );
}
}
body_part_factory.check();
}
void body_part_type::check() const
{
const auto &under_token = get_bp( token );
if( this != &under_token ) {
debugmsg( "Body part %s has duplicate token %d, mapped to %s", id.c_str(), token,
under_token.id.c_str() );
}
if( !id.is_null() && main_part.is_null() ) {
debugmsg( "Body part %s has unset main part", id.c_str() );
}
if( !id.is_null() && opposite_part.is_null() ) {
debugmsg( "Body part %s has unset opposite part", id.c_str() );
}
if( !main_part.is_valid() ) {
debugmsg( "Body part %s has invalid main part %s.", id.c_str(), main_part.c_str() );
}
if( !opposite_part.is_valid() ) {
debugmsg( "Body part %s has invalid opposite part %s.", id.c_str(), opposite_part.c_str() );
}
}
std::string body_part_name( body_part bp, int number )
{
return body_part_name( convert_bp( bp ), number );
}
std::string body_part_name( const bodypart_id &bp, int number )
{
// See comments in `body_part_type::load` about why these two strings are
// not a single translation object with plural enabled.
return number > 1 ? bp->name_multiple.translated() : bp->name.translated();
}
std::string body_part_name_accusative( body_part bp, int number )
{
return body_part_name_accusative( convert_bp( bp ), number );
}
std::string body_part_name_accusative( const bodypart_id &bp, int number )
{
// See comments in `body_part_type::load` about why these two strings are
// not a single translation object with plural enabled.
return number > 1 ? bp->accusative_multiple.translated() : bp->accusative.translated();
}
std::string body_part_name_as_heading( body_part bp, int number )
{
return body_part_name_as_heading( convert_bp( bp ), number );
}
std::string body_part_name_as_heading( const bodypart_id &bp, int number )
{
// See comments in `body_part_type::load` about why these two strings are
// not a single translation object with plural enabled.
return number > 1 ? bp->name_as_heading_multiple.translated() : bp->name_as_heading.translated();
}
std::string body_part_hp_bar_ui_text( const bodypart_id &bp )
{
return _( bp->hp_bar_ui_text );
}
std::string encumb_text( body_part bp )
{
const std::string &txt = get_bp( bp ).encumb_text;
return !txt.empty() ? _( txt ) : txt;
}
body_part random_body_part( bool main_parts_only )
{
const auto &part = human_anatomy->random_body_part();
return main_parts_only ? part->main_part->token : part->token;
}
body_part mutate_to_main_part( body_part bp )
{
return get_bp( bp ).main_part->token;
}
body_part opposite_body_part( body_part bp )
{
return get_bp( bp ).opposite_part->token;
}
bodypart_id bodypart::get_id() const
{
return id;
}
void bodypart::set_hp_to_max()
{
hp_cur = hp_max;
}
bool bodypart::is_at_max_hp() const
{
return hp_cur == hp_max;
}
int bodypart::get_hp_cur() const
{
return hp_cur;
}
int bodypart::get_hp_max() const
{
return hp_max;
}
int bodypart::get_healed_total() const
{
return healed_total;
}
int bodypart::get_damage_bandaged() const
{
return damage_bandaged;
}
int bodypart::get_damage_disinfected() const
{
return damage_disinfected;
}
void bodypart::set_hp_cur( int set )
{
hp_cur = set;
}
void bodypart::set_hp_max( int set )
{
hp_max = set;
}
void bodypart::set_healed_total( int set )
{
healed_total = set;
}
void bodypart::set_damage_bandaged( int set )
{
damage_bandaged = set;
}
void bodypart::set_damage_disinfected( int set )
{
damage_disinfected = set;
}
void bodypart::mod_hp_cur( int mod )
{
hp_cur += mod;
}
void bodypart::mod_hp_max( int mod )
{
hp_max += mod;
}
void bodypart::mod_healed_total( int mod )
{
healed_total += mod;
}
void bodypart::mod_damage_bandaged( int mod )
{
damage_bandaged += mod;
}
void bodypart::mod_damage_disinfected( int mod )
{
damage_disinfected += mod;
}
void bodypart::serialize( JsonOut &json ) const
{
json.start_object();
json.member( "id", id );
json.member( "hp_cur", hp_cur );
json.member( "hp_max", hp_max );
json.member( "damage_bandaged", damage_bandaged );
json.member( "damage_disinfected", damage_disinfected );
json.end_object();
}
void bodypart::deserialize( JsonIn &jsin )
{
JsonObject jo = jsin.get_object();
jo.read( "id", id, true );
jo.read( "hp_cur", hp_cur, true );
jo.read( "hp_max", hp_max, true );
jo.read( "damage_bandaged", damage_bandaged, true );
jo.read( "damage_disinfected", damage_disinfected, true );
}