File: reading_test.cpp

package info (click to toggle)
cataclysm-dda 0.H-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 710,808 kB
  • sloc: cpp: 524,019; python: 11,580; sh: 1,228; makefile: 1,169; xml: 507; javascript: 150; sql: 56; exp: 41; perl: 37
file content (522 lines) | stat: -rw-r--r-- 19,311 bytes parent folder | download
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
#include <iosfwd>
#include <list>
#include <memory>
#include <string>
#include <vector>

#include "avatar.h"
#include "activity_actor_definitions.h"
#include "activity_actor_definitions.h"
#include "calendar.h"
#include "cata_catch.h"
#include "character.h"
#include "item.h"
#include "itype.h"
#include "map_helpers.h"
#include "morale_types.h"
#include "player_helpers.h"
#include "skill.h"
#include "type_id.h"
#include "value_ptr.h"

static const activity_id ACT_READ( "ACT_READ" );

static const efftype_id effect_darkness( "darkness" );

static const flag_id json_flag_INSPIRATIONAL( "INSPIRATIONAL" );

static const limb_score_id limb_score_vision( "vision" );

static const skill_id skill_chemistry( "chemistry" );

static const trait_id trait_HATES_BOOKS( "HATES_BOOKS" );
static const trait_id trait_HYPEROPIC( "HYPEROPIC" );
static const trait_id trait_ILLITERATE( "ILLITERATE" );
static const trait_id trait_LOVES_BOOKS( "LOVES_BOOKS" );
static const trait_id trait_SPIRITUAL( "SPIRITUAL" );

TEST_CASE( "clearing_identified_books", "[reading][book][identify][clear]" )
{
    item book( "child_book" );
    SECTION( "using local avatar" ) {
        avatar dummy;
        dummy.identify( book );
        dummy.clear_identified();
        REQUIRE_FALSE( dummy.has_identified( book.typeId() ) );
    }
    SECTION( "test helper clear_avatar() also clears items identified" ) {
        avatar &dummy = get_avatar();
        dummy.identify( book );
        clear_avatar();
        REQUIRE_FALSE( dummy.has_identified( book.typeId() ) );
    }
}

TEST_CASE( "identifying_unread_books", "[reading][book][identify]" )
{
    clear_avatar();
    Character &dummy = get_avatar();
    dummy.worn.wear_item( dummy, item( "backpack" ), false, false );

    GIVEN( "character has some unidentified books" ) {
        item_location book1 = dummy.i_add( item( "novel_western" ) );
        item_location book2 = dummy.i_add( item( "mag_throwing" ) );

        REQUIRE_FALSE( dummy.has_identified( book1->typeId() ) );
        REQUIRE_FALSE( dummy.has_identified( book2->typeId() ) );

        WHEN( "they read the books for the first time" ) {
            dummy.identify( *book1 );
            dummy.identify( *book2 );

            THEN( "the books should be identified" ) {
                CHECK( dummy.has_identified( book1->typeId() ) );
                CHECK( dummy.has_identified( book2->typeId() ) );
            }
        }
    }
}

