Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly clear NPCs in NPC tests #30031

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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