forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcharacter.cpp
2431 lines (2141 loc) · 75.3 KB
/
character.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
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
#include "character.h"
#include "game.h"
#include "map.h"
#include "map_selector.h"
#include "vehicle_selector.h"
#include "debug.h"
#include "mission.h"
#include "translations.h"
#include "options.h"
#include "map_iterator.h"
#include "field.h"
#include "messages.h"
#include "input.h"
#include "monster.h"
#include "mtype.h"
#include "player.h"
#include "mutation.h"
#include "vehicle.h"
#include "veh_interact.h"
#include "cata_utility.h"
#include <algorithm>
#include <sstream>
#include <numeric>
const efftype_id effect_beartrap( "beartrap" );
const efftype_id effect_bite( "bite" );
const efftype_id effect_bleed( "bleed" );
const efftype_id effect_blind( "blind" );
const efftype_id effect_boomered( "boomered" );
const efftype_id effect_contacts( "contacts" );
const efftype_id effect_crushed( "crushed" );
const efftype_id effect_darkness( "darkness" );
const efftype_id effect_downed( "downed" );
const efftype_id effect_grabbed( "grabbed" );
const efftype_id effect_heavysnare( "heavysnare" );
const efftype_id effect_infected( "infected" );
const efftype_id effect_in_pit( "in_pit" );
const efftype_id effect_lightsnare( "lightsnare" );
const efftype_id effect_sleep( "sleep" );
const efftype_id effect_webbed( "webbed" );
const skill_id skill_dodge( "dodge" );
const skill_id skill_throw( "throw" );
static const trait_id trait_ACIDBLOOD( "ACIDBLOOD" );
static const trait_id trait_ARACHNID_ARMS( "ARACHNID_ARMS" );
static const trait_id trait_ARM_TENTACLES_4( "ARM_TENTACLES_4" );
static const trait_id trait_ARM_TENTACLES_8( "ARM_TENTACLES_8" );
static const trait_id trait_ARM_TENTACLES( "ARM_TENTACLES" );
static const trait_id trait_BADBACK( "BADBACK" );
static const trait_id trait_BENDY2( "BENDY2" );
static const trait_id trait_BENDY3( "BENDY3" );
static const trait_id trait_BIRD_EYE( "BIRD_EYE" );
static const trait_id trait_CEPH_EYES( "CEPH_EYES" );
static const trait_id trait_CEPH_VISION( "CEPH_VISION" );
static const trait_id trait_CHITIN2( "CHITIN2" );
static const trait_id trait_CHITIN3( "CHITIN3" );
static const trait_id trait_CHITIN_FUR3( "CHITIN_FUR3" );
static const trait_id trait_DEBUG_NIGHTVISION( "DEBUG_NIGHTVISION" );
static const trait_id trait_DISORGANIZED( "DISORGANIZED" );
static const trait_id trait_ELFA_FNV( "ELFA_FNV" );
static const trait_id trait_ELFA_NV( "ELFA_NV" );
static const trait_id trait_FEL_NV( "FEL_NV" );
static const trait_id trait_FLIMSY2( "FLIMSY2" );
static const trait_id trait_FLIMSY3( "FLIMSY3" );
static const trait_id trait_FLIMSY( "FLIMSY" );
static const trait_id trait_GLASSJAW( "GLASSJAW" );
static const trait_id trait_HOLLOW_BONES( "HOLLOW_BONES" );
static const trait_id trait_HUGE( "HUGE" );
static const trait_id trait_INSECT_ARMS( "INSECT_ARMS" );
static const trait_id trait_LIGHT_BONES( "LIGHT_BONES" );
static const trait_id trait_MEMBRANE( "MEMBRANE" );
static const trait_id trait_MUT_TOUGH2( "MUT_TOUGH2" );
static const trait_id trait_MUT_TOUGH3( "MUT_TOUGH3" );
static const trait_id trait_MUT_TOUGH( "MUT_TOUGH" );
static const trait_id trait_MYOPIC( "MYOPIC" );
static const trait_id trait_NIGHTVISION2( "NIGHTVISION2" );
static const trait_id trait_NIGHTVISION3( "NIGHTVISION3" );
static const trait_id trait_NIGHTVISION( "NIGHTVISION" );
static const trait_id trait_PACKMULE( "PACKMULE" );
static const trait_id trait_PER_SLIME_OK( "PER_SLIME_OK" );
static const trait_id trait_PER_SLIME( "PER_SLIME" );
static const trait_id trait_SHELL2( "SHELL2" );
static const trait_id trait_SHELL( "SHELL" );
static const trait_id trait_STRONGBACK( "STRONGBACK" );
static const trait_id trait_TAIL_CATTLE( "TAIL_CATTLE" );
static const trait_id trait_TAIL_FLUFFY( "TAIL_FLUFFY" );
static const trait_id trait_TAIL_LONG( "TAIL_LONG" );
static const trait_id trait_TAIL_RAPTOR( "TAIL_RAPTOR" );
static const trait_id trait_TAIL_RAT( "TAIL_RAT" );
static const trait_id trait_TAIL_THICK( "TAIL_THICK" );
static const trait_id trait_THICK_SCALES( "THICK_SCALES" );
static const trait_id trait_THRESH_CEPHALOPOD( "THRESH_CEPHALOPOD" );
static const trait_id trait_THRESH_INSECT( "THRESH_INSECT" );
static const trait_id trait_THRESH_PLANT( "THRESH_PLANT" );
static const trait_id trait_THRESH_SPIDER( "THRESH_SPIDER" );
static const trait_id trait_TOUGH2( "TOUGH2" );
static const trait_id trait_TOUGH3( "TOUGH3" );
static const trait_id trait_TOUGH( "TOUGH" );
static const trait_id trait_URSINE_EYE( "URSINE_EYE" );
static const trait_id trait_WEBBED( "WEBBED" );
static const trait_id trait_WINGS_BAT( "WINGS_BAT" );
static const trait_id trait_WINGS_BUTTERFLY( "WINGS_BUTTERFLY" );
static const trait_id debug_nodmg( "DEBUG_NODMG" );
Character::Character() : Creature(), visitable<Character>()
{
str_max = 0;
dex_max = 0;
per_max = 0;
int_max = 0;
str_cur = 0;
dex_cur = 0;
per_cur = 0;
int_cur = 0;
str_bonus = 0;
dex_bonus = 0;
per_bonus = 0;
int_bonus = 0;
healthy = 0;
healthy_mod = 0;
hunger = 0;
thirst = 0;
fatigue = 0;
stomach_food = 0;
stomach_water = 0;
name = "";
path_settings = pathfinding_settings{ 0, 1000, 1000, true, false, true };
}
field_id Character::bloodType() const
{
if (has_trait( trait_ACIDBLOOD ))
return fd_acid;
if (has_trait( trait_THRESH_PLANT ))
return fd_blood_veggy;
if (has_trait( trait_THRESH_INSECT ) || has_trait( trait_THRESH_SPIDER ))
return fd_blood_insect;
if (has_trait( trait_THRESH_CEPHALOPOD ))
return fd_blood_invertebrate;
return fd_blood;
}
field_id Character::gibType() const
{
return fd_gibs_flesh;
}
bool Character::is_warm() const
{
return true; // TODO: is there a mutation (plant?) that makes a npc not warm blooded?
}
const std::string &Character::symbol() const
{
static const std::string character_symbol("@");
return character_symbol;
}
void Character::mod_stat( const std::string &stat, float modifier )
{
if( stat == "str" ) {
mod_str_bonus( modifier );
} else if( stat == "dex" ) {
mod_dex_bonus( modifier );
} else if( stat == "per" ) {
mod_per_bonus( modifier );
} else if( stat == "int" ) {
mod_int_bonus( modifier );
} else if( stat == "healthy" ) {
mod_healthy( modifier );
} else if( stat == "hunger" ) {
mod_hunger( modifier );
} else {
Creature::mod_stat( stat, modifier );
}
}
int Character::effective_dispersion( int dispersion ) const
{
/** @EFFECT_PER improves effectiveness of gun sights */
dispersion += ( 10 - per_cur ) * 15;
dispersion += encumb( bp_eyes );
return std::max( dispersion, 0 );
}
double Character::aim_per_move( const item& gun, double recoil ) const
{
if( !gun.is_gun() ) {
return 0;
}
// get fastest sight that can be used to improve aim further below @ref recoil
int cost = INT_MAX;
int limit = 0;
if( !gun.has_flag( "DISABLE_SIGHTS" ) && effective_dispersion( gun.type->gun->sight_dispersion ) < recoil ) {
cost = std::max( std::min( gun.volume() / 250_ml, 8 ), 1 );
limit = effective_dispersion( gun.type->gun->sight_dispersion );
}
for( const auto e : gun.gunmods() ) {
const auto mod = e->type->gunmod.get();
if( mod->sight_dispersion < 0 || mod->aim_cost <= 0 ) {
continue; // skip gunmods which don't provide a sight
}
if( effective_dispersion( mod->sight_dispersion ) < recoil && mod->aim_cost < cost ) {
cost = mod->aim_cost;
limit = effective_dispersion( mod->sight_dispersion );
}
}
if( cost == INT_MAX ) {
return 0; // no suitable sights (already at maxium aim)
}
// each 5 points (combined) of hand encumbrance increases aim cost by one unit
cost += round ( ( encumb( bp_hand_l ) + encumb( bp_hand_r ) ) / 10.0 );
/** @EFFECT_DEX increases aiming speed */
cost += 8 - dex_cur;
/** @EFFECT_PISTOL increases aiming speed for pistols */
/** @EFFECT_SMG increases aiming speed for SMGs */
/** @EFFECT_RIFLE increases aiming speed for rifles */
/** @EFFECT_SHOTGUN increases aiming speed for shotguns */
/** @EFFECT_LAUNCHER increases aiming speed for launchers */
cost += ( ( MAX_SKILL / 2 ) - get_skill_level( gun.gun_skill() ) ) * 2;
cost = std::max( cost, 1 );
// constant at which one unit of aim cost ~75 moves
// (presuming aiming from nil to maximum aim via single sight at DEX 8)
int k = 25;
// calculate rate (b) from the exponential function y = a(1-b)^x where a is recoil
double improv = 1.0 - pow( 0.5, 1.0 / ( cost * k ) );
// minimum improvment is 0.1MoA
double aim = std::max( recoil * improv, 0.1 );
// never improve by more than the currently used sights permit
return std::min( aim, recoil - limit );
}
bool Character::move_effects(bool attacking)
{
if (has_effect( effect_downed )) {
/** @EFFECT_DEX increases chance to stand up when knocked down */
/** @EFFECT_STR increases chance to stand up when knocked down, slightly */
if (rng(0, 40) > get_dex() + get_str() / 2) {
add_msg_if_player(_("You struggle to stand."));
} else {
add_msg_player_or_npc(m_good, _("You stand up."),
_("<npcname> stands up."));
remove_effect( effect_downed);
}
return false;
}
if (has_effect( effect_webbed )) {
/** @EFFECT_STR increases chance to escape webs */
if (x_in_y(get_str(), 6 * get_effect_int( effect_webbed ))) {
add_msg_player_or_npc(m_good, _("You free yourself from the webs!"),
_("<npcname> frees themselves from the webs!"));
remove_effect( effect_webbed);
} else {
add_msg_if_player(_("You try to free yourself from the webs, but can't get loose!"));
}
return false;
}
if (has_effect( effect_lightsnare )) {
/** @EFFECT_STR increases chance to escape light snare */
/** @EFFECT_DEX increases chance to escape light snare */
if(x_in_y(get_str(), 12) || x_in_y(get_dex(), 8)) {
remove_effect( effect_lightsnare);
add_msg_player_or_npc(m_good, _("You free yourself from the light snare!"),
_("<npcname> frees themselves from the light snare!"));
item string("string_36", calendar::turn);
item snare("snare_trigger", calendar::turn);
g->m.add_item_or_charges(pos(), string);
g->m.add_item_or_charges(pos(), snare);
} else {
add_msg_if_player(m_bad, _("You try to free yourself from the light snare, but can't get loose!"));
}
return false;
}
if (has_effect( effect_heavysnare )) {
/** @EFFECT_STR increases chance to escape heavy snare, slightly */
/** @EFFECT_DEX increases chance to escape light snare */
if(x_in_y(get_str(), 32) || x_in_y(get_dex(), 16)) {
remove_effect( effect_heavysnare);
add_msg_player_or_npc(m_good, _("You free yourself from the heavy snare!"),
_("<npcname> frees themselves from the heavy snare!"));
item rope("rope_6", calendar::turn);
item snare("snare_trigger", calendar::turn);
g->m.add_item_or_charges(pos(), rope);
g->m.add_item_or_charges(pos(), snare);
} else {
add_msg_if_player(m_bad, _("You try to free yourself from the heavy snare, but can't get loose!"));
}
return false;
}
if (has_effect( effect_beartrap )) {
/* Real bear traps can't be removed without the proper tools or immense strength; eventually this should
allow normal players two options: removal of the limb or removal of the trap from the ground
(at which point the player could later remove it from the leg with the right tools).
As such we are currently making it a bit easier for players and NPC's to get out of bear traps.
*/
/** @EFFECT_STR increases chance to escape bear trap */
if(x_in_y(get_str(), 100)) {
remove_effect( effect_beartrap);
add_msg_player_or_npc(m_good, _("You free yourself from the bear trap!"),
_("<npcname> frees themselves from the bear trap!"));
item beartrap("beartrap", calendar::turn);
g->m.add_item_or_charges(pos(), beartrap);
} else {
add_msg_if_player(m_bad, _("You try to free yourself from the bear trap, but can't get loose!"));
}
return false;
}
if (has_effect( effect_crushed )) {
/** @EFFECT_STR increases chance to escape crushing rubble */
/** @EFFECT_DEX increases chance to escape crushing rubble, slightly */
if(x_in_y(get_str() + get_dex() / 4, 100)) {
remove_effect( effect_crushed);
add_msg_player_or_npc(m_good, _("You free yourself from the rubble!"),
_("<npcname> frees themselves from the rubble!"));
} else {
add_msg_if_player(m_bad, _("You try to free yourself from the rubble, but can't get loose!"));
}
return false;
}
// Below this point are things that allow for movement if they succeed
// Currently we only have one thing that forces movement if you succeed, should we get more
// than this will need to be reworked to only have success effects if /all/ checks succeed
if (has_effect( effect_in_pit )) {
/** @EFFECT_STR increases chance to escape pit */
/** @EFFECT_DEX increases chance to escape pit, slightly */
if (rng(0, 40) > get_str() + get_dex() / 2) {
add_msg_if_player(m_bad, _("You try to escape the pit, but slip back in."));
return false;
} else {
add_msg_player_or_npc(m_good, _("You escape the pit!"),
_("<npcname> escapes the pit!"));
remove_effect( effect_in_pit);
}
}
if( has_effect( effect_grabbed ) && !attacking ) {
int zed_number = 0;
for( auto &&dest : g->m.points_in_radius( pos(), 1, 0 ) ){
if( g->mon_at( dest ) != -1 &&
( g->zombie( g->mon_at( dest ) ).has_flag( MF_GRABS ) ||
g->zombie( g->mon_at( dest ) ).type->has_special_attack( "GRAB" ) ) ) {
zed_number ++;
}
}
if( zed_number == 0 ) {
add_msg_player_or_npc( m_good, _( "You find yourself no longer grabbed." ),
_( "<npcname> finds themselves no longer grabbed." ) );
remove_effect( effect_grabbed );
/** @EFFECT_DEX increases chance to escape grab, if >STR */
/** @EFFECT_STR increases chance to escape grab, if >DEX */
} else if( rng( 0, std::max( get_dex(), get_str() ) ) < rng( get_effect_int( effect_grabbed ), 8 ) ) {
// Randomly compare higher of dex or str to grab intensity.
add_msg_player_or_npc( m_bad, _( "You try break out of the grab, but fail!" ),
_( "<npcname> tries to break out of the grab, but fails!" ) );
return false;
} else {
add_msg_player_or_npc( m_good, _( "You break out of the grab!" ),
_( "<npcname> breaks out of the grab!" ) );
remove_effect( effect_grabbed );
}
}
return true;
}
void Character::add_effect( const efftype_id &eff_id, int dur, body_part bp,
bool permanent, int intensity, bool force )
{
Creature::add_effect( eff_id, dur, bp, permanent, intensity, force );
}
void Character::process_turn()
{
Creature::process_turn();
drop_inventory_overflow();
}
void Character::recalc_hp()
{
int new_max_hp[num_hp_parts];
// Mutated toughness stacks with starting, by design.
float hp_mod = 1.0f + mutation_value( "hp_modifier" ) + mutation_value( "hp_modifier_secondary" );
float hp_adjustment = mutation_value( "hp_adjustment" );
for( auto &elem : new_max_hp ) {
/** @EFFECT_STR_MAX increases base hp */
elem = 60 + str_max * 3 + hp_adjustment;
elem *= hp_mod;
}
if( has_trait( trait_GLASSJAW ) ) {
new_max_hp[hp_head] *= 0.8;
}
for( int i = 0; i < num_hp_parts; i++ ) {
hp_cur[i] *= (float)new_max_hp[i] / (float)hp_max[i];
hp_max[i] = new_max_hp[i];
}
}
// This must be called when any of the following change:
// - effects
// - bionics
// - traits
// - underwater
// - clothes
// With the exception of clothes, all changes to these character attributes must
// occur through a function in this class which calls this function. Clothes are
// typically added/removed with wear() and takeoff(), but direct access to the
// 'wears' vector is still allowed due to refactor exhaustion.
void Character::recalc_sight_limits()
{
sight_max = 9999;
vision_mode_cache.reset();
// Set sight_max.
if( is_blind() ) {
sight_max = 0;
} else if( has_effect( effect_boomered ) && (!(has_trait( trait_PER_SLIME_OK ))) ) {
sight_max = 1;
vision_mode_cache.set( BOOMERED );
} else if (has_effect( effect_in_pit ) ||
(underwater && !has_bionic( bionic_id( "bio_membrane" ) ) &&
!has_trait( trait_MEMBRANE ) && !worn_with_flag("SWIM_GOGGLES") &&
!has_trait( trait_CEPH_EYES ) && !has_trait( trait_PER_SLIME_OK ) ) ) {
sight_max = 1;
} else if (has_active_mutation( trait_SHELL2 )) {
// You can kinda see out a bit.
sight_max = 2;
} else if ( (has_trait( trait_MYOPIC ) || has_trait( trait_URSINE_EYE )) &&
!is_wearing("glasses_eye") && !is_wearing("glasses_monocle") &&
!is_wearing("glasses_bifocal") && !has_effect( effect_contacts )) {
sight_max = 4;
} else if (has_trait( trait_PER_SLIME )) {
sight_max = 6;
} else if( has_effect( effect_darkness ) ) {
vision_mode_cache.set( DARKNESS );
sight_max = 10;
}
// Debug-only NV, by vache's request
if( has_trait( trait_DEBUG_NIGHTVISION ) ) {
vision_mode_cache.set( DEBUG_NIGHTVISION );
}
if( has_nv() ) {
vision_mode_cache.set( NV_GOGGLES );
}
if( has_active_mutation( trait_NIGHTVISION3 ) || is_wearing("rm13_armor_on") ) {
vision_mode_cache.set( NIGHTVISION_3 );
}
if( has_active_mutation( trait_ELFA_FNV ) ) {
vision_mode_cache.set( FULL_ELFA_VISION );
}
if( has_active_mutation( trait_CEPH_VISION ) ) {
vision_mode_cache.set( CEPH_VISION );
}
if (has_active_mutation( trait_ELFA_NV )) {
vision_mode_cache.set( ELFA_VISION );
}
if( has_active_mutation( trait_NIGHTVISION2 ) ) {
vision_mode_cache.set( NIGHTVISION_2 );
}
if( has_active_mutation( trait_FEL_NV ) ) {
vision_mode_cache.set( FELINE_VISION );
}
if( has_active_mutation( trait_URSINE_EYE ) ) {
vision_mode_cache.set( URSINE_VISION );
}
if (has_active_mutation( trait_NIGHTVISION )) {
vision_mode_cache.set(NIGHTVISION_1);
}
if( has_trait( trait_BIRD_EYE ) ) {
vision_mode_cache.set( BIRD_EYE);
}
// Not exactly a sight limit thing, but related enough
if( has_active_bionic( bionic_id( "bio_infrared" ) ) ||
has_trait( trait_id( "INFRARED" ) ) ||
has_trait( trait_id( "LIZ_IR" ) ) ||
worn_with_flag( "IR_EFFECT" ) ) {
vision_mode_cache.set( IR_VISION );
}
if( has_artifact_with( AEP_SUPER_CLAIRVOYANCE ) ) {
vision_mode_cache.set( VISION_CLAIRVOYANCE_SUPER );
}
if( has_artifact_with( AEP_CLAIRVOYANCE ) ) {
vision_mode_cache.set( VISION_CLAIRVOYANCE );
}
}
static float threshold_for_range( float range )
{
constexpr float epsilon = 0.01f;
return LIGHT_AMBIENT_MINIMAL / exp( range * LIGHT_TRANSPARENCY_OPEN_AIR ) - epsilon;
}
float Character::get_vision_threshold( float light_level ) const {
if( vision_mode_cache[DEBUG_NIGHTVISION] ) {
// Debug vision always works with absurdly little light.
return 0.01;
}
// As light_level goes from LIGHT_AMBIENT_MINIMAL to LIGHT_AMBIENT_LIT,
// dimming goes from 1.0 to 2.0.
const float dimming_from_light = 1.0 + (((float)light_level - LIGHT_AMBIENT_MINIMAL) /
(LIGHT_AMBIENT_LIT - LIGHT_AMBIENT_MINIMAL));
float range = get_per() / 3.0f - encumb( bp_eyes ) / 10.0f;
if( vision_mode_cache[NV_GOGGLES] || vision_mode_cache[NIGHTVISION_3] ||
vision_mode_cache[FULL_ELFA_VISION] || vision_mode_cache[CEPH_VISION] ) {
range += 10;
} else if( vision_mode_cache[NIGHTVISION_2] || vision_mode_cache[FELINE_VISION] ||
vision_mode_cache[URSINE_VISION] || vision_mode_cache[ELFA_VISION] ) {
range += 4.5;
} else if( vision_mode_cache[NIGHTVISION_1] ) {
range += 2;
}
if( vision_mode_cache[BIRD_EYE] ) {
range++;
}
return std::min( (float)LIGHT_AMBIENT_LOW, threshold_for_range( range ) * dimming_from_light );
}
bool Character::has_bionic(const bionic_id &b) const
{
for (auto &i : my_bionics) {
if (i.id == b) {
return true;
}
}
return false;
}
bool Character::has_active_bionic(const bionic_id &b) const
{
for (auto &i : my_bionics) {
if (i.id == b) {
return (i.powered);
}
}
return false;
}
std::vector<item_location> Character::nearby( const std::function<bool(const item *, const item *)>& func, int radius ) const
{
std::vector<item_location> res;
visit_items( [&]( const item *e, const item *parent ) {
if( func( e, parent ) ) {
res.emplace_back( const_cast<Character &>( *this ), const_cast<item *>( e ) );
}
return VisitResponse::NEXT;
} );
for( const auto &cur : map_selector( pos(), radius ) ) {
cur.visit_items( [&]( const item *e, const item *parent ) {
if( func( e, parent ) ) {
res.emplace_back( cur, const_cast<item *>( e ) );
}
return VisitResponse::NEXT;
} );
}
for( const auto &cur : vehicle_selector( pos(), radius ) ) {
cur.visit_items( [&]( const item *e, const item *parent ) {
if( func( e, parent ) ) {
res.emplace_back( cur, const_cast<item *>( e ) );
}
return VisitResponse::NEXT;
} );
}
return res;
}
item& Character::i_add(item it)
{
itype_id item_type_id = it.typeId();
last_item = item_type_id;
if( it.is_food() || it.is_ammo() || it.is_gun() || it.is_armor() ||
it.is_book() || it.is_tool() || it.is_melee() || it.is_food_container() ) {
inv.unsort();
}
// if there's a desired invlet for this item type, try to use it
bool keep_invlet = false;
const std::set<char> cur_inv = allocated_invlets();
for (auto iter : assigned_invlet) {
if (iter.second == item_type_id && !cur_inv.count(iter.first)) {
it.invlet = iter.first;
keep_invlet = true;
break;
}
}
auto &item_in_inv = inv.add_item(it, keep_invlet);
item_in_inv.on_pickup( *this );
return item_in_inv;
}
std::list<item> Character::remove_worn_items_with( std::function<bool(item &)> filter )
{
std::list<item> result;
for( auto iter = worn.begin(); iter != worn.end(); ) {
if( filter( *iter ) ) {
iter->on_takeoff( *this );
result.splice( result.begin(), worn, iter++ );
} else {
++iter;
}
}
return result;
}
// Negative positions indicate weapon/clothing, 0 & positive indicate inventory
const item& Character::i_at(int position) const
{
if( position == -1 ) {
return weapon;
}
if( position < -1 ) {
int worn_index = worn_position_to_index(position);
if (size_t(worn_index) < worn.size()) {
auto iter = worn.begin();
std::advance( iter, worn_index );
return *iter;
}
}
return inv.find_item(position);
}
item& Character::i_at(int position)
{
return const_cast<item&>( const_cast<const Character*>(this)->i_at( position ) );
}
int Character::get_item_position( const item *it ) const
{
if( weapon.has_item( *it ) ) {
return -1;
}
int p = 0;
for( const auto &e : worn ) {
if( e.has_item( *it ) ) {
return worn_position_to_index( p );
}
p++;
}
return inv.position_by_item( it );
}
item Character::i_rem(int pos)
{
item tmp;
if (pos == -1) {
tmp = weapon;
weapon = ret_null;
return tmp;
} else if (pos < -1 && pos > worn_position_to_index(worn.size())) {
auto iter = worn.begin();
std::advance( iter, worn_position_to_index( pos ) );
tmp = *iter;
tmp.on_takeoff( *this );
worn.erase( iter );
return tmp;
}
return inv.remove_item(pos);
}
item Character::i_rem(const item *it)
{
auto tmp = remove_items_with( [&it] (const item &i) { return &i == it; }, 1 );
if( tmp.empty() ) {
debugmsg( "did not found item %s to remove it!", it->tname().c_str() );
return ret_null;
}
return tmp.front();
}
void Character::i_rem_keep_contents( const int pos )
{
for( auto &content : i_rem( pos ).contents ) {
i_add_or_drop( content );
}
}
bool Character::i_add_or_drop( item& it, int qty ) {
bool retval = true;
bool drop = it.made_of( LIQUID );
bool add = it.is_gun() || !it.has_flag( "IRREMOVABLE" );
inv.assign_empty_invlet( it );
for( int i = 0; i < qty; ++i ) {
drop |= !can_pickWeight( it, !get_option<bool>( "DANGEROUS_PICKUPS" ) ) || !can_pickVolume( it );
if( drop ) {
retval &= !g->m.add_item_or_charges( pos(), it ).is_null();
} else if ( add ) {
i_add( it );
}
}
return retval;
}
std::set<char> Character::allocated_invlets() const {
std::set<char> invlets = inv.allocated_invlets();
if (weapon.invlet != 0) {
invlets.insert(weapon.invlet);
}
for( const auto &w : worn ) {
if( w.invlet != 0 ) {
invlets.insert( w.invlet );
}
}
return invlets;
}
bool Character::has_active_item(const itype_id & id) const
{
return has_item_with( [id]( const item & it ) {
return it.active && it.typeId() == id;
} );
}
item Character::remove_weapon()
{
item tmp = weapon;
weapon = ret_null;
return tmp;
}
void Character::remove_mission_items( int mission_id )
{
if( mission_id == -1 ) {
return;
}
remove_items_with( has_mission_item_filter { mission_id } );
}
std::vector<const item *> Character::get_ammo( const ammotype &at ) const
{
return items_with( [at]( const item & it ) {
return it.is_ammo() && it.type->ammo->type.count( at );
} );
}
template <typename T, typename Output>
void find_ammo_helper( T& src, const item& obj, bool empty, Output out, bool nested ) {
if( obj.magazine_integral() ) {
// find suitable ammo excluding that already loaded in magazines
ammotype ammo = obj.ammo_type();
src.visit_items( [&src,&nested,&out,ammo]( item *node ) {
if( node->is_magazine() || node->is_gun() || node->is_tool() ) {
// guns/tools never contain usable ammo so most efficient to skip them now
return VisitResponse::SKIP;
}
if( !node->made_of( SOLID ) ) {
// some liquids are ammo but we can't reload with them unless within a container
return VisitResponse::SKIP;
}
if( node->is_ammo_container() && !node->contents.front().made_of( SOLID ) ) {
if( node->contents.front().type->ammo->type.count( ammo ) ) {
out = item_location( src, node );
}
return VisitResponse::SKIP;
}
if( node->is_ammo() && node->type->ammo->type.count( ammo ) ) {
out = item_location( src, node );
}
return nested ? VisitResponse::NEXT : VisitResponse::SKIP;
} );
} else {
// find compatible magazines excluding those already loaded in tools/guns
const auto mags = obj.magazine_compatible();
src.visit_items( [&src,&nested,&out,mags,empty]( item *node ) {
if( node->is_gun() || node->is_tool() ) {
return VisitResponse::SKIP;
}
if( node->is_magazine() ) {
if ( mags.count( node->typeId() ) && ( node->ammo_remaining() || empty ) ) {
out = item_location( src, node );
}
return VisitResponse::SKIP;
}
return nested ? VisitResponse::NEXT : VisitResponse::SKIP;
} );
}
}
std::vector<item_location> Character::find_ammo( const item& obj, bool empty, int radius ) const
{
std::vector<item_location> res;
find_ammo_helper( const_cast<Character &>( *this ), obj, empty, std::back_inserter( res ), true );
if( radius >= 0 ) {
for( auto& cursor : map_selector( pos(), radius ) ) {
find_ammo_helper( cursor, obj, empty, std::back_inserter( res ), false );
}
for( auto& cursor : vehicle_selector( pos(), radius ) ) {
find_ammo_helper( cursor, obj, empty, std::back_inserter( res ), false );
}
}
return res;
}
int Character::weight_carried() const
{
int ret = 0;
ret += weapon.weight();
for (auto &i : worn) {
ret += i.weight();
}
ret += inv.weight();
return ret;
}
units::volume Character::volume_carried() const
{
return inv.volume();
}
int Character::weight_capacity() const
{
if( has_trait( trait_id( "DEBUG_STORAGE" ) ) ) {
// Infinite enough
return INT_MAX;
}
// Get base capacity from creature,
// then apply player-only mutation and trait effects.
int ret = Creature::weight_capacity();
/** @EFFECT_STR increases carrying capacity */
ret += get_str() * 4000;
if( has_trait( trait_id( "BADBACK" ) ) ) {
ret = int(ret * .65);
}
if( has_trait( trait_id( "STRONGBACK" ) ) ) {
ret = int(ret * 1.35);
}
if( has_trait( trait_id( "LIGHT_BONES" ) ) ) {
ret = int(ret * .80);
}
if( has_trait( trait_id( "HOLLOW_BONES" ) ) ) {
ret = int(ret * .60);
}
if (has_artifact_with(AEP_CARRY_MORE)) {
ret += 22500;
}
if (ret < 0) {
ret = 0;
}
return ret;
}
units::volume Character::volume_capacity() const
{
return volume_capacity_reduced_by( 0 );
}
units::volume Character::volume_capacity_reduced_by( units::volume mod ) const
{
if( has_trait( trait_id( "DEBUG_STORAGE" ) ) ) {
return units::volume_max;
}
units::volume ret = -mod;
for (auto &i : worn) {
ret += i.get_storage();
}
if( has_bionic( bionic_id( "bio_storage" ) ) ) {
ret += 2000_ml;
}
if( has_trait( trait_SHELL ) ) {
ret += 4000_ml;
}
if( has_trait( trait_SHELL2 ) && !has_active_mutation( trait_SHELL2 ) ) {
ret += 6000_ml;
}
if( has_trait( trait_PACKMULE ) ) {
ret = ret * 1.4;
}
if( has_trait( trait_DISORGANIZED ) ) {
ret = ret * 0.6;
}
return std::max( ret, 0_ml );
}
bool Character::can_pickVolume( const item &it, bool ) const
{
inventory projected = inv;
projected.add_item( it, true );
return projected.volume() <= volume_capacity();
}
bool Character::can_pickWeight( const item &it, bool safe ) const
{
if (!safe)
{
// Character can carry up to four times their maximum weight
return ( weight_carried() + it.weight() <= ( has_trait( trait_id( "DEBUG_STORAGE" ) ) ? INT_MAX : weight_capacity() * 4 ) );
}
else
{
return ( weight_carried() + it.weight() <= weight_capacity() );
}
}
bool Character::can_use( const item& it, const item& context ) const {
const auto &ctx = !context.is_null() ? context : it;
if( !meets_requirements( it, ctx ) ) {
const std::string unmet( enumerate_unmet_requirements( it, ctx ) );
if( &it == &ctx ) {
//~ %1$s - list of unmet requirements, %2$s - item name.
add_msg_player_or_npc( m_bad, _( "You need at least %1$s to use this %2$s." ),
_( "<npcname> needs at least %1$s to use this %2$s." ),
unmet.c_str(), it.tname().c_str() );
} else {
//~ %1$s - list of unmet requirements, %2$s - item name, %3$s - indirect item name.
add_msg_player_or_npc( m_bad, _( "You need at least %1$s to use this %2$s with your %3$s." ),
_( "<npcname> needs at least %1$s to use this %2$s with their %3$s." ),
unmet.c_str(), it.tname().c_str(), ctx.tname().c_str() );
}
return false;
}
return true;
}
void Character::drop_inventory_overflow() {
if( volume_carried() > volume_capacity() ) {
for( auto &item_to_drop :
inv.remove_randomly_by_volume( volume_carried() - volume_capacity() ) ) {
g->m.add_item_or_charges( pos(), item_to_drop );
}
add_msg_if_player( m_bad, _("Some items tumble to the ground.") );
}
}
bool Character::has_artifact_with(const art_effect_passive effect) const
{
if( weapon.has_effect_when_wielded( effect ) ) {
return true;
}
for( auto & i : worn ) {
if( i.has_effect_when_worn( effect ) ) {
return true;
}
}
return has_item_with( [effect]( const item & it ) {
return it.has_effect_when_carried( effect );
} );
}
bool Character::is_wearing(const itype_id & it) const
{
for (auto &i : worn) {
if (i.typeId() == it) {
return true;
}
}
return false;
}
bool Character::is_wearing_on_bp(const itype_id & it, body_part bp) const
{