TEST_CASE( "reading_a_book_for_fun", "[reading][book][fun]" )
{
    clear_avatar();
    Character &dummy = get_avatar();
    dummy.set_body();
    dummy.worn.wear_item( dummy, item( "backpack" ), false, false );

    GIVEN( "a fun book" ) {
        item_location book = dummy.i_add( item( "novel_western" ) );
        REQUIRE( book->type->book );
        REQUIRE( book->type->book->fun > 0 );
        int book_fun = book->type->book->fun;

        WHEN( "character neither loves nor hates books" ) {
            REQUIRE_FALSE( dummy.has_trait( trait_LOVES_BOOKS ) );
            REQUIRE_FALSE( dummy.has_trait( trait_HATES_BOOKS ) );

            THEN( "the book is a normal amount of fun" ) {
                CHECK( dummy.fun_to_read( *book ) );
                CHECK( dummy.book_fun_for( *book, dummy ) == book_fun );
            }
        }

        WHEN( "character loves books" ) {
            dummy.toggle_trait( trait_LOVES_BOOKS );
            REQUIRE( dummy.has_trait( trait_LOVES_BOOKS ) );

            THEN( "the book is extra fun" ) {
                CHECK( dummy.fun_to_read( *book ) );
                CHECK( dummy.book_fun_for( *book, dummy ) == book_fun + 1 );
            }
        }

        WHEN( "character hates books" ) {
            dummy.toggle_trait( trait_HATES_BOOKS );
            REQUIRE( dummy.has_trait( trait_HATES_BOOKS ) );

            THEN( "the book is no fun at all" ) {
                CHECK_FALSE( dummy.fun_to_read( *book ) );
                CHECK( dummy.book_fun_for( *book, dummy ) == 0 );
            }
        }
    }

    GIVEN( "a fun book that is also inspirational" ) {
        item_location book = dummy.i_add( item( "holybook_pastafarian" ) );
        REQUIRE( book->has_flag( json_flag_INSPIRATIONAL ) );
        REQUIRE( book->type->book );
        REQUIRE( book->type->book->fun > 0 );
        int book_fun = book->type->book->fun;

        WHEN( "character is not spiritual" ) {
            REQUIRE_FALSE( dummy.has_trait( trait_SPIRITUAL ) );

            THEN( "the book is a normal amount of fun" ) {
                CHECK( dummy.fun_to_read( *book ) );
                CHECK( dummy.book_fun_for( *book, dummy ) == book_fun );
            }
        }

        WHEN( "character is spiritual" ) {
            dummy.toggle_trait( trait_SPIRITUAL );
            REQUIRE( dummy.has_trait( trait_SPIRITUAL ) );

            THEN( "the book is thrice the fun" ) {
                CHECK( dummy.fun_to_read( *book ) );
                CHECK( dummy.book_fun_for( *book, dummy ) == book_fun * 3 );
            }
        }
    }
}

TEST_CASE( "character_reading_speed", "[reading][character][speed]" )
{
    clear_avatar();
    Character &dummy = get_avatar();
    dummy.worn.wear_item( dummy, item( "backpack" ), false, false );

    // Note: read_speed() returns number of moves;
    // 6000 == 60 seconds

    WHEN( "character has average intelligence" ) {
        REQUIRE( dummy.get_int() == 8 );

        THEN( "reading speed is normal" ) {
            CHECK( dummy.read_speed() * 60 == 6000 );
        }
    }

    WHEN( "character has below-average intelligence" ) {

        THEN( "reading speed gets slower as intelligence decreases" ) {
            dummy.int_max = 7;
            CHECK( dummy.read_speed() * 60 == 6180 );
            dummy.int_max = 6;
            CHECK( dummy.read_speed() * 60 == 6480 );
            dummy.int_max = 5;
            CHECK( dummy.read_speed() * 60 == 6780 );
            dummy.int_max = 4;
            CHECK( dummy.read_speed() * 60 == 7200 );
        }
    }

    WHEN( "character has above-average intelligence" ) {

        THEN( "reading speed gets faster as intelligence increases" ) {
            dummy.int_max = 9;
            CHECK( dummy.read_speed() * 60 == 5700 );
            dummy.int_max = 10;
            CHECK( dummy.read_speed() * 60 == 5460 );
            dummy.int_max = 12;
            CHECK( dummy.read_speed() * 60 == 5100 );
            dummy.int_max = 14;
            CHECK( dummy.read_speed() * 60 == 4800 );
        }
    }
}

