Skip to content

Commit

Permalink
Use spawn_npc in behaviour_test
Browse files Browse the repository at this point in the history
We were seeing intermittent failures on Mingw in
check_npc_behavior_tree.

This turned out to be because the NPCs the test constructed in a rather
ad hoc manner violated the invariants for the NPC class (in particular,
its two position members were inconsistent).

To avoid that, create the NPC via the new, safer spawn_npc function from
player_helpers.h.
  • Loading branch information
jbytheway committed Jan 9, 2020
1 parent 50aeb2a commit 75afe1b
Showing 1 changed file with 22 additions and 21 deletions.
43 changes: 22 additions & 21 deletions tests/behavior_test.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include <memory>
#include <string>

#include "catch/catch.hpp"
#include "behavior.h"
#include "behavior_oracle.h"
#include "behavior_strategy.h"
Expand All @@ -12,6 +11,9 @@
#include "string_id.h"
#include "weather.h"

#include "catch/catch.hpp"
#include "player_helpers.h"

namespace behavior
{
extern sequential_t default_sequential;
Expand Down Expand Up @@ -131,45 +133,44 @@ TEST_CASE( "behavior_tree", "[behavior]" )
}

// Make assertions about loaded behaviors.
TEST_CASE( "check_npc_behavior_tree", "[behavior]" )
TEST_CASE( "check_npc_behavior_tree", "[npc][behavior]" )
{
behavior::tree npc_needs;
npc_needs.add( &string_id<behavior::node_t>( "npc_needs" ).obj() );
npc test_npc;
test_npc.normalize();
test_npc.setpos( { 50, 50, 0 } );
behavior::character_oracle_t oracle( &test_npc );
npc *test_npc = spawn_npc( { 50, 50 }, "test_talker" );
clear_character( *test_npc );
behavior::character_oracle_t oracle( test_npc );
CHECK( npc_needs.tick( &oracle ) == "idle" );
SECTION( "Freezing" ) {
g->weather.temperature = 0;
test_npc.update_bodytemp();
test_npc->update_bodytemp();
CHECK( npc_needs.tick( &oracle ) == "idle" );
item &sweater = test_npc.i_add( item( itype_id( "sweater" ) ) );
item &sweater = test_npc->i_add( item( itype_id( "sweater" ) ) );
CHECK( npc_needs.tick( &oracle ) == "wear_warmer_clothes" );
item sweater_copy = test_npc.i_rem( &sweater );
test_npc.wear_item( sweater_copy );
item sweater_copy = test_npc->i_rem( &sweater );
test_npc->wear_item( sweater_copy );
CHECK( npc_needs.tick( &oracle ) == "idle" );
test_npc.i_add( item( itype_id( "lighter" ) ) );
test_npc.i_add( item( itype_id( "2x4" ) ) );
test_npc->i_add( item( itype_id( "lighter" ) ) );
test_npc->i_add( item( itype_id( "2x4" ) ) );
CHECK( npc_needs.tick( &oracle ) == "start_fire" );
}
SECTION( "Hungry" ) {
test_npc.set_hunger( 500 );
test_npc.set_stored_kcal( 1000 );
test_npc->set_hunger( 500 );
test_npc->set_stored_kcal( 1000 );
CHECK( npc_needs.tick( &oracle ) == "idle" );
item &food = test_npc.i_add( item( itype_id( "sandwich_cheese_grilled" ) ) );
item_location loc = item_location( test_npc, &food );
item &food = test_npc->i_add( item( itype_id( "sandwich_cheese_grilled" ) ) );
item_location loc = item_location( *test_npc, &food );
CHECK( npc_needs.tick( &oracle ) == "eat_food" );
test_npc.consume( loc );
test_npc->consume( loc );
CHECK( npc_needs.tick( &oracle ) == "idle" );
}
SECTION( "Thirsty" ) {
test_npc.set_thirst( 700 );
test_npc->set_thirst( 700 );
CHECK( npc_needs.tick( &oracle ) == "idle" );
item &water = test_npc.i_add( item( itype_id( "water" ) ) );
item_location loc = item_location( test_npc, &water );
item &water = test_npc->i_add( item( itype_id( "water" ) ) );
item_location loc = item_location( *test_npc, &water );
CHECK( npc_needs.tick( &oracle ) == "drink_water" );
test_npc.consume( loc );
test_npc->consume( loc );
CHECK( npc_needs.tick( &oracle ) == "idle" );
}
}

0 comments on commit 75afe1b

Please sign in to comment.