forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheffect_test.cpp
426 lines (376 loc) · 15.7 KB
/
effect_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
#include "catch/catch.hpp"
#include "effect.h"
#include <string>
#include <vector>
#include "calendar.h"
#include "type_id.h"
// Test `effect` class
// Effect initialization
// ---------------------
// Simple initialization sanity check, tests the constructor and a few accessors:
//
// effect::get_duration
// effect::get_bp
// effect::is_permanent
// effect::get_intensity
// effect::get_start_time
//
// Create an `effect` object with given parameters, and check they were initialized correctly.
static void check_effect_init( const std::string &eff_name, const time_duration &dur,
const std::string &bp_name, const bool permanent, const int intensity,
const time_point &start_time )
{
const efftype_id eff_id( eff_name );
const effect_type &type = eff_id.obj();
const bodypart_str_id bp( bp_name );
effect effect_obj( &type, dur, bp, permanent, intensity, start_time );
CHECK( dur == effect_obj.get_duration() );
CHECK( bp.id() == effect_obj.get_bp() );
CHECK( permanent == effect_obj.is_permanent() );
CHECK( intensity == effect_obj.get_intensity() );
CHECK( start_time == effect_obj.get_start_time() );
}
TEST_CASE( "effect initialization test", "[effect][init]" )
{
// "debugged" effect is defined in data/mods/TEST_DATA/effects.json
check_effect_init( "debugged", 1_days, "head", false, 5, calendar::turn_zero );
check_effect_init( "bite", 1_minutes, "torso", true, 2, calendar::turn );
check_effect_init( "grabbed", 1_turns, "arm_r", false, 1, calendar::turn + 1_hours );
}
// Effect duration
// ---------------
// effect::get_max_duration
// effect::get_duration
// effect::set_duration
// TODO:
// effect::get_start_time
// effect::mod_duration
// effect::mult_duration
//
TEST_CASE( "effect duration", "[effect][duration]" )
{
// "debugged" and "intensified" effects come from JSON effect data (data/mods/TEST_DATA/effects.json)
const efftype_id eff_id( "debugged" );
effect eff_debugged( &eff_id.obj(), 1_turns, bodypart_str_id( "bp_null" ), false, 1,
calendar::turn );
// Current duration from effect initialization
REQUIRE( eff_debugged.get_duration() == 1_turns );
REQUIRE( eff_debugged.get_max_duration() == 1_hours );
SECTION( "duration can be set to a negative value" ) {
eff_debugged.set_duration( -1_turns );
CHECK( to_turns<int>( eff_debugged.get_duration() ) == -1 );
}
SECTION( "set_duration is capped at maximum duration" ) {
eff_debugged.set_duration( 2_hours );
CHECK( eff_debugged.get_duration() == 1_hours );
}
// Example Effect (from EFFECTS_JSON.md):
//
// "id": "drunk",
// "name": [ "Tipsy", "Drunk", "Trashed", "Wasted" ],
// "max_intensity": 4,
// "apply_message": "You feel lightheaded.",
// "int_dur_factor": 1000,
//
// It has "int_dur_factor": 1000, meaning that its intensity will always be equal to its duration /
// 1000 rounded up, and it has "max_intensity": 4 meaning the highest its intensity will go is 4 at
// a duration of 3000 or higher.
SECTION( "set_duration modifies intensity if effect is duration-based" ) {
const efftype_id eff_id( "intensified" );
effect eff_intense( &eff_id.obj(), 1_turns, bodypart_str_id( "bp_null" ), false, 1,
calendar::turn );
REQUIRE( eff_intense.get_int_dur_factor() == 1_minutes );
// At zero duration, intensity is minimum (1)
eff_intense.set_duration( 0_seconds );
CHECK( eff_intense.get_intensity() == 1 );
// At duration == int_dur_factor, intensity is 2
eff_intense.set_duration( 1_minutes );
CHECK( eff_intense.get_intensity() == 2 );
// At duration == 2 * int_dur_factor, intensity is 3
eff_intense.set_duration( 2_minutes );
CHECK( eff_intense.get_intensity() == 3 );
// At duration == 3 * int_dur_factor, intensity is still 3
eff_intense.set_duration( 3_minutes );
CHECK( eff_intense.get_intensity() == 3 );
}
}
// Effect intensity
// ----------------
// effect::get_intensity
// effect::get_max_intensity
// effect::set_intensity
// effect::mod_intensity
//
// TODO:
// effect::get_dur_add_perc
// effect::get_int_dur_factor
// effect::get_int_add_val
//
TEST_CASE( "effect intensity", "[effect][intensity]" )
{
const efftype_id eff_id( "debugged" );
effect eff_debugged( &eff_id.obj(), 3_turns, bodypart_str_id( "bp_null" ), false, 1,
calendar::turn );
REQUIRE( eff_debugged.get_intensity() == 1 );
REQUIRE( eff_debugged.get_max_intensity() == 10 );
SECTION( "intensity cannot be set less than 1" ) {
eff_debugged.set_intensity( 0 );
CHECK( eff_debugged.get_intensity() == 1 );
eff_debugged.set_intensity( -1 );
CHECK( eff_debugged.get_intensity() == 1 );
}
SECTION( "intensity cannot be set greater than maximum" ) {
// These don't go to 11
eff_debugged.set_intensity( 11 );
CHECK( eff_debugged.get_intensity() == 10 );
// or 9+2
eff_debugged.set_intensity( 9 );
eff_debugged.mod_intensity( 2 );
CHECK( eff_debugged.get_intensity() == 10 );
}
// From EFFECTS_JSON.md:
// max_intensity: Used for many later fields, defaults to 1
// max_effective_intensity: How many intensity levels will apply effects. Other intensity levels
// will only increase duration.
//
// If "max_intensity" > 1 and the number of entries in "name" >= "max_intensity" then it will
// attempt to use the proper intensity name.
}
// Effect decay
// ------------
// effect::decay
//
TEST_CASE( "effect decay", "[effect][decay]" )
{
const efftype_id eff_id( "debugged" );
std::vector<efftype_id> rem_ids;
std::vector<bodypart_id> rem_bps;
SECTION( "decay reduces effect duration by 1 turn" ) {
effect eff_debugged( &eff_id.obj(), 2_turns, bodypart_str_id( "bp_null" ), false, 1,
calendar::turn );
// Ensure it will last 2 turns, and is not permanent/paused
REQUIRE( to_turns<int>( eff_debugged.get_duration() ) == 2 );
REQUIRE_FALSE( eff_debugged.is_permanent() );
// First decay - 1 turn left
eff_debugged.decay( rem_ids, rem_bps, calendar::turn, false );
CHECK( to_turns<int>( eff_debugged.get_duration() ) == 1 );
// Effect not removed
CHECK( rem_ids.empty() );
CHECK( rem_bps.empty() );
// Second decay - 0 turns left
eff_debugged.decay( rem_ids, rem_bps, calendar::turn, false );
CHECK( to_turns<int>( eff_debugged.get_duration() ) == 0 );
// Effect still not removed
CHECK( rem_ids.empty() );
CHECK( rem_bps.empty() );
// Third decay - 0 turns left
eff_debugged.decay( rem_ids, rem_bps, calendar::turn, false );
CHECK( to_turns<int>( eff_debugged.get_duration() ) == 0 );
// Effect is removed
CHECK( rem_ids.size() == 1 );
CHECK( rem_bps.size() == 1 );
// Effect ID and body part are pushed to the arrays
CHECK( rem_ids.front() == eff_debugged.get_id() );
CHECK( rem_bps.front() == bodypart_id( "bp_null" ) );
}
SECTION( "decay does not reduce paused/permanent effect duration" ) {
effect eff_debugged( &eff_id.obj(), 2_turns, bodypart_str_id( "bp_null" ), true, 1,
calendar::turn );
// Ensure it will last 2 turns, and is permanent/paused
REQUIRE( to_turns<int>( eff_debugged.get_duration() ) == 2 );
REQUIRE( eff_debugged.is_permanent() );
// Decay twice - should have no effect on duration
eff_debugged.decay( rem_ids, rem_bps, calendar::turn, false );
CHECK( to_turns<int>( eff_debugged.get_duration() ) == 2 );
eff_debugged.decay( rem_ids, rem_bps, calendar::turn, false );
CHECK( to_turns<int>( eff_debugged.get_duration() ) == 2 );
}
// TODO:
// When intensity > 1
// - and duration < max_duration
// - and int_decay_tick is set (from effect JSON)
// - and time % decay_tick == 0
// Then:
// - add int_decay_step to intensity (default -1)
}
// Effect description
// ------------------
// effect::disp_short_desc
//
TEST_CASE( "display short description", "[effect][desc]" )
{
const efftype_id eff_id( "debugged" );
const bodypart_str_id arm_r( "arm_r" );
effect eff_debugged( &eff_id.obj(), 1_turns, arm_r, false, 1, calendar::turn );
// TODO: Determine a case where `reduced` (true/false) makes a difference
CHECK( eff_debugged.disp_short_desc( false ) == "You have been debugged!\n"
"Everything is working perfectly now." );
CHECK( eff_debugged.disp_short_desc( true ) == "You have been debugged!\n"
"Everything is working perfectly now." );
}
// effect::disp_name
// effect::get_speed_name
//
// From EFFECTS_JSON.md:
//
// "name":
// """If "max_intensity" > 1 and the number of entries in "name" >= "max_intensity" then it will
// attempt to use the proper intensity name."""
//
// "speed_name":
// """This is the value used in the list of modifiers on a player's speed. It will default to the
// first entry in "name" if it doesn't exist, and if neither one exists or if "speed_name" is the
// empty string (""), then it will not appear in the list of modifiers on the players speed (though
// the effect might still have an effect)."""
//
TEST_CASE( "effect display and speed name may vary with intensity",
"[effect][intensity][disp_name][speed_name]" )
{
GIVEN( "effect with names for each intensity level" ) {
// "intensified" effect (data/mods/TEST_DATA/effects.json) has 3 names:
// "name": [ "Whoa", "Wut?", "Wow!" ]
// "max_intensity": 3
const efftype_id eid_intensified( "intensified" );
effect eff_intense( &eid_intensified.obj(), 1_turns, bodypart_str_id( "bp_null" ), false, 1,
calendar::turn );
REQUIRE( eff_intense.get_max_intensity() == 3 );
// use_name_ints is true if there are names for each intensity
const effect_type *etype_intense = eff_intense.get_effect_type();
REQUIRE( etype_intense->use_name_ints() );
THEN( "disp_name has the name for the current intensity" ) {
eff_intense.set_intensity( 1 );
CHECK( eff_intense.disp_name() == "Whoa" );
eff_intense.set_intensity( 2 );
CHECK( eff_intense.disp_name() == "Wut?" );
eff_intense.set_intensity( 3 );
CHECK( eff_intense.disp_name() == "Wow!" );
}
// For this effect, "speed_name" is not explicitly defined, so
// get_speed_name is the same as disp_name for each intensity.
THEN( "get_speed_name has the name for the current intensity" ) {
eff_intense.set_intensity( 1 );
CHECK( eff_intense.get_speed_name() == "Whoa" );
eff_intense.set_intensity( 2 );
CHECK( eff_intense.get_speed_name() == "Wut?" );
eff_intense.set_intensity( 3 );
CHECK( eff_intense.get_speed_name() == "Wow!" );
}
}
GIVEN( "effect with a single name and explicit speed_name" ) {
// "debugged" effect has only one name and a speed_name:
// "name": [ "Debugged" ]
// "speed_name": "Optimized"
const efftype_id eid_debugged( "debugged" );
effect eff_debugged( &eid_debugged.obj(), 1_minutes, bodypart_str_id( "bp_null" ), false, 1,
calendar::turn );
THEN( "disp_name has the name, and current intensity if > 1" ) {
eff_debugged.set_intensity( 1 );
CHECK( eff_debugged.disp_name() == "Debugged" );
eff_debugged.set_intensity( 2 );
CHECK( eff_debugged.disp_name() == "Debugged [2]" );
eff_debugged.set_intensity( 3 );
CHECK( eff_debugged.disp_name() == "Debugged [3]" );
}
THEN( "get_speed_name is the speed name for all intensity levels" ) {
eff_debugged.set_intensity( 1 );
CHECK( eff_debugged.get_speed_name() == "Optimized" );
eff_debugged.set_intensity( 2 );
CHECK( eff_debugged.get_speed_name() == "Optimized" );
eff_debugged.set_intensity( 3 );
CHECK( eff_debugged.get_speed_name() == "Optimized" );
}
}
}
// Effect permanence
// -----------------
// effect::is_permanent
// effect::pause_effect
// effect::unpause_effect
//
TEST_CASE( "effect permanence", "[effect][permanent]" )
{
const efftype_id eff_id( "grabbed" );
const bodypart_str_id arm_r( "arm_r" );
// Grab right arm, not permanent
effect eff_grabbed( &eff_id.obj(), 1_turns, arm_r, false, 1, calendar::turn );
CHECK_FALSE( eff_grabbed.is_permanent() );
// Pause makes it permanent
eff_grabbed.pause_effect();
CHECK( eff_grabbed.is_permanent() );
// Pause again does nothing
eff_grabbed.pause_effect();
CHECK( eff_grabbed.is_permanent() );
// Unpause removes permanence
eff_grabbed.unpause_effect();
CHECK_FALSE( eff_grabbed.is_permanent() );
// Unpause again does nothing
eff_grabbed.unpause_effect();
CHECK_FALSE( eff_grabbed.is_permanent() );
}
// Effect body part
// ----------------
// effect::set_bp
// effect::get_bp
//
TEST_CASE( "effect body part", "[effect][bodypart]" )
{
const efftype_id eff_id( "grabbed" );
const bodypart_str_id arm_r( "arm_r" );
const bodypart_str_id arm_l( "arm_l" );
// Grab right arm, initially
effect eff_grabbed( &eff_id.obj(), 1_turns, arm_r, false, 1, calendar::turn );
CHECK( eff_grabbed.get_bp() == arm_r.id() );
// Switch to left arm
eff_grabbed.set_bp( arm_l );
CHECK( eff_grabbed.get_bp() == arm_l.id() );
CHECK_FALSE( eff_grabbed.get_bp() == arm_r.id() );
// Back to right arm
eff_grabbed.set_bp( arm_r );
CHECK( eff_grabbed.get_bp() == arm_r.id() );
CHECK_FALSE( eff_grabbed.get_bp() == arm_l.id() );
}
// Effect modifiers
// ----------------
// TODO:
// effect::get_avg_mod
// effect::get_min_val
// effect::get_max_val
// effect::get_sizing
// effect::get_percentage
//
TEST_CASE( "effect modifiers", "[effect][modifier]" )
{
SECTION( "base_mods apply equally for any intensity" ) {
const efftype_id eff_id( "debugged" );
effect eff_debugged( &eff_id.obj(), 1_minutes, bodypart_str_id( "bp_null" ), false, 1,
calendar::turn );
CHECK( eff_debugged.get_mod( "STR" ) == 1 );
CHECK( eff_debugged.get_mod( "DEX" ) == 2 );
CHECK( eff_debugged.get_mod( "PER" ) == 3 );
CHECK( eff_debugged.get_mod( "INT" ) == 4 );
CHECK( eff_debugged.get_amount( "HEAL_RATE" ) == 2 );
CHECK( eff_debugged.get_amount( "HEAL_HEAD" ) == 200 );
CHECK( eff_debugged.get_amount( "HEAL_TORSO" ) == 150 );
}
// Scaling mods - vary based on intensity
SECTION( "scaling_mods vary based on intensity" ) {
const efftype_id eff_id( "intensified" );
effect eff_intense( &eff_id.obj(), 1_turns, bodypart_str_id( "bp_null" ), false, 1,
calendar::turn );
REQUIRE( eff_intense.get_max_intensity() == 3 );
// Subtracts 1 INT and 2 PER per intensity greater than 1
eff_intense.set_intensity( 1 );
CHECK( eff_intense.get_mod( "INT" ) == 0 );
CHECK( eff_intense.get_mod( "PER" ) == 0 );
eff_intense.set_intensity( 2 );
CHECK( eff_intense.get_mod( "INT" ) == -1 );
CHECK( eff_intense.get_mod( "PER" ) == -2 );
eff_intense.set_intensity( 3 );
CHECK( eff_intense.get_mod( "INT" ) == -2 );
CHECK( eff_intense.get_mod( "PER" ) == -4 );
// Max intensity is 3
eff_intense.set_intensity( 4 );
CHECK( eff_intense.get_mod( "INT" ) == -2 );
CHECK( eff_intense.get_mod( "PER" ) == -4 );
}
}