-
Notifications
You must be signed in to change notification settings - Fork 280
/
char_stamina_test.cpp
431 lines (349 loc) · 16.1 KB
/
char_stamina_test.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
#include "catch/catch.hpp"
#include <memory>
#include "avatar.h"
#include "bodypart.h"
#include "calendar.h"
#include "character.h"
#include "game.h"
#include "item.h"
#include "options.h"
#include "player.h"
#include "player_helpers.h"
#include "state_helpers.h"
#include "type_id.h"
#include "units.h"
// These test cases cover stamina-related functions in the `Character` class, including:
//
// - stamina_move_cost_modifier
// - burn_move_stamina
// - mod_stamina
// - update_stamina
//
// To run all tests in this file:
//
// tests/cata_test [stamina]
//
// Other tags used include: [cost], [move], [burn], [update], [regen]. [encumbrance]
// TODO: cover additional aspects of `burn_move_stamina` and `update_stamina`:
// - stamina burn is modified by bionic muscles
// - stamina recovery is modified by "bio_gills"
// - stimulants (positive or negative) affect stamina recovery in mysterious ways
// Helpers
// -------
// See also `clear_character` in `tests/player_helpers.cpp`
// Return `stamina_move_cost_modifier` in the given move_mode with [0.0 .. 1.0] stamina remaining
static float move_cost_mod( player &dummy, character_movemode move_mode,
float stamina_proportion = 1.0 )
{
// Reset and be able to run
clear_character( dummy );
REQUIRE( dummy.can_run() );
// Walk, run, or crouch
dummy.set_movement_mode( move_mode );
REQUIRE( dummy.movement_mode_is( move_mode ) );
// Adjust stamina to desired proportion and ensure it was set correctly
int new_stamina = static_cast<int>( stamina_proportion * dummy.get_stamina_max() );
dummy.set_stamina( new_stamina );
REQUIRE( dummy.get_stamina() == new_stamina );
// The point of it all: move cost modifier
return dummy.stamina_move_cost_modifier();
}
// Return amount of stamina burned per turn by `burn_move_stamina` in the given movement mode.
static int actual_burn_rate( player &dummy, character_movemode move_mode )
{
// Ensure we can run if necessary (aaaa zombies!)
dummy.set_stamina( dummy.get_stamina_max() );
REQUIRE( dummy.can_run() );
// Walk, run, or crouch
dummy.set_movement_mode( move_mode );
REQUIRE( dummy.movement_mode_is( move_mode ) );
// Measure stamina burned, and ensure it is nonzero
int before_stam = dummy.get_stamina();
dummy.burn_move_stamina( to_moves<int>( 1_turns ) );
int after_stam = dummy.get_stamina();
REQUIRE( before_stam > after_stam );
// How much stamina was actually burned?
return before_stam - after_stam;
}
// Burden the player with a given proportion [0.0 .. inf) of their maximum weight capacity
static void burden_player( player &dummy, float burden_proportion )
{
units::mass capacity = dummy.weight_capacity();
int units = static_cast<int>( capacity * burden_proportion / 1_gram );
// Add a pile of test platinum bits (1g/unit) to reach the desired weight capacity
if( burden_proportion > 0.0 ) {
dummy.i_add( item::spawn( "test_platinum_bit", calendar::turn, units ) );
}
// Ensure we are carrying the expected number of grams
REQUIRE( to_gram( dummy.weight_carried() ) == units );
}
// Return amount of stamina burned per turn by `burn_move_stamina` in the given movement mode,
// while carrying the given proportion [0.0, inf) of their maximum weight capacity.
static int burdened_burn_rate( player &dummy, character_movemode move_mode,
float burden_proportion = 0.0 )
{
clear_character( dummy, false );
burden_player( dummy, burden_proportion );
return actual_burn_rate( dummy, move_mode );
}
// Return the actual amount of stamina regenerated by `update_stamina` in the given number of moves
static float actual_regen_rate( player &dummy, int moves )
{
// Start at 10% stamina, plenty of space for regen
dummy.set_stamina( dummy.get_stamina_max() / 10 );
REQUIRE( dummy.get_stamina() == dummy.get_stamina_max() / 10 );
int before_stam = dummy.get_stamina();
dummy.update_stamina( moves );
int after_stam = dummy.get_stamina();
return after_stam - before_stam;
}
// Test cases
// ----------
TEST_CASE( "stamina movement cost modifier", "[stamina][cost]" )
{
clear_all_state();
player &dummy = g->u;
SECTION( "running cost is double walking cost for the same stamina level" ) {
CHECK( move_cost_mod( dummy, CMM_RUN, 1.0 ) == 2 * move_cost_mod( dummy, CMM_WALK, 1.0 ) );
CHECK( move_cost_mod( dummy, CMM_RUN, 0.5 ) == 2 * move_cost_mod( dummy, CMM_WALK, 0.5 ) );
CHECK( move_cost_mod( dummy, CMM_RUN, 0.0 ) == 2 * move_cost_mod( dummy, CMM_WALK, 0.0 ) );
}
SECTION( "walking cost is double crouching cost for the same stamina level" ) {
CHECK( move_cost_mod( dummy, CMM_WALK, 1.0 ) == 2 * move_cost_mod( dummy, CMM_CROUCH, 1.0 ) );
CHECK( move_cost_mod( dummy, CMM_WALK, 0.5 ) == 2 * move_cost_mod( dummy, CMM_CROUCH, 0.5 ) );
CHECK( move_cost_mod( dummy, CMM_WALK, 0.0 ) == 2 * move_cost_mod( dummy, CMM_CROUCH, 0.0 ) );
}
SECTION( "running cost goes from 2.0 to 1.0 as stamina goes to zero" ) {
CHECK( move_cost_mod( dummy, CMM_RUN, 1.00 ) == Approx( 2.00 ) );
CHECK( move_cost_mod( dummy, CMM_RUN, 0.75 ) == Approx( 1.75 ) );
CHECK( move_cost_mod( dummy, CMM_RUN, 0.50 ) == Approx( 1.50 ) );
CHECK( move_cost_mod( dummy, CMM_RUN, 0.25 ) == Approx( 1.25 ) );
CHECK( move_cost_mod( dummy, CMM_RUN, 0.00 ) == Approx( 1.00 ) );
}
SECTION( "walking cost goes from 1.0 to 0.5 as stamina goes to zero" ) {
CHECK( move_cost_mod( dummy, CMM_WALK, 1.00 ) == Approx( 1.000 ) );
CHECK( move_cost_mod( dummy, CMM_WALK, 0.75 ) == Approx( 0.875 ) );
CHECK( move_cost_mod( dummy, CMM_WALK, 0.50 ) == Approx( 0.750 ) );
CHECK( move_cost_mod( dummy, CMM_WALK, 0.25 ) == Approx( 0.625 ) );
CHECK( move_cost_mod( dummy, CMM_WALK, 0.00 ) == Approx( 0.500 ) );
}
SECTION( "crouching cost goes from 0.5 to 0.25 as stamina goes to zero" ) {
CHECK( move_cost_mod( dummy, CMM_CROUCH, 1.00 ) == Approx( 0.5000 ) );
CHECK( move_cost_mod( dummy, CMM_CROUCH, 0.75 ) == Approx( 0.4375 ) );
CHECK( move_cost_mod( dummy, CMM_CROUCH, 0.50 ) == Approx( 0.3750 ) );
CHECK( move_cost_mod( dummy, CMM_CROUCH, 0.25 ) == Approx( 0.3125 ) );
CHECK( move_cost_mod( dummy, CMM_CROUCH, 0.00 ) == Approx( 0.2500 ) );
}
}
TEST_CASE( "modify character stamina", "[stamina][modify]" )
{
clear_all_state();
player &dummy = g->u;
clear_character( dummy );
REQUIRE_FALSE( dummy.is_npc() );
GIVEN( "character has less than full stamina" ) {
int lost_stamina = dummy.get_stamina_max() / 2;
dummy.set_stamina( dummy.get_stamina_max() - lost_stamina );
REQUIRE( dummy.get_stamina() + lost_stamina == dummy.get_stamina_max() );
WHEN( "they regain only part of their lost stamina" ) {
dummy.mod_stamina( lost_stamina / 2 );
THEN( "stamina is less than maximum" ) {
CHECK( dummy.get_stamina() < dummy.get_stamina_max() );
}
}
WHEN( "they regain all of their lost stamina" ) {
dummy.mod_stamina( lost_stamina );
THEN( "stamina is at maximum" ) {
CHECK( dummy.get_stamina() == dummy.get_stamina_max() );
}
}
WHEN( "they regain more stamina than they lost" ) {
dummy.mod_stamina( lost_stamina + 1 );
THEN( "stamina is at maximum" ) {
CHECK( dummy.get_stamina() == dummy.get_stamina_max() );
}
}
WHEN( "they lose only part of their remaining stamina" ) {
dummy.mod_stamina( -( dummy.get_stamina() / 2 ) );
THEN( "stamina is above zero" ) {
CHECK( dummy.get_stamina() > 0 );
}
}
WHEN( "they lose all of their remaining stamina" ) {
dummy.mod_stamina( -( dummy.get_stamina() ) );
THEN( "stamina is at zero" ) {
CHECK( dummy.get_stamina() == 0 );
}
}
WHEN( "they lose more stamina than they have remaining" ) {
dummy.mod_stamina( -( dummy.get_stamina() + 1 ) );
THEN( "stamina is at zero" ) {
CHECK( dummy.get_stamina() == 0 );
}
}
}
}
TEST_CASE( "stamina burn for movement", "[stamina][burn][move]" )
{
clear_all_state();
player &dummy = g->u;
// Defined in game_balance.json
const int normal_burn_rate = get_option<int>( "PLAYER_BASE_STAMINA_BURN_RATE" );
REQUIRE( normal_burn_rate > 0 );
GIVEN( "player is naked and unburdened" ) {
THEN( "walking burns the normal amount of stamina per turn" ) {
CHECK( burdened_burn_rate( dummy, CMM_WALK, 0.0 ) == normal_burn_rate );
}
THEN( "running burns 14 times the normal amount of stamina per turn" ) {
CHECK( burdened_burn_rate( dummy, CMM_RUN, 0.0 ) == normal_burn_rate * 14 );
}
THEN( "crouching burns 1/2 the normal amount of stamina per turn" ) {
CHECK( burdened_burn_rate( dummy, CMM_CROUCH, 0.0 ) == normal_burn_rate / 2 );
}
}
GIVEN( "player is at their maximum weight capacity" ) {
THEN( "walking burns the normal amount of stamina per turn" ) {
CHECK( burdened_burn_rate( dummy, CMM_WALK, 1.0 ) == normal_burn_rate );
}
THEN( "running burns 14 times the normal amount of stamina per turn" ) {
CHECK( burdened_burn_rate( dummy, CMM_RUN, 1.0 ) == normal_burn_rate * 14 );
}
THEN( "crouching burns 1/2 the normal amount of stamina per turn" ) {
CHECK( burdened_burn_rate( dummy, CMM_CROUCH, 1.0 ) == normal_burn_rate / 2 );
}
}
GIVEN( "player is overburdened" ) {
THEN( "walking burn rate increases by 1 for each percent overburdened" ) {
CHECK( burdened_burn_rate( dummy, CMM_WALK, 1.01 ) == normal_burn_rate + 1 );
CHECK( burdened_burn_rate( dummy, CMM_WALK, 1.02 ) == normal_burn_rate + 2 );
CHECK( burdened_burn_rate( dummy, CMM_WALK, 1.50 ) == normal_burn_rate + 50 );
CHECK( burdened_burn_rate( dummy, CMM_WALK, 1.99 ) == normal_burn_rate + 99 );
CHECK( burdened_burn_rate( dummy, CMM_WALK, 2.00 ) == normal_burn_rate + 100 );
}
THEN( "running burn rate increases by 14 for each percent overburdened" ) {
CHECK( burdened_burn_rate( dummy, CMM_RUN, 1.01 ) == ( normal_burn_rate + 1 ) * 14 );
CHECK( burdened_burn_rate( dummy, CMM_RUN, 1.02 ) == ( normal_burn_rate + 2 ) * 14 );
CHECK( burdened_burn_rate( dummy, CMM_RUN, 1.50 ) == ( normal_burn_rate + 50 ) * 14 );
CHECK( burdened_burn_rate( dummy, CMM_RUN, 1.99 ) == ( normal_burn_rate + 99 ) * 14 );
CHECK( burdened_burn_rate( dummy, CMM_RUN, 2.00 ) == ( normal_burn_rate + 100 ) * 14 );
}
THEN( "crouching burn rate increases by 1/2 for each percent overburdened" ) {
CHECK( burdened_burn_rate( dummy, CMM_CROUCH, 1.01 ) == ( normal_burn_rate + 1 ) / 2 );
CHECK( burdened_burn_rate( dummy, CMM_CROUCH, 1.02 ) == ( normal_burn_rate + 2 ) / 2 );
CHECK( burdened_burn_rate( dummy, CMM_CROUCH, 1.50 ) == ( normal_burn_rate + 50 ) / 2 );
CHECK( burdened_burn_rate( dummy, CMM_CROUCH, 1.99 ) == ( normal_burn_rate + 99 ) / 2 );
CHECK( burdened_burn_rate( dummy, CMM_CROUCH, 2.00 ) == ( normal_burn_rate + 100 ) / 2 );
}
}
}
TEST_CASE( "burning stamina when overburdened may cause pain", "[stamina][burn][pain]" )
{
clear_all_state();
player &dummy = g->u;
int pain_before;
int pain_after;
GIVEN( "character is severely overburdened" ) {
// As overburden percentage goes from (100% .. 350%),
// chance of pain goes from (1/25 .. 1/1)
//
// To guarantee pain when moving and ensure consistent test results,
// set to 350% burden.
clear_character( dummy, false );
burden_player( dummy, 3.5 );
WHEN( "they have zero stamina left" ) {
dummy.set_stamina( 0 );
REQUIRE( dummy.get_stamina() == 0 );
THEN( "they feel pain when carrying too much weight" ) {
pain_before = dummy.get_pain();
dummy.burn_move_stamina( to_moves<int>( 1_turns ) );
pain_after = dummy.get_pain();
CHECK( pain_after > pain_before );
}
}
WHEN( "they have a bad back" ) {
dummy.toggle_trait( trait_id( "BADBACK" ) );
REQUIRE( dummy.has_trait( trait_id( "BADBACK" ) ) );
THEN( "they feel pain when carrying too much weight" ) {
pain_before = dummy.get_pain();
dummy.burn_move_stamina( to_moves<int>( 1_turns ) );
pain_after = dummy.get_pain();
CHECK( pain_after > pain_before );
}
}
}
}
TEST_CASE( "stamina regeneration rate", "[stamina][update][regen]" )
{
clear_all_state();
player &dummy = g->u;
clear_character( dummy, false );
int turn_moves = to_moves<int>( 1_turns );
const float normal_regen_rate = get_option<float>( "PLAYER_BASE_STAMINA_REGEN_RATE" );
REQUIRE( normal_regen_rate > 0 );
GIVEN( "character is not affected by any traits" ) {
REQUIRE( dummy.get_mutations().empty() );
THEN( "they regain stamina at the normal rate per turn" ) {
CHECK( actual_regen_rate( dummy, turn_moves ) == normal_regen_rate * turn_moves );
}
}
}
TEST_CASE( "stamina regen in different movement modes", "[stamina][update][regen][mode]" )
{
clear_all_state();
player &dummy = g->u;
clear_character( dummy );
int turn_moves = to_moves<int>( 1_turns );
dummy.set_movement_mode( CMM_RUN );
REQUIRE( dummy.movement_mode_is( CMM_RUN ) );
float run_regen_rate = actual_regen_rate( dummy, turn_moves );
dummy.set_movement_mode( CMM_WALK );
REQUIRE( dummy.movement_mode_is( CMM_WALK ) );
float walk_regen_rate = actual_regen_rate( dummy, turn_moves );
dummy.set_movement_mode( CMM_CROUCH );
REQUIRE( dummy.movement_mode_is( CMM_CROUCH ) );
float crouch_regen_rate = actual_regen_rate( dummy, turn_moves );
THEN( "run and walk mode give the same stamina regen per turn" ) {
CHECK( run_regen_rate == walk_regen_rate );
}
THEN( "walk and crouch mode give the same stamina regen per turn" ) {
CHECK( walk_regen_rate == crouch_regen_rate );
}
THEN( "crouch and run mode give the same stamina regen per turn" ) {
CHECK( crouch_regen_rate == run_regen_rate );
}
}
TEST_CASE( "stamina regen with mouth encumbrance", "[stamina][update][regen][encumbrance]" )
{
clear_all_state();
player &dummy = g->u;
clear_character( dummy );
int turn_moves = to_moves<int>( 1_turns );
const float normal_regen_rate = get_option<float>( "PLAYER_BASE_STAMINA_REGEN_RATE" );
REQUIRE( normal_regen_rate > 0 );
GIVEN( "character has 10 mouth encumbrance" ) {
dummy.wear_item( item::spawn( "scarf_fur" ) );
REQUIRE( dummy.encumb( body_part_mouth ) == 10 );
THEN( "stamina regen is reduced by 10%" ) {
CHECK( actual_regen_rate( dummy, turn_moves ) == ( normal_regen_rate - 2 ) * turn_moves );
}
}
GIVEN( "character has 30 mouth encumbrance" ) {
// Layering two scarves triples the encumbrance
dummy.wear_item( item::spawn( "scarf_fur" ) );
dummy.wear_item( item::spawn( "scarf_fur" ) );
REQUIRE( dummy.encumb( body_part_mouth ) == 30 );
THEN( "stamina regen is reduced by 30%" ) {
CHECK( actual_regen_rate( dummy, turn_moves ) == ( normal_regen_rate - 6 ) * turn_moves );
}
}
GIVEN( "character has 100+ mouth encumbrance" ) {
dummy.wear_item( item::spawn( "mask_gas" ) );
dummy.wear_item( item::spawn( "mask_gas" ) );
dummy.wear_item( item::spawn( "scarf_fur" ) );
dummy.wear_item( item::spawn( "scarf_fur" ) );
REQUIRE( dummy.encumb( body_part_mouth ) >= 100 );
THEN( "stamina regen is above 0" ) {
CHECK( actual_regen_rate( dummy, turn_moves ) > 0 );
}
}
}