File: move_cost_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 (492 lines) | stat: -rw-r--r-- 17,707 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
#include "avatar.h"
#include "avatar_action.h"
#include "cata_catch.h"
#include "flag.h"
#include "map.h"
#include "map_helpers.h"
#include "player_helpers.h"

// Test cases related to movement cost
//
// Functions:
// - Character::run_cost - Modified base movement cost, with effects of mutations and footwear
//
// Mutation fields:
// - movecost_obstacle_modifier
// - movecost_modifier
// - movecost_flatground_modifier
// - stamina_move_cost_mod
//
// TODO:
// - Shoe only on one foot
// - Mycus on fungus

static const character_modifier_id
character_modifier_limb_footing_movecost_mod( "limb_footing_movecost_mod" );
static const character_modifier_id character_modifier_limb_run_cost_mod( "limb_run_cost_mod" );
static const character_modifier_id
character_modifier_limb_speed_movecost_mod( "limb_speed_movecost_mod" );

static const efftype_id effect_downed( "downed" );

static const move_mode_id move_mode_crouch( "crouch" );
static const move_mode_id move_mode_prone( "prone" );
static const move_mode_id move_mode_run( "run" );
static const move_mode_id move_mode_walk( "walk" );

static const trait_id trait_HOOVES( "HOOVES" );
static const trait_id trait_LEG_TENTACLES( "LEG_TENTACLES" );
static const trait_id trait_PADDED_FEET( "PADDED_FEET" );
static const trait_id trait_TOUGH_FEET( "TOUGH_FEET" );

TEST_CASE( "being_knocked_down_triples_movement_cost", "[move_cost][downed]" )
{
    avatar &ava = get_avatar();
    clear_avatar();

    // Put on sneakers to normalize run cost to 100
    ava.wear_item( item( "sneakers" ) );
    REQUIRE( ava.run_cost( 100 ) == 100 );

    ava.add_effect( effect_downed, 1_turns );
    CHECK( ava.run_cost( 100 ) == 300 );
    CHECK( ava.run_cost( 200 ) == 600 );
    CHECK( ava.run_cost( 300 ) == 900 );
    CHECK( ava.run_cost( 400 ) == 1200 );
}

