Skip to content

Commit

Permalink
Set standard_npc position at construction time
Browse files Browse the repository at this point in the history
This allows tests to avoid calling set_pos on such, which is not safe,
since it assumes the npc is a 'real' one.
  • Loading branch information
jbytheway committed Jan 11, 2020
1 parent 602e620 commit be07a69
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 41 deletions.
20 changes: 10 additions & 10 deletions src/dump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,16 @@ bool game::dump_stats( const std::string &what, dump_mode mode,
int scol = 0; // sorting column

std::map<std::string, standard_npc> test_npcs;
test_npcs[ "S1" ] = standard_npc( "S1", { "gloves_survivor", "mask_lsurvivor" }, 4, 8, 10, 8,
10 /* DEX 10, PER 10 */ );
test_npcs[ "S2" ] = standard_npc( "S2", { "gloves_fingerless", "sunglasses" }, 4, 8, 8, 8,
10 /* PER 10 */ );
test_npcs[ "S3" ] = standard_npc( "S3", { "gloves_plate", "helmet_plate" }, 4, 10, 8, 8,
8 /* STAT 10 */ );
test_npcs[ "S4" ] = standard_npc( "S4", {}, 0, 8, 10, 8, 10 /* DEX 10, PER 10 */ );
test_npcs[ "S5" ] = standard_npc( "S5", {}, 4, 8, 10, 8, 10 /* DEX 10, PER 10 */ );
test_npcs[ "S6" ] = standard_npc( "S6", { "gloves_hsurvivor", "mask_hsurvivor" }, 4, 8, 10, 8,
10 /* DEX 10, PER 10 */ );
test_npcs[ "S1" ] = standard_npc( "S1", { 0, 0, 2 }, { "gloves_survivor", "mask_lsurvivor" },
4, 8, 10, 8, 10 /* DEX 10, PER 10 */ );
test_npcs[ "S2" ] = standard_npc( "S2", { 0, 0, 3 }, { "gloves_fingerless", "sunglasses" },
4, 8, 8, 8, 10 /* PER 10 */ );
test_npcs[ "S3" ] = standard_npc( "S3", { 0, 0, 4 }, { "gloves_plate", "helmet_plate" },
4, 10, 8, 8, 8 /* STAT 10 */ );
test_npcs[ "S4" ] = standard_npc( "S4", { 0, 0, 5 }, {}, 0, 8, 10, 8, 10 /* DEX 10, PER 10 */ );
test_npcs[ "S5" ] = standard_npc( "S5", { 0, 0, 6 }, {}, 4, 8, 10, 8, 10 /* DEX 10, PER 10 */ );
test_npcs[ "S6" ] = standard_npc( "S6", { 0, 0, 7 }, { "gloves_hsurvivor", "mask_hsurvivor" },
4, 8, 10, 8, 10 /* DEX 10, PER 10 */ );

std::map<std::string, item> test_items;
test_items[ "G1" ] = item( "glock_19" ).ammo_set( "9mm" );
Expand Down
4 changes: 2 additions & 2 deletions src/mattack_actors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -537,9 +537,9 @@ void gun_actor::shoot( monster &z, Creature &target, const gun_mode_id &mode ) c
return;
}

standard_npc tmp( _( "The " ) + z.name(), {}, 8, fake_str, fake_dex, fake_int, fake_per );
standard_npc tmp( _( "The " ) + z.name(), z.pos(), {}, 8,
fake_str, fake_dex, fake_int, fake_per );
tmp.set_fake( true );
tmp.setpos( z.pos() );
tmp.set_attitude( z.friendly ? NPCATT_FOLLOW : NPCATT_KILL );
tmp.recoil = 0; // no need to aim

Expand Down
5 changes: 3 additions & 2 deletions src/npc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,12 @@ npc::npc()
}
}

