-
Notifications
You must be signed in to change notification settings - Fork 288
/
Copy pathachievement.cpp
376 lines (327 loc) · 11.8 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
#include "achievement.h"
#include "avatar.h"
#include "event_statistics.h"
#include "generic_factory.h"
#include "stats_tracker.h"
// 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_traecker 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
enum class achievement_comparison {
greater_equal,
last,
};
namespace io
{
template<>
std::string enum_to_string<achievement_comparison>( achievement_comparison data )
{
switch( data ) {
// *INDENT-OFF*
case achievement_comparison::greater_equal: return ">=";
// *INDENT-ON*
case achievement_comparison::last:
break;
}
debugmsg( "Invalid achievement_comparison" );
abort();
}
} // namespace io
template<>
struct enum_traits<achievement_comparison> {
static constexpr achievement_comparison last = achievement_comparison::last;
};
struct achievement_requirement {
string_id<event_statistic> statistic;
achievement_comparison comparison;
int target;
void deserialize( JsonIn &jin ) {
const JsonObject &jo = jin.get_object();
if( !( jo.read( "event_statistic", statistic ) &&
jo.read( "is", comparison ) &&
jo.read( "target", target ) ) ) {
jo.throw_error( "Mandatory field missing for achievement requirement" );
}
}
void check( const string_id<achievement> &id ) const {
if( !statistic.is_valid() ) {
debugmsg( "score %s refers to invalid statistic %s", id.str(), statistic.str() );
}
}
bool satisifed_by( const cata_variant &v ) const {
int value = v.get<int>();
switch( comparison ) {
case achievement_comparison::greater_equal:
return value >= target;
case achievement_comparison::last:
break;
}
debugmsg( "Invalid achievement_requirement comparison value" );
abort();
}
};
void achievement::load_achievement( const JsonObject &jo, const std::string &src )
{
achievement_factory.load( jo, src );
}
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, "description", description_ );
mandatory( jo, was_loaded, "requirements", requirements_ );
}
void achievement::check() const
{
for( const achievement_requirement &req : requirements_ ) {
req.check( id );
}
}
class requirement_watcher : stat_watcher
{
public:
requirement_watcher( achievement_tracker &tracker, const achievement_requirement &req,
stats_tracker &stats ) :
current_value_( req.statistic->value( stats ) ),
tracker_( &tracker ),
requirement_( &req ) {
stats.add_watcher( req.statistic, this );
}
void new_value( const cata_variant &new_value, stats_tracker & ) override;
bool is_satisfied( stats_tracker &stats ) {
return requirement_->satisifed_by( requirement_->statistic->value( stats ) );
}
std::string ui_text() const {
bool is_satisfied = requirement_->satisifed_by( current_value_ );
nc_color c = is_satisfied ? c_green : c_yellow;
int current = current_value_.get<int>();
std::string result = string_format( _( "%s/%s " ), current, requirement_->target );
result += requirement_->statistic->description();
return colorize( result, c );
}
private:
cata_variant current_value_;
achievement_tracker *tracker_;
const achievement_requirement *requirement_;
};
void requirement_watcher::new_value( const cata_variant &new_value, stats_tracker & )
{
current_value_ = new_value;
tracker_->set_requirement( this, requirement_->satisifed_by( new_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";
// *INDENT-ON*
case achievement_completion::last:
break;
}
debugmsg( "Invalid achievement_completion" );
abort();
}
} // namespace io
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.end_object();
}
void achievement_state::deserialize( JsonIn &jsin )
{
JsonObject jo = jsin.get_object();
jo.read( "completion", completion );
jo.read( "last_state_change", last_state_change );
}
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 ) {
// No change
return;
}
// Remove from other; check for completion.
sorted_watchers_[!is_satisfied].erase( watcher );
assert( sorted_watchers_[0].size() + sorted_watchers_[1].size() == watchers_.size() );
if( sorted_watchers_[false].empty() ) {
tracker_->report_achievement( achievement_, achievement_completion::completed );
}
}
std::string achievement_tracker::ui_text( const achievement_state *state ) const
{
auto color_from_completion = []( achievement_completion comp ) {
switch( comp ) {
case achievement_completion::pending:
return c_yellow;
case achievement_completion::completed:
return c_light_green;
case achievement_completion::last:
break;
}
debugmsg( "Invalid achievement_completion" );
abort();
};
achievement_completion comp = state ? state->completion : achievement_completion::pending;
nc_color c = color_from_completion( comp );
std::string result = colorize( achievement_->description(), c ) + "\n";
for( const std::unique_ptr<requirement_watcher> &watcher : watchers_ ) {
result += " " + watcher->ui_text() + "\n";
}
return result;
}
achievements_tracker::achievements_tracker(
stats_tracker &stats,
const std::function<void( const achievement * )> &achievement_attained_callback ) :
stats_( &stats ),
achievement_attained_callback_( achievement_attained_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 )
{
auto it = achievements_status_.find( a->id );
achievement_completion existing_comp =
( it == achievements_status_.end() ) ? achievement_completion::pending
: it->second.completion;
if( existing_comp == comp ) {
return;
}
achievement_state new_state{
comp,
calendar::turn
};
if( it == achievements_status_.end() ) {
achievements_status_.emplace( a->id, new_state );
} else {
it->second = new_state;
}
if( comp == achievement_completion::completed ) {
achievement_attained_callback_( a );
}
}
achievement_completion achievements_tracker::is_completed( const string_id<achievement> &id ) const
{
auto it = achievements_status_.find( id );
if( it == achievements_status_.end() ) {
return achievement_completion::pending;
}
return it->second.completion;
}
std::string achievements_tracker::ui_text_for( const achievement *ach ) const
{
auto state_it = achievements_status_.find( ach->id );
const achievement_state *state = nullptr;
if( state_it != achievements_status_.end() ) {
state = &state_it->second;
}
auto watcher_it = watchers_.find( ach->id );
if( watcher_it == watchers_.end() ) {
return colorize( ach->description() + _( "\nInternal error: achievement lacks watcher." ),
c_red );
}
return watcher_it->second.ui_text( state );
}
void achievements_tracker::clear()
{
watchers_.clear();
initial_achievements_.clear();
achievements_status_.clear();
}
void achievements_tracker::notify( const cata::event &e )
{
if( e.type() == event_type::game_start ) {
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( "initial_achievements", initial_achievements_ );
jsout.member( "achievements_status", achievements_status_ );
jsout.end_object();
}
void achievements_tracker::deserialize( JsonIn &jsin )
{
JsonObject jo = jsin.get_object();
jo.read( "initial_achievements", initial_achievements_ );
jo.read( "achievements_status", achievements_status_ );
init_watchers();
}
void achievements_tracker::init_watchers()
{
for( const achievement *a : valid_achievements() ) {
watchers_.emplace(
std::piecewise_construct, std::forward_as_tuple( a->id ),
std::forward_as_tuple( *a, *this, *stats_ ) );
}
}