forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
achievement.cpp
910 lines (810 loc) · 31.7 KB
/
achievement.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
#include "achievement.h"
#include <cstdlib>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include "cata_assert.h"
#include "color.h"
#include "debug.h"
#include "enum_conversions.h"
#include "enums.h"
#include "event.h"
#include "event_statistics.h"
#include "generic_factory.h"
#include "json.h"
#include "past_games_info.h"
#include "stats_tracker.h"
#include "string_formatter.h"
template <typename E> struct enum_traits;
static const achievement_id achievement_achievement_arcade_mode( "achievement_arcade_mode" );
// Some details about how achievements work
// ========================================
//
// Achievements are built on the stats_tracker, which is in turn built on the
// event_bus. Each of these layers involves subscription / callback style
// interfaces, so the code flow may now be obvious. Here's a quick outline of
// the execution flow to help clarify how it all fits together.
//
// * Various core game code paths generate events via the event bus.
// * The stats_tracker subscribes to the event bus, and receives these events.
// * Events contribute to event_multisets managed by the stats_tracker.
// * (In the docs, these event_multisets are described as "event streams").
// * (Optionally) event_transformations transform these event_multisets into
// other event_multisets based on json-defined transformation rules. These
// are also managed by stats_tracker.
// * event_statistics monitor these event_multisets and summarize them into
// single values. These are also managed by stats_tracker.
// * Each achievement requirement has a corresponding requirement_watcher which
// is alerted to statistic changes via the stats_tracker's watch interface.
// * Each requirement_watcher notifies its achievement_tracker (of which there
// is one per achievement) of the requirement's status on each change to a
// statistic.
// * The achievement_tracker keeps track of which requirements are currently
// satisfied and which are not.
// * When all the requirements are satisfied, the achievement_tracker tells the
// achievement_tracker (only one of these exists per game).
// * The achievement_tracker calls the achievement_attained_callback it was
// given at construction time. This hooks into the actual game logic (e.g.
// telling the player they just got an achievement).
namespace
{
generic_factory<achievement> achievement_factory( "achievement" );
} // namespace
/** @relates string_id */
template<>
const achievement &string_id<achievement>::obj() const
{
return achievement_factory.obj( *this );
}
/** @relates string_id */
template<>
bool string_id<achievement>::is_valid() const
{
return achievement_factory.is_valid( *this );
}
enum class requirement_visibility : int {
always,
when_requirement_completed,
when_achievement_completed,
never,
last
};
template<>
struct enum_traits<requirement_visibility> {
static constexpr requirement_visibility last = requirement_visibility::last;
};
namespace io
{
template<>
std::string enum_to_string<achievement_comparison>( achievement_comparison data )
{
switch( data ) {
// *INDENT-OFF*
case achievement_comparison::equal: return "==";
case achievement_comparison::less_equal: return "<=";
case achievement_comparison::greater_equal: return ">=";
case achievement_comparison::anything: return "anything";
// *INDENT-ON*
case achievement_comparison::last:
break;
}
cata_fatal( "Invalid achievement_comparison" );
}
template<>
std::string enum_to_string<achievement::time_bound::epoch>( achievement::time_bound::epoch data )
{
switch( data ) {
// *INDENT-OFF*
case achievement::time_bound::epoch::cataclysm: return "cataclysm";
case achievement::time_bound::epoch::game_start: return "game_start";
// *INDENT-ON*
case achievement::time_bound::epoch::last:
break;
}
cata_fatal( "Invalid epoch" );
}
template<>
std::string enum_to_string<requirement_visibility>( requirement_visibility data )
{
switch( data ) {
// *INDENT-OFF*
case requirement_visibility::always: return "always";
case requirement_visibility::when_requirement_completed: return "when_requirement_completed";
case requirement_visibility::when_achievement_completed: return "when_achievement_completed";
case requirement_visibility::never: return "never";
// *INDENT-ON*
case requirement_visibility::last:
break;
}
cata_fatal( "Invalid requirement_visibility" );
}
} // namespace io
static nc_color color_from_completion( bool is_conduct, achievement_completion comp, bool is_title )
{
switch( comp ) {
case achievement_completion::pending:
return is_conduct ? c_light_green : ( is_title ? c_white : c_light_cyan );
case achievement_completion::completed:
return c_light_green;
case achievement_completion::failed:
return c_red;
case achievement_completion::last:
break;
}
cata_fatal( "Invalid achievement_completion" );
}
struct achievement_requirement {
string_id<event_statistic> statistic;
achievement_comparison comparison;
cata_variant target;
requirement_visibility visibility = requirement_visibility::always;
cata::optional<translation> description;
bool becomes_false = false; // NOLINT(cata-serialize)
void deserialize( const JsonObject &jo ) {
if( !( jo.read( "event_statistic", statistic ) &&
jo.read( "is", comparison ) &&
( comparison == achievement_comparison::anything ||
jo.read( "target", target ) ) ) ) {
jo.throw_error( "Mandatory field missing for achievement requirement" );
}
jo.read( "visible", visibility, false );
jo.read( "description", description, false );
}
void finalize() {
switch( comparison ) {
case achievement_comparison::equal:
becomes_false = statistic->monotonicity() == monotonically::constant;
return;
case achievement_comparison::less_equal:
becomes_false = is_increasing( statistic->monotonicity() );
return;
case achievement_comparison::greater_equal:
becomes_false = is_decreasing( statistic->monotonicity() );
return;
case achievement_comparison::anything:
becomes_false = true;
return;
case achievement_comparison::last:
break;
}
cata_fatal( "Invalid achievement_comparison" );
}
void check( const achievement_id &id ) const {
if( !statistic.is_valid() ) {
debugmsg( "Achievement %s refers to invalid statistic %s", id.str(), statistic.str() );
}
if( target.type() != cata_variant_type::int_ && !description &&
visibility != requirement_visibility::never &&
comparison != achievement_comparison::anything ) {
debugmsg( "Achievement %s has a non-integer requirement which is sometimes visible. "
"Such requirements must have a description, but this one does not.",
id.str() );
}
if( !target.is_valid() ) {
debugmsg( "Achievement %s has a requirement target %s of type %s, but that is not "
"a valid value of that type.",
id.str(), target.get_string(), io::enum_to_string( target.type() ) );
}
if( comparison != achievement_comparison::anything &&
target.type() != statistic->type() ) {
debugmsg( "Achievement %s has a requirement comparing a value of type %s to a value "
"of type %s. Comparisons should be between values of the same type.",
id.str(), io::enum_to_string( target.type() ),
io::enum_to_string( statistic->type() ) );
}
}
bool satisfied_by( const cata_variant &v ) const {
switch( comparison ) {
case achievement_comparison::equal:
return v == target;
case achievement_comparison::less_equal:
return v.get<int>() <= target.get<int>();
case achievement_comparison::greater_equal:
return v.get<int>() >= target.get<int>();
case achievement_comparison::anything:
return true;
case achievement_comparison::last:
break;
}
cata_fatal( "Invalid achievement_requirement comparison value" );
}
bool is_visible( achievement_completion ach_completed, bool req_completed ) const {
switch( visibility ) {
case requirement_visibility::always:
return true;
case requirement_visibility::when_requirement_completed:
return req_completed;
case requirement_visibility::when_achievement_completed:
return ach_completed == achievement_completion::completed;
case requirement_visibility::never:
return false;
case requirement_visibility::last:
break;
}
cata_fatal( "Invalid requirement_visibility value" );
}
};
static time_point epoch_to_time_point( achievement::time_bound::epoch e )
{
switch( e ) {
case achievement::time_bound::epoch::cataclysm:
return calendar::start_of_cataclysm;
case achievement::time_bound::epoch::game_start:
return calendar::start_of_game;
case achievement::time_bound::epoch::last:
break;
}
cata_fatal( "Invalid epoch" );
}
void achievement::time_bound::deserialize( const JsonObject &jo )
{
if( !( jo.read( "since", epoch_ ) &&
jo.read( "is", comparison_ ) &&
jo.read( "target", period_ ) ) ) {
jo.throw_error( "Mandatory field missing for achievement time_constraint" );
}
}
void achievement::time_bound::check( const achievement_id &id ) const
{
if( comparison_ == achievement_comparison::anything ) {
debugmsg( "Achievement %s has unconstrained \"anything\" time_constraint. "
"Please change it to a consequential comparison.", id.str() );
}
}
time_point achievement::time_bound::target() const
{
return epoch_to_time_point( epoch_ ) + period_;
}
achievement_completion achievement::time_bound::completed() const
{
time_point now = calendar::turn;
switch( comparison_ ) {
case achievement_comparison::equal:
if( now == target() ) {
return achievement_completion::completed;
} else if( now > target() ) {
return achievement_completion::failed;
} else {
return achievement_completion::pending;
}
case achievement_comparison::less_equal:
if( now <= target() ) {
return achievement_completion::completed;
} else {
return achievement_completion::failed;
}
case achievement_comparison::greater_equal:
if( now >= target() ) {
return achievement_completion::completed;
} else {
return achievement_completion::pending;
}
case achievement_comparison::anything:
return achievement_completion::completed;
case achievement_comparison::last:
break;
}
cata_fatal( "Invalid achievement_comparison" );
}
bool achievement::time_bound::becomes_false() const
{
switch( comparison_ ) {
case achievement_comparison::equal:
return false;
case achievement_comparison::less_equal:
return true;
case achievement_comparison::greater_equal:
return false;
case achievement_comparison::anything:
return true;
case achievement_comparison::last:
break;
}
cata_fatal( "Invalid achievement_comparison" );
}
std::string achievement::time_bound::ui_text( bool is_conduct ) const
{
time_point now = calendar::turn;
achievement_completion comp = completed();
nc_color c = color_from_completion( is_conduct, comp, false );
auto translate_epoch = []( epoch e ) {
switch( e ) {
case epoch::cataclysm:
return _( "time of cataclysm" );
case epoch::game_start:
return _( "start of game" );
case epoch::last:
break;
}
cata_fatal( "Invalid epoch" );
};
std::string message = [&]() {
switch( comp ) {
case achievement_completion::pending:
return string_format( _( "At least %s from %s (%s remaining)" ),
to_string( period_ ), translate_epoch( epoch_ ),
to_string( target() - now ) );
case achievement_completion::completed:
switch( comparison_ ) {
case achievement_comparison::equal:
return string_format( _( "Exactly %s from %s" ),
to_string( period_ ), translate_epoch( epoch_ ) );
case achievement_comparison::less_equal:
return string_format( _( "Within %s of %s (%s remaining)" ),
to_string( period_ ), translate_epoch( epoch_ ),
to_string( target() - now ) );
case achievement_comparison::greater_equal:
return string_format( _( "At least %s from %s (passed)" ),
to_string( period_ ), translate_epoch( epoch_ ) );
case achievement_comparison::anything:
return std::string();
case achievement_comparison::last:
break;
}
cata_fatal( "Invalid achievement_comparison" );
case achievement_completion::failed:
return string_format( _( "Within %s of %s (passed)" ),
to_string( period_ ), translate_epoch( epoch_ ) );
case achievement_completion::last:
break;
}
cata_fatal( "Invalid achievement_completion" );
}
();
return colorize( message, c );
}
void achievement::load_achievement( const JsonObject &jo, const std::string &src )
{
achievement_factory.load( jo, src );
}
void achievement::finalize()
{
for( achievement &a : const_cast<std::vector<achievement>&>( achievement::get_all() ) ) {
for( achievement_requirement &req : a.requirements_ ) {
req.finalize();
}
}
}
void achievement::check_consistency()
{
achievement_factory.check();
}
const std::vector<achievement> &achievement::get_all()
{
return achievement_factory.get_all();
}
void achievement::reset()
{
achievement_factory.reset();
}
void achievement::load( const JsonObject &jo, const std::string & )
{
mandatory( jo, was_loaded, "name", name_ );
is_conduct_ = jo.get_string( "type" ) == "conduct";
optional( jo, was_loaded, "description", description_ );
optional( jo, was_loaded, "hidden_by", hidden_by_ );
optional( jo, was_loaded, "time_constraint", time_constraint_ );
mandatory( jo, was_loaded, "requirements", requirements_ );
}
void achievement::check() const
{
for( const achievement_id &a : hidden_by_ ) {
if( !a.is_valid() ) {
debugmsg( "Achievement %s specifies hidden_by achievement %s, but the latter does not "
"exist.", id.str(), a.str() );
continue;
}
if( is_conduct() != a->is_conduct() ) {
debugmsg( "Achievement %s is hidden by achievement %s, but one is a conduct and the "
"other is not. This is not supported.", id.str(), a.str() );
}
}
bool all_requirements_become_false = true;
if( time_constraint_ ) {
time_constraint_->check( id );
all_requirements_become_false = time_constraint_->becomes_false();
}
for( const achievement_requirement &req : requirements_ ) {
req.check( id );
if( !req.becomes_false ) {
all_requirements_become_false = false;
}
}
// handled with debug mode
if( id == achievement_achievement_arcade_mode ) {
all_requirements_become_false = false;
}
if( all_requirements_become_false && !is_conduct() ) {
debugmsg( "All requirements for achievement %s become false, so this achievement will be "
"impossible or trivial", id.str() );
}
if( !all_requirements_become_false && is_conduct() ) {
debugmsg( "All requirements for conduct %s must become false, but at least one does not",
id.str() );
}
}
static cata::optional<std::string> text_for_requirement(
const achievement_requirement &req,
const cata_variant ¤t_value,
achievement_completion ach_completed )
{
bool is_satisfied = req.satisfied_by( current_value );
if( !req.is_visible( ach_completed, is_satisfied ) ) {
return cata::nullopt;
}
nc_color c = is_satisfied ? c_green : c_yellow;
std::string result;
if( req.description ) {
result = req.description->translated();
} else if( req.comparison == achievement_comparison::anything ) {
result = string_format( _( "Triggered by %s" ), req.statistic->description().translated() );
} else if( current_value.type() == cata_variant_type::int_ ) {
int current = current_value.get<int>();
int target = req.target.get<int>();
result = string_format( _( "%s/%s %s" ), current, target,
req.statistic->description().translated( target ) );
} else {
// The tricky part here is formatting an arbitrary cata_variant value.
// It might be possible, but for now we punt the problem to content
// developers.
result = "ERROR: Non-integer requirements must provide a custom description";
}
return colorize( result, c );
}
static std::string format_requirements( const std::vector<cata::optional<std::string>> &req_texts,
nc_color c )
{
bool some_missing = false;
std::string result;
for( const cata::optional<std::string> &req_text : req_texts ) {
if( req_text ) {
result += " " + *req_text + "\n";
} else {
some_missing = true;
}
}
if( some_missing && !result.empty() ) {
result += colorize( _( " (further requirements hidden)" ), c );
}
return result;
}
class requirement_watcher : stat_watcher
{
public:
requirement_watcher( achievement_tracker &tracker, const achievement_requirement &req,
stats_tracker &stats ) :
tracker_( &tracker ),
requirement_( &req ),
current_value_( stats.add_watcher( req.statistic, this ) ) {
}
const cata_variant ¤t_value() const {
return current_value_;
}
const achievement_requirement &requirement() const {
return *requirement_;
}
void new_value( const cata_variant &new_value, stats_tracker & ) override;
bool is_satisfied( stats_tracker &/*stats*/ ) {
return requirement_->satisfied_by( current_value_ );
}
cata::optional<std::string> ui_text() const {
return text_for_requirement( *requirement_, current_value_,
achievement_completion::pending );
}
private:
achievement_tracker *tracker_;
const achievement_requirement *requirement_;
cata_variant current_value_;
};
void requirement_watcher::new_value( const cata_variant &new_value, stats_tracker & )
{
if( !tracker_->time_is_expired() ) {
current_value_ = new_value;
}
// set_requirement can result in this being deleted, so it must be the last
// thing in this function
tracker_->set_requirement( this, requirement_->satisfied_by( current_value_ ) );
}
namespace io
{
template<>
std::string enum_to_string<achievement_completion>( achievement_completion data )
{
switch( data ) {
// *INDENT-OFF*
case achievement_completion::pending: return "pending";
case achievement_completion::completed: return "completed";
case achievement_completion::failed: return "failed";
// *INDENT-ON*
case achievement_completion::last:
break;
}
cata_fatal( "Invalid achievement_completion" );
}
} // namespace io
std::string achievement_state::ui_text( const achievement *ach ) const
{
// First: the achievement name and description
nc_color c = color_from_completion( ach->is_conduct(), completion, false );
nc_color c_title = color_from_completion( ach->is_conduct(), completion, true );
std::string result = colorize( ach->name(), c_title ) + "\n";
if( !ach->description().empty() ) {
result += " " + colorize( ach->description(), c ) + "\n";
}
if( completion == achievement_completion::completed ) {
std::string message = string_format(
_( "Completed %s" ), to_string( last_state_change ) );
result += " " + colorize( message, c ) + "\n";
} else if( completion == achievement_completion::failed ) {
std::string message = string_format(
_( "Failed %s" ), to_string( last_state_change ) );
result += " " + colorize( message, c ) + "\n";
} else {
// Next: the time constraint
if( ach->time_constraint() ) {
result += " " + ach->time_constraint()->ui_text( ach->is_conduct() ) + "\n";
}
}
// Next: the requirements
const std::vector<achievement_requirement> &reqs = ach->requirements();
// If these two vectors are of different sizes then the definition must
// have changed since it was completed / failed, so we don't print any
// requirements info.
std::vector<cata::optional<std::string>> req_texts;
if( final_values.size() == reqs.size() ) {
for( size_t i = 0; i < final_values.size(); ++i ) {
req_texts.push_back( text_for_requirement( reqs[i], final_values[i], completion ) );
}
result += format_requirements( req_texts, c );
}
return result;
}
void achievement_state::serialize( JsonOut &jsout ) const
{
jsout.start_object();
jsout.member_as_string( "completion", completion );
jsout.member( "last_state_change", last_state_change );
jsout.member( "final_values", final_values );
jsout.end_object();
}
void achievement_state::deserialize( const JsonObject &jo )
{
jo.read( "completion", completion );
jo.read( "last_state_change", last_state_change );
jo.read( "final_values", final_values );
}
achievement_tracker::achievement_tracker( const achievement &a, achievements_tracker &tracker,
stats_tracker &stats ) :
achievement_( &a ),
tracker_( &tracker )
{
for( const achievement_requirement &req : a.requirements() ) {
watchers_.push_back( std::make_unique<requirement_watcher>( *this, req, stats ) );
}
for( const std::unique_ptr<requirement_watcher> &watcher : watchers_ ) {
bool is_satisfied = watcher->is_satisfied( stats );
sorted_watchers_[is_satisfied].insert( watcher.get() );
}
}
void achievement_tracker::set_requirement( requirement_watcher *watcher, bool is_satisfied )
{
if( sorted_watchers_[is_satisfied].insert( watcher ).second ) {
// Remove from other
sorted_watchers_[!is_satisfied].erase( watcher );
cata_assert( sorted_watchers_[0].size() + sorted_watchers_[1].size() == watchers_.size() );
}
achievement_completion time_comp =
achievement_->time_constraint() ?
achievement_->time_constraint()->completed() : achievement_completion::completed;
if( sorted_watchers_[false].empty() && time_comp == achievement_completion::completed &&
!achievement_->is_conduct() ) {
// report_achievement can result in this being deleted, so it must be
// the last thing in the function
tracker_->report_achievement( achievement_, achievement_completion::completed );
return;
}
if( time_comp == achievement_completion::failed ||
( !is_satisfied && watcher->requirement().becomes_false ) ) {
// report_achievement can result in this being deleted, so it must be
// the last thing in the function
tracker_->report_achievement( achievement_, achievement_completion::failed );
}
}
bool achievement_tracker::time_is_expired() const
{
return achievement_->time_constraint() &&
achievement_->time_constraint()->completed() == achievement_completion::failed;
}
std::vector<cata_variant> achievement_tracker::current_values() const
{
std::vector<cata_variant> result;
result.reserve( watchers_.size() );
for( const std::unique_ptr<requirement_watcher> &watcher : watchers_ ) {
result.push_back( watcher->current_value() );
}
return result;
}
std::string achievement_tracker::ui_text() const
{
// Determine overall achievement status
if( time_is_expired() ) {
return achievement_state{
achievement_completion::failed,
achievement_->time_constraint()->target(),
current_values()
} .ui_text( achievement_ );
}
// First: the achievement name and description
nc_color c = color_from_completion( achievement_->is_conduct(),
achievement_completion::pending,
false );
nc_color c_title = color_from_completion( achievement_->is_conduct(),
achievement_completion::pending,
true );
std::string result = colorize( achievement_->name(), c_title ) + "\n";
if( !achievement_->description().empty() ) {
result += " " + colorize( achievement_->description(), c ) + "\n";
}
// Note if it's been completed in other games
const achievement_completion_info *other_games =
get_past_games().achievement( achievement_->id );
if( other_games && !other_games->games_completed.empty() ) {
std::string message =
string_format( _( "Previously completed by %s" ),
other_games->games_completed.front()->avatar_name() );
size_t num_completions = other_games->games_completed.size();
if( num_completions > 1 ) {
message +=
string_format(
n_gettext( " and %d other", " and %d others", num_completions - 1 ),
num_completions - 1 );
}
result += " " + colorize( message, c_blue ) + "\n";
}
// Next: the time constraint
if( achievement_->time_constraint() ) {
result += " " + achievement_->time_constraint()->ui_text( achievement_->is_conduct() ) +
"\n";
}
// Next: the requirements
std::vector<cata::optional<std::string>> req_texts;
for( const std::unique_ptr<requirement_watcher> &watcher : watchers_ ) {
req_texts.push_back( watcher->ui_text() );
}
result += format_requirements( req_texts, c );
return result;
}
achievements_tracker::achievements_tracker(
stats_tracker &stats,
const std::function<void( const achievement *, bool )> &achievement_attained_callback,
const std::function<void( const achievement *, bool )> &achievement_failed_callback,
bool active ) :
stats_( &stats ),
active_( active ),
achievement_attained_callback_( achievement_attained_callback ),
achievement_failed_callback_( achievement_failed_callback )
{}
achievements_tracker::~achievements_tracker() = default;
std::vector<const achievement *> achievements_tracker::valid_achievements() const
{
std::vector<const achievement *> result;
for( const achievement &ach : achievement::get_all() ) {
if( initial_achievements_.count( ach.id ) ) {
result.push_back( &ach );
}
}
return result;
}
void achievements_tracker::report_achievement( const achievement *a, achievement_completion comp )
{
cata_assert( comp != achievement_completion::pending );
cata_assert( !achievements_status_.count( a->id ) );
auto tracker_it = trackers_.find( a->id );
achievements_status_.emplace(
a->id,
achievement_state{
comp,
calendar::turn,
tracker_it->second.current_values()
}
);
if( comp == achievement_completion::completed ) {
achievement_attained_callback_( a, is_enabled() );
} else if( comp == achievement_completion::failed ) {
achievement_failed_callback_( a, is_enabled() );
}
trackers_.erase( tracker_it );
}
achievement_completion achievements_tracker::is_completed( const achievement_id &id ) const
{
auto it = achievements_status_.find( id );
if( it == achievements_status_.end() ) {
// It might still have failed; check for time expiry
auto tracker_it = trackers_.find( id );
if( tracker_it != trackers_.end() && tracker_it->second.time_is_expired() ) {
return achievement_completion::failed;
}
return achievement_completion::pending;
}
return it->second.completion;
}
bool achievements_tracker::is_hidden( const achievement *ach ) const
{
// hard code for arcade mode achievement
if( ach->id == achievement_achievement_arcade_mode ) {
return true;
}
achievement_completion end_state =
ach->is_conduct() ? achievement_completion::failed : achievement_completion::completed;
if( is_completed( ach->id ) == end_state ) {
return false;
}
for( const achievement_id &hidden_by : ach->hidden_by() ) {
if( is_completed( hidden_by ) != end_state ) {
return true;
}
}
return false;
}
std::string achievements_tracker::ui_text_for( const achievement *ach ) const
{
auto state_it = achievements_status_.find( ach->id );
if( state_it != achievements_status_.end() ) {
return state_it->second.ui_text( ach );
}
auto tracker_it = trackers_.find( ach->id );
if( tracker_it == trackers_.end() ) {
return colorize( ach->description() + _( "\nInternal error: achievement lacks watcher." ),
c_red );
}
return tracker_it->second.ui_text();
}
void achievements_tracker::clear()
{
enabled_ = true;
trackers_.clear();
initial_achievements_.clear();
achievements_status_.clear();
}
void achievements_tracker::notify( const cata::event &e )
{
if( e.type() == event_type::game_start ) {
cata_assert( initial_achievements_.empty() );
for( const achievement &ach : achievement::get_all() ) {
initial_achievements_.insert( ach.id );
}
init_watchers();
}
}
void achievements_tracker::serialize( JsonOut &jsout ) const
{
jsout.start_object();
jsout.member( "enabled", enabled_ );
jsout.member( "initial_achievements", initial_achievements_ );
jsout.member( "achievements_status", achievements_status_ );
jsout.end_object();
}
void achievements_tracker::deserialize( const JsonObject &jo )
{
if( !jo.read( "enabled", enabled_ ) ) {
enabled_ = true;
}
jo.read( "initial_achievements", initial_achievements_ );
jo.read( "achievements_status", achievements_status_ );
init_watchers();
}
void achievements_tracker::init_watchers()
{
if( !active_ ) {
return;
}
for( const achievement *a : valid_achievements() ) {
if( achievements_status_.count( a->id ) ) {
continue;
}
trackers_.emplace(
std::piecewise_construct, std::forward_as_tuple( a->id ),
std::forward_as_tuple( *a, *this, *stats_ ) );
}
}