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
|
#include <sstream>
#include <string>
#include "catch/catch.hpp"
#include "game.h"
#include "monattack.h"
#include "monster.h"
#include "npc.h"
static float brute_probability( Creature &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_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 )
{
std::stringstream ss;
ss << "Details for " << dude.disp_name() << std::endl;
ss << "get_hit() == " << dude.get_hit() << std::endl;
ss << "get_hit_base() == " << dude.get_hit_base() << std::endl;
ss << "get_hit_weapon() == " << dude.get_hit_weapon( dude.weapon ) << std::endl;
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();
}
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 );
}
}
const int num_iters = 10000;
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", {}, 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", {}, 3, 8, 8, 8, 8 );
dude.weapon = item( "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", {}, 8, 10, 10, 10, 10 );
dude.weapon = item( "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", {}, 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", {}, 3, 8, 8, 8, 8 );
dude.weapon = item( "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", {}, 8, 10, 10, 10, 10 );
dude.weapon = item( "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", {}, 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", { "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", { "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", {}, 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", { "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", { "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", {}, 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", { "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", { "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 );
}
}
|