forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbodypart.h
617 lines (514 loc) · 19.5 KB
/
bodypart.h
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
#pragma once
#ifndef CATA_SRC_BODYPART_H
#define CATA_SRC_BODYPART_H
#include <array>
#include <cstddef>
#include <initializer_list>
#include <iosfwd>
#include <set>
#include <string>
#include <utility>
#include <vector>
#include "damage.h"
#include "enums.h"
#include "flat_set.h"
#include "int_id.h"
#include "string_id.h"
#include "translations.h"
#include "subbodypart.h"
#include "type_id.h"
class JsonObject;
class JsonOut;
class JsonValue;
struct body_part_type;
template <typename E> struct enum_traits;
using bodypart_str_id = string_id<body_part_type>;
using bodypart_id = int_id<body_part_type>;
extern const bodypart_str_id body_part_head;
extern const bodypart_str_id body_part_eyes;
extern const bodypart_str_id body_part_mouth;
extern const bodypart_str_id body_part_torso;
extern const bodypart_str_id body_part_arm_l;
extern const bodypart_str_id body_part_arm_r;
extern const bodypart_str_id body_part_hand_l;
extern const bodypart_str_id body_part_hand_r;
extern const bodypart_str_id body_part_leg_l;
extern const bodypart_str_id body_part_foot_l;
extern const bodypart_str_id body_part_leg_r;
extern const bodypart_str_id body_part_foot_r;
// The order is important ; pldata.h has to be in the same order
enum body_part : int {
bp_torso = 0,
bp_head,
bp_eyes,
bp_mouth,
bp_arm_l,
bp_arm_r,
bp_hand_l,
bp_hand_r,
bp_leg_l,
bp_leg_r,
bp_foot_l,
bp_foot_r,
num_bp
};
template<>
struct enum_traits<body_part> {
static constexpr body_part last = body_part::num_bp;
};
enum class side : int;
// Drench cache
enum water_tolerance {
WT_IGNORED = 0,
WT_NEUTRAL,
WT_GOOD,
NUM_WATER_TOLERANCE
};
/**
* Contains all valid @ref body_part values in the order they are
* defined in. Use this to iterate over them.
*/
constexpr std::array<body_part, 12> all_body_parts = {{
bp_torso, bp_head, bp_eyes, bp_mouth,
bp_arm_l, bp_arm_r, bp_hand_l, bp_hand_r,
bp_leg_l, bp_leg_r, bp_foot_l, bp_foot_r
}
};
struct stat_hp_mods {
float str_mod = 3.0f;
float dex_mod = 0.0f;
float int_mod = 0.0f;
float per_mod = 0.0f;
float health_mod = 0.0f;
bool was_loaded = false;
void load( const JsonObject &jsobj );
void deserialize( const JsonObject &jo );
};
struct limb_score {
public:
static void load_limb_scores( const JsonObject &jo, const std::string &src );
static void reset();
void load( const JsonObject &jo, const std::string &src );
static const std::vector<limb_score> &get_all();
const limb_score_id &getId() const {
return id;
}
const translation &name() const {
return _name;
}
bool affected_by_wounds() const {
return wound_affect;
}
bool affected_by_encumb() const {
return encumb_affect;
}
private:
limb_score_id id;
translation _name;
bool wound_affect = true;
bool encumb_affect = true;
bool was_loaded = false;
friend class generic_factory<limb_score>;
};
struct bp_limb_score {
limb_score_id id = limb_score_id::NULL_ID();
float score = 0.0f;
float max = 0.0f;
};
struct body_part_type {
public:
/**
* the different types of body parts there are.
* this allows for the ability to group limbs or determine a limb of a certain type
*/
enum class type {
// this is where helmets go, and is a vital part.
head,
// the torso is generally the center of mass of a creature
torso,
// provides sight
sensor,
// you eat and scream with this
mouth,
// may manipulate objects to some degree, is a main part
arm,
// manipulates objects. usually is not a main part.
hand,
// provides motive power
leg,
// helps with balance. usually is not a main part
foot,
// may reduce fall damage
wing,
// may provide balance or manipulation
tail,
// more of a general purpose limb, such as horns.
other,
num_types
};
bodypart_str_id id;
bool was_loaded = false;
// Those are stored untranslated
translation name;
translation name_multiple;
translation accusative;
translation accusative_multiple;
translation name_as_heading;
translation name_as_heading_multiple;
translation smash_message;
translation hp_bar_ui_text;
translation encumb_text;
// Legacy "string id"
std::string legacy_id;
// Legacy enum "int id"
body_part token = num_bp;
/** Size of the body part for melee targeting. */
float hit_size = 0.0f;
/** Sub-location of the body part used for encumberance, coverage and determining protection
*/
std::vector<sub_bodypart_str_id> sub_parts;
/**
* How hard is it to hit a given body part, assuming "owner" is hit.
* Higher number means good hits will veer towards this part,
* lower means this part is unlikely to be hit by inaccurate attacks.
* Formula is `chance *= pow(hit_roll, hit_difficulty)`
*/
float hit_difficulty = 0.0f;
// "Parent" of this part for damage purposes - main parts are their own "parents"
bodypart_str_id main_part;
// "Parent" of this part for connectedness - should be next part towards head.
// Head connects to itself.
bodypart_str_id connected_to;
// A part that has no opposite is its own opposite (that's pretty Zen)
bodypart_str_id opposite_part;
// Parts with no opposites have BOTH here
side part_side = side::BOTH;
body_part_type::type limb_type = body_part_type::type::num_types;
cata::flat_set <body_part_type::type> secondary_types;
bool has_type( const body_part_type::type &type ) const;
// Threshold to start encumbrance scaling
int encumbrance_threshold = 0;
// Limit of encumbrance, after reaching this point the limb contributes no scores
int encumbrance_limit = 0;
// Health at which the limb stops contributing its conditional flags / techs
int health_limit = 0;
// If true, extra encumbrance on this limb affects dodge effectiveness
bool encumb_impacts_dodge = false;
float smash_efficiency = 0.5f;
//Morale parameters
float hot_morale_mod = 0.0f;
float cold_morale_mod = 0.0f;
float stylish_bonus = 0.0f;
int squeamish_penalty = 0;
int fire_warmth_bonus = 0;
//Innate environmental protection
int env_protection = 0;
int base_hp = 60;
stat_hp_mods hp_mods;
// Innate healing rate of the bodypart
int heal_bonus = 0;
float mend_rate = 1.0f;
// if a limb is vital and at 0 hp, you die.
bool is_vital = false;
bool is_limb = false;
// Intrinsic temperature bonus of the bodypart
int temp_min = 0;
// Temperature bonus to apply when not overheated
int temp_max = 0;
int drench_max = 0;
int drench_increment = 2;
int drying_chance = 1;
int drying_increment = 1;
// Wetness morale bonus/malus of the limb
int wet_morale = 0;
cata::flat_set<json_character_flag> flags;
cata::flat_set<json_character_flag> conditional_flags;
bool has_flag( const json_character_flag &flag ) const;
// Limb-specific attacks
std::set<matec_id> techniques;
int technique_enc_limit = 50;
bool unarmed_bonus = false;
// return a random sub part from the weighted list of subparts
// if secondary is true instead returns a part from only the secondary sublocations
sub_bodypart_id random_sub_part( bool secondary ) const;
void load( const JsonObject &jo, const std::string &src );
void finalize();
void check() const;
static void load_bp( const JsonObject &jo, const std::string &src );
static const std::vector<body_part_type> &get_all();
// Clears all bps
static void reset();
// Post-load finalization
static void finalize_all();
// Verifies that body parts make sense
static void check_consistency();
float get_limb_score( const limb_score_id &id ) const {
for( const bp_limb_score &bpls : limb_scores ) {
if( bpls.id == id ) {
return bpls.score;
}
}
return 0.0f;
}
float get_limb_score_max( const limb_score_id &id ) const {
for( const bp_limb_score &bpls : limb_scores ) {
if( bpls.id == id ) {
return bpls.max;
}
}
return 0.0f;
}
bool has_limb_score( const limb_score_id &id ) const {
for( const bp_limb_score &bpls : limb_scores ) {
if( bpls.id == id ) {
return true;
}
}
return false;
}
int bionic_slots() const {
return bionic_slots_;
}
float unarmed_damage( const damage_type &dt ) const;
float unarmed_arpen( const damage_type &dt ) const;
float damage_resistance( const damage_type &dt ) const;
float damage_resistance( const damage_unit &du ) const;
private:
int bionic_slots_ = 0;
// limb score values
std::vector<bp_limb_score> limb_scores;
damage_instance damage;
// Protection from various damage types
resistances armor;
};
template<>
struct enum_traits<body_part_type::type> {
static constexpr body_part_type::type last = body_part_type::type::num_types;
};
struct layer_details {
std::vector<int> pieces;
int max = 0;
int total = 0;
// if the layer is conflicting
bool is_conflicting = false;
std::vector<sub_bodypart_id> covered_sub_parts;
void reset();
int layer( int encumbrance, bool conflicts );
bool operator ==( const layer_details &rhs ) const {
return max == rhs.max &&
total == rhs.total &&
pieces == rhs.pieces;
}
};
struct encumbrance_data {
int encumbrance = 0;
int armor_encumbrance = 0;
int layer_penalty = 0;
std::array<layer_details, static_cast<size_t>( layer_level::NUM_LAYER_LEVELS )>
layer_penalty_details;
bool add_sub_locations( const layer_level level, const std::vector<sub_bodypart_id> &sub_parts ) {
bool return_val = false;
for( const sub_bodypart_id &sbp : sub_parts ) {
bool found = false;
for( const sub_bodypart_id &layer_sbp : layer_penalty_details[static_cast<size_t>
( level )].covered_sub_parts ) {
// if we find a location return true since we should add penalty
if( sbp == layer_sbp ) {
found = true;
}
}
// if we've found it already in the list mark our return value as true
if( found ) {
return_val = true;
}
// otherwise we should add it to the list
else {
layer_penalty_details[static_cast<size_t>( level )].covered_sub_parts.push_back( sbp );
}
}
return return_val;
}
bool add_sub_locations( const layer_level level,
const std::vector<sub_bodypart_str_id> &sub_parts ) {
bool return_val = false;
for( const sub_bodypart_str_id &temp : sub_parts ) {
const sub_bodypart_id &sbp = temp;
bool found = false;
for( const sub_bodypart_id &layer_sbp : layer_penalty_details[static_cast<size_t>
( level )].covered_sub_parts ) {
// if we find a location return true since we should add penalty
if( sbp == layer_sbp ) {
found = true;
}
}
// if we've found it already in the list mark our return value as true
if( found ) {
return_val = true;
}
// otherwise we should add it to the list
else {
layer_penalty_details[static_cast<size_t>( level )].covered_sub_parts.push_back( sbp );
}
}
return return_val;
}
void layer( const layer_level level, const int encumbrance, bool conflicts ) {
layer_penalty_details[static_cast<size_t>( level )].layer( encumbrance, conflicts );
}
void reset() {
*this = encumbrance_data();
}
bool operator ==( const encumbrance_data &rhs ) const {
return encumbrance == rhs.encumbrance &&
armor_encumbrance == rhs.armor_encumbrance &&
layer_penalty == rhs.layer_penalty &&
layer_penalty_details == rhs.layer_penalty_details;
}
};
class bodypart
{
private:
bodypart_str_id id;
int hp_cur = 0;
int hp_max = 0;
int wetness = 0;
int temp_cur = 5000; // BODYTEMP_NORM = 5000
int temp_conv = 5000;
int frostbite_timer = 0;
int healed_total = 0;
int damage_bandaged = 0;
int damage_disinfected = 0;
encumbrance_data encumb_data; // NOLINT(cata-serialize)
std::array<int, NUM_WATER_TOLERANCE> mut_drench; // NOLINT(cata-serialize)
// adjust any limb "value" based on how wounded the limb is. scaled to 0-75%
float wound_adjusted_limb_value( float val ) const;
// Same idea as for wounds, though not all scores get this applied. Should be applied after wounds.
float encumb_adjusted_limb_value( float val ) const;
// If the limb score is affected by a skill, adjust it by the skill's level (used for swimming)
float skill_adjusted_limb_value( float val, int skill ) const;
public:
bodypart(): id( bodypart_str_id::NULL_ID() ), mut_drench() {}
explicit bodypart( bodypart_str_id id ): id( id ), hp_cur( id->base_hp ), hp_max( id->base_hp ),
mut_drench() {}
bodypart_id get_id() const;
void set_hp_to_max();
bool is_at_max_hp() const;
float get_wetness_percentage() const;
int get_encumbrance_threshold() const;
// Check if we're above our encumbrance limit
bool is_limb_overencumbered() const;
bool has_conditional_flag( const json_character_flag &flag ) const;
// Get our limb attacks
std::set<matec_id> get_limb_techs() const;
// Get modified limb score as defined in limb_scores.json.
// override forces the limb score to be affected by encumbrance/wounds (-1 == no override).
float get_limb_score( const limb_score_id &score, int skill = -1, int override_encumb = -1,
int override_wounds = -1 ) const;
float get_limb_score_max( const limb_score_id &score ) const;
int get_hp_cur() const;
int get_hp_max() const;
int get_healed_total() const;
int get_damage_bandaged() const;
int get_damage_disinfected() const;
int get_drench_capacity() const;
int get_wetness() const;
int get_frostbite_timer() const;
int get_temp_cur() const;
int get_temp_conv() const;
std::array<int, NUM_WATER_TOLERANCE> get_mut_drench() const;
const encumbrance_data &get_encumbrance_data() const;
void set_hp_cur( int set );
void set_hp_max( int set );
void set_healed_total( int set );
void set_damage_bandaged( int set );
void set_damage_disinfected( int set );
void set_wetness( int set );
void set_temp_cur( int set );
void set_temp_conv( int set );
void set_frostbite_timer( int set );
void set_encumbrance_data( const encumbrance_data &set );
void set_mut_drench( const std::pair<water_tolerance, int> &set );
void mod_hp_cur( int mod );
void mod_hp_max( int mod );
void mod_healed_total( int mod );
void mod_damage_bandaged( int mod );
void mod_damage_disinfected( int mod );
void mod_wetness( int mod );
void mod_temp_cur( int mod );
void mod_temp_conv( int mod );
void mod_frostbite_timer( int mod );
void serialize( JsonOut &json ) const;
void deserialize( const JsonObject &jo );
};
class body_part_set
{
private:
cata::flat_set<bodypart_str_id> parts;
explicit body_part_set( const cata::flat_set<bodypart_str_id> &other ) : parts( other ) { }
public:
body_part_set() = default;
body_part_set( std::initializer_list<bodypart_str_id> bps ) {
for( const bodypart_str_id &bp : bps ) {
set( bp );
}
}
body_part_set unify_set( const body_part_set &rhs );
body_part_set intersect_set( const body_part_set &rhs );
body_part_set make_intersection( const body_part_set &rhs ) const;
body_part_set substract_set( const body_part_set &rhs );
void fill( const std::vector<bodypart_id> &bps );
bool test( const bodypart_str_id &bp ) const {
return parts.count( bp ) > 0;
}
void set( const bodypart_str_id &bp ) {
parts.insert( bp );
}
void reset( const bodypart_str_id &bp ) {
parts.erase( bp );
}
bool any() const {
return !parts.empty();
}
bool none() const {
return parts.empty();
}
size_t count() const {
return parts.size();
}
cata::flat_set<bodypart_str_id>::iterator begin() const {
return parts.begin();
}
cata::flat_set<bodypart_str_id>::iterator end() const {
return parts.end();
}
void clear() {
parts.clear();
}
template<typename Stream>
void serialize( Stream &s ) const {
s.write( parts );
}
template<typename Value = JsonValue, std::enable_if_t<std::is_same<std::decay_t<Value>, JsonValue>::value>* = nullptr>
void deserialize( const Value &s ) {
s.read( parts );
}
};
/** Returns the new id for old token */
const bodypart_str_id &convert_bp( body_part bp );
/** Returns the opposite side. */
side opposite_side( side s );
// identify the index of a body part's "other half", or itself if not
const std::array<size_t, 12> bp_aiOther = {{0, 1, 2, 3, 5, 4, 7, 6, 9, 8, 11, 10}};
/** Returns the matching name of the body_part token. */
std::string body_part_name( const bodypart_id &bp, int number = 1 );
/** Returns the matching accusative name of the body_part token, i.e. "Shrapnel hits your X".
* These are identical to body_part_name above in English, but not in some other languages. */
std::string body_part_name_accusative( const bodypart_id &bp, int number = 1 );
/** Returns the name of the body parts in a context where the name is used as
* a heading or title e.g. "Left Arm". */
std::string body_part_name_as_heading( const bodypart_id &bp, int number );
/** Returns the body part text to be displayed in the HP bar */
std::string body_part_hp_bar_ui_text( const bodypart_id &bp );
/** Returns the matching encumbrance text for a given body_part token. */
std::string encumb_text( const bodypart_id &bp );
#endif // CATA_SRC_BODYPART_H