File: cardio_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 (336 lines) | stat: -rw-r--r-- 14,054 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
#include "avatar.h"
#include "cata_catch.h"
#include "game.h"
#include "options.h"
#include "map.h"
#include "map_helpers.h"
#include "player_helpers.h"

// Cardio Fitness
// --------------
// The hidden "Cardio Fitness" stat returned by Character::get_cardiofit() is the main factor in
// calculations related to cardiovascular health, including weariness, stamina, and stamina regen,
// more or less as described in the original issue #44370 "Rule #1. CARDIO".
//
// For player characters, cardio fitness is derived from a combination of several attributes:
//
//   (
//       [ Trait modifiers ]          // Good/bad cardio mutations like Languorous and Hyperactive
//                                    // Trait modifers defaults to 1.0f, so it is the baseline
//     + [ Health ] / 1000            // Hidden health modifier, -200..+200
//     + [ Proficiency modifiers ]    // NOT IMPLEMENTED
//     + [ Athletics skill ] / 100    // Formerly swimming skill, now more versatile and valuable
//   ) * [ Cardio_Accumulator ]       // Adjustment based on daily activity, starting at 1000
//
// For NPCs (having no cardio), it's 2000.
//
// Important functions:
// - Character::get_stamina
// - Character::set_stamina
// - Character::get_stamina_max
// - Character::get_cardiofit
// - Character::get_cardio_acc
// - Character::set_cardio_acc
// - avatar::update_cardio_acc <-- Critical

static const move_mode_id move_mode_run( "run" );

static const skill_id skill_swimming( "swimming" );

// Base cardio for default character
static const int base_cardio = 1000;
// Base stamina
// MAX_STAMINA_BASE + CARDIOFIT_STAMINA_SCALING * base_cardio == 3500 + 5*1000
static const int base_stamina = 8500;

// Ensure the configured options from game_balance.json are what the tests assume they are
static void verify_default_cardio_options()
{
    const int max_stamina_base = get_option<int>( "PLAYER_MAX_STAMINA_BASE" );
    const int cardiofit_stamina_scaling = get_option<int>( "PLAYER_CARDIOFIT_STAMINA_SCALING" );
    REQUIRE( max_stamina_base == 3500 );
    REQUIRE( cardiofit_stamina_scaling == 5 );
}

// Count the number of steps (tiles) until character runs out of stamina or becomes winded.
static int running_steps( Character &they, const ter_id &terrain = t_pavement )
{
    map &here = get_map();
    // Please take off your shoes when entering, and no NPCs allowed
    REQUIRE_FALSE( they.is_wearing_shoes() );
    REQUIRE_FALSE( they.is_npc() );
    // You put your left foot in, you put your right foot in
    const tripoint left = they.pos();
    const tripoint right = left + tripoint_east;
    // You ensure two tiles of terrain to hokey-pokey in
    here.ter_set( left, terrain );
    here.ter_set( right, terrain );
    REQUIRE( here.ter( left ) == terrain );
    REQUIRE( here.ter( right ) == terrain );
    // Count how many steps (1-tile moves) it takes to become winded
    int steps = 0;
    int turns = 0;
    const int STOP_STEPS = 1000; // Safe exit in case of Superman
    // Track changes to moves and stamina
    int last_moves = they.get_speed();
    int last_stamina = they.get_stamina_max();
    // Take a deep breath and start running
    they.moves = last_moves;
    they.set_stamina( last_stamina );
    they.set_movement_mode( move_mode_run );
    // Run as long as possible
    while( they.can_run() && steps < STOP_STEPS ) {
        // Step right on even steps, left on odd steps
        if( steps % 2 == 0 ) {
            REQUIRE( they.pos() == left );
            REQUIRE( g->walk_move( right, false, false ) );
        } else {
            REQUIRE( they.pos() == right );
            REQUIRE( g->walk_move( left, false, false ) );
        }
        ++steps;

        // Ensure moves are decreasing, or else a turn will never pass
        REQUIRE( they.moves < last_moves );
        const int move_cost = last_moves - they.moves;
        // When moves run out, one turn has passed
        if( they.moves <= 0 ) {
            // Get "speed" moves back each turn
            they.moves += they.get_speed();
            calendar::turn += 1_turns;
            turns += 1;

            // Update body for stamina regen
            they.update_body();
            const int stamina_cost = last_stamina - they.get_stamina();
            // Total stamina must also be decreasing; if not, quit
            CAPTURE( turns );
            CAPTURE( steps );
            CAPTURE( move_cost );
            CAPTURE( stamina_cost );
            REQUIRE( they.get_stamina() < last_stamina );
            last_stamina = they.get_stamina();
        }
        last_moves = they.moves;
    }
    // Reset to starting position
    they.setpos( left );
    return steps;
}