TEST_CASE( "estimated_reading_time_for_a_book", "[reading][book][time]" )
{
    avatar dummy;
    //Give eyes to our dummy
    dummy.set_body();
    REQUIRE( dummy.has_part( bodypart_id( "eyes" ) ) );
    REQUIRE( dummy.get_limb_score( limb_score_vision ) != 0 );

    dummy.worn.wear_item( dummy, item( "backpack" ), false, false );

    // Easy, medium, and hard books
    item_location child = dummy.i_add( item( "child_book" ) );
    item_location western = dummy.i_add( item( "novel_western" ) );
    item_location alpha = dummy.i_add( item( "recipe_alpha" ) );

    // Ensure the books are actually books
    REQUIRE( child->type->book );
    REQUIRE( western->type->book );
    REQUIRE( alpha->type->book );

    // Convert time to read from minutes to moves, for easier comparison later
    time_duration moves_child = child->type->book->time;
    time_duration moves_western = western->type->book->time;
    time_duration moves_alpha = alpha->type->book->time;

    GIVEN( "some unidentified books and plenty of light" ) {
        REQUIRE_FALSE( dummy.has_identified( child->typeId() ) );
        REQUIRE_FALSE( dummy.has_identified( western->typeId() ) );

        // Get some light
        dummy.i_add( item( "atomic_lamp" ) );
        REQUIRE( dummy.fine_detail_vision_mod() == 1 );

        THEN( "identifying books takes 1/10th of the normal reading time" ) {
            CHECK( dummy.time_to_read( *western, dummy ) == moves_western / 10 );
            CHECK( dummy.time_to_read( *child, dummy ) == moves_child / 10 );
        }
    }

    GIVEN( "some identified books and plenty of light" ) {
        // Identify the books
        dummy.identify( *child );
        dummy.identify( *western );
        dummy.identify( *alpha );
        REQUIRE( dummy.has_identified( child->typeId() ) );
        REQUIRE( dummy.has_identified( western->typeId() ) );
        REQUIRE( dummy.has_identified( alpha->typeId() ) );

        // Get some light
        dummy.i_add( item( "atomic_lamp" ) );
        REQUIRE( dummy.fine_detail_vision_mod() == 1 );

        WHEN( "player has average intelligence" ) {
            dummy.int_max = 8;
            REQUIRE( dummy.get_int() == 8 );
            REQUIRE( dummy.read_speed() * 60 == 6000 ); // 60s, "normal"

            THEN( "they can read books at their reading level in the normal amount time" ) {
                CHECK( dummy.time_to_read( *child, dummy ) == moves_child );
                CHECK( dummy.time_to_read( *western, dummy ) == moves_western );
            }
            AND_THEN( "they can read books above their reading level, but it takes longer" ) {
                CHECK( dummy.time_to_read( *alpha, dummy ) > moves_alpha );
            }
        }

        WHEN( "player has below average intelligence" ) {
            dummy.int_max = 6;
            REQUIRE( dummy.get_int() == 6 );
            REQUIRE( dummy.read_speed() * 60 == 6480 ); // 65s

            THEN( "they take longer than average to read any book" ) {
                CHECK( dummy.time_to_read( *child, dummy ) > moves_child );
                CHECK( dummy.time_to_read( *western, dummy ) > moves_western );
                CHECK( dummy.time_to_read( *alpha, dummy ) > moves_alpha );
            }
        }

        WHEN( "player has above average intelligence" ) {
            dummy.int_max = 10;
            REQUIRE( dummy.get_int() == 10 );
            REQUIRE( dummy.read_speed() * 60 == 5460 ); // 55s

            THEN( "they take less time than average to read any book" ) {
                CHECK( dummy.time_to_read( *child, dummy ) < moves_child );
                CHECK( dummy.time_to_read( *western, dummy ) < moves_western );
                CHECK( dummy.time_to_read( *alpha, dummy ) < moves_alpha );
            }
        }
    }
}