TEST_CASE( "footwear_may_affect_movement_cost", "[move_cost][shoes]" )
{
    avatar &ava = get_avatar();
    map &here = get_map();
    clear_avatar();
    clear_map();

    // Ensure no interference from mutations
    REQUIRE( ava.mutation_value( "movecost_modifier" ) == 1 );
    REQUIRE( ava.mutation_value( "movecost_flatground_modifier" ) == 1 );
    REQUIRE( ava.mutation_value( "movecost_obstacle_modifier" ) == 1 );
    // Ensure expected base modifiers
    REQUIRE( ava.get_modifier( character_modifier_limb_run_cost_mod ) == 1 );
    REQUIRE( ava.get_modifier( character_modifier_limb_speed_movecost_mod ) == 1 );
    REQUIRE( ava.get_modifier( character_modifier_limb_footing_movecost_mod ) == 1 );

    SECTION( "without shoes" ) {
        ava.clear_worn();
        REQUIRE_FALSE( ava.is_wearing_shoes() );
        // Having no shoes adds +8 per foot to base run cost
        CHECK( ava.run_cost( 100 ) == 116 );
    }

    SECTION( "wearing sneakers" ) {
        ava.clear_worn();
        ava.wear_item( item( "sneakers" ) );
        REQUIRE( ava.is_wearing_shoes() );
        REQUIRE( ava.get_modifier( character_modifier_limb_run_cost_mod ) == Approx( 1.0 ) );
        // Sneakers eliminate the no-shoes penalty
        CHECK( ava.run_cost( 100 ) == 100 );
    }

    SECTION( "wearing swim fins" ) {
        ava.clear_worn();
        ava.wear_item( item( "swim_fins" ) );
        REQUIRE( ava.is_wearing_shoes() );
        REQUIRE( ava.get_modifier( character_modifier_limb_run_cost_mod ) == Approx( 1.11696 ) );
        // Swim fins multiply cost by 1.5
        CHECK( ava.run_cost( 100 ) == 167 );
    }

    GIVEN( "wearing rollerblades (ROLLER_INLINE)" ) {
        ava.clear_worn();
        ava.wear_item( item( "roller_blades" ) );
        REQUIRE( ava.worn_with_flag( flag_ROLLER_INLINE ) );
        REQUIRE( ava.get_modifier( character_modifier_limb_run_cost_mod ) == Approx( 1.11696 ) );
        WHEN( "on pavement and running" ) {
            ava.set_movement_mode( move_mode_run );
            here.ter_set( ava.pos(), t_pavement );
            THEN( "much faster than sneakers" ) {
                CHECK( ava.run_cost( 100 ) == 27 );
            }
        }
        WHEN( "on grass" ) {
            here.ter_set( ava.pos(), t_grass );
            THEN( "much slower than sneakers" ) {
                CHECK( ava.run_cost( 100 ) == 167 );
            }
        }
    }

    GIVEN( "wearing roller skates (ROLLER_QUAD)" ) {
        ava.clear_worn();
        ava.wear_item( item( "rollerskates" ) );
        REQUIRE( ava.worn_with_flag( flag_ROLLER_QUAD ) );
        REQUIRE( ava.get_modifier( character_modifier_limb_run_cost_mod ) == Approx( 1.11696 ) );
        WHEN( "on pavement and running" ) {
            ava.set_movement_mode( move_mode_run );
            here.ter_set( ava.pos(), t_pavement );
            THEN( "faster than sneakers" ) {
                CHECK( ava.run_cost( 100 ) == 39 );
            }
        }
        WHEN( "on grass" ) {
            here.ter_set( ava.pos(), t_grass );
            THEN( "slower than sneakers" ) {
                CHECK( ava.run_cost( 100 ) == 145 );
            }
        }
    }

    GIVEN( "wearing heelys (ROLLER_ONE)" ) {
        ava.clear_worn();
        ava.wear_item( item( "roller_shoes_on" ) );
        REQUIRE( ava.worn_with_flag( flag_ROLLER_ONE ) );
        REQUIRE( ava.get_modifier( character_modifier_limb_run_cost_mod ) == Approx( 1.0 ) );
        WHEN( "on pavement and running" ) {
            ava.set_movement_mode( move_mode_run );
            here.ter_set( ava.pos(), t_pavement );
            THEN( "slightly faster than sneakers" ) {
                CHECK( ava.run_cost( 100 ) == 42 );
            }
        }
        WHEN( "on grass" ) {
            here.ter_set( ava.pos(), t_grass );
            THEN( "slightly slower than sneakers" ) {
                CHECK( ava.run_cost( 100 ) == 110 );
            }
        }
    }
}