// Give character a trait, and verify their expected cardio, max stamina, and running distance.
static void check_trait_cardio_stamina_run( Character &they, std::string trait_name,
        const int expect_cardio_fit, const int expect_stamina_max, const int expect_run_tiles )
{
    clear_avatar();
    if( trait_name.empty() ) {
        trait_name = "NONE";
    } else {
        set_single_trait( they, trait_name );
    }
    //it's possible suddenly becoming huge or tiny will cause BMI issues so we reset stored kcal to new normal
    they.set_stored_kcal( they.get_healthy_kcal() );
    GIVEN( "trait: " + trait_name ) {
        CHECK( they.get_cardiofit() == Approx( expect_cardio_fit ).margin( 2 ) );
        CHECK( they.get_stamina_max() == Approx( expect_stamina_max ).margin( 5 ) );
        //CHECK( they.get_stamina_max() == Approx( 3500 + 3 * expect_cardio_fit ).margin( 2 ) );
        CHECK( running_steps( they ) == Approx( expect_run_tiles ).margin( 3 ) );
    }
}

// Baseline character with default attributes and no traits
TEST_CASE( "base_cardio", "[cardio][base]" )
{
    verify_default_cardio_options();
    Character &they = get_player_character();

    clear_map();
    clear_avatar();

    // Ensure no initial effects that would affect cardio
    REQUIRE( they.get_lifestyle() == 0 );
    REQUIRE( static_cast<int>( they.get_skill_level( skill_swimming ) ) == 0 );
    // Ensure starting cardio are what we expect
    REQUIRE( they.get_cardiofit() == 1000 );

    SECTION( "Base character with no traits" ) {
        // pre-Cardio, could run 96 steps
        // post-Cardio, can run 84 steps in-game, test case reached 87
        // correctly counting moves/steps instead of turns, test case reaches 83
        check_trait_cardio_stamina_run( they, "", base_cardio, base_stamina, 83 );
    }
}

// Trait Modifiers
// ---------------
// Mutations/traits can influence cardio fitness level in a two ways:
//
// Some traits affect cardio fitness directly:
//
// - cardio_multiplier: Multiplies maximum cardio
//   - Languorous: Bad cardio, less total stamina
//   - Indefatigable, Hyperactive: Good cardio, more total stamina
//
// Some traits affect stamina regen and total running distance without affecting cardio:
//
// - stamina_regen_modifier
//   - Fast Metabolism, Persistence Hunter: Increased stamina regeneration
//
TEST_CASE( "cardio_is_and_is_not_affected_by_certain_traits", "[cardio][traits]" )
{
    verify_default_cardio_options();
    Character &they = get_player_character();

    clear_map();
    clear_avatar();

    // Ensure no initial effects that would affect cardio
    REQUIRE( they.get_lifestyle() == 0 );
    REQUIRE( static_cast<int>( they.get_skill_level( skill_swimming ) ) == 0 );
    // Ensure starting cardio are what we expect
    REQUIRE( they.get_cardiofit() == 1000 );

    SECTION( "Base character with no traits" ) {
        // pre-Cardio, could run 96 steps
        check_trait_cardio_stamina_run( they, "", base_cardio, base_stamina, 81 );
    }

    // Body Size has no effect on running distance.
    SECTION( "Traits affecting body size" ) {
        check_trait_cardio_stamina_run( they, "SMALL2", base_cardio, base_stamina, 81 );
        check_trait_cardio_stamina_run( they, "SMALL", base_cardio, base_stamina, 81 );
        check_trait_cardio_stamina_run( they, "LARGE", base_cardio, base_stamina, 81 );
        check_trait_cardio_stamina_run( they, "HUGE", base_cardio, base_stamina, 81 );
    }

    SECTION( "Traits with cardio_multiplier" ) {
        // These traits were formerly implemented by max_stamina_modifier, which multiplied
        // maximum stamina. Now that cardio fitness is actually implemented, these traits
        // directly affect total cardio fitness, and thus maximum stamina (and running distance).
        // Languorous
        check_trait_cardio_stamina_run( they, "BADCARDIO", 0.7 * base_cardio, 7000, 66 );
        // Indefatigable
        check_trait_cardio_stamina_run( they, "GOODCARDIO", 1.3 * base_cardio, 10000, 103 );
        // Hyperactive
        check_trait_cardio_stamina_run( they, "GOODCARDIO2", 1.6 * base_cardio, 11500, 121 );
    }

    SECTION( "Traits with metabolism_modifier AND stamina_regen_modifier" ) {
        // Fast Metabolism
        check_trait_cardio_stamina_run( they, "HUNGER", base_cardio, base_stamina, 83 );
        // Very Fast Metabolism
        check_trait_cardio_stamina_run( they, "HUNGER2", base_cardio, base_stamina, 85 );
        // Extreme Metabolism
        check_trait_cardio_stamina_run( they, "HUNGER3", base_cardio, base_stamina, 88 );
    }

    SECTION( "Traits with ONLY stamina_regen_modifier" ) {
        check_trait_cardio_stamina_run( they, "PERSISTENCE_HUNTER", base_cardio, base_stamina, 83 );
        check_trait_cardio_stamina_run( they, "PERSISTENCE_HUNTER2", base_cardio, base_stamina, 84 );
    }

    SECTION( "Traits with ONLY metabolism_modifier" ) {
        check_trait_cardio_stamina_run( they, "COLDBLOOD", base_cardio, base_stamina, 81 );
        check_trait_cardio_stamina_run( they, "COLDBLOOD2", base_cardio, base_stamina, 81 );
        check_trait_cardio_stamina_run( they, "COLDBLOOD3", base_cardio, base_stamina, 81 );
        check_trait_cardio_stamina_run( they, "COLDBLOOD4", base_cardio, base_stamina, 81 );
        check_trait_cardio_stamina_run( they, "LIGHTEATER", base_cardio, base_stamina, 81 );
        check_trait_cardio_stamina_run( they, "MET_RAT", base_cardio, base_stamina, 81 );
    }
}

