-
Notifications
You must be signed in to change notification settings - Fork 280
/
melee_test.cpp
281 lines (242 loc) · 10.6 KB
/
melee_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
#include "catch/catch.hpp"
#include <cstddef>
#include <sstream>
#include <string>
#include "creature.h"
#include "game_constants.h"
#include "item.h"
#include "itype.h"
#include "melee.h"
#include "monattack.h"
#include "monster.h"
#include "npc.h"
#include "player.h"
#include "point.h"
#include "type_id.h"
static float brute_probability( monster &attacker, Creature &target, const size_t iters )
{
// Note: not using deal_melee_attack because it trains dodge, which causes problems here
size_t hits = 0;
for( size_t i = 0; i < iters; i++ ) {
const int spread = attacker.hit_roll() - target.dodge_roll();
if( spread > 0 ) {
hits++;
}
}
return static_cast<float>( hits ) / iters;
}
static float brute_probability( player &attacker, Creature &target, const size_t iters )
{
const item &weapon = attacker.primary_weapon();
const attack_statblock &attack = melee::default_attack( weapon );
size_t hits = 0;
for( size_t i = 0; i < iters; i++ ) {
const int spread = attacker.hit_roll( weapon, attack ) - target.dodge_roll();
if( spread > 0 ) {
hits++;
}
}
return static_cast<float>( hits ) / iters;
}
static float brute_special_probability( monster &attacker, Creature &target, const size_t iters )
{
size_t hits = 0;
for( size_t i = 0; i < iters; i++ ) {
if( !mattack::dodge_check( &attacker, &target ) ) {
hits++;
}
}
return static_cast<float>( hits ) / iters;
}
static std::string full_attack_details( const player &dude )
{
const item &weapon = dude.primary_weapon();
const attack_statblock &attack = melee::default_attack( weapon );
std::stringstream ss;
ss << "Details for " << dude.disp_name() << '\n';
ss << "get_hit() == " << dude.get_hit() << '\n';
ss << "get_melee() == " << dude.get_melee() << '\n';
ss << "get_hit_weapon() == " << dude.get_hit_weapon( weapon, attack ) << '\n';
return ss.str();
}
inline std::string percent_string( const float f )
{
// Using stringstream for prettier precision printing
std::stringstream ss;
ss << 100.0f * f << "%";
return ss.str();
}
static void check_near( float prob, const float expected, const float tolerance )
{
const float low = expected - tolerance;
const float high = expected + tolerance;
THEN( "The chance to hit is between " + percent_string( low ) +
" and " + percent_string( high ) ) {
REQUIRE( prob > low );
REQUIRE( prob < high );
}
}
static const int num_iters = 10000;
static constexpr tripoint dude_pos( HALF_MAPSIZE_X, HALF_MAPSIZE_Y, 0 );
TEST_CASE( "Character attacking a zombie", "[.melee]" )
{
monster zed( mtype_id( "mon_zombie" ) );
INFO( "Zombie has get_dodge() == " + std::to_string( zed.get_dodge() ) );
SECTION( "8/8/8/8, no skills, unarmed" ) {
standard_npc dude( "TestCharacter", dude_pos, {}, 0, 8, 8, 8, 8 );
const float prob = brute_probability( dude, zed, num_iters );
INFO( full_attack_details( dude ) );
check_near( prob, 0.6f, 0.1f );
}
SECTION( "8/8/8/8, 3 all skills, two-by-four" ) {
standard_npc dude( "TestCharacter", dude_pos, {}, 3, 8, 8, 8, 8 );
dude.set_primary_weapon( item::spawn( "2x4" ) );
const float prob = brute_probability( dude, zed, num_iters );
INFO( full_attack_details( dude ) );
check_near( prob, 0.8f, 0.05f );
}
SECTION( "10/10/10/10, 8 all skills, katana" ) {
standard_npc dude( "TestCharacter", dude_pos, {}, 8, 10, 10, 10, 10 );
dude.set_primary_weapon( item::spawn( "katana" ) );
const float prob = brute_probability( dude, zed, num_iters );
INFO( full_attack_details( dude ) );
check_near( prob, 0.975f, 0.025f );
}
}
TEST_CASE( "Character attacking a manhack", "[.melee]" )
{
monster manhack( mtype_id( "mon_manhack" ) );
INFO( "Manhack has get_dodge() == " + std::to_string( manhack.get_dodge() ) );
SECTION( "8/8/8/8, no skills, unarmed" ) {
standard_npc dude( "TestCharacter", dude_pos, {}, 0, 8, 8, 8, 8 );
const float prob = brute_probability( dude, manhack, num_iters );
INFO( full_attack_details( dude ) );
check_near( prob, 0.2f, 0.05f );
}
SECTION( "8/8/8/8, 3 all skills, two-by-four" ) {
standard_npc dude( "TestCharacter", dude_pos, {}, 3, 8, 8, 8, 8 );
dude.set_primary_weapon( item::spawn( "2x4" ) );
const float prob = brute_probability( dude, manhack, num_iters );
INFO( full_attack_details( dude ) );
check_near( prob, 0.4f, 0.05f );
}
SECTION( "10/10/10/10, 8 all skills, katana" ) {
standard_npc dude( "TestCharacter", dude_pos, {}, 8, 10, 10, 10, 10 );
dude.set_primary_weapon( item::spawn( "katana" ) );
const float prob = brute_probability( dude, manhack, num_iters );
INFO( full_attack_details( dude ) );
check_near( prob, 0.7f, 0.05f );
}
}
TEST_CASE( "Zombie attacking a character", "[.melee]" )
{
monster zed( mtype_id( "mon_zombie" ) );
INFO( "Zombie has get_hit() == " + std::to_string( zed.get_hit() ) );
SECTION( "8/8/8/8, no skills, unencumbered" ) {
standard_npc dude( "TestCharacter", dude_pos, {}, 0, 8, 8, 8, 8 );
const float prob = brute_probability( zed, dude, num_iters );
INFO( "Has get_dodge() == " + std::to_string( dude.get_dodge() ) );
THEN( "Character has no significant dodge bonus or penalty" ) {
REQUIRE( dude.get_dodge_bonus() < 0.5f );
REQUIRE( dude.get_dodge_bonus() > -0.5f );
}
THEN( "Character's dodge skill is roughly equal to zombie's attack skill" ) {
REQUIRE( dude.get_dodge() < zed.get_hit() + 0.5f );
REQUIRE( dude.get_dodge() > zed.get_hit() - 0.5f );
}
check_near( prob, 0.5f, 0.05f );
}
SECTION( "10/10/10/10, 3 all skills, good cotton armor" ) {
standard_npc dude( "TestCharacter", dude_pos,
{ "hoodie", "jeans", "long_underpants", "long_undertop", "longshirt" },
3, 10, 10, 10, 10 );
const float prob = brute_probability( zed, dude, num_iters );
INFO( "Has get_dodge() == " + std::to_string( dude.get_dodge() ) );
check_near( prob, 0.2f, 0.05f );
}
SECTION( "10/10/10/10, 8 all skills, survivor suit" ) {
standard_npc dude( "TestCharacter", dude_pos, { "survivor_suit" }, 8, 10, 10, 10, 10 );
const float prob = brute_probability( zed, dude, num_iters );
INFO( "Has get_dodge() == " + std::to_string( dude.get_dodge() ) );
check_near( prob, 0.025f, 0.0125f );
}
}
TEST_CASE( "Manhack attacking a character", "[.melee]" )
{
monster manhack( mtype_id( "mon_manhack" ) );
INFO( "Manhack has get_hit() == " + std::to_string( manhack.get_hit() ) );
SECTION( "8/8/8/8, no skills, unencumbered" ) {
standard_npc dude( "TestCharacter", dude_pos, {}, 0, 8, 8, 8, 8 );
const float prob = brute_probability( manhack, dude, num_iters );
INFO( "Has get_dodge() == " + std::to_string( dude.get_dodge() ) );
THEN( "Character has no significant dodge bonus or penalty" ) {
REQUIRE( dude.get_dodge_bonus() < 0.5f );
REQUIRE( dude.get_dodge_bonus() > -0.5f );
}
check_near( prob, 0.9f, 0.05f );
}
SECTION( "10/10/10/10, 3 all skills, good cotton armor" ) {
standard_npc dude( "TestCharacter", dude_pos,
{ "hoodie", "jeans", "long_underpants", "long_undertop", "longshirt" },
3, 10, 10, 10, 10 );
const float prob = brute_probability( manhack, dude, num_iters );
INFO( "Has get_dodge() == " + std::to_string( dude.get_dodge() ) );
check_near( prob, 0.6f, 0.05f );
}
SECTION( "10/10/10/10, 8 all skills, survivor suit" ) {
standard_npc dude( "TestCharacter", dude_pos, { "survivor_suit" }, 8, 10, 10, 10, 10 );
const float prob = brute_probability( manhack, dude, num_iters );
INFO( "Has get_dodge() == " + std::to_string( dude.get_dodge() ) );
check_near( prob, 0.25f, 0.05f );
}
}
TEST_CASE( "Hulk smashing a character", "[.], [melee], [monattack]" )
{
monster zed( mtype_id( "mon_zombie_hulk" ) );
INFO( "Hulk has get_hit() == " + std::to_string( zed.get_hit() ) );
SECTION( "8/8/8/8, no skills, unencumbered" ) {
standard_npc dude( "TestCharacter", dude_pos, {}, 0, 8, 8, 8, 8 );
const float prob = brute_special_probability( zed, dude, num_iters );
INFO( "Has get_dodge() == " + std::to_string( dude.get_dodge() ) );
THEN( "Character has no significant dodge bonus or penalty" ) {
REQUIRE( dude.get_dodge_bonus() < 0.5f );
REQUIRE( dude.get_dodge_bonus() > -0.5f );
}
check_near( prob, 0.95f, 0.05f );
}
SECTION( "10/10/10/10, 3 all skills, good cotton armor" ) {
standard_npc dude( "TestCharacter", dude_pos,
{ "hoodie", "jeans", "long_underpants", "long_undertop", "longshirt" },
3, 10, 10, 10, 10 );
const float prob = brute_special_probability( zed, dude, num_iters );
INFO( "Has get_dodge() == " + std::to_string( dude.get_dodge() ) );
check_near( prob, 0.75f, 0.05f );
}
SECTION( "10/10/10/10, 8 all skills, survivor suit" ) {
standard_npc dude( "TestCharacter", dude_pos, { "survivor_suit" }, 8, 10, 10, 10, 10 );
const float prob = brute_special_probability( zed, dude, num_iters );
INFO( "Has get_dodge() == " + std::to_string( dude.get_dodge() ) );
check_near( prob, 0.2f, 0.05f );
}
}
TEST_CASE( "Character selects best attack against creature", "[melee]" )
{
SECTION( "Monster with huge bash armor" ) {
monster target( mtype_id( "mon_test_bash" ) );
standard_npc dude( "TestCharacter", dude_pos, {}, 0, 8, 8, 8, 8 );
dude.set_primary_weapon( item::spawn( "test_lucern_hammer" ) );
const item &weapon = dude.primary_weapon();
const attack_statblock &attack = melee::pick_attack( dude, weapon, target );
REQUIRE( weapon.type->attacks.count( "THRUST" ) == 1 );
CHECK( attack.damage == weapon.type->attacks.at( "THRUST" ).damage );
}
SECTION( "Monster with huge stab armor" ) {
monster target( mtype_id( "mon_test_stab" ) );
standard_npc dude( "TestCharacter", dude_pos, {}, 0, 8, 8, 8, 8 );
dude.set_primary_weapon( item::spawn( "test_lucern_hammer" ) );
const item &weapon = dude.primary_weapon();
const attack_statblock &attack = melee::pick_attack( dude, weapon, target );
REQUIRE( weapon.type->attacks.count( "BASH" ) == 1 );
CHECK( attack.damage == weapon.type->attacks.at( "BASH" ).damage );
}
}