TEST_CASE( "reasons_for_not_being_able_to_read", "[reading][reasons]" )
{
    clear_avatar();
    clear_map();
    Character &dummy = get_avatar();
    dummy.set_body();
    dummy.worn.wear_item( dummy, item( "backpack" ), false, false );
    set_time_to_day();
    std::vector<std::string> reasons;
    std::vector<std::string> expect_reasons;

    item_location child = dummy.i_add( item( "child_book" ) );
    item_location western = dummy.i_add( item( "novel_western" ) );
    item_location alpha = dummy.i_add( item( "recipe_alpha" ) );

    SECTION( "you cannot read what is not readable" ) {
        item_location rag = dummy.i_add( item( "rag" ) );
        REQUIRE_FALSE( rag->is_book() );

        CHECK( dummy.get_book_reader( *rag, reasons ) == nullptr );
        expect_reasons = { "Your rag is not good reading material." };
        CHECK( reasons == expect_reasons );
    }

    SECTION( "you cannot read in darkness" ) {
        if( GENERATE( true, false ) ) {
            dummy.add_env_effect( effect_darkness, bodypart_id( "eyes" ), 3, 1_hours );
            CAPTURE( "darkness effect" );
        } else {
            set_time( calendar::turn - time_past_midnight( calendar::turn ) );
            CAPTURE( "actual darkness" );
        }
        REQUIRE( dummy.fine_detail_vision_mod() > 4 );

        CHECK( dummy.get_book_reader( *child, reasons ) == nullptr );
        expect_reasons = { "It's too dark to read!" };
        CHECK( reasons == expect_reasons );
        set_time_to_day();
    }

    GIVEN( "some identified books and plenty of light" ) {
        // Identify the books
        dummy.identify( *child );
        dummy.identify( *western );
        dummy.identify( *alpha );

        // Get some light
        dummy.i_add( item( "atomic_lamp" ) );
        REQUIRE( dummy.fine_detail_vision_mod() == 1 );

        THEN( "you cannot read while illiterate" ) {
            dummy.toggle_trait( trait_ILLITERATE );
            REQUIRE( dummy.has_trait( trait_ILLITERATE ) );

            CHECK( dummy.get_book_reader( *western, reasons ) == nullptr );
            expect_reasons = { "You're illiterate!" };
            CHECK( reasons == expect_reasons );
        }

        THEN( "you cannot read while farsighted without reading glasses" ) {
            dummy.toggle_trait( trait_HYPEROPIC );
            REQUIRE( dummy.has_trait( trait_HYPEROPIC ) );

            CHECK( dummy.get_book_reader( *western, reasons ) == nullptr );
            expect_reasons = { "Your eyes won't focus without reading glasses." };
            CHECK( reasons == expect_reasons );
        }

        THEN( "you cannot read without enough skill to understand the book" ) {
            dummy.set_knowledge_level( skill_chemistry, 5 );

            CHECK( dummy.get_book_reader( *alpha, reasons ) == nullptr );
            expect_reasons = { "applied science 6 needed to understand.  You have 5" };
            CHECK( reasons == expect_reasons );
        }

        THEN( "you cannot read boring books when your morale is too low" ) {
            dummy.add_morale( MORALE_FEELING_BAD, -50, -100 );
            REQUIRE_FALSE( dummy.has_morale_to_read() );

            CHECK( dummy.get_book_reader( *alpha, reasons ) == nullptr );
            expect_reasons = { "What's the point of studying?  (Your morale is too low!)" };
            CHECK( reasons == expect_reasons );
        }

        WHEN( "there is nothing preventing you from reading" ) {
            REQUIRE_FALSE( dummy.has_trait( trait_ILLITERATE ) );
            REQUIRE_FALSE( dummy.has_trait( trait_HYPEROPIC ) );
            REQUIRE_FALSE( dummy.in_vehicle );
            REQUIRE( dummy.has_morale_to_read() );

            THEN( "you can read!" ) {
                CHECK( dummy.get_book_reader( *western, reasons ) != nullptr );
                expect_reasons = {};
                CHECK( reasons == expect_reasons );
            }
        }
    }
}

TEST_CASE( "determining_book_mastery", "[reading][book][mastery]" )
{
    static const auto book_has_skill = []( const item & book ) -> bool {
        REQUIRE( book.is_book() );
        return bool( book.type->book->skill );
    };

    avatar dummy;
    dummy.set_body();
    dummy.worn.wear_item( dummy, item( "backpack" ), false, false );

    item_location child = dummy.i_add( item( "child_book" ) );
    item_location alpha = dummy.i_add( item( "recipe_alpha" ) );

    SECTION( "you cannot determine mastery for non-book items" ) {
        item_location rag = dummy.i_add( item( "rag" ) );
        REQUIRE_FALSE( rag->is_book() );
        CHECK( dummy.get_book_mastery( *rag ) == book_mastery::CANT_DETERMINE );
    }
    SECTION( "you cannot determine mastery for unidentified books" ) {
        REQUIRE( alpha->is_book() );
        REQUIRE_FALSE( dummy.has_identified( alpha->typeId() ) );
        CHECK( dummy.get_book_mastery( *child ) == book_mastery::CANT_DETERMINE );
    }
    GIVEN( "some identified books" ) {
        dummy.identify( *child );
        dummy.identify( *alpha );
        REQUIRE( dummy.has_identified( child->typeId() ) );
        REQUIRE( dummy.has_identified( alpha->typeId() ) );

        WHEN( "it gives/requires no skill" ) {
            REQUIRE_FALSE( book_has_skill( *child ) );
            THEN( "you've already mastered it" ) {
                CHECK( dummy.get_book_mastery( *child ) == book_mastery::MASTERED );
            }
        }
        WHEN( "it gives/requires skills" ) {
            REQUIRE( book_has_skill( *alpha ) );

            THEN( "you won't understand it if your skills are too low" ) {
                dummy.set_knowledge_level( skill_chemistry, 5 );
                CHECK( dummy.get_book_mastery( *alpha ) == book_mastery::CANT_UNDERSTAND );
            }
            THEN( "you can learn from it with enough skill" ) {
                dummy.set_knowledge_level( skill_chemistry, 6 );
                CHECK( dummy.get_book_mastery( *alpha ) == book_mastery::LEARNING );
            }
            THEN( "you already mastered it if you have too much skill" ) {
                dummy.set_knowledge_level( skill_chemistry, 7 );
                CHECK( dummy.get_book_mastery( *alpha ) == book_mastery::MASTERED );
            }
        }
    }
}

