forked from cataclysmbnteam/Cataclysm-BN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
avatar.cpp
1330 lines (1175 loc) · 47 KB
/
avatar.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 "avatar.h"
#include <algorithm>
#include <array>
#include <climits>
#include <cstdlib>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <optional>
#include <set>
#include <unordered_map>
#include <utility>
#include "action.h"
#include "bodypart.h"
#include "calendar.h"
#include "cata_utility.h"
#include "catacharset.h"
#include "character.h"
#include "character_effects.h"
#include "character_id.h"
#include "character_functions.h"
#include "character_martial_arts.h"
#include "character_stat.h"
#include "clzones.h"
#include "color.h"
#include "debug.h"
#include "diary.h"
#include "effect.h"
#include "enums.h"
#include "event.h"
#include "event_bus.h"
#include "faction.h"
#include "game.h"
#include "game_constants.h"
#include "help.h"
#include "inventory.h"
#include "item.h"
#include "item_contents.h"
#include "item_location.h"
#include "itype.h"
#include "iuse.h"
#include "kill_tracker.h"
#include "magic_teleporter_list.h"
#include "map.h"
#include "map_memory.h"
#include "martialarts.h"
#include "messages.h"
#include "mission.h"
#include "monster.h"
#include "morale.h"
#include "morale_types.h"
#include "mtype.h"
#include "npc.h"
#include "options.h"
#include "output.h"
#include "overmap.h"
#include "pathfinding.h"
#include "pimpl.h"
#include "player.h"
#include "player_activity.h"
#include "ranged.h"
#include "recipe.h"
#include "ret_val.h"
#include "rng.h"
#include "skill.h"
#include "stomach.h"
#include "string_formatter.h"
#include "string_id.h"
#include "translations.h"
#include "type_id.h"
#include "ui.h"
#include "value_ptr.h"
#include "vehicle.h"
#include "vpart_position.h"
static const activity_id ACT_READ( "ACT_READ" );
static const bionic_id bio_eye_optic( "bio_eye_optic" );
static const bionic_id bio_memory( "bio_memory" );
static const bionic_id bio_watch( "bio_watch" );
static const efftype_id effect_alarm_clock( "alarm_clock" );
static const efftype_id effect_contacts( "contacts" );
static const efftype_id effect_sleep( "sleep" );
static const efftype_id effect_slept_through_alarm( "slept_through_alarm" );
static const itype_id itype_guidebook( "guidebook" );
static const trait_id trait_CENOBITE( "CENOBITE" );
static const trait_id trait_HYPEROPIC( "HYPEROPIC" );
static const trait_id trait_ILLITERATE( "ILLITERATE" );
static const trait_id trait_PROF_DICEMASTER( "PROF_DICEMASTER" );
static const std::string flag_FIX_FARSIGHT( "FIX_FARSIGHT" );
class JsonIn;
class JsonOut;
static void skim_book_msg( const item &book, avatar &u );
avatar &get_avatar()
{
return g->u;
}
avatar::avatar()
{
player_map_memory = std::make_unique<map_memory>();
show_map_memory = true;
active_mission = nullptr;
grab_type = OBJECT_NONE;
a_diary = nullptr;
}
avatar::~avatar() = default;
avatar::avatar( avatar && ) = default;
avatar &avatar::operator=( avatar && ) = default;
void avatar::toggle_map_memory()
{
show_map_memory = !show_map_memory;
}
bool avatar::should_show_map_memory()
{
return show_map_memory;
}
bool avatar::save_map_memory()
{
return player_map_memory->save( g->m.getabs( pos() ) );
}
void avatar::load_map_memory()
{
player_map_memory->load( g->m.getabs( pos() ) );
}
void avatar::prepare_map_memory_region( const tripoint &p1, const tripoint &p2 )
{
player_map_memory->prepare_region( p1, p2 );
}
const memorized_terrain_tile &avatar::get_memorized_tile( const tripoint &pos ) const
{
return player_map_memory->get_tile( pos );
}
void avatar::memorize_tile( const tripoint &pos, const std::string &ter, const int subtile,
const int rotation )
{
player_map_memory->memorize_tile( pos, ter, subtile, rotation );
}
void avatar::memorize_symbol( const tripoint &pos, const int symbol )
{
player_map_memory->memorize_symbol( pos, symbol );
}
int avatar::get_memorized_symbol( const tripoint &p ) const
{
return player_map_memory->get_symbol( p );
}
void avatar::clear_memorized_tile( const tripoint &pos )
{
player_map_memory->clear_memorized_tile( pos );
}
bool avatar::has_memorized_tile_for_autodrive( const tripoint &p ) const
{
return player_map_memory->has_memory_for_autodrive( p );
}
std::vector<mission *> avatar::get_active_missions() const
{
return active_missions;
}
std::vector<mission *> avatar::get_completed_missions() const
{
return completed_missions;
}
std::vector<mission *> avatar::get_failed_missions() const
{
return failed_missions;
}
mission *avatar::get_active_mission() const
{
return active_mission;
}
void avatar::reset_all_missions()
{
active_mission = nullptr;
active_missions.clear();
completed_missions.clear();
failed_missions.clear();
}
tripoint_abs_omt avatar::get_active_mission_target() const
{
if( active_mission == nullptr ) {
return overmap::invalid_tripoint;
}
return active_mission->get_target();
}
void avatar::set_active_mission( mission &cur_mission )
{
const auto iter = std::find( active_missions.begin(), active_missions.end(), &cur_mission );
if( iter == active_missions.end() ) {
debugmsg( "new active mission %d is not in the active_missions list", cur_mission.get_id() );
} else {
active_mission = &cur_mission;
}
}
void avatar::on_mission_assignment( mission &new_mission )
{
active_missions.push_back( &new_mission );
set_active_mission( new_mission );
}
void avatar::on_mission_finished( mission &cur_mission )
{
if( cur_mission.has_failed() ) {
failed_missions.push_back( &cur_mission );
add_msg_if_player( m_bad, _( "Mission \"%s\" is failed." ), cur_mission.name() );
} else {
completed_missions.push_back( &cur_mission );
add_msg_if_player( m_good, _( "Mission \"%s\" is successfully completed." ),
cur_mission.name() );
}
const auto iter = std::find( active_missions.begin(), active_missions.end(), &cur_mission );
if( iter == active_missions.end() ) {
debugmsg( "completed mission %d was not in the active_missions list", cur_mission.get_id() );
} else {
active_missions.erase( iter );
}
if( &cur_mission == active_mission ) {
if( active_missions.empty() ) {
active_mission = nullptr;
} else {
active_mission = active_missions.front();
}
}
}
const player *avatar::get_book_reader( const item &book, std::vector<std::string> &reasons ) const
{
const player *reader = nullptr;
if( !book.is_book() ) {
reasons.push_back( string_format( _( "Your %s is not good reading material." ),
book.tname() ) );
return nullptr;
}
// Check for conditions that immediately disqualify the player from reading:
const optional_vpart_position vp = get_map().veh_at( pos() );
if( vp && vp->vehicle().player_in_control( *this ) ) {
reasons.emplace_back( _( "It's a bad idea to read while driving!" ) );
return nullptr;
}
const auto &type = book.type->book;
if( !character_funcs::is_fun_to_read( *this, book ) && !has_morale_to_read() ) {
// Low morale still permits skimming
reasons.emplace_back( _( "What's the point of studying? (Your morale is too low!)" ) );
return nullptr;
}
const skill_id &skill = type->skill;
const int skill_level = get_skill_level( skill );
if( skill && skill_level < type->req ) {
reasons.push_back( string_format( _( "%s %d needed to understand. You have %d" ),
skill.obj().name(), type->req, skill_level ) );
return nullptr;
}
// Check for conditions that disqualify us only if no NPCs can read to us
if( type->intel > 0 && has_trait( trait_ILLITERATE ) ) {
reasons.emplace_back( _( "You're illiterate!" ) );
} else if( has_trait( trait_HYPEROPIC ) && !worn_with_flag( flag_FIX_FARSIGHT ) &&
!has_effect( effect_contacts ) && !has_bionic( bio_eye_optic ) ) {
reasons.emplace_back( _( "Your eyes won't focus without reading glasses." ) );
} else if( !character_funcs::can_see_fine_details( *this ) ) {
// Too dark to read only applies if the player can read to himself
reasons.emplace_back( _( "It's too dark to read!" ) );
return nullptr;
} else {
return this;
}
//Check for NPCs to read for you, negates Illiterate and Far Sighted
//The fastest-reading NPC is chosen
if( is_deaf() ) {
reasons.emplace_back( _( "Maybe someone could read that to you, but you're deaf!" ) );
return nullptr;
}
int time_taken = INT_MAX;
auto candidates = character_funcs::get_crafting_helpers( *this );
for( const npc *elem : candidates ) {
// Check for disqualifying factors:
if( type->intel > 0 && elem->has_trait( trait_ILLITERATE ) ) {
reasons.push_back( string_format( _( "%s is illiterate!" ),
elem->disp_name() ) );
} else if( skill && elem->get_skill_level( skill ) < type->req ) {
reasons.push_back( string_format( _( "%s %d needed to understand. %s has %d" ),
skill.obj().name(), type->req, elem->disp_name(), elem->get_skill_level( skill ) ) );
} else if( elem->has_trait( trait_HYPEROPIC ) && !elem->worn_with_flag( flag_FIX_FARSIGHT ) &&
!elem->has_effect( effect_contacts ) ) {
reasons.push_back( string_format( _( "%s needs reading glasses!" ),
elem->disp_name() ) );
} else if( !character_funcs::fine_detail_vision_mod( *this ) &&
!character_funcs::fine_detail_vision_mod( *elem ) ) {
reasons.push_back( string_format(
_( "It's too dark for %s to read!" ),
elem->disp_name() ) );
} else if( !elem->sees( *this ) ) {
reasons.push_back( string_format( _( "%s could read that to you, but they can't see you." ),
elem->disp_name() ) );
} else if( !character_funcs::is_fun_to_read( *elem, book ) && !elem->has_morale_to_read() ) {
// Low morale still permits skimming
reasons.push_back( string_format( _( "%s morale is too low!" ), elem->disp_name( true ) ) );
} else if( elem->is_blind() ) {
reasons.push_back( string_format( _( "%s is blind." ), elem->disp_name() ) );
} else {
int proj_time = time_to_read( book, *elem );
if( proj_time < time_taken ) {
reader = elem;
time_taken = proj_time;
}
}
}
//end for all candidates
return reader;
}
int avatar::time_to_read( const item &book, const player &reader, const player *learner ) 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 = character_funcs::is_fun_to_read( reader, book ) ||
reader.get_skill_level( skill ) < type->level;
int reading_speed = try_understand ? std::max( reader.read_speed(), read_speed() ) : read_speed();
if( learner ) {
reading_speed = std::max( reading_speed, learner->read_speed() );
}
int retval = type->time * reading_speed;
retval *= std::min( character_funcs::fine_detail_vision_mod( *this ),
character_funcs::fine_detail_vision_mod( reader ) );
const int effective_int = std::min( { get_int(), reader.get_int(), learner ? learner->get_int() : INT_MAX } );
if( type->intel > effective_int && !reader.has_trait( trait_PROF_DICEMASTER ) ) {
retval += type->time * ( type->intel - effective_int ) * 100;
}
return retval;
}
diary *avatar::get_avatar_diary()
{
if( a_diary == nullptr ) {
a_diary = std::make_unique<diary>();
}
return a_diary.get();
}
/**
* Explanation of ACT_READ activity values:
*
* position: ID of the reader
* targets: 1-element vector with the item_location (always in inventory/wielded) of the book being read
* index: We are studying until the player with this ID gains a level; 0 indicates reading once
* values: IDs of the NPCs who will learn something
* str_values: Parallel to values, these contain the learning penalties (as doubles in string form) as follows:
* Experience gained = Experience normally gained * penalty
*/
bool avatar::read( item_location loc, const bool continuous )
{
if( !loc ) {
add_msg( m_info, _( "Never mind." ) );
return false;
}
item &it = *loc;
if( !has_identified( it.typeId() ) ) {
// We insta-identify the book, then try to read it
items_identified.insert( it.typeId() );
skim_book_msg( it, *this );
}
std::vector<std::string> fail_messages;
const player *reader = get_book_reader( it, fail_messages );
if( reader == nullptr ) {
// We can't read, and neither can our followers
for( const std::string &reason : fail_messages ) {
add_msg( m_bad, reason );
}
return false;
}
const int time_taken = time_to_read( it, *reader );
add_msg( m_debug, "avatar::read: time_taken = %d", time_taken );
player_activity act( ACT_READ, time_taken, continuous ? activity.index : 0,
reader->getID().get_value() );
act.targets.emplace_back( loc );
if( it.typeId() == itype_guidebook ) {
// special guidebook effect: print a misc. hint when read
if( reader != this ) {
add_msg( m_info, fail_messages[0] );
dynamic_cast<const npc *>( reader )->say( get_hint() );
} else {
add_msg( m_info, get_hint() );
}
mod_moves( -100 );
return false;
}
const auto &type = it.type->book;
const skill_id &skill = type->skill;
const std::string skill_name = skill ? skill.obj().name() : "";
// Find NPCs to join the study session:
std::map<npc *, std::string> learners;
//reading only for fun
std::map<npc *, std::string> fun_learners;
std::map<npc *, std::string> nonlearners;
auto candidates = character_funcs::get_crafting_helpers( *this );
for( npc *elem : candidates ) {
const int lvl = elem->get_skill_level( skill );
const bool is_fun_to_read = character_funcs::is_fun_to_read( *elem, it );
const bool skill_req = ( is_fun_to_read && ( !skill || lvl >= type->req ) ) ||
( skill && lvl < type->level && lvl >= type->req );
const bool morale_req = is_fun_to_read || elem->has_morale_to_read();
if( !skill_req && elem != reader ) {
if( skill && lvl < type->req ) {
nonlearners.insert( { elem, string_format( _( " (needs %d %s)" ), type->req, skill_name ) } );
} else if( skill ) {
nonlearners.insert( { elem, string_format( _( " (already has %d %s)" ), type->level, skill_name ) } );
} else {
nonlearners.insert( { elem, _( " (uninterested)" ) } );
}
} else if( elem->is_deaf() && reader != elem ) {
nonlearners.insert( { elem, _( " (deaf)" ) } );
} else if( !morale_req ) {
nonlearners.insert( { elem, _( " (too sad)" ) } );
} else if( skill && lvl < type->level ) {
const double penalty = static_cast<double>( time_taken ) / time_to_read( it, *reader, elem );
learners.insert( {elem, elem == reader ? _( " (reading aloud to you)" ) : ""} );
act.values.push_back( elem->getID().get_value() );
act.str_values.push_back( std::to_string( penalty ) );
} else {
fun_learners.insert( {elem, elem == reader ? _( " (reading aloud to you)" ) : "" } );
act.values.push_back( elem->getID().get_value() );
act.str_values.emplace_back( "1" );
}
}
if( !continuous ) {
//only show the menu if there's useful information or multiple options
if( skill || !nonlearners.empty() || !fun_learners.empty() ) {
uilist menu;
// Some helpers to reduce repetition:
auto length = []( const std::pair<npc *, std::string> &elem ) {
return utf8_width( elem.first->disp_name() ) + utf8_width( elem.second );
};
auto max_length = [&length]( const std::map<npc *, std::string> &m ) {
auto max_ele = std::max_element( m.begin(),
m.end(), [&length]( const std::pair<npc *, std::string> &left,
const std::pair<npc *, std::string> &right ) {
return length( left ) < length( right );
} );
return max_ele == m.end() ? 0 : length( *max_ele );
};
auto get_text =
[&]( const std::map<npc *, std::string> &m, const std::pair<npc *, std::string> &elem ) {
const int lvl = elem.first->get_skill_level( skill );
const std::string lvl_text = skill ? string_format( _( " | current level: %d" ), lvl ) : "";
const std::string name_text = elem.first->disp_name() + elem.second;
return string_format( "%s%s", left_justify( name_text, max_length( m ) ), lvl_text );
};
auto add_header = [&menu]( const std::string & str ) {
menu.addentry( -1, false, -1, "" );
uilist_entry header( -1, false, -1, str, c_yellow, c_yellow );
header.force_color = true;
menu.entries.push_back( header );
};
menu.title = !skill ? string_format( _( "Reading %s" ), it.type_name() ) :
//~ %1$s: book name, %2$s: skill name, %3$d and %4$d: skill levels
string_format( _( "Reading %1$s (can train %2$s from %3$d to %4$d)" ), it.type_name(),
skill_name, type->req, type->level );
if( skill ) {
const int lvl = get_skill_level( skill );
menu.addentry( getID().get_value(), lvl < type->level, '0',
string_format( _( "Read until you gain a level | current level: %d" ), lvl ) );
} else {
menu.addentry( -1, false, '0', _( "Read until you gain a level" ) );
}
menu.addentry( 0, true, '1', _( "Read once" ) );
if( skill && !learners.empty() ) {
add_header( _( "Read until this NPC gains a level:" ) );
for( const auto &elem : learners ) {
menu.addentry( elem.first->getID().get_value(), true, -1,
get_text( learners, elem ) );
}
}
if( !fun_learners.empty() ) {
add_header( _( "Reading for fun:" ) );
for( const auto &elem : fun_learners ) {
menu.addentry( -1, false, -1, get_text( fun_learners, elem ) );
}
}
if( !nonlearners.empty() ) {
add_header( _( "Not participating:" ) );
for( const auto &elem : nonlearners ) {
menu.addentry( -1, false, -1, get_text( nonlearners, elem ) );
}
}
menu.query( true );
if( menu.ret == UILIST_CANCEL ) {
add_msg( m_info, _( "Never mind." ) );
return false;
}
act.index = menu.ret;
}
if( it.type->use_methods.count( "MA_MANUAL" ) ) {
if( martial_arts_data->has_martialart( martial_art_learned_from( *it.type ) ) ) {
add_msg_if_player( m_info, _( "You already know all this book has to teach." ) );
activity.set_to_null();
return false;
}
uilist menu;
menu.title = string_format( _( "Train %s from manual:" ),
martial_art_learned_from( *it.type )->name );
menu.addentry( -1, true, '1', _( "Train once" ) );
menu.addentry( getID().get_value(), true, '0', _( "Train until tired or success" ) );
menu.query( true );
if( menu.ret == UILIST_CANCEL ) {
add_msg( m_info, _( "Never mind." ) );
return false;
}
act.index = menu.ret;
}
add_msg( m_info, _( "Now reading %s, %s to stop early." ),
it.type_name(), press_x( ACTION_PAUSE ) );
}
// Print some informational messages, but only the first time or if the information changes
if( !continuous || activity.position != act.position ) {
if( reader != this ) {
add_msg( m_info, fail_messages[0] );
add_msg( m_info, _( "%s reads aloud…" ), reader->disp_name() );
} else if( !learners.empty() || !fun_learners.empty() ) {
add_msg( m_info, _( "You read aloud…" ) );
}
}
if( !continuous ||
!std::all_of( learners.begin(), learners.end(), [&]( const std::pair<npc *, std::string> &elem ) {
return std::count( activity.values.begin(), activity.values.end(),
elem.first->getID().get_value() ) != 0;
} ) ||
!std::all_of( activity.values.begin(), activity.values.end(), [&]( int elem ) {
return learners.find( g->find_npc( character_id( elem ) ) ) != learners.end();
} ) ) {
if( learners.size() == 1 ) {
add_msg( m_info, _( "%s studies with you." ), learners.begin()->first->disp_name() );
} else if( !learners.empty() ) {
const std::string them = enumerate_as_string( learners.begin(),
learners.end(), [&]( const std::pair<npc *, std::string> &elem ) {
return elem.first->disp_name();
} );
add_msg( m_info, _( "%s study with you." ), them );
}
// Don't include the reader as it would be too redundant.
std::set<std::string> readers;
for( const auto &elem : fun_learners ) {
if( elem.first != reader ) {
readers.insert( elem.first->disp_name() );
}
}
if( readers.size() == 1 ) {
add_msg( m_info, _( "%s reads with you for fun." ), readers.begin()->c_str() );
} else if( !readers.empty() ) {
const std::string them = enumerate_as_string( readers );
add_msg( m_info, _( "%s read with you for fun." ), them );
}
}
const float vision_mod = std::min(
character_funcs::fine_detail_vision_mod( *this ),
character_funcs::fine_detail_vision_mod( *reader )
);
if( vision_mod > character_funcs::FINE_VISION_PERFECT ) {
add_msg( m_warning,
_( "It's difficult for %s to see fine details right now. Reading will take longer than usual." ),
reader->disp_name() );
}
const int intelligence = get_int();
const bool complex_penalty = type->intel > std::min( intelligence, reader->get_int() ) &&
!reader->has_trait( trait_PROF_DICEMASTER );
const player *complex_player = reader->get_int() < intelligence ? reader : this;
if( complex_penalty && !continuous ) {
add_msg( m_warning,
_( "This book is too complex for %s to easily understand. It will take longer to read." ),
complex_player->disp_name() );
}
// push an indentifier of martial art book to the action handling
if( it.type->use_methods.count( "MA_MANUAL" ) ) {
if( get_stamina() < get_stamina_max() / 10 ) {
add_msg( m_info, _( "You are too exhausted to train martial arts." ) );
return false;
}
act.str_values.clear();
act.str_values.emplace_back( "martial_art" );
}
assign_activity( act );
// Reinforce any existing morale bonus/penalty, so it doesn't decay
// away while you read more.
const time_duration decay_start = 1_turns * time_taken / 1000;
std::set<Character *> apply_morale = { this };
for( const auto &elem : learners ) {
apply_morale.insert( elem.first );
}
for( const auto &elem : fun_learners ) {
apply_morale.insert( elem.first );
}
for( Character *ch : apply_morale ) {
int fun = character_funcs::get_book_fun_for( *ch, it );
ch->add_morale( MORALE_BOOK, 0, fun * 15, decay_start + 30_minutes,
decay_start, false, it.type );
}
return true;
}
void avatar::grab( object_type grab_type, const tripoint &grab_point )
{
this->grab_type = grab_type;
this->grab_point = grab_point;
path_settings->avoid_rough_terrain = grab_type != OBJECT_NONE;
}
object_type avatar::get_grab_type() const
{
return grab_type;
}
static void skim_book_msg( const item &book, avatar &u )
{
if( !book.type->book ) {
return;
}
const auto &reading = book.type->book;
const skill_id &skill = reading->skill;
if( skill && u.get_skill_level_object( skill ).can_train() ) {
add_msg( m_info, _( "Can bring your %s skill to %d." ),
skill.obj().name(), reading->level );
if( reading->req != 0 ) {
add_msg( m_info, _( "Requires %s level %d to understand." ),
skill.obj().name(), reading->req );
}
}
if( reading->intel != 0 ) {
add_msg( m_info, _( "Requires intelligence of %d to easily read." ), reading->intel );
}
//It feels wrong to use a pointer to *this, but I can't find any other player pointers in this method.
if( character_funcs::get_book_fun_for( u, book ) != 0 ) {
add_msg( m_info, _( "Reading this book affects your morale by %d" ),
character_funcs::get_book_fun_for( u, book ) );
}
if( book.type->use_methods.count( "MA_MANUAL" ) ) {
const matype_id style_to_learn = martial_art_learned_from( *book.type );
add_msg( m_info, _( "You can learn %s style from it." ), style_to_learn->name );
add_msg( m_info, _( "This fighting style is %s to learn." ),
martialart_difficulty( style_to_learn ) );
add_msg( m_info, _( "It would be easier to master if you'd have skill expertise in %s." ),
style_to_learn->primary_skill->name() );
add_msg( m_info, _( "A training session with this book takes %s" ),
to_string( time_duration::from_minutes( reading->time ) ) );
} else {
add_msg( m_info, vgettext( "A chapter of this book takes %d minute to read.",
"A chapter of this book takes %d minutes to read.", reading->time ),
reading->time );
}
std::vector<std::string> recipe_list;
for( const auto &elem : reading->recipes ) {
// If the player knows it, they recognize it even if it's not clearly stated.
if( elem.is_hidden() && !u.knows_recipe( elem.recipe ) ) {
continue;
}
recipe_list.push_back( elem.name );
}
if( !recipe_list.empty() ) {
std::string recipe_line =
string_format( vgettext( "This book contains %1$zu crafting recipe: %2$s",
"This book contains %1$zu crafting recipes: %2$s",
recipe_list.size() ),
recipe_list.size(),
enumerate_as_string( recipe_list ) );
add_msg( m_info, recipe_line );
}
if( recipe_list.size() != reading->recipes.size() ) {
add_msg( m_info, _( "It might help you figuring out some more recipes." ) );
}
add_msg( _( "You note that you have a copy of %s in your possession." ), book.type_name() );
}
void avatar::do_read( item_location loc )
{
if( !loc ) {
activity.set_to_null();
return;
}
item &book = *loc;
const auto &reading = book.type->book;
if( !reading ) {
activity.set_to_null();
return;
}
const skill_id &skill = reading->skill;
if( !has_identified( book.typeId() ) ) {
// Note that we've read the book.
items_identified.insert( book.typeId() );
skim_book_msg( book, *this );
activity.set_to_null();
return;
}
const bool allow_recipes = get_option<bool>( "ALLOW_LEARNING_BOOK_RECIPES" );
//learners and their penalties
std::vector<std::pair<player *, double>> learners;
for( size_t i = 0; i < activity.values.size(); i++ ) {
player *n = g->find_npc( character_id( activity.values[i] ) );
if( n != nullptr ) {
const std::string &s = activity.get_str_value( i, "1" );
learners.push_back( { n, strtod( s.c_str(), nullptr ) } );
}
// Otherwise they must have died/teleported or something
}
learners.push_back( { this, 1.0 } );
//whether to continue reading or not
bool continuous = false;
// NPCs who learned a little about the skill
std::set<std::string> little_learned;
std::set<std::string> cant_learn;
std::list<std::string> out_of_chapters;
for( auto &elem : learners ) {
player *learner = elem.first;
int book_fun_for = character_funcs::get_book_fun_for( *learner, book );
if( book_fun_for != 0 ) {
learner->add_morale( MORALE_BOOK, book_fun_for * 5, book_fun_for * 15, 1_hours, 30_minutes, true,
book.type );
}
book.mark_chapter_as_read( *learner );
const auto available_recipes = book.get_available_recipes( *learner );
std::vector<const recipe *> learnable_recipes;
for( const std::pair<const recipe *, int> &p : available_recipes ) {
if( allow_recipes && !learner->knows_recipe( p.first ) ) {
learnable_recipes.push_back( p.first );
learner->learn_recipe( p.first );
if( learner->is_player() ) {
add_msg( m_info, _( "You memorize a recipe for %s." ), p.first->result_name() );
}
}
}
if( skill && learner->get_skill_level( skill ) < reading->level &&
learner->get_skill_level_object( skill ).can_train() ) {
SkillLevel &skill_level = learner->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 + learner->get_int() / 4 );
int max_ex = reading->time / 5 + learner->get_int() / 2 - originalSkillLevel;
if( has_active_bionic( bio_memory ) ) {
min_ex += 2;
}
min_ex = adjust_for_focus( min_ex );
max_ex = adjust_for_focus( max_ex );
if( max_ex < 2 ) {
max_ex = 2;
}
if( max_ex > 10 ) {
max_ex = 10;
}
if( max_ex < min_ex ) {
max_ex = min_ex;
}
min_ex *= ( originalSkillLevel + 1 ) * elem.second;
min_ex = std::max( min_ex, 1 );
max_ex *= ( originalSkillLevel + 1 ) * elem.second;
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>(
learner->getID(), skill, skill_level.level() );
if( learner->is_player() ) {
add_msg( m_good, _( "You increase %s to level %d." ), skill.obj().name(),
originalSkillLevel + 1 );
} else {
add_msg( m_good, _( "%s increases their %s level." ), learner->disp_name(), skill_name );
}
} else {
//skill_level == originalSkillLevel
if( activity.index == learner->getID().get_value() ) {
continuous = true;
}
if( learner->is_player() ) {
add_msg( m_info, _( "You learn a little about %s! (%d%%)" ), skill_name, skill_level.exercise() );
} else {
little_learned.insert( learner->disp_name() );
}
}
if( learnable_recipes.empty() && ( skill_level == reading->level || !skill_level.can_train() ) ) {
if( learner->is_player() ) {
add_msg( m_info, _( "You can no longer learn from %s." ), book.type_name() );
} else {
cant_learn.insert( learner->disp_name() );
}
}
} else if( learnable_recipes.empty() && skill ) {
if( learner->is_player() ) {
add_msg( m_info, _( "You can no longer learn from %s." ), book.type_name() );
} else {
cant_learn.insert( learner->disp_name() );
}
}
}
//end for all learners
if( little_learned.size() == 1 ) {
add_msg( m_info, _( "%s learns a little about %s!" ), little_learned.begin()->c_str(),
skill.obj().name() );
} else if( !little_learned.empty() ) {
const std::string little_learned_msg = enumerate_as_string( little_learned );
add_msg( m_info, _( "%s learn a little about %s!" ), little_learned_msg, skill.obj().name() );
}
if( !cant_learn.empty() ) {
const std::string names = enumerate_as_string( cant_learn );
add_msg( m_info, _( "%s can no longer learn from %s." ), names, book.type_name() );
}
if( !out_of_chapters.empty() ) {
const std::string names = enumerate_as_string( out_of_chapters );
add_msg( m_info, _( "Rereading the %s isn't as much fun for %s." ),
book.type_name(), names );
if( out_of_chapters.front() == disp_name() && one_in( 6 ) ) {
add_msg( m_info, _( "Maybe you should find something new to read…" ) );
}
}
// NPCs can't learn martial arts from manuals (yet).
auto m = book.type->use_methods.find( "MA_MANUAL" );
if( m != book.type->use_methods.end() ) {
const matype_id style_to_learn = martial_art_learned_from( *book.type );
skill_id skill_used = style_to_learn->primary_skill;
int difficulty = std::max( 1, style_to_learn->learn_difficulty );
difficulty = std::max( 1, 20 + difficulty * 2 - get_skill_level( skill_used ) * 2 );
add_msg( m_debug, _( "Chance to learn one in: %d" ), difficulty );
if( one_in( difficulty ) ) {
m->second.call( *this, book, false, pos() );
continuous = false;
} else {
if( activity.index == getID().get_value() ) {
continuous = true;
switch( rng( 1, 5 ) ) {
case 1:
add_msg( m_info,
_( "You train the moves according to the book, but can't get a grasp of the style, so you start from the beginning." ) );
break;
case 2:
add_msg( m_info,
_( "This martial art is not easy to grasp. You start training the moves from the beginning." ) );
break;
case 3:
add_msg( m_info,
_( "You decide to read the manual and train even more. In martial arts, patience leads to mastery." ) );
break;
case 4:
case 5:
add_msg( m_info, _( "You try again. This training will finally pay off." ) );
break;
}
} else {
add_msg( m_info, _( "You train for a while." ) );
}
}
}
if( continuous ) {
activity.set_to_null();
read( loc, true );
if( activity ) {
return;
}
}
activity.set_to_null();
}
bool avatar::has_identified( const itype_id &item_id ) const
{
return items_identified.count( item_id ) > 0;
}
void avatar::wake_up()
{
if( has_effect( effect_sleep ) ) {
if( calendar::turn - get_effect( effect_sleep ).get_start_time() > 2_hours ) {
print_health();
}
// alarm was set and player hasn't slept through the alarm.
if( has_effect( effect_alarm_clock ) && !has_effect( effect_slept_through_alarm ) ) {
add_msg( _( "It looks like you woke up before your alarm." ) );
remove_effect( effect_alarm_clock );
} else if( has_effect( effect_slept_through_alarm ) ) {
if( has_bionic( bio_watch ) ) {
add_msg( m_warning, _( "It looks like you've slept through your internal alarm…" ) );
} else {
add_msg( m_warning, _( "It looks like you've slept through the alarm…" ) );
}
}
}
Character::wake_up();
}
void avatar::vomit()
{
if( stomach.get_calories() > 0 ) {
// Remove all joy from previously eaten food and apply the penalty
rem_morale( MORALE_FOOD_GOOD );
rem_morale( MORALE_FOOD_HOT );
// bears must suffer too
rem_morale( MORALE_HONEY );
// 1.5 times longer
add_morale( MORALE_VOMITED, -20, -40, 90_minutes, 45_minutes, false );
} else {
add_msg( m_warning, _( "You retched, but your stomach is empty." ) );
}
Character::vomit();
}
bool avatar::is_hallucination() const
{
return false;
}