Followers and guards keep to their obligations when getting food

- food options judged against obligations when found and when started
- an NPC that overrides its obligations for food says why
- a hungry follower whose obligations keep it from food says it is hungry
This commit is contained in:
dobbry-vechur
2026-10-24 00:58:38 +04:00
parent 8f731cf178
commit 4cf6dc8245
6 changed files with 549 additions and 95 deletions
+12 -5
View File
@@ -147,13 +147,17 @@ status_t character_oracle_t::can_take_shelter( std::string_view ) const
return n->find_nearby_shelters().empty() ? status_t::failure : status_t::running;
}
// carries an item it may consume for the need
// carries an item it may consume for the need, and may stay put to consume it
static bool carries_supply_for( const Character &subject, const npc::need_goal_id id )
{
const npc *n = dynamic_cast<const npc *>( &subject );
if( !n || !n->intake_useful( id ) ) {
return false;
}
// meal or drink holds the NPC where it stands
if( !n->need_view( id, npc_option_cost::stay( n->pos_abs() ) ).may_consider() ) {
return false;
}
// const_cast: the inventory cache refreshes itself, so the query is not const
for( const item_location &loc : const_cast<npc *>( n )->cache_get_items_with( "is_food",
&item::is_food ) ) {
@@ -182,10 +186,13 @@ status_t character_oracle_t::can_obtain_food( std::string_view ) const
if( !n || !n->intake_useful( npc::need_goal_id::eat_food ) ) {
return status_t::failure;
}
// const_cast: see npc::find_food_candidates()
// Camp food is included in the candidate list as need_source::camp_food.
return const_cast<npc *>( n )->find_food_candidates().empty()
? status_t::failure : status_t::running;
// as in can_obtain_water(); const_cast: see npc::find_food_candidates()
for( const npc::need_candidate &c : const_cast<npc *>( n )->find_food_candidates() ) {
if( n->need_view( npc::need_goal_id::eat_food, n->food_option_cost( c ) ).may_consider() ) {
return status_t::running;
}
}
return status_t::failure;
}
status_t character_oracle_t::can_obtain_water( std::string_view ) const
+2
View File
@@ -1700,6 +1700,8 @@ class npc : public Character
// is routed and nullopt only marks a stand_on spot nobody can stand on
std::optional<npc_option_cost> trip_cost( const npc_trip_spec &spec, bool responsive ) const;
std::optional<npc_option_cost> water_option_cost( const need_candidate &c ) const;
std::optional<npc_option_cost> food_option_cost( const need_candidate &c ) const;
std::optional<npc_option_cost> source_trip_cost( const need_candidate &c ) const;
std::optional<npc_option_cost> rest_option_cost( const tripoint_abs_ms &spot ) const;
npc_option_cost rest_option_best_case() const;
+12 -5
View File
@@ -107,6 +107,8 @@ static need_levels levels_for( const npc::need_goal_id id )
static_cast<float>( sleepiness_levels::DEAD_TIRED ) / 1000.0f,
static_cast<float>( sleepiness_levels::EXHAUSTED ) / 1000.0f };
case npc::need_goal_id::eat_food:
// calorie urgency, share of healthy stored calories missing
return { food_act_level, 0.4f, 0.6f };
case npc::need_goal_id::regulate_temperature:
break;
}
@@ -401,6 +403,9 @@ void npc::explain_override( const need_goal_id id )
speech = _( "I can't stay awake any longer." );
break;
case need_goal_id::eat_food:
issue = "need_override_food";
speech = _( "I really need something to eat. I'll be right back." );
break;
case need_goal_id::regulate_temperature:
return;
}
@@ -411,10 +416,12 @@ void npc::explain_override( const need_goal_id id )
npc_need_view npc::best_need_view( const need_goal_id id )
{
const npc_option_cost in_place = npc_option_cost::in_place( pos_abs() );
if( id == need_goal_id::drink_water ) {
if( id == need_goal_id::drink_water || id == need_goal_id::eat_food ) {
const bool water = id == need_goal_id::drink_water;
behavior::character_oracle_t oracle( this );
if( oracle.has_water( "" ) == behavior::status_t::running ) {
return need_view( id, in_place );
const behavior::status_t carried = water ? oracle.has_water( "" ) : oracle.has_food( "" );
if( carried == behavior::status_t::running ) {
return need_view( id, npc_option_cost::stay( pos_abs() ) );
}
npc_need_view held;
held.raw = current_need_urgency( id );
@@ -422,11 +429,11 @@ npc_need_view npc::best_need_view( const need_goal_id id )
return held;
}
const std::set<tripoint_abs_ms> &failed = failed_targets_for( id );
for( const need_candidate &c : find_water_candidates() ) {
for( const need_candidate &c : water ? find_water_candidates() : find_food_candidates() ) {
if( failed.count( c.target ) != 0 ) {
continue;
}
const npc_need_view v = need_view( id, water_option_cost( c ) );
const npc_need_view v = need_view( id, water ? water_option_cost( c ) : food_option_cost( c ) );
if( v.eligible() ) {
return v;
}
+98 -83
View File
@@ -6372,6 +6372,7 @@ bool npc::complain()
static const std::string hunger_string = "hunger";
static const std::string thirst_string = "thirst";
static const std::string water_out_of_reach_string = "water_out_of_reach";
static const std::string food_out_of_reach_string = "food_out_of_reach";
if( !is_player_ally() || !get_player_view().sees( here, *this ) ) {
return false;
@@ -6425,6 +6426,20 @@ bool npc::complain()
// Hunger and thirst complaints only fire when NPC has food needs
if( needs_food() ) {
// food in sight, but obligations keep NPC from going: it says it's hungry
if( complaint_due( food_out_of_reach_string, 30_minutes ) ) {
behavior::character_oracle_t oracle( this );
if( oracle.needs_food_badly( "" ) == behavior::status_t::running ) {
const npc_need_view food = best_need_view( need_goal_id::eat_food );
if( food.kind == npc_option_kind::actual && !food.eligible() &&
complain_about( food_out_of_reach_string, 30_minutes,
chat_snippets().snip_hungry.translated() ) ) {
complaints.insert_or_assign( hunger_string, calendar::turn );
return true;
}
}
}
// Hunger every 3-6 hours
// Complaint frequency scales with hunger level
if( get_hunger() > NPC_HUNGER_COMPLAIN &&
@@ -6442,7 +6457,7 @@ bool npc::complain()
if( water.kind == npc_option_kind::actual && !water.eligible() &&
complain_about( water_out_of_reach_string, 30_minutes,
chat_snippets().snip_thirsty.translated() ) ) {
// plea is the thirst complaint, so it won't repeat next turn
// plea counts as the thirst complaint, which waits out its interval
complaints.insert_or_assign( thirst_string, calendar::turn );
return true;
}
@@ -6828,21 +6843,33 @@ bool npc::wear_item_at( item_location loc )
return true;
}
std::optional<npc_option_cost> npc::water_option_cost( const need_candidate &c ) const
// walk to within reach of the source, then a stay there to eat or drink
std::optional<npc_option_cost> npc::source_trip_cost( const need_candidate &c ) const
{
if( c.source_kind == need_source::camp_water ) {
return npc_option_cost::in_place( pos_abs() );
}
std::optional<npc_option_cost> cost =
trip_cost( npc_trip_spec::within_range( get_map().get_bub( c.target ), 1, true ), true );
// a harvest or a drink by sips keeps the NPC there for a while
if( cost && ( c.source_kind == need_source::harvestable ||
c.source_kind == need_source::water_terrain ) ) {
if( cost ) {
cost->hold = npc_option_hold::occupy;
}
return cost;
}
std::optional<npc_option_cost> npc::water_option_cost( const need_candidate &c ) const
{
if( c.source_kind == need_source::camp_water ) {
return npc_option_cost::stay( pos_abs() );
}
return source_trip_cost( c );
}
std::optional<npc_option_cost> npc::food_option_cost( const need_candidate &c ) const
{
if( c.source_kind == need_source::camp_food ) {
return npc_option_cost::stay( pos_abs() );
}
return source_trip_cost( c );
}
std::optional<npc_option_cost> npc::rest_option_cost( const tripoint_abs_ms &spot ) const
{
return trip_cost( npc_trip_spec::stand_on( get_map().get_bub( spot ), true ), false );
@@ -7654,14 +7681,16 @@ void npc::end_need_pursuit( const need_goal_id id )
}
}
// TODO: only water and sleep declare their pursued option; eat_food and regulate_temperature are
// judged in place, so obligations never end their walks or activities
// TODO: regulate_temperature is judged in place, so obligations never end its walks or activities
std::optional<npc_option_cost> npc::pursued_option_cost( const need_goal_id id ) const
{
const need_plan &plan = plan_for( id );
if( id == need_goal_id::drink_water && plan.active() ) {
return water_option_cost( { plan.source_kind, plan.target, 0.0f } );
}
if( id == need_goal_id::eat_food && plan.active() ) {
return food_option_cost( { plan.source_kind, plan.target, 0.0f } );
}
if( id == need_goal_id::go_to_sleep ) {
if( plan.active() ) {
return rest_option_cost( plan.target );
@@ -7967,40 +7996,41 @@ npc::need_result npc::execute_eat_food()
// 1. Try camp food (calories only, skip the water path),
// inventory food, then adjacent ground food.
// every meal runs as an activity: progress, not yet satisfaction
if( consume_food_from_camp( cf ) ) {
plan.clear();
failed.clear();
return need_result::progressed;
}
if( consume_food( cf ) ) {
plan.clear();
failed.clear();
return need_result::progressed;
}
{
map &food_map = get_map();
for( scored_item &c : find_nearby_food( cf ) ) {
if( square_dist( pos_bub(), c.loc.pos_bub( food_map ) ) <= 1 ) {
if( consume_food_at( c.loc ) ) {
plan.clear();
failed.clear();
return need_result::progressed;
// a meal is an activity that holds the NPC here: progress, not satisfaction; obligations
// gate it
const npc_option_cost meal_here = npc_option_cost::stay( pos_abs() );
if( need_view( need_goal_id::eat_food, meal_here ).eligible() ) {
bool started = consume_food_from_camp( cf ) || consume_food( cf );
if( !started ) {
for( scored_item &c : find_nearby_food( cf ) ) {
if( square_dist( pos_bub(), c.loc.pos_bub( here ) ) <= 1 && consume_food_at( c.loc ) ) {
started = true;
break;
}
}
}
if( started ) {
record_selected_option( need_goal_id::eat_food, meal_here );
plan.clear();
failed.clear();
return need_result::progressed;
}
}
using need_source = npc_short_term_cache::need_source;
const auto candidates = find_food_candidates();
// as in execute_drink_water()
const auto eligible = [this]( const need_candidate & c ) {
return need_view( need_goal_id::eat_food, food_option_cost( c ) ).eligible();
};
// 2. Validate existing target against the candidate scan.
if( plan.active() ) {
bool still_valid = false;
for( const need_candidate &c : candidates ) {
if( c.target == plan.target && c.source_kind == plan.source_kind ) {
still_valid = true;
still_valid = eligible( c );
break;
}
}
@@ -8019,7 +8049,7 @@ npc::need_result npc::execute_eat_food()
c.source_kind == need_source::camp_water ) {
continue;
}
if( failed.count( c.target ) == 0 ) {
if( failed.count( c.target ) == 0 && eligible( c ) ) {
plan.goal = "eat_food";
plan.source_kind = c.source_kind;
plan.target = c.target;
@@ -8040,21 +8070,17 @@ npc::need_result npc::execute_eat_food()
return need_result::impossible;
}
const npc_option_cost unrouted = npc_option_cost::unrouted( plan.target );
record_selected_option( need_goal_id::eat_food,
food_option_cost( { plan.source_kind, plan.target, 0.0f } ).value_or( unrouted ) );
// 4. Execute based on source kind.
const tripoint_bub_ms target_bub = here.get_bub( plan.target );
if( plan.source_kind == need_source::ground_item ) {
if( square_dist( pos_bub(), target_bub ) <= 1 ) {
for( scored_item &c : find_nearby_food( cf ) ) {
if( here.get_abs( c.loc.pos_bub( here ) ) == plan.target &&
consume_food_at( c.loc ) ) {
plan.clear();
failed.clear();
return need_result::progressed;
}
}
} else if( low_danger &&
move_to_and_verify( npc_trip_spec::within_range( target_bub, 1, true ) ) ) {
// food in reach is eaten in place or not at all
if( square_dist( pos_bub(), target_bub ) > 1 && low_danger &&
move_to_and_verify( npc_trip_spec::within_range( target_bub, 1, true ) ) ) {
plan.last_result = need_result::progressed;
plan.no_progress_turns = 0;
return need_result::progressed;
@@ -8102,9 +8128,6 @@ npc::need_result npc::execute_drink_water()
std::set<tripoint_abs_ms> &failed = ai_cache.water_failed_targets;
map &here = get_map();
// recorded only after drinking, so a failed try can't hide a walk's verdict
const npc_option_cost here_and_now = npc_option_cost::in_place( pos_abs() );
if( !intake_useful( need_goal_id::drink_water ) ) {
plan.clear();
return need_result::satisfied;
@@ -8112,34 +8135,33 @@ npc::need_result npc::execute_drink_water()
// 1. Try camp water, inventory drinks, adjacent ground drinks,
// then adjacent water terrain sources.
if( consume_food_from_camp( cf ) ) {
record_selected_option( need_goal_id::drink_water, here_and_now );
plan.clear();
failed.clear();
return need_result::satisfied;
}
if( consume_food( cf ) ) {
record_selected_option( need_goal_id::drink_water, here_and_now );
plan.clear();
failed.clear();
return need_result::progressed;
}
{
map &water_map = get_map();
for( scored_item &c : find_nearby_food( cf ) ) {
if( square_dist( pos_bub(), c.loc.pos_bub( water_map ) ) <= 1 ) {
if( consume_food_at( c.loc ) ) {
record_selected_option( need_goal_id::drink_water, here_and_now );
plan.clear();
failed.clear();
return need_result::progressed;
// a sip or a drink holds the NPC here, as a meal does; recorded only after drinking, so a
// failed try can't hide a walk's verdict
const npc_option_cost drink_here = npc_option_cost::stay( pos_abs() );
if( need_view( need_goal_id::drink_water, drink_here ).eligible() ) {
if( consume_food_from_camp( cf ) ) {
record_selected_option( need_goal_id::drink_water, drink_here );
plan.clear();
failed.clear();
return need_result::satisfied;
}
bool started = consume_food( cf );
if( !started ) {
for( scored_item &c : find_nearby_food( cf ) ) {
if( square_dist( pos_bub(), c.loc.pos_bub( here ) ) <= 1 && consume_food_at( c.loc ) ) {
started = true;
break;
}
}
}
}
for( scored_water_source &ws : find_nearby_water_sources() ) {
if( square_dist( pos_bub(), ws.pos ) <= 1 ) {
if( drink_from_water_source( ws.pos ) ) {
if( started ) {
record_selected_option( need_goal_id::drink_water, drink_here );
plan.clear();
failed.clear();
return need_result::progressed;
}
for( scored_water_source &ws : find_nearby_water_sources() ) {
if( square_dist( pos_bub(), ws.pos ) <= 1 && drink_from_water_source( ws.pos ) ) {
// sipping by the turn: the source stays the plan, so obligations judge each sip
// as a stay
plan.goal = "drink_water";
@@ -8148,7 +8170,7 @@ npc::need_result npc::execute_drink_water()
plan.last_result = need_result::satisfied;
plan.no_progress_turns = 0;
record_selected_option( need_goal_id::drink_water,
water_option_cost( { plan.source_kind, plan.target, 0.0f } ).value_or( here_and_now ) );
water_option_cost( { plan.source_kind, plan.target, 0.0f } ).value_or( drink_here ) );
failed.clear();
return need_result::satisfied;
}
@@ -8158,7 +8180,8 @@ npc::need_result npc::execute_drink_water()
using need_source = npc_short_term_cache::need_source;
const std::vector<need_candidate> candidates = find_water_candidates();
// a disallowed source never becomes a sticky plan; lazy because each judgment routes
// a disallowed source never becomes a sticky plan; judged lazily, as each judgment routes
// a trip
const auto eligible = [this]( const need_candidate & c ) {
return need_view( need_goal_id::drink_water, water_option_cost( c ) ).eligible();
};
@@ -8216,17 +8239,9 @@ npc::need_result npc::execute_drink_water()
const tripoint_bub_ms target_bub = here.get_bub( plan.target );
if( plan.source_kind == need_source::ground_item ) {
if( square_dist( pos_bub(), target_bub ) <= 1 ) {
for( scored_item &c : find_nearby_food( cf ) ) {
if( here.get_abs( c.loc.pos_bub( here ) ) == plan.target &&
consume_food_at( c.loc ) ) {
plan.clear();
failed.clear();
return need_result::progressed;
}
}
} else if( low_danger &&
move_to_and_verify( npc_trip_spec::within_range( target_bub, 1, true ) ) ) {
// a drink in reach is drunk in place or not at all
if( square_dist( pos_bub(), target_bub ) > 1 && low_danger &&
move_to_and_verify( npc_trip_spec::within_range( target_bub, 1, true ) ) ) {
plan.last_result = need_result::progressed;
plan.no_progress_turns = 0;
return need_result::progressed;
+345
View File
@@ -1903,6 +1903,65 @@ TEST_CASE( "orders_end_self_care", "[npc][needs][obligation]" )
}
}
TEST_CASE( "bottled_drink_is_judged_as_occupying", "[npc][needs][water][obligation]" )
{
drink_setup();
map &here = get_map();
npc &guy = spawn_follower( leader_pos + tripoint( 2, 0, 0 ) );
guy.worn.wear_item( guy, item( itype_backpack ), false, false );
settle_body( guy, 0 );
guy.stomach.empty();
guy.guts.empty();
item bottle( itype_bottle_plastic );
REQUIRE( bottle.put_in( item( itype_water_clean, calendar::turn, 2 ),
pocket_type::CONTAINER ).success() );
REQUIRE( guy.i_add( bottle ) );
here.build_map_cache( 0 );
const auto drinking = []( const npc & n ) {
return n.activity.id() == ACT_CONSUME;
};
behavior::character_oracle_t oracle( &guy );
GIVEN( "the leader stays" ) {
guy.set_thirst( 600 );
REQUIRE( oracle.has_water( "" ) == behavior::status_t::running );
run_npc_turn( guy );
THEN( "it drinks from its pack" ) {
CHECK( drinking( guy ) );
CHECK( guy.get_intention().verdict.permission == npc_permission::allowed );
}
}
GIVEN( "the leader walks off" ) {
get_avatar().setpos( here, leader_pos + tripoint( 12, 0, 0 ) );
REQUIRE_FALSE( guy.obligation_reach()->contains( guy.pos_abs() ) );
WHEN( "it's thirsty" ) {
guy.set_thirst( 600 );
THEN( "bottle waits and it keeps up" ) {
CHECK( oracle.has_water( "" ) == behavior::status_t::failure );
const int before = dist_to_leader( guy );
bool drank_on_the_way = false;
run_npc_turns( guy, 30, [&]( const npc & n ) {
drank_on_the_way = drank_on_the_way || drinking( n );
return dist_to_leader( n ) <= 4;
} );
CHECK_FALSE( drank_on_the_way );
CHECK( dist_to_leader( guy ) < before );
CHECK( guy.charges_of( itype_water_clean ) == 2 );
}
}
WHEN( "it's thirsty past insisting" ) {
guy.set_thirst( 850 );
run_npc_turn( guy );
THEN( "it drinks and says it holds up the group" ) {
CHECK( drinking( guy ) );
CHECK( guy.get_committed_goal() == "drink_water" );
CHECK( guy.get_intention().verdict.permission == npc_permission::discouraged );
CHECK( guy.last_complaint( "need_override_water" ) );
}
}
}
}
TEST_CASE( "routine_meal_at_healthy_weight_with_high_hunger", "[npc][needs][food]" )
{
drink_setup();
@@ -2042,6 +2101,292 @@ TEST_CASE( "idle_care_pass_drinks_from_the_pack_below_the_act_level", "[npc][nee
}
}
// stored calories short of healthy by urgency, nothing in the stomach
static void make_hungry( npc &guy, const float urgency )
{
guy.set_stored_kcal( static_cast<int>( guy.get_healthy_kcal() * ( 1.0f - urgency ) ) );
guy.stomach.empty();
guy.guts.empty();
REQUIRE( guy.current_need_urgency( npc::need_goal_id::eat_food ) == Approx( urgency ).margin(
0.01 ) );
}
// calories eaten and not yet stored; make_hungry() starts it at zero
static int pending_kcal( const npc &guy )
{
return guy.stomach.get_calories() + guy.guts.get_calories();
}
static void put_oranges( const tripoint_bub_ms &p, const int count )
{
for( int i = 0; i < count; ++i ) {
get_map().add_item_or_charges( p, item( itype_orange ) );
}
REQUIRE( oranges_at( p ) == count );
}
TEST_CASE( "follower_eats_within_reach_and_keeps_following", "[npc][needs][food][behavior]" )
{
drink_setup();
map &here = get_map();
npc &guy = spawn_follower( leader_pos + tripoint( 2, 0, 0 ) );
settle_body( guy, 0 );
make_hungry( guy, 0.3f );
SECTION( "food inside the spacing" ) {
const tripoint_bub_ms pile = leader_pos + tripoint( 2, 3, 0 );
put_oranges( pile, 3 );
here.build_map_cache( 0 );
int farthest = 0;
run_npc_turns( guy, 200, [&]( const npc & n ) {
farthest = std::max( farthest, dist_to_leader( n ) );
return pending_kcal( n ) > 0;
} );
CHECK( pending_kcal( guy ) > 0 );
CHECK( oranges_at( pile ) < 3 );
CHECK( farthest <= 4 );
WHEN( "the leader walks on" ) {
get_avatar().setpos( here, leader_pos + tripoint( 12, 0, 0 ) );
run_npc_turns( guy, 60, []( const npc & n ) {
return dist_to_leader( n ) <= 4;
} );
THEN( "it follows" ) {
CHECK( dist_to_leader( guy ) <= 4 );
}
}
}
SECTION( "food past the spacing" ) {
// in sight, 8 tiles from the leader
const tripoint_bub_ms pile = leader_pos + tripoint( 8, 0, 0 );
put_oranges( pile, 3 );
here.build_map_cache( 0 );
int farthest = 0;
for( int turn = 0; turn < 20; ++turn ) {
run_npc_turn( guy );
farthest = std::max( farthest, dist_to_leader( guy ) );
}
CHECK( farthest <= 4 );
CHECK( oranges_at( pile ) == 3 );
CHECK_FALSE( guy.get_food_plan().active() );
}
SECTION( "meal from the pack waits while an order stands" ) {
guy.worn.wear_item( guy, item( itype_backpack ), false, false );
REQUIRE( guy.i_add( item( itype_orange ) ) );
guy.goto_to_this_pos = here.get_abs( leader_pos + tripoint( -4, 0, 0 ) );
here.build_map_cache( 0 );
guy.set_moves( 100 );
guy.execute_need_goal( "eat_food" );
CHECK_FALSE( guy.is_eating() );
CHECK( guy.amount_of( itype_orange ) == 1 );
behavior::character_oracle_t oracle( &guy );
CHECK( oracle.has_food( "" ) == behavior::status_t::failure );
}
}
TEST_CASE( "follower_asks_for_a_food_stop", "[npc][needs][food][behavior]" )
{
drink_setup();
map &here = get_map();
npc &guy = spawn_follower( leader_pos + tripoint( 3, 0, 0 ) );
settle_body( guy, 0 );
// in sight, 8 tiles from the leader
const tripoint_bub_ms pile = leader_pos + tripoint( 8, 0, 0 );
put_oranges( pile, 3 );
here.build_map_cache( 0 );
WHEN( "hungry" ) {
make_hungry( guy, 0.3f );
int farthest = 0;
for( int turn = 0; turn < 20; ++turn ) {
run_npc_turn( guy );
farthest = std::max( farthest, dist_to_leader( guy ) );
}
THEN( "it stays there and says it's hungry" ) {
CHECK( farthest <= 4 );
CHECK( oranges_at( pile ) == 3 );
CHECK( guy.last_complaint( "food_out_of_reach" ) );
CHECK( guy.last_complaint( "hunger" ) == guy.last_complaint( "food_out_of_reach" ) );
const npc_need_view food = guy.best_need_view( npc::need_goal_id::eat_food );
CHECK( food.kind == npc_option_kind::actual );
CHECK( food.verdict.permission == npc_permission::discouraged );
}
}
WHEN( "hungry past insisting" ) {
make_hungry( guy, 0.5f );
run_npc_turn( guy );
THEN( "it says so and goes" ) {
CHECK( guy.get_committed_goal() == "eat_food" );
CHECK( guy.get_intention().verdict.permission == npc_permission::discouraged );
CHECK( guy.last_complaint( "need_override_food" ) );
run_npc_turns( guy, 200, []( const npc & n ) {
return pending_kcal( n ) > 0;
} );
CHECK( pending_kcal( guy ) > 0 );
CHECK( oranges_at( pile ) < 3 );
}
}
}
TEST_CASE( "follower_beyond_spacing_leaves_food_in_reach", "[npc][needs][food][water][obligation]" )
{
drink_setup();
map &here = get_map();
// leader stands still, follower is past spacing of 4
npc &guy = spawn_follower( leader_pos + tripoint( 8, 0, 0 ) );
settle_body( guy, 0 );
guy.stomach.empty();
guy.guts.empty();
const tripoint_bub_ms beside = guy.pos_bub() + tripoint::south;
behavior::character_oracle_t oracle( &guy );
const auto keeps_up = [&]( const std::function<bool( const npc & )> &consumed ) {
const int before = dist_to_leader( guy );
bool took = false;
for( int turn = 0; turn < 20; ++turn ) {
run_npc_turn( guy );
took = took || consumed( guy );
}
CAPTURE( before, dist_to_leader( guy ), guy.get_committed_goal() );
CHECK_FALSE( took );
CHECK( dist_to_leader( guy ) < before );
};
GIVEN( "hungry, short of insisting, an orange beside it" ) {
// hunger outweighs the leader's pull, short of the 0.4 insist level
make_hungry( guy, 0.38f );
put_oranges( beside, 1 );
here.build_map_cache( 0 );
THEN( "orange is judged as a stay outside the spacing" ) {
CHECK( oracle.can_obtain_food( "" ) == behavior::status_t::failure );
const npc_need_view food = guy.best_need_view( npc::need_goal_id::eat_food );
CHECK( food.kind == npc_option_kind::actual );
CHECK_FALSE( food.eligible() );
AND_THEN( "it keeps up with the leader and leaves the orange" ) {
keeps_up( []( const npc & n ) {
return n.get_committed_goal() == "eat_food" || n.is_eating();
} );
CHECK( oranges_at( beside ) == 1 );
}
}
}
GIVEN( "thirsty, short of insisting, a juicy orange beside it" ) {
guy.set_thirst( 600 );
put_oranges( beside, 1 );
here.build_map_cache( 0 );
THEN( "orange is judged as a stay outside the spacing" ) {
CHECK( oracle.can_obtain_water( "" ) == behavior::status_t::failure );
const npc_need_view water = guy.best_need_view( npc::need_goal_id::drink_water );
CHECK( water.kind == npc_option_kind::actual );
CHECK_FALSE( water.eligible() );
AND_THEN( "it keeps up with the leader and leaves the orange" ) {
keeps_up( []( const npc & n ) {
return n.get_committed_goal() == "drink_water" || n.is_eating();
} );
CHECK( oranges_at( beside ) == 1 );
}
}
}
}
TEST_CASE( "guard_eats_without_leaving_post", "[npc][needs][food][guard]" )
{
drink_setup();
map &here = get_map();
const tripoint_bub_ms post( 30, 30, 0 );
npc &guy = spawn_npc( post.xy(), "test_talker" );
clear_character( guy, true );
guy.setpos( here, post );
guy.set_mission( NPC_MISSION_GUARD );
guy.set_guard_pos( guy.pos_abs() );
REQUIRE( guy.is_on_shift() );
settle_body( guy, 0 );
const auto from_post = [&]( const npc & n ) {
return rl_dist( n.pos_abs(), here.get_abs( post ) );
};
SECTION( "food near the post" ) {
// past the duty's pull, short of the post's emergency
make_hungry( guy, 0.5f );
const tripoint_bub_ms pile = post + tripoint( 3, 0, 0 );
put_oranges( pile, 3 );
here.build_map_cache( 0 );
int farthest = 0;
run_npc_turns( guy, 200, [&]( const npc & n ) {
farthest = std::max( farthest, from_post( n ) );
return pending_kcal( n ) > 0;
} );
CHECK( pending_kcal( guy ) > 0 );
CHECK( oranges_at( pile ) < 3 );
CHECK( farthest <= 4 );
}
SECTION( "food too far for a quick trip" ) {
make_hungry( guy, 0.5f );
const tripoint_bub_ms pile = post + tripoint( 6, 0, 0 );
put_oranges( pile, 3 );
here.build_map_cache( 0 );
for( int turn = 0; turn < 15; ++turn ) {
run_npc_turn( guy );
CHECK( guy.pos_bub() == post );
}
CHECK( oranges_at( pile ) == 3 );
}
SECTION( "too far, but starving" ) {
make_hungry( guy, 0.7f );
const tripoint_bub_ms pile = post + tripoint( 6, 0, 0 );
put_oranges( pile, 3 );
here.build_map_cache( 0 );
run_npc_turns( guy, 200, []( const npc & n ) {
return pending_kcal( n ) > 0;
} );
CHECK( pending_kcal( guy ) > 0 );
CHECK( oranges_at( pile ) < 3 );
CHECK( guy.last_complaint( "need_override_food" ) );
}
}
TEST_CASE( "food_harvest_ends_when_the_leader_walks_off", "[npc][needs][food][obligation]" )
{
drink_setup();
map &here = get_map();
// apple trees bear fruit in autumn
set_time( calendar::turn_zero + 2 * calendar::season_length() + 12_hours );
npc &guy = spawn_follower( leader_pos + tripoint( 2, 0, 0 ) );
guy.worn.wear_item( guy, item( itype_backpack ), false, false );
settle_body( guy, 0 );
const tripoint_bub_ms tree = leader_pos + tripoint( 3, 0, 0 );
here.ter_set( tree, ter_t_tree_apple );
here.build_map_cache( 0 );
REQUIRE( here.is_harvestable( tree ) );
const auto harvesting = []( const npc & n ) {
return n.activity.id() == ACT_HARVEST || n.activity.id() == ACT_FORAGE;
};
SECTION( "hungry" ) {
make_hungry( guy, 0.3f );
run_npc_turn( guy );
REQUIRE( guy.get_committed_goal() == "eat_food" );
REQUIRE( guy.get_food_plan().source_kind == npc_short_term_cache::need_source::harvestable );
REQUIRE( harvesting( guy ) );
get_avatar().setpos( here, leader_pos + tripoint( -10, 0, 0 ) );
run_npc_turn( guy );
CHECK_FALSE( harvesting( guy ) );
CHECK( guy.get_committed_goal() != "eat_food" );
}
SECTION( "hungry past insisting" ) {
make_hungry( guy, 0.5f );
run_npc_turn( guy );
REQUIRE( guy.get_committed_goal() == "eat_food" );
REQUIRE( harvesting( guy ) );
get_avatar().setpos( here, leader_pos + tripoint( -10, 0, 0 ) );
run_npc_turn( guy );
CHECK( harvesting( guy ) );
CHECK( guy.get_intention().verdict.permission == npc_permission::discouraged );
const std::optional<time_point> said = guy.last_complaint( "need_override_food" );
REQUIRE( said );
run_npc_turn( guy );
CHECK( guy.last_complaint( "need_override_food" ) == said );
}
}
static void sleep_setup()
{
// time first: placing the leader starts its time_since_relocated clock
+80 -2
View File
@@ -1636,6 +1636,44 @@ TEST_CASE( "camp_meal_and_water_take_their_time", "[npc][needs][camp]" )
}
}
TEST_CASE( "camp_water_waits_while_the_leader_is_away", "[npc][needs][camp][obligation]" )
{
clear_avatar();
clear_map_with_vision();
set_time_to_day();
get_player_character().camps.clear();
get_weather().forced_temperature = 20_C;
const tripoint_bub_ms mid{ MAPSIZE_X / 2, MAPSIZE_Y / 2, 0 };
basecamp &camp = place_camp( mid, your_fac, 500 );
REQUIRE( camp.has_water() );
npc &guy = spawn_camp_eater( mid );
guy.set_stored_kcal( guy.get_healthy_kcal() );
get_avatar().setpos( get_map(), mid + tripoint( 12, 0, 0 ) );
REQUIRE( guy.obligation_reach() );
REQUIRE_FALSE( guy.obligation_reach()->contains( guy.pos_abs() ) );
get_map().build_map_cache( 0 );
behavior::character_oracle_t oracle( &guy );
WHEN( "it's thirsty" ) {
guy.set_thirst( 600 );
guy.set_moves( 100 );
THEN( "camp water is stay outside the spacing; no sip" ) {
CHECK( oracle.can_obtain_water( "" ) == behavior::status_t::failure );
CHECK( guy.execute_need_goal( "drink_water" ) != npc::need_result::satisfied );
CHECK( guy.stomach.get_water() == 0_ml );
}
}
WHEN( "it's thirsty past insisting" ) {
guy.set_thirst( 850 );
guy.set_moves( 100 );
THEN( "it sips camp water" ) {
CHECK( oracle.can_obtain_water( "" ) == behavior::status_t::running );
CHECK( guy.execute_need_goal( "drink_water" ) == npc::need_result::satisfied );
CHECK( guy.stomach.get_water() > 0_ml );
}
}
}
TEST_CASE( "camp_meal_eater_answers_at_the_next_decision", "[npc][needs][camp][obligation]" )
{
clear_avatar();
@@ -6435,6 +6473,11 @@ TEST_CASE( "npc_food_executor_contract", "[npc][needs][forage]" )
SECTION( "closer unreachable target skipped for farther reachable one" ) {
calendar::turn = calendar::turn_zero + 2 * calendar::season_length() + 12_hours;
// no obligation: a trip is judged on its destination alone, so a walled-in target is
// planned and times out
guy.set_fac( faction_tacoma_commune );
guy.set_attitude( NPCATT_NULL );
REQUIRE_FALSE( guy.obligation_reach() );
using need_source = npc_short_term_cache::need_source;
// Close tree behind glass (visible through glass, but impassable
@@ -6479,6 +6522,11 @@ TEST_CASE( "npc_food_executor_contract", "[npc][needs][forage]" )
SECTION( "failed target becomes reachable after generation reset" ) {
calendar::turn = calendar::turn_zero + 2 * calendar::season_length() + 12_hours;
// no obligation: a trip is judged on its destination alone, so a walled-in target is
// planned and times out
guy.set_fac( faction_tacoma_commune );
guy.set_attitude( NPCATT_NULL );
REQUIRE_FALSE( guy.obligation_reach() );
// Single tree behind glass (visible, unreachable without bashing).
const tripoint_bub_ms tree = guy.pos_bub() + tripoint( 2, 0, 0 );
@@ -6672,6 +6720,36 @@ TEST_CASE( "npc_can_obtain_food_filters_harvests", "[npc][needs][forage]" )
here.build_map_cache( 0 );
CHECK( oracle.can_obtain_food( "" ) == behavior::status_t::failure );
}
SECTION( "follower takes only the harvest its reach allows" ) {
clear_avatar();
get_avatar().setpos( here, guy.pos_bub() + tripoint::north );
guy.set_fac( faction_your_followers );
guy.set_attitude( NPCATT_FOLLOW );
guy.rules.set_flag( ally_rule::follow_close );
REQUIRE( guy.is_walking_with() );
// hungry enough to eat, not to leave the leader for it
guy.set_stored_kcal( guy.get_healthy_kcal() * 7 / 10 );
guy.stomach.empty();
guy.guts.empty();
WHEN( "tree stands beside it" ) {
here.ter_set( adj, ter_t_tree_apple );
here.build_map_cache( 0 );
THEN( "it may harvest" ) {
CHECK( oracle.can_obtain_food( "" ) == behavior::status_t::running );
}
}
WHEN( "tree stands past the follow spacing" ) {
const tripoint_bub_ms far = guy.pos_bub() + tripoint( 6, 0, 0 );
here.ter_set( far, ter_t_tree_apple );
here.build_map_cache( 0 );
REQUIRE( here.is_harvestable( far ) );
REQUIRE_FALSE( guy.obligation_reach()->contains( here.get_abs( far ) ) );
THEN( "it may not" ) {
CHECK( oracle.can_obtain_food( "" ) == behavior::status_t::failure );
}
}
}
}
// can_obtain_food should recognize camp food the same way can_obtain_water
@@ -8602,11 +8680,11 @@ TEST_CASE( "impossible_dispatch_preserves_failed_targets", "[npc][needs]" )
npc &guy = spawn_npc( { 50, 50 }, "test_talker" );
clear_character( guy, true );
guy.set_fac( faction_your_followers );
guy.set_attitude( NPCATT_FOLLOW );
guy.set_stored_kcal( 20000 );
guy.set_hunger( -1 );
guy.set_thirst( 0 );
// no obligation: trip is judged on its destination alone
REQUIRE_FALSE( guy.obligation_reach() );
map &here = get_map();