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 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210
|
#include "bodypart.h"
#include <cstdlib>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#include "body_part_set.h"
#include "debug.h"
#include "enum_conversions.h"
#include "generic_factory.h"
#include "subbodypart.h"
#include "json.h"
#include "rng.h"
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_bp_null( "bp_null" );
const bodypart_str_id body_part_eyes( "eyes" );
const bodypart_str_id body_part_foot_l( "foot_l" );
const bodypart_str_id body_part_foot_r( "foot_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_head( "head" );
const bodypart_str_id body_part_leg_l( "leg_l" );
const bodypart_str_id body_part_leg_r( "leg_r" );
const bodypart_str_id body_part_mouth( "mouth" );
const bodypart_str_id body_part_torso( "torso" );
const sub_bodypart_str_id sub_body_part_sub_limb_debug( "sub_limb_debug" );
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;
}
cata_fatal( "Invalid side" );
}
template<>
std::string enum_to_string<body_part_type::type>( body_part_type::type data )
{
switch( data ) {
// *INDENT-OFF*
case body_part_type::type::arm: return "arm";
case body_part_type::type::other: return "other";
case body_part_type::type::foot: return "foot";
case body_part_type::type::hand: return "hand";
case body_part_type::type::head: return "head";
case body_part_type::type::leg: return "leg";
case body_part_type::type::mouth: return "mouth";
case body_part_type::type::sensor: return "sensor";
case body_part_type::type::tail: return "tail";
case body_part_type::type::torso: return "torso";
case body_part_type::type::wing: return "wing";
// *INDENT-ON*
case body_part_type::type::num_types:
break;
}
cata_fatal( "Invalid body part type." );
}
} // namespace io
namespace
{
generic_factory<body_part_type> body_part_factory( "body part" );
generic_factory<limb_score> limb_score_factory( "limb score" );
} // 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 },
};
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<>
const limb_score &string_id<limb_score>::obj() const
{
return limb_score_factory.obj( *this );
}
/** @relates string_id */
template<>
bool string_id<limb_score>::is_valid() const
{
return limb_score_factory.is_valid( *this );
}
void limb_score::load_limb_scores( const JsonObject &jo, const std::string &src )
{
limb_score_factory.load( jo, src );
}
void limb_score::reset()
{
limb_score_factory.reset();
}
const std::vector<limb_score> &limb_score::get_all()
{
return limb_score_factory.get_all();
}
void limb_score::load( const JsonObject &jo, const std::string_view )
{
mandatory( jo, was_loaded, "id", id );
mandatory( jo, was_loaded, "name", _name );
optional( jo, was_loaded, "affected_by_wounds", wound_affect, true );
optional( jo, was_loaded, "affected_by_encumb", encumb_affect, true );
}
/**@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() ) {}
const bodypart_str_id &convert_bp( body_part bp )
{
static const std::vector<bodypart_str_id> body_parts = {
body_part_torso,
body_part_head,
body_part_eyes,
body_part_mouth,
body_part_arm_l,
body_part_arm_r,
body_part_hand_l,
body_part_hand_r,
body_part_leg_l,
body_part_leg_r,
body_part_foot_l,
body_part_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 )];
}
void body_part_type::load_bp( const JsonObject &jo, const std::string &src )
{
body_part_factory.load( jo, src );
}
body_part_type::type body_part_type::primary_limb_type() const
{
return _primary_limb_type;
}
bool body_part_type::has_type( const body_part_type::type &type ) const
{
return limbtypes.count( type ) > 0;
}
bool body_part_type::has_flag( const json_character_flag &flag ) const
{
return flags.count( flag ) > 0;
}
sub_bodypart_id body_part_type::random_sub_part( bool secondary ) const
{
int total_weight = 0;
for( const sub_bodypart_str_id &bp : sub_parts ) {
// filter for secondary sub locations
if( secondary == bp->secondary ) {
total_weight += bp->max_coverage;
}
}
int roll = rng( 1, total_weight );
for( const sub_bodypart_str_id &bp : sub_parts ) {
// filter for secondary sub locations
if( secondary == bp->secondary ) {
if( roll <= bp->max_coverage ) {
return bp.id();
}
roll = roll - bp->max_coverage;
}
}
// should never get here
return sub_bodypart_id();
}
const std::vector<body_part_type> &body_part_type::get_all()
{
return body_part_factory.get_all();
}
void body_part_type::load( const JsonObject &jo, const std::string_view )
{
mandatory( jo, was_loaded, "id", id );
mandatory( jo, was_loaded, "name", name );
// This is NOT the plural of `name`; it's a name referring to the pair of
// bodyparts which this bodypart belongs to, and thus should not be implemented
// using "n_gettext" 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
mandatory( jo, was_loaded, "heading_multiple", name_as_heading_multiple );
optional( jo, was_loaded, "hp_bar_ui_text", hp_bar_ui_text );
mandatory( jo, was_loaded, "encumbrance_text", encumb_text );
mandatory( jo, was_loaded, "hit_size", hit_size );
mandatory( jo, was_loaded, "hit_difficulty", hit_difficulty );
mandatory( jo, was_loaded, "base_hp", base_hp );
optional( jo, was_loaded, "stat_hp_mods", hp_mods );
optional( jo, was_loaded, "heal_bonus", heal_bonus, 0 );
optional( jo, was_loaded, "mend_rate", mend_rate, 1.0f );
mandatory( jo, was_loaded, "drench_capacity", drench_max );
optional( jo, was_loaded, "drench_increment", drench_increment, 2 );
optional( jo, was_loaded, "drying_chance", drying_chance, drench_max );
optional( jo, was_loaded, "drying_increment", drying_increment, 1 );
optional( jo, was_loaded, "wet_morale", wet_morale, 0 );
optional( jo, was_loaded, "ugliness", ugliness, 0 );
optional( jo, was_loaded, "ugliness_mandatory", ugliness_mandatory, 0 );
optional( jo, was_loaded, "is_limb", is_limb, false );
optional( jo, was_loaded, "is_vital", is_vital, false );
if( jo.has_array( "limb_types" ) ) {
limbtypes.clear();
body_part_type::type first_type = body_part_type::type::num_types;
bool set_first_type = true;
for( JsonValue jval : jo.get_array( "limb_types" ) ) {
float weight = 1.0f;
body_part_type::type limb_type;
if( jval.test_array() ) {
JsonArray jarr = jval.get_array();
limb_type = io::string_to_enum<body_part_type::type>( jarr.get_string( 0 ) );
weight = jarr.get_float( 1 );
set_first_type = false;
} else {
limb_type = io::string_to_enum<body_part_type::type>( jval.get_string() );
}
limbtypes.emplace( limb_type, weight );
if( first_type == body_part_type::type::num_types ) {
first_type = limb_type;
}
}
// set cached primary type if no weights specified
if( set_first_type ) {
_primary_limb_type = first_type;
}
} else {
limbtypes.clear();
body_part_type::type limb_type = {};
mandatory( jo, was_loaded, "limb_type", limb_type );
limbtypes.emplace( limb_type, 1.0f );
}
if( _primary_limb_type == body_part_type::type::num_types ) {
float high = 0.f;
for( auto &bp_type : limbtypes ) {
if( high < bp_type.second ) {
high = bp_type.second;
_primary_limb_type = bp_type.first;
}
}
}
// tokens are actually legacy code that should be on their way out.
if( !was_loaded ) {
optional( jo, was_loaded, "legacy_id", legacy_id, "BP_NULL" );
if( legacy_id != "BP_NULL" ) {
token = legacy_id_to_enum( legacy_id );
}
} else {
// we need to clear this because any bodypart using copy-from will not be a legacy part.
legacy_id = "BP_NULL";
token = body_part::num_bp;
}
optional( jo, was_loaded, "env_protection", env_protection, 0 );
int legacy_fire_warmth_bonus = units::to_legacy_bodypart_temp_delta( fire_warmth_bonus );
optional( jo, was_loaded, "fire_warmth_bonus", legacy_fire_warmth_bonus, 0 );
fire_warmth_bonus = units::from_legacy_bodypart_temp_delta( legacy_fire_warmth_bonus );
mandatory( jo, was_loaded, "main_part", main_part );
if( main_part == id ) {
mandatory( jo, was_loaded, "connected_to", connected_to );
} else {
connected_to = main_part;
}
mandatory( jo, was_loaded, "opposite_part", opposite_part );
optional( jo, was_loaded, "smash_message", smash_message );
optional( jo, was_loaded, "smash_efficiency", smash_efficiency, 0.5f );
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, "feels_discomfort", feels_discomfort, true );
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 );
optional( jo, was_loaded, "flags", flags );
optional( jo, was_loaded, "conditional_flags", conditional_flags );
optional( jo, was_loaded, "encumbrance_threshold", encumbrance_threshold, 0 );
optional( jo, was_loaded, "encumbrance_limit", encumbrance_limit, 100 );
optional( jo, was_loaded, "health_limit", health_limit, 1 );
optional( jo, was_loaded, "techniques", techniques );
optional( jo, was_loaded, "technique_encumbrance_limit", technique_enc_limit, 50 );
optional( jo, was_loaded, "bmi_encumbrance_threshold", bmi_encumbrance_threshold, 999 );
optional( jo, was_loaded, "bmi_encumbrance_scalar", bmi_encumbrance_scalar, 0 );
if( jo.has_member( "limb_scores" ) ) {
limb_scores.clear();
const JsonArray &jarr = jo.get_array( "limb_scores" );
for( const JsonValue jval : jarr ) {
bp_limb_score bpls;
const limb_score_id id = limb_score_id( jval.get_array().get_string( 0 ) );
bpls.score = jval.get_array().get_float( 1 );
bpls.max = jval.get_array().size() > 2 ? jval.get_array().get_float( 2 ) : bpls.score;
limb_scores[id] = bpls;
}
}
if( jo.has_array( "effects_on_hit" ) ) {
effects_on_hit.clear();
for( const JsonObject effect_jo : jo.get_array( "effects_on_hit" ) ) {
bp_onhit_effect eff;
eff.load( effect_jo );
effects_on_hit.push_back( eff );
}
}
if( jo.has_array( "temp_mod" ) ) {
JsonArray temp_array = jo.get_array( "temp_mod" );
temp_min = units::from_legacy_bodypart_temp_delta( temp_array.get_int( 0 ) );
temp_max = units::from_legacy_bodypart_temp_delta( temp_array.get_int( 1 ) );
}
if( jo.has_array( "unarmed_damage" ) ) {
unarmed_bonus = true;
damage = damage_instance();
damage = load_damage_instance( jo.get_array( "unarmed_damage" ) );
}
if( jo.has_object( "armor" ) ) {
armor = resistances();
armor = load_resistances_instance( jo.get_object( "armor" ) );
}
mandatory( jo, was_loaded, "side", part_side );
mandatory( jo, was_loaded, "sub_parts", sub_parts );
if( jo.has_array( "encumbrance_per_weight" ) ) {
const JsonArray &jarr = jo.get_array( "encumbrance_per_weight" );
for( const JsonObject jval : jarr ) {
units::mass weight = 0_gram;
int encumbrance = 0;
assign( jval, "weight", weight, true );
mandatory( jval, was_loaded, "encumbrance", encumbrance );
encumbrance_per_weight.insert( std::pair<units::mass, int>( weight, encumbrance ) );
}
}
}
void bp_onhit_effect::load( const JsonObject &jo )
{
mandatory( jo, false, "id", id );
optional( jo, false, "global", global );
optional( jo, false, "dmg_type", dtype, damage_type_id::NULL_ID() );
optional( jo, false, "dmg_threshold", dmg_threshold, 1 );
optional( jo, false, "dmg_scale_increment", scale_increment, 1.0f );
optional( jo, false, "chance", chance, 100 );
optional( jo, false, "chance_dmg_scaling", chance_dmg_scaling, 0.0f );
optional( jo, false, "intensity", intensity, 1 );
optional( jo, false, "intensity_dmg_scaling", intensity_dmg_scaling, 0.0f );
optional( jo, false, "max_intensity", max_intensity, INT_MAX );
optional( jo, false, "duration", duration, 1 );
optional( jo, false, "duration_dmg_scaling", duration_dmg_scaling, 0.0f );
optional( jo, false, "max_duration", max_duration, INT_MAX );
}
void body_part_type::reset()
{
body_part_factory.reset();
}
void body_part_type::finalize_all()
{
body_part_factory.finalize();
}
void body_part_type::finalize()
{
finalize_damage_map( armor.resist_vals );
}
void body_part_type::check_consistency()
{
for( const body_part bp : all_body_parts ) {
const bodypart_str_id &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 body_part_type &under_token = convert_bp( token ).obj();
if( this != &under_token && token != body_part::num_bp ) {
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() );
} else if( opposite_part->opposite_part != id ) {
debugmsg( "Bodypart %s has inconsistent opposite part!", id.str() );
}
for( const std::pair<const limb_score_id, bp_limb_score> &bpls : limb_scores ) {
if( !bpls.first.is_valid() ) {
debugmsg( "Body part %s has invalid limb score %s.", id.str(), bpls.first.str() );
}
if( bpls.second.score > bpls.second.max ) {
debugmsg( "Body part %s has higher %s score than max.", id.str(), bpls.first.str() );
}
}
for( const std::pair<const damage_type_id, float> &dt : armor.resist_vals ) {
if( !dt.first.is_valid() ) {
debugmsg( "Invalid armor type \"%s\" for body part %s", dt.first.c_str(), id.c_str() );
}
}
for( const damage_unit &dt : damage.damage_units ) {
if( !dt.type.is_valid() ) {
debugmsg( "Invalid unarmed_damage type \"%s\" for body part %s", dt.type.c_str(), id.c_str() );
}
}
// Check that connected_to leads eventually to the root bodypart (currently always head),
// without any loops
std::unordered_set<bodypart_str_id> visited = { id };
bodypart_str_id next = connected_to;
while( !visited.count( next ) ) {
visited.insert( next );
next = next->connected_to;
}
if( next != next->connected_to ) {
debugmsg( "Loop in body part connectedness starting from %s", id.str() );
}
}
float body_part_type::get_limb_score( const limb_score_id &id ) const
{
const auto it = limb_scores.find( id );
return it == limb_scores.end() ? 0.0f : it->second.score;
}
float body_part_type::get_limb_score_max( const limb_score_id &id ) const
{
const auto it = limb_scores.find( id );
return it == limb_scores.end() ? 0.0f : it->second.max;
}
bool body_part_type::has_limb_score( const limb_score_id &id ) const
{
return limb_scores.count( id );
}
float body_part_type::unarmed_damage( const damage_type_id &dt ) const
{
return damage.type_damage( dt );
}
float body_part_type::unarmed_arpen( const damage_type_id &dt ) const
{
return damage.type_arpen( dt );
}
float body_part_type::damage_resistance( const damage_type_id &dt ) const
{
return armor.type_resist( dt );
}
float body_part_type::damage_resistance( const damage_unit &du ) const
{
return armor.get_effective_resist( du );
}
std::set<translation, localized_comparator> body_part_type::consolidate(
std::vector<sub_bodypart_id> &covered )
{
std::set<translation, localized_comparator> to_return;
std::vector<bodypart_id> full_bps;
//first try to compress sets of sub body parts together into a full limb
for( size_t i = 0; i < covered.size(); i++ ) {
const sub_bodypart_id &sbp = covered[i];
if( sbp == sub_body_part_sub_limb_debug ) {
// if we have already covered this continue
continue;
}
// try to find all matching sublimbs from the parent
bool found_all = true;
for( const sub_bodypart_str_id &searching : sbp->parent->sub_parts ) {
//skip secondary locations for this
if( !searching->secondary ) {
found_all = std::find( covered.begin(), covered.end(), searching.id() ) != covered.end() &&
found_all;
}
}
// if found all consolidate all the limb bits together
// TODO: shouldn't need so many loops to do this
if( found_all ) {
full_bps.emplace_back( sbp->parent );
// set the initial to a skipped value
for( const sub_bodypart_str_id &searching : sbp->parent->sub_parts ) {
if( !searching->secondary ) {
auto sbp_it = std::find( covered.begin(), covered.end(), searching.id() );
*sbp_it = sub_body_part_sub_limb_debug;
}
}
}
}
// now try and compress together matching limbs
for( size_t i = 0; i < full_bps.size(); i++ ) {
const bodypart_id &bp = full_bps[i];
if( bp == bodypart_str_id::NULL_ID().id() ) {
//its already been covered
continue;
}
// bodyparts with no opposite are their own opposite
if( bp->opposite_part.id() == bp ) {
// if no opposite don't look for one
to_return.insert( bp->name_as_heading );
continue;
}
auto bp_itt = std::find( full_bps.begin(), full_bps.end(), bp->opposite_part );
if( bp_itt == full_bps.end() ) {
// if we didn't find the match just add the limb we were just looking at
to_return.insert( bp->name_as_heading );
continue;
}
*bp_itt = bodypart_str_id::NULL_ID().id();
to_return.insert( bp->name_as_heading_multiple );
}
for( size_t i = 0; i < covered.size(); i++ ) {
const sub_bodypart_id &sbp = covered[i];
if( sbp == sub_body_part_sub_limb_debug ) {
// if we have already covered this value as a pair continue
continue;
}
sub_bodypart_id temp;
// if our sub part has an opposite
if( sbp->opposite != sub_body_part_sub_limb_debug ) {
temp = sbp->opposite;
} else {
// if it doesn't have an opposite add it to the return vector alone and continue
to_return.insert( sbp->name );
continue;
}
bool found = false;
for( sub_bodypart_id &sbp_it : covered ) {
// go through each body part and test if its partner is there as well
if( temp == sbp_it ) {
// add the multiple name not the single
to_return.insert( sbp->name_multiple );
found = true;
// set the found part to a null value
sbp_it = sub_body_part_sub_limb_debug;
break;
}
}
// if we didn't find its pair print it normally
if( !found ) {
to_return.insert( sbp->name );
}
}
return to_return;
}
std::set<translation, localized_comparator> body_part_type::consolidate(
std::vector<bodypart_id> &covered )
{
std::set<translation, localized_comparator> to_return;
// now try and compress together matching limbs
for( size_t i = 0; i < covered.size(); i++ ) {
const bodypart_id &bp = covered[i];
if( bp == bodypart_str_id::NULL_ID().id() ) {
//its already been covered
continue;
}
// bodyparts with no opposite are their own opposite
if( bp->opposite_part.id() == bp ) {
// if no opposite don't look for one
to_return.insert( bp->name_as_heading );
continue;
}
auto bp_itt = std::find( covered.begin(), covered.end(), bp->opposite_part );
if( bp_itt == covered.end() ) {
// if we didn't find the match just add the limb we were just looking at
to_return.insert( bp->name_as_heading );
continue;
}
*bp_itt = bodypart_str_id::NULL_ID().id();
to_return.insert( bp->name_as_heading_multiple );
}
return to_return;
}
bool encumbrance_data::add_sub_location(
const layer_level level, const sub_bodypart_id sbp )
{
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 ) {
return true;
}
}
// otherwise we should add it to the list
layer_penalty_details[static_cast<size_t>( level )].covered_sub_parts.push_back( sbp );
return false;
}
bool encumbrance_data::add_sub_location(
const layer_level level, const sub_bodypart_str_id sbp )
{
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.id() == layer_sbp ) {
return true;
}
}
// otherwise we should add it to the list
layer_penalty_details[static_cast<size_t>( level )].covered_sub_parts.push_back( sbp.id() );
return false;
}
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( 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( 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.translated();
}
std::string encumb_text( const bodypart_id &bp )
{
return bp->encumb_text.translated();
}
body_part_set body_part_set::unify_set( const body_part_set &rhs )
{
for( const bodypart_str_id &i : rhs ) {
if( !test( i ) ) {
set( i );
}
}
return *this;
}
body_part_set body_part_set::intersect_set( const body_part_set &rhs )
{
body_part_set temp;
for( const bodypart_str_id &j : rhs ) {
if( test( j ) ) {
temp.set( j );
}
}
clear();
unify_set( temp );
return *this;
}
body_part_set body_part_set::substract_set( const body_part_set &rhs )
{
for( const bodypart_str_id &j : rhs ) {
if( test( j ) ) {
reset( j );
}
}
return *this;
}
body_part_set body_part_set::make_intersection( const body_part_set &rhs ) const
{
body_part_set new_intersection;
new_intersection.unify_set( *this );
return new_intersection.intersect_set( rhs );
}
void body_part_set::fill( const std::vector<bodypart_id> &bps )
{
for( const bodypart_id &bp : bps ) {
parts.insert( bp.id() );
}
}
void body_part_set::serialize( JsonOut &s ) const
{
s.write( parts );
}
void body_part_set::deserialize( const JsonValue &s )
{
s.read( parts );
}
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;
}
float bodypart::get_wetness_percentage() const
{
if( id->drench_max == 0 ) {
return 0.0f;
} else {
return static_cast<float>( wetness ) / id->drench_max;
}
}
int bodypart::get_encumbrance_threshold() const
{
return id->encumbrance_threshold;
}
bool bodypart::is_limb_overencumbered() const
{
return get_encumbrance_data().encumbrance >= id->encumbrance_limit;
}
bool bodypart::has_conditional_flag( const json_character_flag &flag ) const
{
return id->conditional_flags.count( flag ) > 0 && hp_cur > id->health_limit &&
!is_limb_overencumbered();
}
std::set<matec_id> bodypart::get_limb_techs() const
{
std::set<matec_id> result;
if( !x_in_y( get_encumbrance_data().encumbrance, id->technique_enc_limit ) &&
hp_cur > id->health_limit ) {
result.insert( id->techniques.begin(), id->techniques.end() );
}
return result;
}
std::vector<bp_onhit_effect> bodypart::get_onhit_effects( damage_type_id dtype ) const
{
std::vector<bp_onhit_effect> result;
for( const bp_onhit_effect &effect : id->effects_on_hit ) {
if( effect.dtype == dtype || effect.dtype == damage_type_id::NULL_ID() ) {
result.push_back( effect );
}
}
return result;
}
float bodypart::wound_adjusted_limb_value( const float val ) const
{
double percent = static_cast<double>( get_hp_cur() ) /
static_cast<double>( get_hp_max() );
if( percent > 0.75 ) {
return val;
}
percent /= 0.75;
return val * static_cast<float>( percent );
}
float bodypart::encumb_adjusted_limb_value( const float val ) const
{
int enc = get_encumbrance_data().encumbrance;
// Check if we're over our encumbrance limit, return 0 if so
if( is_limb_overencumbered() ) {
return 0;
}
// Reduce encumbrance by the limb's encumbrance threshold, limiting to 0
enc = std::max( 0, ( enc - get_encumbrance_threshold() ) );
// This is designed to get a 5% adjustment for an increase of 3 encumbrance, with further
// adjustments decreasing to avoid a multiplier of 0 (or infinity if reciprocal).
return val * 19.0f / ( 19.0f + enc / 3.0f );
}
float bodypart::skill_adjusted_limb_value( float val, int skill ) const
{
const float mitigated_score = val * ( 1.0f + ( skill / 10.0f ) );
return std::min( val, mitigated_score );
}
float bodypart::get_limb_score( const limb_score_id &score, int skill, int override_encumb,
int override_wounds ) const
{
bool process_wounds = override_wounds == 1 || ( override_wounds == -1 &&
score->affected_by_wounds() );
bool process_encumb = override_encumb == 1 || ( override_encumb == -1 &&
score->affected_by_encumb() );
float sc = id->get_limb_score( score );
if( process_wounds ) {
sc = wound_adjusted_limb_value( sc );
}
if( process_encumb ) {
sc = encumb_adjusted_limb_value( sc );
}
if( skill >= 0 ) {
sc = skill_adjusted_limb_value( sc, skill );
}
return sc;
}
float bodypart::get_limb_score_max( const limb_score_id &score ) const
{
return id->get_limb_score_max( score );
}
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;
}
const encumbrance_data &bodypart::get_encumbrance_data() const
{
return encumb_data;
}
int bodypart::get_drench_capacity() const
{
return id->drench_max;
}
int bodypart::get_wetness() const
{
return wetness;
}
int bodypart::get_frostbite_timer() const
{
return frostbite_timer;
}
units::temperature bodypart::get_temp_cur() const
{
return temp_cur;
}
units::temperature bodypart::get_temp_conv() const
{
return temp_conv;
}
int bodypart::get_bmi_encumbrance_threshold() const
{
return id->bmi_encumbrance_threshold;
}
float bodypart::get_bmi_encumbrance_scalar() const
{
return id->bmi_encumbrance_scalar;
}
std::array<int, NUM_WATER_TOLERANCE> bodypart::get_mut_drench() const
{
return mut_drench;
}
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::set_encumbrance_data( const encumbrance_data &set )
{
encumb_data = set;
}
void bodypart::set_mut_drench( const std::pair<water_tolerance, int> &set )
{
if( set.first < WT_IGNORED || set.first > NUM_WATER_TOLERANCE ) {
debugmsg( "Tried to use invalid water tolerance enum in set_mut_drench()." );
}
mut_drench[set.first] = set.second;
}
void bodypart::set_wetness( int set )
{
wetness = set;
}
void bodypart::set_temp_cur( units::temperature set )
{
temp_cur = set;
}
void bodypart::set_temp_conv( units::temperature set )
{
temp_conv = set;
}
void bodypart::set_frostbite_timer( int set )
{
frostbite_timer = 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::mod_wetness( int mod )
{
wetness += mod;
}
void bodypart::mod_temp_cur( units::temperature_delta mod )
{
temp_cur += mod;
}
void bodypart::mod_temp_conv( units::temperature_delta mod )
{
temp_conv += mod;
}
void bodypart::mod_frostbite_timer( int mod )
{
frostbite_timer += 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( "healed_total", healed_total );
json.member( "damage_bandaged", damage_bandaged );
json.member( "damage_disinfected", damage_disinfected );
json.member( "wetness", wetness );
json.member( "temp_cur", units::to_legacy_bodypart_temp( temp_cur ) );
json.member( "temp_conv", units::to_legacy_bodypart_temp( temp_conv ) );
json.member( "frostbite_timer", frostbite_timer );
json.end_object();
}
void bodypart::deserialize( const JsonObject &jo )
{
jo.read( "id", id, true );
jo.read( "hp_cur", hp_cur, true );
jo.read( "hp_max", hp_max, true );
jo.read( "healed_total", healed_total );
jo.read( "damage_bandaged", damage_bandaged, true );
jo.read( "damage_disinfected", damage_disinfected, true );
jo.read( "wetness", wetness, true );
if( int legacy_temp_cur; jo.read( "temp_cur", legacy_temp_cur, true ) ) {
temp_cur = units::from_legacy_bodypart_temp( legacy_temp_cur );
}
if( int legacy_temp_conv; jo.read( "temp_conv", legacy_temp_conv, true ) ) {
temp_conv = units::from_legacy_bodypart_temp( legacy_temp_conv );
}
jo.read( "frostbite_timer", frostbite_timer, true );
}
void stat_hp_mods::load( const JsonObject &jsobj )
{
optional( jsobj, was_loaded, "str_mod", str_mod, 3.0f );
optional( jsobj, was_loaded, "dex_mod", dex_mod, 0.0f );
optional( jsobj, was_loaded, "int_mod", int_mod, 0.0f );
optional( jsobj, was_loaded, "per_mod", per_mod, 0.0f );
optional( jsobj, was_loaded, "health_mod", health_mod, 0.0f );
}
void stat_hp_mods::deserialize( const JsonObject &jo )
{
load( jo );
}
|