TEST_CASE( "reading_a_book_for_skill", "[reading][book][skill]" )
{
    clear_avatar();
    Character &dummy = get_avatar();
    dummy.set_body();
    dummy.worn.wear_item( dummy, item( "backpack" ), false, false );

    item_location alpha = dummy.i_add( item( "recipe_alpha" ) );
    REQUIRE( alpha->is_book() );

    dummy.identify( *alpha );
    REQUIRE( dummy.has_identified( alpha->typeId() ) );

    GIVEN( "a book you can learn from" ) {
        dummy.set_knowledge_level( skill_chemistry, 6 );
        REQUIRE( dummy.get_book_mastery( *alpha ) == book_mastery::LEARNING );

        dummy.set_focus( 100 );
        WHEN( "reading the book 100 times" ) {
            const cata::value_ptr<islot_book> bkalpha_islot = alpha->type->book;
            SkillLevel &avatarskill = dummy.get_skill_level_object( bkalpha_islot->skill );

            for( int i = 0; i < 100; ++i ) {
                read_activity_actor::read_book( *dummy.as_character(), bkalpha_islot, avatarskill, 1.0 );
            }

            THEN( "gained a skill level" ) {
                CHECK( dummy.get_knowledge_level( skill_chemistry ) > 6 );
                CHECK( static_cast<int>( dummy.get_skill_level( skill_chemistry ) ) < 6 );
                CHECK( dummy.get_book_mastery( *alpha ) == book_mastery::MASTERED );
            }
        }
    }
}

TEST_CASE( "reading_a_book_with_an_ebook_reader", "[reading][book][ereader]" )
{
    avatar &dummy = get_avatar();
    clear_avatar();

    WHEN( "reading a book" ) {

        dummy.worn.wear_item( dummy, item( "backpack" ), false, false );
        dummy.i_add( item( "atomic_lamp" ) );
        REQUIRE( dummy.fine_detail_vision_mod() == 1 );

        item_location ereader = dummy.i_add( item( "test_ebook_reader" ) );

        item book{"test_textbook_fabrication"};
        ereader->put_in( book, pocket_type::EBOOK );

        item battery( "test_battery_disposable" );
        battery.ammo_set( battery.ammo_default(), 100 );
        ereader->put_in( battery, pocket_type::MAGAZINE_WELL );

        THEN( "player can read the book" ) {

            item_location booklc{dummy, &book};
            read_activity_actor actor( dummy.time_to_read( *booklc, dummy ), booklc, ereader, true );
            dummy.activity = player_activity( actor );

            dummy.activity.start_or_resume( dummy, false );
            REQUIRE( dummy.activity.id() == ACT_READ );
            dummy.activity.do_turn( dummy );

            CHECK( dummy.activity.id() == ACT_READ );

            AND_THEN( "ereader runs out of battery" ) {
                ereader->ammo_consume( 100, dummy.pos(), &dummy );
                dummy.activity.do_turn( dummy );

                THEN( "reading stops" ) {
                    CHECK( dummy.activity.id() != ACT_READ );
                }
            }
        }
    }
}