TEST_CASE( "mutations_may_affect_movement_cost", "[move_cost][mutation]" )
{
    avatar &ava = get_avatar();
    clear_avatar();

    // Base movement cost for relative comparisons
    // This is the speed of an unmutated character wearing sneakers on both feet
    const int base_cost = 100;

    GIVEN( "no mutations" ) {
        THEN( "wearing sneakers gives baseline 100 movement speed" ) {
            ava.clear_worn();
            ava.wear_item( item( "sneakers" ) );
            CHECK( ava.run_cost( 100 ) == Approx( base_cost ) );
        }
        THEN( "being barefoot gives a +16 movement cost penalty" ) {
            ava.clear_worn();
            CHECK( ava.run_cost( 100 ) == Approx( base_cost + 16 ) );
        }
    }

    GIVEN( "LEG_TENTACLES" ) {
        ava.toggle_trait( trait_LEG_TENTACLES );
        THEN( "barefoot penalty does not apply, but movement is slower" ) {
            ava.clear_worn();
            CHECK( ava.run_cost( 100 ) == Approx( 1.2 * base_cost ) );
        }
    }

    GIVEN( "HOOVES" ) {
        ava.toggle_trait( trait_HOOVES );
        THEN( "barefoot penalty does not apply" ) {
            ava.clear_worn();
            CHECK( ava.run_cost( 100 ) == Approx( base_cost ) );
        }
    }

    GIVEN( "TOUGH_FEET" ) {
        ava.toggle_trait( trait_TOUGH_FEET );
        THEN( "barefoot penalty does not apply" ) {
            ava.clear_worn();
            CHECK( ava.run_cost( 100 ) == Approx( base_cost ) );
        }
    }

    GIVEN( "PADDED_FEET" ) {
        ava.toggle_trait( trait_PADDED_FEET );
        THEN( "wearing sneakers gives baseline 100 movement speed" ) {
            ava.clear_worn();
            ava.wear_item( item( "sneakers" ) );
            CHECK( ava.run_cost( 100 ) == Approx( base_cost ) );
        }
        THEN( "being barefoot is faster than wearing sneakers" ) {
            ava.clear_worn();
            CHECK( ava.run_cost( 100 ) == Approx( 0.9 * base_cost ) );
        }
    }
}

