Skip to content

Commit

Permalink
Properly clear NPCs in NPC tests
Browse files Browse the repository at this point in the history
The npc_can_target_player test was sometimes failing due to the hostile
NPC choosing to target another NPC instead of the player.

This, in turn, was caused by the clearing of NPCs not being done
properly.  They were unloaded, but then loaded again, and not all were
killed.

Write a new clear_npcs in map_helpers.h which works more reliably, and
call that instead.
  • Loading branch information
jbytheway authored and kevingranade committed Apr 28, 2019
1 parent 1c62c69 commit 8533048
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 29 deletions.
10 changes: 10 additions & 0 deletions tests/map_helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
#include "map.h"
#include "mapdata.h"
#include "monster.h"
#include "npc.h"
#include "player.h"
#include "field.h"
#include "enums.h"
#include "game_constants.h"
#include "overmapbuffer.h"
#include "pimpl.h"

void wipe_map_terrain()
Expand All @@ -36,6 +38,14 @@ void clear_creatures()
g->unload_npcs();
}

void clear_npcs()
{
for( npc &n : g->all_npcs() ) {
n.die( nullptr );
overmap_buffer.remove_npc( n.getID() );
}
}

void clear_fields( const int zlevel )
{
const int mapsize = g->m.getmapsize() * SEEX;
Expand Down
1 change: 1 addition & 0 deletions tests/map_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class monster;

void wipe_map_terrain();
void clear_creatures();
void clear_npcs();
void clear_fields( int zlevel );
void clear_map();
void clear_map_and_put_player_underground();
Expand Down
47 changes: 18 additions & 29 deletions tests/npc_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "field.h"
#include "game.h"
#include "map.h"
#include "map_helpers.h"
#include "npc.h"
#include "npc_class.h"
#include "overmapbuffer.h"
Expand Down Expand Up @@ -69,6 +70,17 @@ npc create_model()
return model_npc;
}

std::string get_list_of_npcs( const std::string &title )
{

std::ostringstream npc_list;
npc_list << title << ":\n";
for( const npc &n : g->all_npcs() ) {
npc_list << " " << &n << ": " << n.name << '\n';
}
return npc_list.str();
}

TEST_CASE( "on_load-sane-values", "[.]" )
{
SECTION( "Awake for 10 minutes, gaining hunger/thirst/fatigue" ) {
Expand Down Expand Up @@ -305,20 +317,8 @@ TEST_CASE( "npc-movement" )
g->place_player( tripoint( 60, 60, 0 ) );

// kill npcs before removing vehicles so they are correctly unboarded
for( int y = 0; y < height; ++y ) {
for( int x = 0; x < width; ++x ) {
const tripoint p = g->u.pos() + point( x, y );
Creature *cre = g->critter_at( p );
if( cre != nullptr && cre != &g->u ) {
npc *guy = dynamic_cast<npc *>( cre );
cre->die( nullptr );
if( guy ) {
overmap_buffer.remove_npc( guy->getID() );
}
}
}
}
g->unload_npcs();
clear_npcs();
clear_creatures();
// remove existing vehicles
VehicleList vehs = g->m.get_vehicles( g->u.pos(), g->u.pos() + point( width - 1, height - 1 ) );
for( auto &veh : vehs ) {
Expand Down Expand Up @@ -439,21 +439,8 @@ TEST_CASE( "npc_can_target_player" )

g->place_player( tripoint( 10, 10, 0 ) );

// kill npcs before removing vehicles so they are correctly unboarded
for( int y = 0; y < height; ++y ) {
for( int x = 0; x < width; ++x ) {
const tripoint p = g->u.pos() + point( x, y );
Creature *cre = g->critter_at( p );
if( cre != nullptr && cre != &g->u ) {
npc *guy = dynamic_cast<npc *>( cre );
cre->die( nullptr );
if( guy ) {
overmap_buffer.remove_npc( guy->getID() );
}
}
}
}
g->unload_npcs();
clear_npcs();
clear_creatures();

const auto spawn_npc = []( const int x, const int y, const std::string & npc_class ) {
const string_id<npc_template> test_guy( npc_class );
Expand All @@ -471,6 +458,8 @@ TEST_CASE( "npc_can_target_player" )
hostile->set_attitude( NPCATT_KILL );
hostile->name = "Enemy NPC";

INFO( get_list_of_npcs( "NPCs after spawning one" ) );

hostile->regen_ai_cache();
REQUIRE( hostile->current_target() != nullptr );
CHECK( hostile->current_target() == static_cast<Creature *>( &g->u ) );
Expand Down

0 comments on commit 8533048

Please sign in to comment.