standard_npc::standard_npc( const std::string &name, const std::vector<itype_id> &clothing,
standard_npc::standard_npc( const std::string &name, const tripoint &pos,
const std::vector<itype_id> &clothing,
int sk_lvl, int s_str, int s_dex, int s_int, int s_per )
{
this->name = name;
position = tripoint_zero;
position = pos;

str_cur = std::max( s_str, 0 );
str_max = std::max( s_str, 0 );
Expand Down
4 changes: 3 additions & 1 deletion src/npc.h
Original file line number Diff line number Diff line change
Expand Up @@ -1365,7 +1365,9 @@ class npc : public player
class standard_npc : public npc
{
public:
standard_npc( const std::string &name = "", const std::vector<itype_id> &clothing = {},
standard_npc( const std::string &name = "",
const tripoint &pos = tripoint( HALF_MAPSIZE_X, HALF_MAPSIZE_Y, 0 ),
const std::vector<itype_id> &clothing = {},
int sk_lvl = 4, int s_str = 8, int s_dex = 8, int s_int = 8, int s_per = 8 );
};

Expand Down
2 changes: 1 addition & 1 deletion tests/crafting_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ TEST_CASE( "crafting_with_a_companion", "[.]" )
REQUIRE( r->skill_used );

GIVEN( "a companion who can help with crafting" ) {
standard_npc who( "helper", {}, 0 );
standard_npc who( "helper" );

who.set_attitude( NPCATT_FOLLOW );
who.spawn_at_sm( 0, 0, 0 );
Expand Down
43 changes: 24 additions & 19 deletions tests/melee_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,30 +67,32 @@ static void check_near( float prob, const float expected, const float tolerance
}
}

const int num_iters = 10000;
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", {}, 0, 8, 8, 8, 8 );
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", {}, 3, 8, 8, 8, 8 );
standard_npc dude( "TestCharacter", dude_pos, {}, 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 );
standard_npc dude( "TestCharacter", dude_pos, {}, 8, 10, 10, 10, 10 );
dude.weapon = item( "katana" );
const float prob = brute_probability( dude, zed, num_iters );
INFO( full_attack_details( dude ) );
Expand All @@ -104,22 +106,22 @@ TEST_CASE( "Character attacking a manhack", "[.melee]" )
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 );
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", {}, 3, 8, 8, 8, 8 );
standard_npc dude( "TestCharacter", dude_pos, {}, 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 );
standard_npc dude( "TestCharacter", dude_pos, {}, 8, 10, 10, 10, 10 );
dude.weapon = item( "katana" );
const float prob = brute_probability( dude, manhack, num_iters );
INFO( full_attack_details( dude ) );
Expand All @@ -133,7 +135,7 @@ TEST_CASE( "Zombie attacking a character", "[.melee]" )
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 );
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" ) {
Expand All @@ -150,15 +152,16 @@ TEST_CASE( "Zombie attacking a character", "[.melee]" )
}

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 );
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", { "survivor_suit" }, 8, 10, 10, 10, 10 );
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 );
Expand All @@ -171,7 +174,7 @@ TEST_CASE( "Manhack attacking a character", "[.melee]" )
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 );
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" ) {
Expand All @@ -183,15 +186,16 @@ TEST_CASE( "Manhack attacking a character", "[.melee]" )
}

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 );
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", { "survivor_suit" }, 8, 10, 10, 10, 10 );
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 );
Expand All @@ -204,7 +208,7 @@ TEST_CASE( "Hulk smashing a character", "[.], [melee], [monattack]" )
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 );
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" ) {
Expand All @@ -216,15 +220,16 @@ TEST_CASE( "Hulk smashing a character", "[.], [melee], [monattack]" )
}

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 );
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", { "survivor_suit" }, 8, 10, 10, 10, 10 );
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 );
Expand Down
12 changes: 7 additions & 5 deletions tests/ranged_balance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
#include "item_location.h"
#include "json.h"
#include "player.h"
#include "player_helpers.h"
#include "material.h"
#include "skill.h"
#include "type_id.h"
#include "point.h"

Expand Down Expand Up @@ -101,9 +103,7 @@ static void arm_shooter( npc &shooter, const std::string &gun_type,

static void equip_shooter( npc &shooter, const std::vector<std::string> &apparel )
{
const tripoint shooter_pos( 60, 60, 0 );
CHECK( !shooter.in_vehicle );
shooter.setpos( shooter_pos );
shooter.worn.clear();
shooter.inv.clear();
for( const std::string article : apparel ) {
Expand Down Expand Up @@ -246,10 +246,12 @@ static void assert_encumbrance( npc &shooter, int encumbrance )
}
}

static constexpr tripoint shooter_pos( 60, 60, 0 );

TEST_CASE( "unskilled_shooter_accuracy", "[ranged] [balance]" )
{
clear_map();
standard_npc shooter( "Shooter", {}, 0, 8, 8, 8, 7 );
standard_npc shooter( "Shooter", shooter_pos, {}, 0, 8, 8, 8, 7 );
equip_shooter( shooter, { "bastsandals", "armguard_chitin", "armor_chitin", "beekeeping_gloves", "fencing_mask" } );
assert_encumbrance( shooter, 10 );

Expand Down Expand Up @@ -278,7 +280,7 @@ TEST_CASE( "unskilled_shooter_accuracy", "[ranged] [balance]" )
TEST_CASE( "competent_shooter_accuracy", "[ranged] [balance]" )
{
clear_map();
standard_npc shooter( "Shooter", {}, 5, 10, 10, 10, 10 );
standard_npc shooter( "Shooter", shooter_pos, {}, 5, 10, 10, 10, 10 );
equip_shooter( shooter, { "cloak_wool", "footrags_wool", "gloves_wraps_fur", "glasses_safety", "balclava" } );
assert_encumbrance( shooter, 5 );

Expand Down Expand Up @@ -307,7 +309,7 @@ TEST_CASE( "competent_shooter_accuracy", "[ranged] [balance]" )
TEST_CASE( "expert_shooter_accuracy", "[ranged] [balance]" )
{
clear_map();
standard_npc shooter( "Shooter", {}, 10, 20, 20, 20, 20 );
standard_npc shooter( "Shooter", shooter_pos, {}, 10, 20, 20, 20, 20 );
equip_shooter( shooter, { } );
assert_encumbrance( shooter, 0 );

Expand Down
2 changes: 1 addition & 1 deletion tests/throwing_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

TEST_CASE( "throwing distance test", "[throwing], [balance]" )
{
const standard_npc thrower( "Thrower", {}, 4, 10, 10, 10, 10 );
const standard_npc thrower( "Thrower", tripoint( 60, 60, 0 ), {}, 4, 10, 10, 10, 10 );
item grenade( "grenade" );
CHECK( thrower.throw_range( grenade ) >= 30 );
CHECK( thrower.throw_range( grenade ) <= 35 );
Expand Down

0 comments on commit be07a69

Please sign in to comment.