-
Notifications
You must be signed in to change notification settings - Fork 280
/
effective_dps_test.cpp
226 lines (185 loc) · 8.55 KB
/
effective_dps_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
#include "catch/catch.hpp"
#include <algorithm>
#include <cstdlib>
#include <memory>
#include "avatar.h"
#include "game.h"
#include "item.h"
#include "melee.h"
#include "monster.h"
#include "player_helpers.h"
#include "sounds.h"
#include "state_helpers.h"
#include "ret_val.h"
#include "type_id.h"
struct itype;
// Run a large number of trials of a player attacking a monster with a given weapon,
// and return the average damage done per second.
static double weapon_dps_trials( avatar &attacker, monster &defender, item &weapon )
{
constexpr int trials = 1000;
int total_damage = 0;
int total_moves = 0;
clear_character( attacker );
REQUIRE( attacker.can_wield( weapon ).success() );
melee::clear_stats();
melee_statistic_data melee_stats = melee::get_stats();
// rerun the trials in groups of 1000 until 100 crits occur
for( int i = 0; i < 10 && melee_stats.actual_crit_count < 100;
i++, melee_stats = melee::get_stats() ) {
for( int j = 0; j < trials; j++ ) {
// Reset and re-wield weapon before each attack to prevent skill-up during trials
clear_character( attacker );
attacker.wield( item::spawn( weapon ) );
// Verify that wielding worked (and not e.g. using martial arts instead)
REQUIRE( attacker.primary_weapon().type == weapon.type );
int before_moves = attacker.get_moves();
// Keep the defender at maximum health
const int starting_hp = defender.get_hp_max();
defender.set_hp( starting_hp );
// Attack once
attacker.melee_attack( defender, false );
// Tally total damage and moves
total_damage += std::max( 0, starting_hp - defender.get_hp() );
total_moves += std::abs( attacker.get_moves() - before_moves );
// Every hit or miss enqueues a new sound
// Ideally, we'd have sound vector get cleared after every test, but it's not easy
sounds::reset_sounds();
}
}
// Scale damage-per-move to damage-per-second
// 1 second == 100 moves
return 100.0f * total_damage / total_moves;
}
// Compare actual DPS with estimated effective DPS for an attacker/defender/weapon combo.
static void check_actual_dps( avatar &attacker, monster &defender, item &weapon )
{
clear_character( attacker );
double expect_dps = weapon.effective_dps( attacker, defender );
double actual_dps = weapon_dps_trials( attacker, defender, weapon );
CHECK( actual_dps == Approx( expect_dps ).epsilon( 0.2f ) );
}
static void check_accuracy_dps( avatar &attacker, monster &defender, item &wpn1, item &wpn2,
item &wpn3 )
{
clear_character( attacker );
melee::clear_stats();
double dps_wpn1 = weapon_dps_trials( attacker, defender, wpn1 );
const melee_statistic_data wpn1_stats = melee::get_stats();
melee::clear_stats();
double dps_wpn2 = weapon_dps_trials( attacker, defender, wpn2 );
const melee_statistic_data wpn2_stats = melee::get_stats();
melee::clear_stats();
double dps_wpn3 = weapon_dps_trials( attacker, defender, wpn3 );
const melee_statistic_data wpn3_stats = melee::get_stats();
REQUIRE( wpn1_stats.hit_count > 0 );
REQUIRE( wpn2_stats.hit_count > 0 );
REQUIRE( wpn3_stats.hit_count > 0 );
CAPTURE( wpn1_stats.attack_count );
CAPTURE( wpn2_stats.attack_count );
CAPTURE( wpn3_stats.attack_count );
double wpn1_hit_rate = static_cast<double>( wpn1_stats.hit_count ) / wpn1_stats.attack_count;
double wpn2_hit_rate = static_cast<double>( wpn2_stats.hit_count ) / wpn2_stats.attack_count;
double wpn3_hit_rate = static_cast<double>( wpn3_stats.hit_count ) / wpn3_stats.attack_count;
CHECK( wpn2_hit_rate > wpn1_hit_rate );
CHECK( wpn3_hit_rate > wpn2_hit_rate );
CHECK( dps_wpn2 > dps_wpn1 );
CHECK( dps_wpn3 > dps_wpn2 );
}
TEST_CASE( "effective damage per second", "[effective][dps]" )
{
clear_all_state();
avatar &dummy = g->u;
clear_character( dummy );
item &clumsy_sword = *item::spawn_temporary( "test_clumsy_sword" );
item &normal_sword = *item::spawn_temporary( "test_normal_sword" );
item &good_sword = *item::spawn_temporary( "test_balanced_sword" );
SECTION( "against a debug monster with no armor or dodge" ) {
monster mummy( mtype_id( "debug_mon" ) );
CHECK( clumsy_sword.effective_dps( dummy, mummy ) == Approx( 25.0f ).epsilon( 0.15f ) );
CHECK( good_sword.effective_dps( dummy, mummy ) == Approx( 38.0f ).epsilon( 0.15f ) );
}
SECTION( "against an agile target" ) {
monster smoker( mtype_id( "mon_zombie_smoker" ) );
REQUIRE( smoker.get_dodge() >= 4 );
CHECK( clumsy_sword.effective_dps( dummy, smoker ) == Approx( 11.0f ).epsilon( 0.15f ) );
CHECK( good_sword.effective_dps( dummy, smoker ) == Approx( 25.0f ).epsilon( 0.15f ) );
}
SECTION( "against an armored target" ) {
monster soldier( mtype_id( "mon_zombie_soldier" ) );
CHECK( clumsy_sword.effective_dps( dummy, soldier ) == Approx( 11.0f ).epsilon( 0.15f ) );
CHECK( good_sword.effective_dps( dummy, soldier ) == Approx( 19.0f ).epsilon( 0.15f ) );
}
SECTION( "effect of STR and DEX on damage per second" ) {
monster mummy( mtype_id( "debug_mon" ) );
SECTION( "STR 6, DEX 6" ) {
dummy.str_max = 6;
dummy.dex_max = 6;
CHECK( clumsy_sword.effective_dps( dummy, mummy ) == Approx( 20.0f ).epsilon( 0.15f ) );
CHECK( normal_sword.effective_dps( dummy, mummy ) == Approx( 26.0f ).epsilon( 0.15f ) );
CHECK( good_sword.effective_dps( dummy, mummy ) == Approx( 33.0f ).epsilon( 0.15f ) );
}
SECTION( "STR 8, DEX 10" ) {
dummy.str_max = 8;
dummy.dex_max = 10;
CHECK( clumsy_sword.effective_dps( dummy, mummy ) == Approx( 25.0f ).epsilon( 0.15f ) );
CHECK( normal_sword.effective_dps( dummy, mummy ) == Approx( 32.0f ).epsilon( 0.15f ) );
CHECK( good_sword.effective_dps( dummy, mummy ) == Approx( 38.0f ).epsilon( 0.15f ) );
}
SECTION( "STR 10, DEX 10" ) {
dummy.str_max = 10;
dummy.dex_max = 10;
CHECK( clumsy_sword.effective_dps( dummy, mummy ) == Approx( 27.0f ).epsilon( 0.15f ) );
CHECK( normal_sword.effective_dps( dummy, mummy ) == Approx( 34.0f ).epsilon( 0.15f ) );
CHECK( good_sword.effective_dps( dummy, mummy ) == Approx( 41.0f ).epsilon( 0.15f ) );
}
}
}
TEST_CASE( "effective vs actual damage per second", "[actual][dps][!mayfail]" )
{
clear_all_state();
avatar &dummy = g->u;
clear_character( dummy );
monster soldier( mtype_id( "mon_zombie_soldier" ) );
monster smoker( mtype_id( "mon_zombie_smoker" ) );
monster survivor( mtype_id( "mon_zombie_survivor" ) );
item &clumsy_sword = *item::spawn_temporary( "test_clumsy_sword" );
item &normal_sword = *item::spawn_temporary( "test_normal_sword" );
item &good_sword = *item::spawn_temporary( "test_balanced_sword" );
SECTION( "soldier zombie" ) {
check_actual_dps( dummy, soldier, clumsy_sword );
check_actual_dps( dummy, soldier, normal_sword );
check_actual_dps( dummy, soldier, good_sword );
}
SECTION( "smoker zombie" ) {
check_actual_dps( dummy, smoker, clumsy_sword );
check_actual_dps( dummy, smoker, normal_sword );
check_actual_dps( dummy, smoker, good_sword );
}
SECTION( "survivor zombie" ) {
check_actual_dps( dummy, survivor, clumsy_sword );
check_actual_dps( dummy, survivor, normal_sword );
check_actual_dps( dummy, survivor, good_sword );
}
}
TEST_CASE( "accuracy increases success", "[accuracy][dps]" )
{
clear_all_state();
avatar &dummy = g->u;
clear_character( dummy );
monster soldier( mtype_id( "mon_zombie_soldier" ) );
monster smoker( mtype_id( "mon_zombie_smoker" ) );
monster survivor( mtype_id( "mon_zombie_survivor" ) );
item &clumsy_sword = *item::spawn_temporary( "test_clumsy_sword" );
item &normal_sword = *item::spawn_temporary( "test_normal_sword" );
item &good_sword = *item::spawn_temporary( "test_balanced_sword" );
SECTION( "soldier zombie" ) {
check_accuracy_dps( dummy, soldier, clumsy_sword, normal_sword, good_sword );
}
SECTION( "smoker zombie" ) {
check_accuracy_dps( dummy, smoker, clumsy_sword, normal_sword, good_sword );
}
SECTION( "survivor zombie" ) {
check_accuracy_dps( dummy, survivor, clumsy_sword, normal_sword, good_sword );
}
}