TEST_CASE( "Crawl_score_effects_on_movement_cost", "[move_cost]" )
{
    // No limb damage
    GIVEN( "Character is uninjured and unencumbered" ) {
        avatar &u = get_avatar();
        clear_avatar();
        clear_map();
        u.wear_item( item( "sneakers" ) );
        u.moves = 0;

        WHEN( "is walking" ) {
            u.set_movement_mode( move_mode_walk );
            REQUIRE( u.moves == 0 );
            THEN( "no crawling modifier" ) {
                CHECK( u.run_cost( 100 ) == 100 );
                avatar_action::move( u, get_map(), point_south );
                CHECK( u.moves == -100 );
            }
        }
        WHEN( "is crouching" ) {
            u.set_movement_mode( move_mode_crouch );
            REQUIRE( u.moves == 0 );
            THEN( "no crawling modifier" ) {
                CHECK( u.run_cost( 100 ) == 200 );
                avatar_action::move( u, get_map(), point_south );
                CHECK( u.moves == -200 );
            }
        }
        WHEN( "is prone" ) {
            u.set_movement_mode( move_mode_prone );
            REQUIRE( u.moves == 0 );
            THEN( "apply crawling modifier" ) {
                CHECK( u.run_cost( 100 ) == 600 );
                avatar_action::move( u, get_map(), point_south );
                CHECK( u.moves == -600 );
            }
        }
    }
    GIVEN( "Character is uninjured and has 30 arm encumbrance" ) {
        avatar &u = get_avatar();
        clear_avatar();
        u.wear_item( item( "sneakers" ) );
        u.wear_item( item( "test_briefcase" ) );
        u.moves = 0;

        WHEN( "is walking" ) {
            u.set_movement_mode( move_mode_walk );
            REQUIRE( u.moves == 0 );
            THEN( "no crawling modifier" ) {
                CHECK( u.run_cost( 100 ) == 100 );
                avatar_action::move( u, get_map(), point_south );
                CHECK( u.moves == -100 );
            }
        }
        WHEN( "is crouching" ) {
            u.set_movement_mode( move_mode_crouch );
            REQUIRE( u.moves == 0 );
            THEN( "no crawling modifier" ) {
                CHECK( u.run_cost( 100 ) == 200 );
                avatar_action::move( u, get_map(), point_south );
                CHECK( u.moves == -200 );
            }
        }
        WHEN( "is prone" ) {
            u.set_movement_mode( move_mode_prone );
            REQUIRE( u.moves == 0 );
            THEN( "apply crawling modifier" ) {
                CHECK( u.run_cost( 100 ) == 660 );
                avatar_action::move( u, get_map(), point_south );
                CHECK( u.moves == -660 );
            }
        }
    }
    GIVEN( "Character is uninjured and has 37 encumbrance, all limbs" ) {
        avatar &u = get_avatar();
        clear_avatar();
        u.wear_item( item( "test_hazmat_suit" ) );
        u.moves = 0;

        WHEN( "is walking" ) {
            u.set_movement_mode( move_mode_walk );
            REQUIRE( u.moves == 0 );
            THEN( "no crawling modifier" ) {
                CHECK( u.run_cost( 100 ) == 154 );
                avatar_action::move( u, get_map(), point_south );
                CHECK( u.moves == -154 );
            }
        }
        WHEN( "is crouching" ) {
            u.set_movement_mode( move_mode_crouch );
            REQUIRE( u.moves == 0 );
            THEN( "no crawling modifier" ) {
                CHECK( u.run_cost( 100 ) == 309 );
                avatar_action::move( u, get_map(), point_south );
                CHECK( u.moves == -309 );
            }
        }
        WHEN( "is prone" ) {
            u.set_movement_mode( move_mode_prone );
            REQUIRE( u.moves == 0 );
            THEN( "apply crawling modifier" ) {
                CHECK( u.run_cost( 100 ) == 932 );
                avatar_action::move( u, get_map(), point_south );
                CHECK( u.moves == -932 );
            }
        }
    }
    // Damaged arm
    GIVEN( "Character has damaged arm and is unencumbered" ) {
        avatar &u = get_avatar();
        clear_avatar();
        u.wear_item( item( "sneakers" ) );
        u.set_part_hp_cur( body_part_arm_l, 10 );
        u.moves = 0;

        WHEN( "is walking" ) {
            u.set_movement_mode( move_mode_walk );
            REQUIRE( u.moves == 0 );
            THEN( "no crawling modifier" ) {
                CHECK( u.run_cost( 100 ) == 100 );
                avatar_action::move( u, get_map(), point_south );
                CHECK( u.moves == -100 );
            }
        }
        WHEN( "is crouching" ) {
            u.set_movement_mode( move_mode_crouch );
            REQUIRE( u.moves == 0 );
            THEN( "no crawling modifier" ) {
                CHECK( u.run_cost( 100 ) == 200 );
                avatar_action::move( u, get_map(), point_south );
                CHECK( u.moves == -200 );
            }
        }
        WHEN( "is prone" ) {
            u.set_movement_mode( move_mode_prone );
            REQUIRE( u.moves == 0 );
            THEN( "apply crawling modifier" ) {
                CHECK( u.run_cost( 100 ) == 802 );
                avatar_action::move( u, get_map(), point_south );
                CHECK( u.moves == -802 );
            }
        }
    }
    GIVEN( "Character has damaged arm and 30 arm encumbrance" ) {
        avatar &u = get_avatar();
        clear_avatar();
        u.wear_item( item( "sneakers" ) );
        u.wear_item( item( "test_briefcase" ) );
        u.set_part_hp_cur( body_part_arm_l, 10 );
        u.moves = 0;

        WHEN( "is walking" ) {
            u.set_movement_mode( move_mode_walk );
            REQUIRE( u.moves == 0 );
            THEN( "no crawling modifier" ) {
                CHECK( u.run_cost( 100 ) == 100 );
                avatar_action::move( u, get_map(), point_south );
                CHECK( u.moves == -100 );
            }
        }
        WHEN( "is crouching" ) {
            u.set_movement_mode( move_mode_crouch );
            REQUIRE( u.moves == 0 );
            THEN( "no crawling modifier" ) {
                CHECK( u.run_cost( 100 ) == 200 );
                avatar_action::move( u, get_map(), point_south );
                CHECK( u.moves == -200 );
            }
        }
        WHEN( "is prone" ) {
            u.set_movement_mode( move_mode_prone );
            REQUIRE( u.moves == 0 );
            THEN( "apply crawling modifier" ) {
                CHECK( u.run_cost( 100 ) == 818 );
                avatar_action::move( u, get_map(), point_south );
                CHECK( u.moves == -818 );
            }
        }
    }
    GIVEN( "Character has damaged arm and 37 encumbrance, all limbs" ) {
        avatar &u = get_avatar();
        clear_avatar();
        u.wear_item( item( "test_hazmat_suit" ) );
        u.set_part_hp_cur( body_part_arm_l, 10 );
        u.moves = 0;

        WHEN( "is walking" ) {
            u.set_movement_mode( move_mode_walk );
            REQUIRE( u.moves == 0 );
            THEN( "no crawling modifier" ) {
                CHECK( u.run_cost( 100 ) == 154 );
                avatar_action::move( u, get_map(), point_south );
                CHECK( u.moves == -154 );
            }
        }
        WHEN( "is crouching" ) {
            u.set_movement_mode( move_mode_crouch );
            REQUIRE( u.moves == 0 );
            THEN( "no crawling modifier" ) {
                CHECK( u.run_cost( 100 ) == 309 );
                avatar_action::move( u, get_map(), point_south );
                CHECK( u.moves == -309 );
            }
        }
        WHEN( "is prone" ) {
            u.set_movement_mode( move_mode_prone );
            REQUIRE( u.moves == 0 );
            THEN( "apply crawling modifier" ) {
                CHECK( u.run_cost( 100 ) == 1245 );
                avatar_action::move( u, get_map(), point_south );
                CHECK( u.moves == -1245 );
            }
        }
    }
    // Broken legs
    GIVEN( "Character has 2 broken legs and is unencumbered" ) {
        avatar &u = get_avatar();
        clear_avatar();
        u.wear_item( item( "sneakers" ) );
        u.set_part_hp_cur( body_part_leg_l, 0 );
        u.set_part_hp_cur( body_part_leg_r, 0 );
        u.moves = 0;

        // Can't walk or couch w/ broken legs
        WHEN( "is prone" ) {
            u.set_movement_mode( move_mode_prone );
            REQUIRE( u.moves == 0 );
            THEN( "apply crawling modifier" ) {
                CHECK( u.run_cost( 100 ) == 1000 );
                avatar_action::move( u, get_map(), point_south );
                CHECK( u.moves == -1000 );
            }
        }
    }
    GIVEN( "Character has 2 broken legs arm and 30 arm encumbrance" ) {
        avatar &u = get_avatar();
        clear_avatar();
        u.wear_item( item( "sneakers" ) );
        u.wear_item( item( "test_briefcase" ) );
        u.set_part_hp_cur( body_part_leg_l, 0 );
        u.set_part_hp_cur( body_part_leg_r, 0 );
        u.moves = 0;

        // Can't walk or couch w/ broken legs
        WHEN( "is prone" ) {
            u.set_movement_mode( move_mode_prone );
            REQUIRE( u.moves == 0 );
            THEN( "apply crawling modifier" ) {
                CHECK( u.run_cost( 100 ) == 1179 );
                avatar_action::move( u, get_map(), point_south );
                CHECK( u.moves == -1179 );
            }
        }
    }
    GIVEN( "Character has 2 broken legs and 37 encumbrance, all limbs" ) {
        avatar &u = get_avatar();
        clear_avatar();
        u.wear_item( item( "test_hazmat_suit" ) );
        u.set_part_hp_cur( body_part_leg_l, 0 );
        u.set_part_hp_cur( body_part_leg_r, 0 );
        u.moves = 0;

        // Can't walk or couch w/ broken legs
        WHEN( "is prone" ) {
            u.set_movement_mode( move_mode_prone );
            REQUIRE( u.moves == 0 );
            THEN( "apply crawling modifier" ) {
                CHECK( u.run_cost( 100 ) == 1561 );
                avatar_action::move( u, get_map(), point_south );
                CHECK( u.moves == -1561 );
            }
        }
    }
}