TEST_CASE( "cardio_affects_stamina_regeneration", "[cardio][stamina]" )
{
    // With baseline cardio, stamina regen is X
    // With low cardio, stamina regen is X--
    // With high cardio, stamina regen is X++
}

TEST_CASE( "cardio_affects_weariness", "[cardio][weariness]" )
{
    // Weariness threshold is 1/2 cardio
}

TEST_CASE( "cardio_is_affected_by_activity_level_each_day", "[cardio][activity]" )
{
    // When activity increases, cardio goes up
    // When activity decreases, cardio goes down
    // When activity stays the same, cardio stays the same

    // Given a starting character
    // When they get no exercise for a week
    // Then their cardio should decrease slightly
    // When they get moderate exercise for a week
    // Then their cardio should increase slightly
}

TEST_CASE( "cardio_is_not_affected_by_character_height", "[cardio][height]" )
{
    verify_default_cardio_options();
    Character &they = get_player_character();
    clear_avatar();

    REQUIRE( they.size_class == creature_size::medium );

    SECTION( "No difference in cardio between heights" ) {
        they.set_base_height( Character::default_height( they.size_class ) );
        CHECK( they.get_cardiofit() == base_cardio );
        they.set_base_height( Character::max_height( they.size_class ) );
        CHECK( they.get_cardiofit() == base_cardio );
        they.set_base_height( Character::min_height( they.size_class ) );
        CHECK( they.get_cardiofit() == base_cardio );
    }
}

TEST_CASE( "cardio_is_affected_by_character_weight", "[cardio][weight]" )
{
    // Underweight, overweight
}

TEST_CASE( "cardio_is_affected_by_character_health", "[cardio][health]" )
{
    verify_default_cardio_options();
    Character &they = get_player_character();
    clear_avatar();

    SECTION( "Hidden health stat adds directly to cardio fitness" ) {
        they.set_lifestyle( 0 );
        CHECK( they.get_cardiofit() == base_cardio );
        they.set_lifestyle( 200 );
        CHECK( they.get_cardiofit() == static_cast<int>( base_cardio * 1.20f ) );
        they.set_lifestyle( -200 );
        CHECK( they.get_cardiofit() == static_cast<int>( base_cardio * 0.60f ) );
    }
}

TEST_CASE( "cardio_is_affected_by_athletics_skill", "[cardio][athletics]" )
{
    verify_default_cardio_options();
    Character &they = get_player_character();
    clear_avatar();

    SECTION( "Athletics skill adds 10 per level to cardio fitness" ) {
        they.set_skill_level( skill_swimming, 0 );
        CHECK( they.get_cardiofit() == base_cardio );
        they.set_skill_level( skill_swimming, 1 );
        CHECK( they.get_cardiofit() == static_cast<int>( base_cardio * 1.01f ) );
        they.set_skill_level( skill_swimming, 2 );
        CHECK( they.get_cardiofit() == static_cast<int>( base_cardio * 1.02f ) );
        they.set_skill_level( skill_swimming, 3 );
        CHECK( they.get_cardiofit() == static_cast<int>( base_cardio * 1.03f ) );
        they.set_skill_level( skill_swimming, 4 );
        CHECK( they.get_cardiofit() == static_cast<int>( base_cardio * 1.04f ) );
        they.set_skill_level( skill_swimming, 5 );
        CHECK( they.get_cardiofit() == static_cast<int>( base_cardio * 1.05f ) );
        they.set_skill_level( skill_swimming, 6 );
        CHECK( they.get_cardiofit() == static_cast<int>( base_cardio * 1.06f ) );
        they.set_skill_level( skill_swimming, 7 );
        CHECK( they.get_cardiofit() == static_cast<int>( base_cardio * 1.07f ) );
        they.set_skill_level( skill_swimming, 8 );
        CHECK( they.get_cardiofit() == static_cast<int>( base_cardio * 1.08f ) );
        they.set_skill_level( skill_swimming, 9 );
        CHECK( they.get_cardiofit() == static_cast<int>( base_cardio * 1.09f ) );
        they.set_skill_level( skill_swimming, 10 );
        CHECK( they.get_cardiofit() == static_cast<int>( base_cardio * 1.10f ) );
    }
}