forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
char_suffer_test.cpp
504 lines (435 loc) · 19 KB
/
char_suffer_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
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
#include <iosfwd>
#include <list>
#include <map>
#include <memory>
#include <string>
#include <vector>
#include "avatar.h"
#include "calendar.h"
#include "cata_catch.h"
#include "character.h"
#include "creature.h"
#include "flag.h"
#include "game.h"
#include "item.h"
#include "map_helpers.h"
#include "player_helpers.h"
#include "test_statistics.h"
#include "type_id.h"
static const efftype_id effect_grabbed( "grabbed" );
static const trait_id trait_ALBINO( "ALBINO" );
static const trait_id trait_SUNBURN( "SUNBURN" );
// Tests for Character suffering
//
// Covers functions:
// - Character::suffer_from_sunburn
// Make character suffer for a while
static void test_suffer( Character &dummy, const time_duration &dur, bool update_body = false )
{
const int num_turns = to_turns<int>( dur );
for( int turn = 0; turn < num_turns; ++turn ) {
dummy.suffer();
if( update_body ) {
dummy.update_body( calendar::turn, calendar::turn + 1_turns );
}
}
}
// Return total focus lost just from suffering over a given time duration (may exceed maximum focus)
static int test_suffer_focus_lost( Character &dummy, const time_duration &dur )
{
int focus_lost = 0;
const int num_turns = to_turns<int>( dur );
for( int turn = 0; turn < num_turns; ++turn ) {
dummy.set_focus( 100 );
dummy.suffer();
focus_lost += 100 - dummy.get_focus();
}
return focus_lost;
}
// Return total hit points lost for each body part over a given time duration (may exceed max HP)
static std::map<bodypart_id, int> test_suffer_bodypart_hp_lost( Character &dummy,
const time_duration &dur )
{
const std::vector<bodypart_id> body_parts_with_hp = dummy.get_all_body_parts(
get_body_part_flags::only_main );
// Total hit points lost for each body part
std::map<bodypart_id, int> bp_hp_lost;
for( const bodypart_id &bp : body_parts_with_hp ) {
bp_hp_lost[bp] = 0;
}
// Every turn, heal completely, then suffer and accumulate HP loss for each body part
const int num_turns = to_turns<int>( dur );
for( int turn = 0; turn < num_turns; ++turn ) {
dummy.healall( 100 );
dummy.suffer();
for( const bodypart_id &bp : body_parts_with_hp ) {
bp_hp_lost[bp] += dummy.get_part_hp_max( bp.id() ) - dummy.get_part_hp_cur( bp.id() );
}
}
return bp_hp_lost;
}
// Return total pain points just from suffering over a given time duration
static int test_suffer_pain_felt( Character &dummy, const time_duration &dur )
{
int pain_felt = 0;
const int num_turns = to_turns<int>( dur );
for( int turn = 0; turn < num_turns; ++turn ) {
dummy.set_pain( 0 );
dummy.suffer();
pain_felt += dummy.get_pain();
}
return pain_felt;
}
// Suffering from albinism (ALBINO trait)
//
// - Albinism suffering effects occur about once per minute (1/60 chance per turn)
// - If character is wielding an umbrella and wearing sunglasses, all effects are negated
// - If character has body parts more than 1% exposed, they suffer focus loss or pain
// - Most of the time (59/60 chance), 1 focus is lost, or 2 without sunglasses
// - Sometimes (1/60 chance), there is 1 pain instead, or 2 without sunglasses
//
TEST_CASE( "suffering from albinism", "[char][suffer][albino]" )
{
clear_map();
avatar &dummy = get_avatar();
clear_character( dummy );
g->reset_light_level();
int focus_lost = 0;
// TODO: The random chance of pain is too unprectable to test reliably.
// As a result any test with small non-zero values has been disabled.
// The values should still be correct
// Need sunglasses to protect the eyes, no matter how covered the rest of the body is
item shades( "test_sunglasses" );
// Umbrella can protect completely
item umbrella( "test_umbrella" );
// hazmat suit has 100% coverage, but eyes and mouth are exposed
item hazmat( "test_hazmat_suit" );
// zentai has 100% coverage over all body parts
item zentai( "test_zentai" );
// long-sleeve shirt has 90% coverage on torso and arms
item longshirt( "test_longshirt" );
GIVEN( "avatar is in sunlight with the albino trait" ) {
calendar::turn = calendar::turn_zero + 12_hours;
REQUIRE( g->is_in_sunlight( dummy.pos() ) );
dummy.toggle_trait( trait_ALBINO );
REQUIRE( dummy.has_trait( trait_ALBINO ) );
WHEN( "totally naked and exposed" ) {
dummy.worn.clear();
// 60 times * 12 bodyparts * 0.25 chance for medium effect
THEN( "they lose about 165 focus per hour" ) {
focus_lost = test_suffer_focus_lost( dummy, 1_hours );
CHECK( focus_lost == Approx( 180 ).margin( 90 ) );
}
// THEN( "they suffer about 2 pain per hour" ) {
// 1 pain per hour for unshaded eyes
// 1 pain per hour for exposed skin
// Without running a long test, chance of pain is too low to measure effectively
// This assertion will pass when pain is between 0 and 12 in an hour
//pain_felt = test_suffer_pain_felt( dummy, 1_hours );
//CHECK( pain_felt == Approx( 2 ).margin( 10 ) );
// }
}
WHEN( "wielding an umbrella and wearing sunglasses" ) {
dummy.wield( umbrella );
REQUIRE( dummy.get_wielded_item()->has_flag( flag_RAIN_PROTECT ) );
dummy.wear_item( shades, false );
REQUIRE( dummy.worn_with_flag( flag_SUN_GLASSES ) );
THEN( "they suffer no pain or loss of focus" ) {
focus_lost = test_suffer_focus_lost( dummy, 1_hours );
CHECK( dummy.get_pain() == 0 );
CHECK( focus_lost == 0 );
}
}
WHEN( "entire body is covered with clothing" ) {
dummy.worn.clear();
dummy.wear_item( zentai, false );
// WHEN( "not wearing sunglasses" ) {
// REQUIRE_FALSE( dummy.worn_with_flag( flag_SUN_GLASSES ) );
// 60 times * 1 bodyparts * 0.1 chance for severe effect
// THEN( "they suffer about 6 pain per hour" ) {
// test_suffer( dummy, 3_hours );
// CHECK( dummy.get_pain() == Approx( 18 ).margin( 17 ) );
// }
// 60 times * 1 bodyparts * 0.25 chance for medium effect
// THEN( "they lose about 15 focus per hour" ) {
// focus_lost = test_suffer_focus_lost( dummy, 1_hours );
// CHECK( focus_lost == Approx( 15 ).margin( 14 ) );
// }
// }
WHEN( "wearing sunglasses" ) {
dummy.wear_item( shades, false );
REQUIRE( dummy.worn_with_flag( flag_SUN_GLASSES ) );
THEN( "they suffer no pain or loss of focus" ) {
focus_lost = test_suffer_focus_lost( dummy, 1_hours );
CHECK( dummy.get_pain() == 0 );
CHECK( focus_lost == 0 );
}
}
}
}
}
// Suffering from Solar Sensitivity (SUNBURN trait)
//
// - Solar Sens. suffering effects occur about 3 times per minute (1/20 chance per turn)
// - If character is wielding an umbrella and wearing sunglasses, all effects are negated
// - If character has body parts more than 1% exposed, they suffer pain and loss of HP
// - Chance of pain and HP loss is directly proportional to skin exposure on each body part
// -
//
TEST_CASE( "suffering from sunburn", "[char][suffer][sunburn]" )
{
clear_map();
clear_avatar();
Character &dummy = get_player_character();
g->reset_light_level();
const std::vector<bodypart_id> body_parts_with_hp = dummy.get_all_body_parts(
get_body_part_flags::only_main );
int focus_lost = 0;
int pain_felt = 0;
item shades( "test_sunglasses" );
item umbrella( "test_umbrella" );
item zentai( "test_zentai" );
item longshirt( "test_longshirt" );
GIVEN( "avatar is in sunlight with the solar sensitivity trait" ) {
calendar::turn = calendar::turn_zero + 12_hours;
REQUIRE( g->is_in_sunlight( dummy.pos() ) );
dummy.toggle_trait( trait_SUNBURN );
REQUIRE( dummy.has_trait( trait_SUNBURN ) );
std::map<bodypart_id, int> bp_hp_lost;
WHEN( "totally naked and exposed, with sunglasses" ) {
dummy.worn.clear();
dummy.wear_item( shades, false );
REQUIRE( dummy.worn_with_flag( flag_SUN_GLASSES ) );
// THEN( "they suffer injuries on every body part" ) {
// Should lose an average of 1 HP per minute from each body part with hit points
// (head, torso, both arms, both legs)
// 60 * 0.1 * 2 (as two body parts contribute to each part with HP, e.g. l. arm + l. hand both damage l. arm)
// bp_hp_lost = test_suffer_bodypart_hp_lost( dummy, 1_hours );
// for( const bodypart_id &bp : body_parts_with_hp ) {
// CAPTURE( bp.id().str() );
// CHECK( bp_hp_lost[bp] == Approx( 12 ).margin( 11 ) );
// }
// }
THEN( "they suffer pain several times a minute" ) {
// 60 * 0.25 * 11 (num body parts, excluding eyes)
pain_felt = test_suffer_pain_felt( dummy, 1_hours );
CHECK( pain_felt == Approx( 15 * 11 ).margin( 14 * 11 ) );
}
}
WHEN( "naked and wielding an umbrella, with sunglasses" ) {
dummy.worn.clear();
dummy.wield( umbrella );
REQUIRE( dummy.get_wielded_item()->has_flag( flag_RAIN_PROTECT ) );
dummy.wear_item( shades, false );
REQUIRE( dummy.worn_with_flag( flag_SUN_GLASSES ) );
// Umbrella completely shields the skin from exposure when wielded
THEN( "they suffer no injury" ) {
bp_hp_lost = test_suffer_bodypart_hp_lost( dummy, 1_hours );
for( const bodypart_id &bp : body_parts_with_hp ) {
CAPTURE( bp.id().str() );
CHECK( bp_hp_lost[bp] == 0 );
}
}
THEN( "they suffer no pain" ) {
pain_felt = test_suffer_pain_felt( dummy, 1_hours );
CHECK( pain_felt == 0 );
}
}
WHEN( "wielding an umbrella, without sunglasses" ) {
dummy.worn.clear();
dummy.wield( umbrella );
REQUIRE( dummy.get_wielded_item()->has_flag( flag_RAIN_PROTECT ) );
REQUIRE_FALSE( dummy.worn_with_flag( flag_SUN_GLASSES ) );
THEN( "they suffer only head injury" ) {
bp_hp_lost = test_suffer_bodypart_hp_lost( dummy, 1_hours );
for( const bodypart_id &bp : body_parts_with_hp ) {
CAPTURE( bp.id().str() );
// if( bp == bodypart_id( "head" ) ) {
// 60 * 0.1
// CHECK( bp_hp_lost[bp] == Approx( 6 ).margin( 4 ) );
// } else
if( bp != bodypart_id( "head" ) ) {
CHECK( bp_hp_lost[bp] == 0 );
}
}
}
// THEN( "they suffer pain" ) {
// 60 * 0.25
// pain_felt = test_suffer_pain_felt( dummy, 1_hours );
// CHECK( pain_felt == Approx( 15 ).margin( 14 ) );
// }
}
WHEN( "torso and arms are 90% covered" ) {
dummy.worn.clear();
dummy.wear_item( longshirt, false );
THEN( "damage to torso is 0 and halved for arms" ) {
time_duration t = 1_hours;
bp_hp_lost = test_suffer_bodypart_hp_lost( dummy, t );
for( const bodypart_id &bp : body_parts_with_hp ) {
CAPTURE( bp.id().str() );
if( bp.id().str() == "torso" ) {
CHECK( bp_hp_lost[bp] == 0 );
}
// else if( bp.id().str() == "arm_l" || bp.id().str() == "arm_r" ) {
// Hands are exposed
// 120 * 0.1 * 1
// CHECK( bp_hp_lost[bp] == Approx( 12 ).margin( 11 ) );
// } else {
// legs+feet combine, and head+mouth combine (doubled damage)
// 120 * 0.1 * 2
// CHECK( bp_hp_lost[bp] == Approx( 24 ).margin( 23 ) );
// }
}
}
}
WHEN( "entire body is covered" ) {
dummy.worn.clear();
dummy.wear_item( zentai, false );
WHEN( "not wearing sunglasses" ) {
REQUIRE_FALSE( dummy.worn_with_flag( flag_SUN_GLASSES ) );
THEN( "they suffer loss of focus" ) {
// Heavy and medium effects take priority.
// Although the chance for focus loss is written as 1.0 it is in reality 0.65 at 100% exposure
// 0.65 = 1.0 - 0.1 - 0.25
// 39 = 0.65 * 1 * 60
focus_lost = test_suffer_focus_lost( dummy, 1_hours );
CHECK( focus_lost == Approx( 39 ).margin( 30 ) );
}
// THEN( "they suffer pain" ) {
// 60 * 0.25
// from exposed eyes as they count as a fully exposed body part
// pain_felt = test_suffer_pain_felt( dummy, 1_hours );
// CHECK( pain_felt == Approx( 15 ).margin( 14 ) );
// }
}
WHEN( "wearing sunglasses" ) {
dummy.wear_item( shades, false );
REQUIRE( dummy.worn_with_flag( flag_SUN_GLASSES ) );
THEN( "they suffer no injury" ) {
bp_hp_lost = test_suffer_bodypart_hp_lost( dummy, 10_minutes );
for( const bodypart_id &bp : body_parts_with_hp ) {
CAPTURE( bp.id().str() );
CHECK( bp_hp_lost[bp] == 0 );
}
}
THEN( "they suffer no pain or loss of focus" ) {
focus_lost = test_suffer_focus_lost( dummy, 1_hours );
CHECK( dummy.get_pain() == 0 );
CHECK( focus_lost == 0 );
}
}
}
}
}
TEST_CASE( "suffering from asphyxiation", "[char][suffer][oxygen]" )
{
clear_map();
clear_avatar();
Character &dummy = get_player_character();
dummy.set_underwater( false );
dummy.oxygen = dummy.get_oxygen_max();
REQUIRE( dummy.oxygen == 46 );
GIVEN( "character is not grabbed or underwater" ) {
REQUIRE( !dummy.is_underwater() );
REQUIRE( !dummy.has_effect( effect_grabbed, body_part_torso ) );
dummy.oxygen = 0;
WHEN( "at full stamina" ) {
REQUIRE( dummy.get_stamina() == dummy.get_stamina_max() );
THEN( "recover 5 stamina per turn" ) {
test_suffer( dummy, 3_turns, true );
CHECK( dummy.oxygen == 15 );
}
}
WHEN( "at 3/4 stamina" ) {
dummy.set_stamina( ( dummy.get_stamina_max() * 3 ) / 4 );
REQUIRE( dummy.get_stamina() == ( dummy.get_stamina_max() * 3 ) / 4 );
THEN( "recover 3 stamina per turn" ) {
test_suffer( dummy, 3_turns, true );
CHECK( dummy.oxygen == 9 );
}
}
WHEN( "at 1/2 stamina" ) {
dummy.set_stamina( dummy.get_stamina_max() / 2 );
REQUIRE( dummy.get_stamina() == dummy.get_stamina_max() / 2 );
THEN( "recover 2 stamina per turn" ) {
test_suffer( dummy, 3_turns, true );
CHECK( dummy.oxygen == 6 );
}
}
WHEN( "at 1/4 stamina" ) {
dummy.set_stamina( dummy.get_stamina_max() / 4 );
REQUIRE( dummy.get_stamina() == dummy.get_stamina_max() / 4 );
THEN( "recover 1 stamina per turn" ) {
test_suffer( dummy, 3_turns, true );
CHECK( dummy.oxygen == 3 );
}
}
WHEN( "at 1/10 stamina" ) {
dummy.set_stamina( dummy.get_stamina_max() / 10 );
REQUIRE( dummy.get_stamina() == dummy.get_stamina_max() / 10 );
THEN( "recover 1 stamina per turn" ) {
test_suffer( dummy, 3_turns, true );
CHECK( dummy.oxygen == 3 );
}
}
WHEN( "at 0 stamina" ) {
dummy.set_stamina( 0 );
REQUIRE( dummy.get_stamina() == 0 );
THEN( "recover 1 stamina per turn" ) {
test_suffer( dummy, 3_turns, true );
CHECK( dummy.oxygen == 3 );
}
}
}
GIVEN( "character is underwater" ) {
dummy.set_underwater( true );
REQUIRE( dummy.is_underwater() );
REQUIRE( dummy.oxygen == 46 );
REQUIRE( dummy.get_stamina() == dummy.get_stamina_max() );
THEN( "they lose 1 oxygen per turn" ) {
test_suffer( dummy, 10_turns, true );
CHECK( dummy.oxygen == 36 );
}
}
GIVEN( "character is grabbed" ) {
REQUIRE( dummy.oxygen == 46 );
REQUIRE( !dummy.is_underwater() );
REQUIRE( dummy.get_stamina() == dummy.get_stamina_max() );
WHEN( "grabbed intensity = 2" ) {
dummy.add_effect( effect_grabbed, 20_turns, body_part_torso, false, 2, true );
REQUIRE( dummy.has_effect( effect_grabbed, body_part_torso ) );
REQUIRE( dummy.get_effect_int( effect_grabbed, body_part_torso ) == 2 );
THEN( "they lose 0 or 1 oxygen per turn" ) {
test_suffer( dummy, 10_turns, true );
CHECK( dummy.oxygen == Approx( 41 ).margin( 5 ) );
}
}
WHEN( "grabbed intensity = 4" ) {
dummy.add_effect( effect_grabbed, 20_turns, body_part_torso, false, 4, true );
REQUIRE( dummy.has_effect( effect_grabbed, body_part_torso ) );
REQUIRE( dummy.get_effect_int( effect_grabbed, body_part_torso ) == 4 );
THEN( "they lose 1 oxygen per turn" ) {
test_suffer( dummy, 10_turns, true );
CHECK( dummy.oxygen == 36 );
}
}
WHEN( "grabbed intensity = 6" ) {
dummy.add_effect( effect_grabbed, 20_turns, body_part_torso, false, 6, true );
REQUIRE( dummy.has_effect( effect_grabbed, body_part_torso ) );
REQUIRE( dummy.get_effect_int( effect_grabbed, body_part_torso ) == 6 );
THEN( "they lose 1 or 2 oxygen per turn" ) {
test_suffer( dummy, 10_turns, true );
CHECK( dummy.oxygen == Approx( 31 ).margin( 5 ) );
}
}
WHEN( "grabbed intensity = 8" ) {
dummy.add_effect( effect_grabbed, 20_turns, body_part_torso, false, 8, true );
REQUIRE( dummy.has_effect( effect_grabbed, body_part_torso ) );
REQUIRE( dummy.get_effect_int( effect_grabbed, body_part_torso ) == 8 );
THEN( "they lose 2 oxygen per turn" ) {
test_suffer( dummy, 10_turns, true );
CHECK( dummy.oxygen == 26 );
}
}
}
}