-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
npc.cpp
3351 lines (2969 loc) · 101 KB
/
npc.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 "npc.h"
#include <climits>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <functional>
#include <limits>
#include "auto_pickup.h"
#include "avatar.h"
#include "coordinate_conversions.h"
#include "effect.h"
#include "event_bus.h"
#include "game.h"
#include "game_inventory.h"
#include "item_group.h"
#include "itype.h"
#include "iuse_actor.h"
#include "json.h"
#include "map.h"
#include "mapdata.h"
#include "map_iterator.h"
#include "memorial_logger.h"
#include "messages.h"
#include "mission.h"
#include "morale_types.h"
#include "mutation.h"
#include "npc_class.h"
#include "output.h"
#include "overmap.h"
#include "overmapbuffer.h"
#include "skill.h"
#include "sounds.h"
#include "string_formatter.h"
#include "text_snippets.h"
#include "trait_group.h"
#include "veh_type.h"
#include "vehicle.h"
#include "vpart_position.h"
#include "bodypart.h"
#include "cata_utility.h"
#include "character.h"
#include "damage.h"
#include "debug.h"
#include "faction.h"
#include "game_constants.h"
#include "item.h"
#include "iuse.h"
#include "math_defines.h"
#include "monster.h"
#include "pathfinding.h"
#include "player_activity.h"
#include "ret_val.h"
#include "rng.h"
#include "tileray.h"
#include "translations.h"
#include "units.h"
#include "visitable.h"
#include "int_id.h"
#include "pldata.h"
#include "clzones.h"
#include "enums.h"
#include "flat_set.h"
#include "stomach.h"
#include "cata_string_consts.h"
class basecamp;
class monfaction;
void starting_clothes( npc &who, const npc_class_id &type, bool male );
void starting_inv( npc &who, const npc_class_id &type );
npc::npc()
: restock( calendar::turn_zero )
, companion_mission_time( calendar::before_time_starts )
, companion_mission_time_ret( calendar::before_time_starts )
, last_updated( calendar::turn )
{
submap_coords = point_zero;
position.x = -1;
position.y = -1;
position.z = 500;
last_player_seen_pos = cata::nullopt;
last_seen_player_turn = 999;
wanted_item_pos = no_goal_point;
guard_pos = no_goal_point;
goal = no_goal_point;
fetching_item = false;
has_new_items = true;
worst_item_value = 0;
str_max = 0;
dex_max = 0;
int_max = 0;
per_max = 0;
marked_for_death = false;
death_drops = true;
dead = false;
hit_by_player = false;
hallucination = false;
moves = 100;
mission = NPC_MISSION_NULL;
myclass = npc_class_id::NULL_ID();
fac_id = faction_id::NULL_ID();
patience = 0;
attitude = NPCATT_NULL;
*path_settings = pathfinding_settings( 0, 1000, 1000, 10, true, true, true, false );
for( direction threat_dir : npc_threat_dir ) {
ai_cache.threat_map[ threat_dir ] = 0.0f;
}
}
standard_npc::standard_npc( const std::string &name, const tripoint &pos,
const std::vector<itype_id> &clothing,
int sk_lvl, int s_str, int s_dex, int s_int, int s_per )
{
this->name = name;
position = pos;
str_cur = std::max( s_str, 0 );
str_max = std::max( s_str, 0 );
dex_cur = std::max( s_dex, 0 );
dex_max = std::max( s_dex, 0 );
per_cur = std::max( s_per, 0 );
per_max = std::max( s_per, 0 );
int_cur = std::max( s_int, 0 );
int_max = std::max( s_int, 0 );
recalc_hp();
for( int i = 0; i < num_hp_parts; i++ ) {
hp_cur[i] = hp_max[i];
}
for( auto &e : Skill::skills ) {
set_skill_level( e.ident(), std::max( sk_lvl, 0 ) );
}
for( const auto &e : clothing ) {
wear_item( item( e ) );
}
for( item &e : worn ) {
if( e.has_flag( "VARSIZE" ) ) {
e.item_tags.insert( "FIT" );
}
}
}
npc::npc( npc && ) = default;
npc &npc::operator=( npc && ) = default;
static std::map<string_id<npc_template>, npc_template> npc_templates;
void npc_template::load( const JsonObject &jsobj )
{
npc_template tem;
npc &guy = tem.guy;
guy.idz = jsobj.get_string( "id" );
guy.name.clear();
jsobj.read( "name_unique", tem.name_unique );
jsobj.read( "name_suffix", tem.name_suffix );
if( jsobj.has_string( "gender" ) ) {
if( jsobj.get_string( "gender" ) == "male" ) {
tem.gender_override = gender::male;
} else {
tem.gender_override = gender::female;
}
} else {
tem.gender_override = gender::random;
}
if( jsobj.has_string( "faction" ) ) {
guy.set_fac_id( jsobj.get_string( "faction" ) );
}
if( jsobj.has_int( "class" ) ) {
guy.myclass = npc_class::from_legacy_int( jsobj.get_int( "class" ) );
} else if( jsobj.has_string( "class" ) ) {
guy.myclass = npc_class_id( jsobj.get_string( "class" ) );
}
guy.set_attitude( static_cast<npc_attitude>( jsobj.get_int( "attitude" ) ) );
guy.mission = static_cast<npc_mission>( jsobj.get_int( "mission" ) );
guy.chatbin.first_topic = jsobj.get_string( "chat" );
if( jsobj.has_string( "mission_offered" ) ) {
guy.miss_ids.emplace_back( mission_type_id( jsobj.get_string( "mission_offered" ) ) );
} else if( jsobj.has_array( "mission_offered" ) ) {
for( const std::string line : jsobj.get_array( "mission_offered" ) ) {
guy.miss_ids.emplace_back( mission_type_id( line ) );
}
}
npc_templates.emplace( string_id<npc_template>( guy.idz ), std::move( tem ) );
}
void npc_template::reset()
{
npc_templates.clear();
}
void npc_template::check_consistency()
{
for( const auto &e : npc_templates ) {
const auto &guy = e.second.guy;
if( !guy.myclass.is_valid() ) {
debugmsg( "Invalid NPC class %s", guy.myclass.c_str() );
}
}
}
template<>
bool string_id<npc_template>::is_valid() const
{
return npc_templates.count( *this ) > 0;
}
template<>
const npc_template &string_id<npc_template>::obj() const
{
const auto found = npc_templates.find( *this );
if( found == npc_templates.end() ) {
debugmsg( "Tried to get invalid npc: %s", c_str() );
static const npc_template dummy{};
return dummy;
}
return found->second;
}
void npc::load_npc_template( const string_id<npc_template> &ident )
{
auto found = npc_templates.find( ident );
if( found == npc_templates.end() ) {
debugmsg( "Tried to get invalid npc: %s", ident.c_str() );
return;
}
const npc_template &tem = found->second;
const npc &tguy = tem.guy;
idz = tguy.idz;
myclass = npc_class_id( tguy.myclass );
randomize( myclass );
if( !tem.name_unique.empty() ) {
name = tem.name_unique.translated();
}
if( !tem.name_suffix.empty() ) {
//~ %1$s: npc name, %2$s: name suffix
name = string_format( pgettext( "npc name", "%1$s, %2$s" ), name, tem.name_suffix );
}
if( tem.gender_override != npc_template::gender::random ) {
male = tem.gender_override == npc_template::gender::male;
}
fac_id = tguy.fac_id;
set_fac( fac_id );
attitude = tguy.attitude;
mission = tguy.mission;
chatbin.first_topic = tguy.chatbin.first_topic;
for( const mission_type_id &miss_id : tguy.miss_ids ) {
add_new_mission( mission::reserve_new( miss_id, getID() ) );
}
}
npc::~npc() = default;
void npc::randomize( const npc_class_id &type )
{
if( !getID().is_valid() ) {
setID( g->assign_npc_id() );
}
weapon = item( "null", 0 );
inv.clear();
personality.aggression = rng( -10, 10 );
personality.bravery = rng( -3, 10 );
personality.collector = rng( -1, 10 );
personality.altruism = rng( -10, 10 );
moves = 100;
mission = NPC_MISSION_NULL;
male = one_in( 2 );
pick_name();
if( !type.is_valid() ) {
debugmsg( "Invalid NPC class %s", type.c_str() );
myclass = npc_class_id::NULL_ID();
} else if( type.is_null() ) {
myclass = npc_class::random_common();
} else {
myclass = type;
}
const auto &the_class = myclass.obj();
str_max = the_class.roll_strength();
dex_max = the_class.roll_dexterity();
int_max = the_class.roll_intelligence();
per_max = the_class.roll_perception();
for( auto &skill : Skill::skills ) {
int level = myclass->roll_skill( skill.ident() );
set_skill_level( skill.ident(), level );
}
if( type.is_null() ) { // Untyped; no particular specialization
} else if( type == NC_EVAC_SHOPKEEP ) {
personality.collector += rng( 1, 5 );
} else if( type == NC_BARTENDER ) {
personality.collector += rng( 1, 5 );
} else if( type == NC_JUNK_SHOPKEEP ) {
personality.collector += rng( 1, 5 );
} else if( type == NC_ARSONIST ) {
personality.aggression += rng( 0, 1 );
personality.collector += rng( 0, 2 );
} else if( type == NC_SOLDIER ) {
personality.aggression += rng( 1, 3 );
personality.bravery += rng( 0, 5 );
} else if( type == NC_HACKER ) {
personality.bravery -= rng( 1, 3 );
personality.aggression -= rng( 0, 2 );
} else if( type == NC_DOCTOR ) {
personality.aggression -= rng( 0, 4 );
cash += 10000 * rng( 0, 3 ) * rng( 0, 3 );
} else if( type == NC_TRADER ) {
personality.collector += rng( 1, 5 );
cash += 25000 * rng( 1, 10 );
} else if( type == NC_NINJA ) {
personality.bravery += rng( 0, 3 );
personality.collector -= rng( 1, 6 );
// TODO: give ninja his styles back
} else if( type == NC_COWBOY ) {
personality.aggression += rng( 0, 2 );
personality.bravery += rng( 1, 5 );
} else if( type == NC_SCIENTIST ) {
personality.aggression -= rng( 1, 5 );
personality.bravery -= rng( 2, 8 );
personality.collector += rng( 0, 2 );
} else if( type == NC_BOUNTY_HUNTER ) {
personality.aggression += rng( 1, 6 );
personality.bravery += rng( 0, 5 );
} else if( type == NC_THUG ) {
personality.aggression += rng( 1, 6 );
personality.bravery += rng( 0, 5 );
} else if( type == NC_SCAVENGER ) {
personality.aggression += rng( 1, 3 );
personality.bravery += rng( 1, 4 );
}
//A universal barter boost to keep NPCs competitive with players
//The int boost from trade wasn't active... now that it is, most
//players will vastly outclass npcs in trade without a little help.
mod_skill_level( skill_barter, rng( 2, 4 ) );
recalc_hp();
for( int i = 0; i < num_hp_parts; i++ ) {
hp_cur[i] = hp_max[i];
}
starting_weapon( myclass );
starting_clothes( *this, myclass, male );
starting_inv( *this, myclass );
has_new_items = true;
empty_traits();
// Add fixed traits
for( const auto &tid : trait_group::traits_from( myclass->traits ) ) {
set_mutation( tid );
}
// Run mutation rounds
for( const auto &mr : type->mutation_rounds ) {
int rounds = mr.second.roll();
for( int i = 0; i < rounds; ++i ) {
mutate_category( mr.first );
}
}
// Add bionics
for( const auto &bl : type->bionic_list ) {
int chance = bl.second;
if( rng( 0, 100 ) <= chance ) {
add_bionic( bl.first );
}
}
// Add spells for magiclysm mod
for( std::pair<spell_id, int> spell_pair : type->_starting_spells ) {
this->magic.learn_spell( spell_pair.first, *this, true );
spell &sp = this->magic.get_spell( spell_pair.first );
while( sp.get_level() < spell_pair.second && !sp.is_max_level() ) {
sp.gain_level();
}
}
}
void npc::randomize_from_faction( faction *fac )
{
// Personality = aggression, bravery, altruism, collector
set_fac( fac->id );
randomize( npc_class_id::NULL_ID() );
}
void npc::set_fac( const faction_id &id )
{
if( my_fac ) {
my_fac->remove_member( getID() );
}
my_fac = g->faction_manager_ptr->get( id );
if( my_fac ) {
if( !is_fake() && !is_hallucination() ) {
my_fac->add_to_membership( getID(), disp_name(), known_to_u );
}
fac_id = my_fac->id;
} else {
return;
}
apply_ownership_to_inv();
}
void npc::apply_ownership_to_inv()
{
for( auto &e : inv_dump() ) {
e->set_owner( *this );
}
}
faction_id npc::get_fac_id() const
{
return fac_id;
}
faction *npc::get_faction() const
{
if( !my_fac ) {
return g->faction_manager_ptr->get( faction_id( "no_faction" ) );
}
return my_fac;
}
// item id from group "<class-name>_<what>" or from fallback group
// may still be a null item!
static item random_item_from( const npc_class_id &type, const std::string &what,
const std::string &fallback )
{
auto result = item_group::item_from( type.str() + "_" + what, calendar::turn );
if( result.is_null() ) {
result = item_group::item_from( fallback, calendar::turn );
}
return result;
}
// item id from "<class-name>_<what>" or from "npc_<what>"
static item random_item_from( const npc_class_id &type, const std::string &what )
{
return random_item_from( type, what, "npc_" + what );
}
// item id from "<class-name>_<what>_<gender>" or from "npc_<what>_<gender>"
static item get_clothing_item( const npc_class_id &type, const std::string &what, bool male )
{
item result;
//Check if class has gendered clothing
//Then check if it has an ungendered version
//Only if all that fails, grab from the default class.
if( male ) {
result = random_item_from( type, what + "_male", "null" );
} else {
result = random_item_from( type, what + "_female", "null" );
}
if( result.is_null() ) {
if( male ) {
result = random_item_from( type, what, "npc_" + what + "_male" );
} else {
result = random_item_from( type, what, "npc_" + what + "_female" );
}
}
return result;
}
void starting_clothes( npc &who, const npc_class_id &type, bool male )
{
std::vector<item> ret;
if( item_group::group_is_defined( type->worn_override ) ) {
ret = item_group::items_from( type->worn_override );
} else {
ret.push_back( get_clothing_item( type, "pants", male ) );
ret.push_back( get_clothing_item( type, "shirt", male ) );
ret.push_back( get_clothing_item( type, "underwear_top", male ) );
ret.push_back( get_clothing_item( type, "underwear_bottom", male ) );
ret.push_back( get_clothing_item( type, "underwear_feet", male ) );
ret.push_back( get_clothing_item( type, "shoes", male ) );
ret.push_back( random_item_from( type, "gloves" ) );
ret.push_back( random_item_from( type, "coat" ) );
ret.push_back( random_item_from( type, "vest" ) );
ret.push_back( random_item_from( type, "masks" ) );
// Why is the alternative group not named "npc_glasses" but "npc_eyes"?
ret.push_back( random_item_from( type, "glasses", "npc_eyes" ) );
ret.push_back( random_item_from( type, "hat" ) );
ret.push_back( random_item_from( type, "scarf" ) );
ret.push_back( random_item_from( type, "storage" ) );
ret.push_back( random_item_from( type, "holster" ) );
ret.push_back( random_item_from( type, "belt" ) );
ret.push_back( random_item_from( type, "wrist" ) );
ret.push_back( random_item_from( type, "extra" ) );
}
for( item &it : who.worn ) {
it.on_takeoff( who );
}
who.worn.clear();
for( item &it : ret ) {
if( it.has_flag( "VARSIZE" ) ) {
it.item_tags.insert( "FIT" );
}
if( who.can_wear( it ).success() ) {
it.on_wear( who );
who.worn.push_back( it );
it.set_owner( who );
}
}
}
void starting_inv( npc &who, const npc_class_id &type )
{
std::list<item> res;
who.inv.clear();
if( item_group::group_is_defined( type->carry_override ) ) {
who.inv += item_group::items_from( type->carry_override );
return;
}
res.emplace_back( "lighter" );
// If wielding a gun, get some additional ammo for it
if( who.weapon.is_gun() ) {
item ammo( who.weapon.ammo_default() );
ammo = ammo.in_its_container();
if( ammo.made_of( LIQUID ) ) {
item container( "bottle_plastic" );
container.put_in( ammo );
ammo = container;
}
// TODO: Move to npc_class
// NC_COWBOY and NC_BOUNTY_HUNTER get 5-15 whilst all others get 3-6
int qty = 1 + ( type == NC_COWBOY ||
type == NC_BOUNTY_HUNTER );
qty = rng( qty, qty * 2 );
while( qty-- != 0 && who.can_pickVolume( ammo ) ) {
// TODO: give NPC a default magazine instead
res.push_back( ammo );
}
}
if( type == NC_ARSONIST ) {
res.emplace_back( "molotov" );
}
int qty = ( type == NC_EVAC_SHOPKEEP ||
type == NC_TRADER ) ? 5 : 2;
qty = rng( qty, qty * 3 );
while( qty-- != 0 ) {
item tmp = random_item_from( type, "misc" ).in_its_container();
if( !tmp.is_null() ) {
if( !one_in( 3 ) && tmp.has_flag( "VARSIZE" ) ) {
tmp.item_tags.insert( "FIT" );
}
if( who.can_pickVolume( tmp ) ) {
res.push_back( tmp );
}
}
}
res.erase( std::remove_if( res.begin(), res.end(), [&]( const item & e ) {
return e.has_flag( "TRADER_AVOID" );
} ), res.end() );
for( auto &it : res ) {
it.set_owner( who );
}
who.inv += res;
}
void npc::revert_after_activity()
{
mission = previous_mission;
attitude = previous_attitude;
activity = player_activity();
current_activity_id = activity_id::NULL_ID();
clear_destination();
backlog.clear();
}
npc_mission npc::get_previous_mission()
{
return previous_mission;
}
npc_attitude npc::get_previous_attitude()
{
return previous_attitude;
}
bool npc::get_known_to_u()
{
return known_to_u;
}
void npc::set_known_to_u( bool known )
{
known_to_u = known;
if( my_fac ) {
my_fac->add_to_membership( getID(), disp_name(), known_to_u );
}
}
void npc::setpos( const tripoint &pos )
{
position = pos;
const point pos_om_old = sm_to_om_copy( submap_coords );
submap_coords.x = g->get_levx() + pos.x / SEEX;
submap_coords.y = g->get_levy() + pos.y / SEEY;
const point pos_om_new = sm_to_om_copy( submap_coords );
if( !is_fake() && pos_om_old != pos_om_new ) {
overmap &om_old = overmap_buffer.get( pos_om_old );
overmap &om_new = overmap_buffer.get( pos_om_new );
if( const auto ptr = om_old.erase_npc( getID() ) ) {
om_new.insert_npc( ptr );
} else {
// Don't move the npc pointer around to avoid having two overmaps
// with the same npc pointer
debugmsg( "could not find npc %s on its old overmap", name );
}
}
}
void npc::travel_overmap( const tripoint &pos )
{
const point pos_om_old = sm_to_om_copy( submap_coords );
spawn_at_sm( pos.x, pos.y, pos.z );
const point pos_om_new = sm_to_om_copy( submap_coords );
if( global_omt_location() == goal ) {
reach_omt_destination();
}
if( !is_fake() && pos_om_old != pos_om_new ) {
overmap &om_old = overmap_buffer.get( pos_om_old );
overmap &om_new = overmap_buffer.get( pos_om_new );
if( const auto ptr = om_old.erase_npc( getID() ) ) {
om_new.insert_npc( ptr );
} else {
// Don't move the npc pointer around to avoid having two overmaps
// with the same npc pointer
debugmsg( "could not find npc %s on its old overmap", name );
}
}
}
void npc::spawn_at_sm( int x, int y, int z )
{
spawn_at_precise( point( x, y ), tripoint( rng( 0, SEEX - 1 ), rng( 0, SEEY - 1 ), z ) );
}
void npc::spawn_at_precise( const point &submap_offset, const tripoint &square )
{
submap_coords = submap_offset;
submap_coords.x += square.x / SEEX;
submap_coords.y += square.y / SEEY;
position.x = square.x % SEEX;
position.y = square.y % SEEY;
position.z = square.z;
}
tripoint npc::global_square_location() const
{
return sm_to_ms_copy( submap_coords ) + tripoint( posx() % SEEX, posy() % SEEY, position.z );
}
void npc::place_on_map()
{
// The global absolute position (in map squares) of the npc is *always*
// "submap_coords.x * SEEX + posx() % SEEX" (analog for y).
// The main map assumes that pos is in its own (local to the main map)
// coordinate system. We have to change pos to match that assumption
const int dmx = submap_coords.x - g->get_levx();
const int dmy = submap_coords.y - g->get_levy();
const int offset_x = position.x % SEEX;
const int offset_y = position.y % SEEY;
// value of "submap_coords.x * SEEX + posx()" is unchanged
setpos( tripoint( offset_x + dmx * SEEX, offset_y + dmy * SEEY, posz() ) );
if( g->is_empty( pos() ) || is_mounted() ) {
return;
}
for( const tripoint &p : closest_tripoints_first( pos(), SEEX + 1 ) ) {
if( g->is_empty( p ) ) {
setpos( p );
return;
}
}
debugmsg( "Failed to place NPC in a valid location near (%d,%d,%d)", posx(), posy(), posz() );
}
skill_id npc::best_skill() const
{
int highest_level = std::numeric_limits<int>::min();
skill_id highest_skill( skill_id::NULL_ID() );
for( const auto &p : *_skills ) {
if( p.first.obj().is_combat_skill() ) {
const int level = p.second.level();
if( level > highest_level ) {
highest_level = level;
highest_skill = p.first;
}
}
}
return highest_skill;
}
int npc::best_skill_level() const
{
int highest_level = std::numeric_limits<int>::min();
for( const auto &p : *_skills ) {
if( p.first.obj().is_combat_skill() ) {
const int level = p.second.level();
if( level > highest_level ) {
highest_level = level;
}
}
}
return highest_level;
}
void npc::starting_weapon( const npc_class_id &type )
{
if( item_group::group_is_defined( type->weapon_override ) ) {
weapon = item_group::item_from( type->weapon_override, calendar::turn );
return;
}
const skill_id best = best_skill();
// if NPC has no suitable skills default to stabbing weapon
if( !best || best == skill_stabbing ) {
weapon = random_item_from( type, "stabbing", "survivor_stabbing" );
} else if( best == skill_bashing ) {
weapon = random_item_from( type, "bashing", "survivor_bashing" );
} else if( best == skill_cutting ) {
weapon = random_item_from( type, "cutting", "survivor_cutting" );
} else if( best == skill_throw ) {
weapon = random_item_from( type, "throw" );
} else if( best == skill_archery ) {
weapon = random_item_from( type, "archery" );
} else if( best == skill_pistol ) {
weapon = random_item_from( type, "pistol", "guns_pistol_common" );
} else if( best == skill_shotgun ) {
weapon = random_item_from( type, "shotgun", "guns_shotgun_common" );
} else if( best == skill_smg ) {
weapon = random_item_from( type, "smg", "guns_smg_common" );
} else if( best == skill_rifle ) {
weapon = random_item_from( type, "rifle", "guns_rifle_common" );
}
if( weapon.is_gun() ) {
weapon.ammo_set( weapon.ammo_default() );
}
weapon.set_owner( get_faction()->id );
}
bool npc::can_read( const item &book, std::vector<std::string> &fail_reasons )
{
if( !book.is_book() ) {
fail_reasons.push_back( string_format( _( "This %s is not good reading material." ),
book.tname() ) );
return false;
}
player *pl = dynamic_cast<player *>( this );
if( !pl ) {
return false;
}
const auto &type = book.type->book;
const skill_id &skill = type->skill;
const int skill_level = pl->get_skill_level( skill );
if( skill && skill_level < type->req ) {
fail_reasons.push_back( string_format( _( "I'm not smart enough to read this book." ) ) );
return false;
}
if( !skill || skill_level >= type->level ) {
fail_reasons.push_back( string_format( _( "I won't learn anything from this book." ) ) );
return false;
}
// Check for conditions that disqualify us
if( type->intel > 0 && has_trait( trait_ILLITERATE ) ) {
fail_reasons.emplace_back( _( "I can't read!" ) );
} else if( has_trait( trait_HYPEROPIC ) && !worn_with_flag( "FIX_FARSIGHT" ) &&
!has_effect( effect_contacts ) && !has_bionic( bio_eye_optic ) ) {
fail_reasons.emplace_back( _( "I can't read without my glasses." ) );
} else if( fine_detail_vision_mod() > 4 ) {
// Too dark to read only applies if the player can read to himself
fail_reasons.emplace_back( _( "It's too dark to read!" ) );
return false;
}
return true;
}
int npc::time_to_read( const item &book, const player &reader ) const
{
const auto &type = book.type->book;
const skill_id &skill = type->skill;
// The reader's reading speed has an effect only if they're trying to understand the book as they read it
// Reading speed is assumed to be how well you learn from books (as opposed to hands-on experience)
const bool try_understand = reader.fun_to_read( book ) ||
reader.get_skill_level( skill ) < type->level;
int reading_speed = try_understand ? std::max( reader.read_speed(), read_speed() ) : read_speed();
int retval = type->time * reading_speed;
retval *= std::min( fine_detail_vision_mod(), reader.fine_detail_vision_mod() );
if( type->intel > reader.get_int() && !reader.has_trait( trait_PROF_DICEMASTER ) ) {
retval += type->time * ( type->intel - reader.get_int() ) * 100;
}
return retval;
}
void npc::finish_read( item &book )
{
const auto &reading = book.type->book;
if( !reading ) {
revert_after_activity();
return;
}
const skill_id &skill = reading->skill;
// NPCs dont need to identify the book or learn recipes yet.
// NPCs dont read to other NPCs yet.
const bool display_messages = my_fac->id == faction_id( "your_followers" ) && g->u.sees( pos() );
bool continuous = false; //whether to continue reading or not
std::set<std::string> little_learned; // NPCs who learned a little about the skill
std::set<std::string> cant_learn;
std::list<std::string> out_of_chapters;
if( book_fun_for( book, *this ) != 0 ) {
//Fun bonus is no longer calculated here.
add_morale( MORALE_BOOK, book_fun_for( book, *this ) * 5, book_fun_for( book,
*this ) * 15, 1_hours, 30_minutes, true,
book.type );
}
book.mark_chapter_as_read( *this );
if( skill && get_skill_level( skill ) < reading->level &&
get_skill_level_object( skill ).can_train() ) {
SkillLevel &skill_level = get_skill_level_object( skill );
const int originalSkillLevel = skill_level.level();
// Calculate experience gained
/** @EFFECT_INT increases reading comprehension */
// Enhanced Memory Banks modestly boosts experience
int min_ex = std::max( 1, reading->time / 10 + get_int() / 4 );
int max_ex = reading->time / 5 + get_int() / 2 - originalSkillLevel;
if( has_active_bionic( bio_memory ) ) {
min_ex += 2;
}
if( max_ex < 2 ) {
max_ex = 2;
}
if( max_ex > 10 ) {
max_ex = 10;
}
if( max_ex < min_ex ) {
max_ex = min_ex;
}
const std::string &s = activity.get_str_value( 0, "1" );
double penalty = strtod( s.c_str(), nullptr );
min_ex *= ( originalSkillLevel + 1 ) * penalty;
min_ex = std::max( min_ex, 1 );
max_ex *= ( originalSkillLevel + 1 ) * penalty;
max_ex = std::max( min_ex, max_ex );
skill_level.readBook( min_ex, max_ex, reading->level );
std::string skill_name = skill.obj().name();
if( skill_level != originalSkillLevel ) {
g->events().send<event_type::gains_skill_level>( getID(), skill, skill_level.level() );
if( display_messages ) {
add_msg( m_good, _( "%s increases their %s level." ), disp_name(), skill_name );
// NPC reads until they gain a level, then stop.
revert_after_activity();
return;
}
} else {
continuous = true;
if( display_messages ) {
add_msg( m_info, _( "%s learns a little about %s!" ), disp_name(), skill.obj().name() );
}
}
if( ( skill_level == reading->level || !skill_level.can_train() ) ||
( ( has_trait( trait_SCHIZOPHRENIC ) ||
has_artifact_with( AEP_SCHIZO ) ) && one_in( 25 ) ) ) {
if( display_messages ) {
add_msg( m_info, _( "%s can no longer learn from %s." ), disp_name(), book.type_name() );
}
}
} else if( skill ) {
if( display_messages ) {
add_msg( m_info, _( "%s can no longer learn from %s." ), disp_name(), book.type_name() );
}
}
// NPCs can't learn martial arts from manuals (yet)
if( continuous ) {
activity.set_to_null();
player *pl = dynamic_cast<player *>( this );
if( pl ) {
start_read( book, pl );
}
if( activity ) {
return;
}
}
activity.set_to_null();
revert_after_activity();
}
void npc::start_read( item &chosen, player *pl )
{
const int time_taken = time_to_read( chosen, *pl );
const double penalty = static_cast<double>( time_taken ) / time_to_read( chosen, *pl );
player_activity act( ACT_READ, time_taken, 0, pl->getID().get_value() );
act.targets.emplace_back( item_location( *this, &chosen ) );
act.str_values.push_back( to_string( penalty ) );
// push an indentifier of martial art book to the action handling
if( chosen.type->use_methods.count( "MA_MANUAL" ) ) {
act.str_values.clear();
act.str_values.emplace_back( "martial_art" );
}
assign_activity( act );
}
void npc::do_npc_read()
{
// Can read items from inventory or within one tile (including in vehicles)
player *pl = dynamic_cast<player *>( this );
if( !pl ) {
return;
}
auto loc = game_menus::inv::read( *pl );
if( loc ) {
std::vector<std::string> fail_reasons;
Character *ch = dynamic_cast<Character *>( pl );
if( !ch ) {
return;
}
item &chosen = i_at( loc.obtain( *ch ) );
if( can_read( chosen, fail_reasons ) ) {
if( g->u.sees( pos() ) ) {
add_msg( m_info, _( "%s starts reading." ), disp_name() );
}
start_read( chosen, pl );
} else {
for( const auto &elem : fail_reasons ) {
say( elem );
}
}
} else {
add_msg( _( "Never mind." ) );
}
}
bool npc::wear_if_wanted( const item &it )
{
// Note: this function isn't good enough to use with NPC AI alone
// Restrict it to player's orders for now
if( !it.is_armor() ) {
return false;
}
// TODO: Make it depend on stuff
static const std::array<int, num_bp> max_encumb = {{
30, // bp_torso - Higher if ranged?
100, // bp_head
30, // bp_eyes - Lower if using ranged?
30, // bp_mouth
30, // bp_arm_l
30, // bp_arm_r