Merge pull request #89265 from dobbry-vechur/feat/npc-needs-truthful-food

NPCs eat better
This commit is contained in:
Maleclypse
2026-10-25 19:37:26 -05:00
committed by GitHub
24 changed files with 3500 additions and 714 deletions
+7 -7
View File
@@ -95,21 +95,21 @@
"type": "behavior",
"id": "npc_homeostasis",
"strategy": "fallback",
"conditions": [ { "predicate": "npc_needs_warmth_badly" } ],
"children": [ "npc_obtain_warmth", "npc_make_fire" ],
"score": "npc_warmth_urgency"
"conditions": [ { "predicate": "npc_needs_temperature_regulation" } ],
"children": [ "npc_regulate_temperature", "npc_make_fire" ],
"score": "npc_temperature_urgency"
},
{
"type": "behavior",
"id": "npc_obtain_warmth",
"conditions": [ { "predicate": "npc_can_obtain_warmth" } ],
"goal": "seek_warmth"
"id": "npc_regulate_temperature",
"conditions": [ { "predicate": "npc_can_regulate_temperature" } ],
"goal": "regulate_temperature"
},
{
"type": "behavior",
"id": "npc_make_fire",
"conditions": [ { "predicate": "npc_can_make_fire" } ],
"goal": "seek_warmth"
"goal": "regulate_temperature"
},
{
"type": "behavior",
+9
View File
@@ -998,6 +998,15 @@
"based_on": "neither",
"can_resume": false
},
{
"id": "ACT_CAMP_MEAL",
"type": "activity_type",
"activity_level": "NO_EXERCISE",
"verb": "eating",
"ignored_distractions": [ "hunger", "thirst" ],
"based_on": "time",
"can_resume": false
},
{
"id": "ACT_CONSUME",
"type": "activity_type",
+61
View File
@@ -29,6 +29,7 @@
#include "advanced_inv.h"
#include "avatar.h"
#include "avatar_action.h"
#include "basecamp.h"
#include "bionics.h"
#include "bodypart.h"
#include "butchery.h"
@@ -123,6 +124,7 @@
#include "shearing.h"
#include "skill.h"
#include "sounds.h"
#include "stomach.h"
#include "string_formatter.h"
#include "talker.h"
#include "temp_crafting_inventory.h"
@@ -160,6 +162,7 @@ static const activity_id ACT_BOLTCUTTING( "ACT_BOLTCUTTING" );
static const activity_id ACT_BUILD( "ACT_BUILD" );
static const activity_id ACT_BUTCHER( "ACT_BUTCHER" );
static const activity_id ACT_BUTCHER_FULL( "ACT_BUTCHER_FULL" );
static const activity_id ACT_CAMP_MEAL( "ACT_CAMP_MEAL" );
static const activity_id ACT_CHOP_LOGS( "ACT_CHOP_LOGS" );
static const activity_id ACT_CHOP_PLANKS( "ACT_CHOP_PLANKS" );
static const activity_id ACT_CHOP_TREE( "ACT_CHOP_TREE" );
@@ -6242,6 +6245,10 @@ void consume_activity_actor::finish( player_activity &act, Character &who )
item_location consume_loc = consume_location;
// food that changed hands, was claimed or serves no need is left uneaten
if( !was_canceled && consume_loc && who.is_npc() && !who.as_npc()->may_eat_from( consume_loc ) ) {
was_canceled = true;
}
if( !was_canceled ) {
if( consume_loc ) {
who.consume( consume_loc, /*force=*/true );
@@ -6322,6 +6329,59 @@ std::unique_ptr<activity_actor> consume_activity_actor::deserialize( JsonValue &
return actor.clone();
}
static item camp_meal_of( const basecamp &camp, const int kcal )
{
nutrients portion;
portion.calories = static_cast<int64_t>( kcal ) * 1000;
return camp.make_fake_food( portion );
}
void camp_meal_activity_actor::start( player_activity &act, Character &who )
{
const basecamp *bcp = overmap_buffer.find_camp( camp.xy() ).value_or( nullptr );
if( bcp == nullptr || kcal <= 0 ) {
act.set_to_null();
return;
}
act.moves_total = to_moves<int>( who.get_consume_time( camp_meal_of( *bcp, kcal ) ) );
act.moves_left = act.moves_total;
}
void camp_meal_activity_actor::finish( player_activity &act, Character &who )
{
act.set_to_null();
basecamp *bcp = overmap_buffer.find_camp( camp.xy() ).value_or( nullptr );
if( bcp == nullptr || !bcp->allowed_access_by( who ) ) {
return;
}
// whether more food fits is asked again at the end, as for any meal
if( who.is_npc() && !who.as_npc()->intake_fits( npc::need_goal_id::eat_food ) ) {
return;
}
// refused meal satisfies nothing; NPC turns to its own food
if( bcp->feed_workers( who, bcp->camp_food_supply( -kcal ), camp_meal::personal ) <= 0 &&
who.is_npc() ) {
who.as_npc()->refuse_camp_meal();
}
}
void camp_meal_activity_actor::serialize( JsonOut &jsout ) const
{
jsout.start_object();
jsout.member( "camp", camp );
jsout.member( "kcal", kcal );
jsout.end_object();
}
std::unique_ptr<activity_actor> camp_meal_activity_actor::deserialize( JsonValue &jsin )
{
camp_meal_activity_actor actor( tripoint_abs_omt(), 0 );
JsonObject data = jsin.get_object();
data.read( "camp", actor.camp );
data.read( "kcal", actor.kcal );
return actor.clone();
}
void try_sleep_activity_actor::start( player_activity &act, Character &who )
{
act.moves_total = to_moves<int>( duration );
@@ -15474,6 +15534,7 @@ deserialize_functions = {
{ ACT_BUILD, &build_construction_activity_actor::deserialize },
{ ACT_BUTCHER, &butchery_activity_actor::deserialize },
{ ACT_BUTCHER_FULL, &butchery_activity_actor::deserialize },
{ ACT_CAMP_MEAL, &camp_meal_activity_actor::deserialize },
{ ACT_CHOP_LOGS, &chop_logs_activity_actor::deserialize },
{ ACT_CHOP_PLANKS, &chop_planks_activity_actor::deserialize },
{ ACT_CHOP_TREE, &chop_tree_activity_actor::deserialize },
+32
View File
@@ -1945,6 +1945,10 @@ class consume_activity_actor : public activity_actor
const item_location &location() const {
return consume_location;
}
// false once food is gone, taken or not found by a load
bool has_food() const {
return consume_location || !consume_item.is_null();
}
const activity_id &get_type() const override {
static const activity_id ACT_CONSUME( "ACT_CONSUME" );
@@ -1964,6 +1968,34 @@ class consume_activity_actor : public activity_actor
static std::unique_ptr<activity_actor> deserialize( JsonValue &jsin );
};
// meal from a camp's larder, eaten over its consume time; portion leaves the larder only when
// the meal ends, so an interrupted meal takes nothing
class camp_meal_activity_actor : public activity_actor
{
private:
tripoint_abs_omt camp;
int kcal = 0;
public:
camp_meal_activity_actor( const tripoint_abs_omt &camp, int kcal ) : camp( camp ), kcal( kcal ) {}
const activity_id &get_type() const override {
static const activity_id ACT_CAMP_MEAL( "ACT_CAMP_MEAL" );
return ACT_CAMP_MEAL;
}
void start( player_activity &act, Character &who ) override;
void do_turn( player_activity &, Character & ) override {}
void finish( player_activity &act, Character &who ) override;
std::unique_ptr<activity_actor> clone() const override {
return std::make_unique<camp_meal_activity_actor>( *this );
}
void serialize( JsonOut &jsout ) const override;
static std::unique_ptr<activity_actor> deserialize( JsonValue &jsin );
};
class try_sleep_activity_actor : public activity_actor
{
private:
+3 -3
View File
@@ -38,7 +38,7 @@ make_score_function( float ( character_oracle_t::* fun )( std::string_view ) con
std::unordered_map<std::string, std::function<status_t( const oracle_t *, std::string_view ) >>
predicate_map = {{
{ "npc_needs_warmth_badly", make_function( &character_oracle_t::needs_warmth_badly ) },
{ "npc_needs_temperature_regulation", make_function( &character_oracle_t::needs_temperature_regulation ) },
{ "npc_needs_water_badly", make_function( &character_oracle_t::needs_water_badly ) },
{ "npc_needs_food_badly", make_function( &character_oracle_t::needs_food_badly ) },
{ "npc_can_wear_warmer_clothes", make_function( &character_oracle_t::can_wear_warmer_clothes ) },
@@ -48,7 +48,7 @@ predicate_map = {{
{ "npc_has_food", make_function( &character_oracle_t::has_food ) },
{ "npc_can_obtain_food", make_function( &character_oracle_t::can_obtain_food ) },
{ "npc_can_obtain_water", make_function( &character_oracle_t::can_obtain_water ) },
{ "npc_can_obtain_warmth", make_function( &character_oracle_t::can_obtain_warmth ) },
{ "npc_can_regulate_temperature", make_function( &character_oracle_t::can_regulate_temperature ) },
{ "npc_needs_sleep_badly", make_function( &character_oracle_t::needs_sleep_badly ) },
{ "npc_can_sleep", make_function( &character_oracle_t::can_sleep ) },
{ "npc_in_danger", make_function( &character_oracle_t::in_danger ) },
@@ -75,7 +75,7 @@ std::unordered_map<std::string, std::function<float( const oracle_t *, std::stri
score_predicate_map = {{
{ "npc_thirst_urgency", make_score_function( &character_oracle_t::thirst_urgency ) },
{ "npc_hunger_urgency", make_score_function( &character_oracle_t::hunger_urgency ) },
{ "npc_warmth_urgency", make_score_function( &character_oracle_t::warmth_urgency ) },
{ "npc_temperature_urgency", make_score_function( &character_oracle_t::temperature_urgency ) },
{ "npc_sleepiness_urgency", make_score_function( &character_oracle_t::sleepiness_urgency ) },
{ "npc_duty_urgency", make_score_function( &character_oracle_t::duty_urgency ) },
{ "npc_following_urgency", make_score_function( &character_oracle_t::npc_following_urgency ) },
+2
View File
@@ -3194,6 +3194,8 @@ class Character : public Creature, public visitable
float get_bmi_fat() const;
float get_bmi_lean() const;
bool has_calorie_deficit() const;
// serving stays under the engorged line and the eater is not already full
bool can_stomach( const units::volume &serving ) const;
// returns amount of calories burned in a day given various metabolic factors
int get_bmr() const;
// add spent calories to calorie diary (if avatar)
+25 -22
View File
@@ -22,7 +22,6 @@
#include "overmapbuffer.h"
#include "point.h"
#include "ret_val.h"
#include "stomach.h"
#include "type_id.h"
#include "units.h"
#include "vehicle.h"
@@ -72,7 +71,7 @@ namespace behavior
// To avoid a local minima when the character has access to warmth in a shelter but gets cold
// when they go outside, this method needs to only alert when travel time to known shelter
// approaches time to freeze.
status_t character_oracle_t::needs_warmth_badly( std::string_view ) const
status_t character_oracle_t::needs_temperature_regulation( std::string_view ) const
{
// Use player::temp_conv to predict whether the Character is "in trouble".
for( const bodypart_id &bp : subject->get_all_body_parts() ) {
@@ -90,7 +89,7 @@ status_t character_oracle_t::needs_water_badly( std::string_view ) const
return status_t::success;
}
// Check thirst threshold.
if( subject->get_thirst() > 520 ) {
if( subject->get_thirst() > npc_thirst_act_level ) {
return status_t::running;
}
return status_t::success;
@@ -106,9 +105,8 @@ status_t character_oracle_t::needs_food_badly( std::string_view ) const
if( subject->get_hunger() >= 300 && subject->get_starvation() > base_metabolic_rate ) {
return status_t::running;
}
// Long-term: severe caloric deficit (same threshold as address_needs extreme path).
if( subject->get_stored_kcal() + subject->stomach.get_calories() <
subject->get_healthy_kcal() * 3 / 4 ) {
// Long-term: short of calories, the same line where intake stops
if( calories_short( *subject ) ) {
return status_t::running;
}
return status_t::success;
@@ -149,16 +147,21 @@ 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, 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 ) {
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 ) ) {
if( n->may_use_source( loc ) && n->can_satisfy( *loc, id ) ) {
if( n->may_take( loc ) && n->can_satisfy( *loc, id ) ) {
return true;
}
}
@@ -180,26 +183,27 @@ status_t character_oracle_t::has_food( std::string_view ) const
status_t character_oracle_t::can_obtain_food( std::string_view ) const
{
const npc *n = dynamic_cast<const npc *>( subject );
if( !n ) {
if( !n || !n->intake_useful( npc::need_goal_id::eat_food ) ) {
return status_t::failure;
}
// TODO: const_cast because will_eat/rate_food are not const-qualified.
// Safe today (they do not mutate), but fragile if they gain side effects.
// Fix by making the candidate query const once those methods are.
// 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
{
const npc *n = dynamic_cast<const npc *>( subject );
if( !n ) {
if( !n || !n->intake_useful( npc::need_goal_id::drink_water ) ) {
return status_t::failure;
}
// obligation check as in execute_drink_water; failed targets still count, since the
// executor retries them once every source has failed
// TODO: const_cast as in can_obtain_food
// const_cast: see npc::find_food_candidates()
for( const npc::need_candidate &c : const_cast<npc *>( n )->find_water_candidates() ) {
if( n->need_view( npc::need_goal_id::drink_water, n->water_option_cost( c ) ).may_consider() ) {
return status_t::running;
@@ -208,7 +212,7 @@ status_t character_oracle_t::can_obtain_water( std::string_view ) const
return status_t::failure;
}
status_t character_oracle_t::can_obtain_warmth( std::string_view ) const
status_t character_oracle_t::can_regulate_temperature( std::string_view ) const
{
const npc *n = dynamic_cast<const npc *>( subject );
if( !n ) {
@@ -223,9 +227,8 @@ status_t character_oracle_t::can_obtain_warmth( std::string_view ) const
} ) ) {
return status_t::running;
}
// TODO: same const_cast caveat as can_obtain_food above.
// find_nearby_warm_clothing uses can_wear which is not const-qualified.
if( !const_cast<npc *>( n )->find_warmth_candidates().empty() ) {
// const_cast: see npc::find_food_candidates()
if( !const_cast<npc *>( n )->find_temperature_candidates().empty() ) {
return status_t::running;
}
// Already indoors with no other warmth sources: the executor will
@@ -273,7 +276,7 @@ float character_oracle_t::hunger_urgency( std::string_view ) const
return std::clamp( 1.0f - kcal_frac, 0.0f, 1.0f );
}
float character_oracle_t::warmth_urgency( std::string_view ) const
float character_oracle_t::temperature_urgency( std::string_view ) const
{
// 0 = all bodyparts at BODYTEMP_NORM (37C),
// 1 = coldest bodypart at BODYTEMP_FREEZING (28C).
+3 -3
View File
@@ -21,7 +21,7 @@ class character_oracle_t : public oracle_t
/**
* Predicates used by AI to determine goals.
*/
status_t needs_warmth_badly( std::string_view ) const;
status_t needs_temperature_regulation( std::string_view ) const;
status_t needs_water_badly( std::string_view ) const;
status_t needs_food_badly( std::string_view ) const;
status_t can_wear_warmer_clothes( std::string_view ) const;
@@ -31,7 +31,7 @@ class character_oracle_t : public oracle_t
status_t has_food( std::string_view ) const;
status_t can_obtain_food( std::string_view ) const;
status_t can_obtain_water( std::string_view ) const;
status_t can_obtain_warmth( std::string_view ) const;
status_t can_regulate_temperature( std::string_view ) const;
status_t needs_sleep_badly( std::string_view ) const;
status_t can_sleep( std::string_view ) const;
/**
@@ -54,7 +54,7 @@ class character_oracle_t : public oracle_t
*/
float thirst_urgency( std::string_view ) const;
float hunger_urgency( std::string_view ) const;
float warmth_urgency( std::string_view ) const;
float temperature_urgency( std::string_view ) const;
float sleepiness_urgency( std::string_view ) const;
float duty_urgency( std::string_view ) const;
float npc_following_urgency( std::string_view ) const;
+7 -3
View File
@@ -1099,9 +1099,7 @@ ret_val<edible_rating> Character::will_eat( const item &food, bool interactive )
units::volume in_stomach_volume =
food.volume( false, false, 1 ) * compute_effective_food_volume_ratio( food );
if( food.is_food() &&
( stomach.would_be_engorged_with( *this, in_stomach_volume, has_calorie_deficit() ) ||
has_effect( effect_hunger_full ) || has_effect( effect_hunger_engorged ) ) ) {
if( food.is_food() && !can_stomach( in_stomach_volume ) ) {
if( edible ) {
add_consequence( _( "You're full already and will be forcing yourself to eat." ), TOO_FULL );
} else {
@@ -1648,6 +1646,12 @@ void Character::modify_morale( item &food, const int nutr )
}
}
bool Character::can_stomach( const units::volume &serving ) const
{
return !stomach.would_be_engorged_with( *this, serving, has_calorie_deficit() ) &&
!has_effect( effect_hunger_full ) && !has_effect( effect_hunger_engorged );
}
// Used when determining stomach fullness from eating.
double Character::compute_effective_food_volume_ratio( const item &food ) const
{
+3 -28
View File
@@ -2363,34 +2363,9 @@ void iexamine_helper::handle_harvest( Character &you, const itype_id &itemid, bo
if( harvest.has_temperature() ) {
harvest.set_item_temperature( get_weather().get_temperature( you.pos_bub() ) );
}
// Only auto-eat when the NPC is actively seeking food or water
// (self-care need execution). Generic harvest jobs (farming zones,
// player-assigned tasks) should collect, not consume.
bool npc_self_care = false;
if( you.is_npc() ) {
const npc &n = dynamic_cast<const npc &>( you );
const std::string &goal = n.get_committed_goal();
npc_self_care = ( goal == "eat_food" || goal == "drink_water" );
}
if( npc_self_care && harvest.is_food() &&
( you.get_hunger() > 0 || you.has_calorie_deficit() ) &&
you.will_eat( harvest ).success() ) {
const time_duration consume_time = you.get_consume_time( harvest );
trinary result = you.consume( harvest );
if( result != trinary::NONE ) {
you.mod_moves( -to_moves<int>( consume_time ) );
}
if( result == trinary::SOME ) {
// Partial consumption: stash or drop the remainder.
if( !force_drop && you.can_pickVolume( harvest, true ) &&
you.can_pickWeight( harvest, true ) ) {
you.i_add( harvest );
} else {
get_map().add_item_or_charges( you.pos_bub(), harvest );
}
}
} else if( !force_drop && you.can_pickVolume( harvest, true ) &&
you.can_pickWeight( harvest, false ) ) {
// harvest is kept; NPC eats it later like any carried food
if( !force_drop && you.can_pickVolume( harvest, true ) &&
you.can_pickWeight( harvest, false ) ) {
you.i_add( harvest );
you.add_msg_if_player( _( "You harvest: %s." ), harvest.tname() );
you.add_msg_if_npc( _( "<npcname> harvests: %s." ), harvest.tname() );
+66 -18
View File
@@ -45,11 +45,13 @@
#include "string_formatter.h"
#include "translation.h"
#include "type_id.h"
#include "units_fwd.h"
class Creature;
class JsonObject;
class JsonOut;
class JsonValue;
class activity_actor;
class cata_path;
class character_id;
class const_talker;
@@ -606,7 +608,7 @@ struct npc_short_term_cache {
};
// Typed identifiers for executorized need goals.
enum class need_goal_id : int { eat_food, drink_water, seek_warmth, go_to_sleep };
enum class need_goal_id : int { eat_food, drink_water, regulate_temperature, go_to_sleep };
// Per-goal executor state. Tracks a concrete target so the executor
// does not rescan and dither every turn.
@@ -639,8 +641,8 @@ struct npc_short_term_cache {
// interact with failure history.
std::set<tripoint_abs_ms> food_failed_targets;
std::set<tripoint_abs_ms> water_failed_targets;
need_plan warmth_plan;
std::set<tripoint_abs_ms> warmth_failed_targets;
need_plan temperature_plan;
std::set<tripoint_abs_ms> temperature_failed_targets;
need_plan sleep_plan;
std::set<tripoint_abs_ms> sleep_failed_targets;
@@ -662,6 +664,9 @@ struct npc_short_term_cache {
// Cache of locations the NPC has searched recently in npc::find_item()
lru_cache<tripoint_abs_ms, int> searched_tiles;
// diagnostics: number of move() decisions and need executor runs
int decisions = 0;
int executor_calls = 0;
// returns the value of the distance between a friendly creature and the closest enemy to that
// friendly creature.
// returns nullopt if not applicable
@@ -1282,10 +1287,11 @@ class npc : public Character
// Shared query layer: both BT predicates and executor target
// acquisition use these, so policy (visibility, locks, zones,
// will_eat, rate_food) is defined in one place. Non-const because
// the underlying will_eat/rate_food calls are not const-qualified.
// basecamp::allowed_access_by() takes non-const Character, find_fire_spot() is
// non-const
std::vector<need_candidate> find_food_candidates();
std::vector<need_candidate> find_water_candidates();
std::vector<need_candidate> find_warmth_candidates();
std::vector<need_candidate> find_temperature_candidates();
std::vector<scored_item> find_nearby_food( consume_filter filter = consume_filter::any );
std::vector<scored_item> find_nearby_warm_clothing();
std::vector<scored_shelter> find_nearby_shelters() const;
@@ -1302,6 +1308,12 @@ class npc : public Character
int trip_route_calls() const {
return ai_cache.trip_route_calls;
}
int decisions_made() const {
return ai_cache.decisions;
}
int executor_calls() const {
return ai_cache.executor_calls;
}
npc_action address_player();
npc_action long_term_goal_action();
int evaluate_sleep_spot( tripoint_bub_ms p );
@@ -1368,6 +1380,8 @@ class npc : public Character
bool find_job_to_perform();
// Same as if the player pressed '.'
void move_pause();
// hourly bionic and weapon checks a pause makes; true when wielding took the turn
bool pause_upkeep();
void set_movement_mode( const move_mode_id &mode ) override;
@@ -1385,15 +1399,21 @@ class npc : public Character
bool can_take_that( const item &it );
// Consider hoarding
bool wants_take_that( const item &it );
// what's taken from a source: an item or a charge of it with its contents, or liquid
// poured out of its container
enum class take_kind : int { whole, drink_extract };
// Consider a life of crime
bool would_take_that( const item &it, const tripoint_bub_ms &p ) const;
bool would_take_that( const item &it, const tripoint_bub_ms &p,
take_kind take = take_kind::whole ) const;
// same for a vehicle's cargo
bool would_take_that( const vehicle &veh, const tripoint_bub_ms &p ) const;
bool would_take_that( const vehicle &veh, const tripoint_bub_ms &p,
take_kind take = take_kind::whole ) const;
// player's things go by trust and witnesses; what names the thing in the debug log
bool would_steal_from_player( const tripoint_bub_ms &p,
const std::function<std::string()> &what ) const;
// another faction's goods: only if it shares them or is hostile
bool would_take_faction_goods( const faction_id &owner ) const;
// another faction's goods: only if it shares them or is hostile; a drink poured out also
// when it shares public goods
bool would_take_faction_goods( const faction_id &owner, take_kind take ) const;
// Look around and pick an item
void find_item();
// Move to, or grab, our targeted item
@@ -1404,6 +1424,10 @@ class npc : public Character
bool empty_before_pickup();
/** Whether a drink from a container on the ground may start or go on here. */
bool may_drink_here() const;
/** Trouble that ends a meal or a drink and bars starting one. */
bool in_harms_way() const;
/** Drops the activity for good and a fetch with it; the task it interrupted takes over. */
void abandon_activity();
/** Picks up items and returns a list of them. */
std::list<item> pick_up_item_map( const tripoint_bub_ms &where );
std::list<item> pick_up_item_vehicle( vehicle &veh, int part_index );
@@ -1431,18 +1455,41 @@ class npc : public Character
bool saw_player_recently() const;
// need's item filter plus will_eat (diet and stomach room)
bool can_satisfy( const item &it, npc_short_term_cache::need_goal_id id ) const;
// stomach room left for the need's intake; a meal already in stomach ends it
bool intake_fits( npc_short_term_cache::need_goal_id id ) const;
// intake_fits() with the need at its act level
bool intake_useful( npc_short_term_cache::need_goal_id id ) const;
// access: reservation, no-go areas and zones, plus owners of the item, its containers and
// cargo vehicle
bool may_use_source( const item_location &loc ) const;
bool may_use_source( const item_location &loc, take_kind take ) const;
static take_kind take_kind_for( const item_location &loc );
// may_use_source() with the take_kind that loc implies
bool may_take( const item_location &loc ) const;
// access and diet for a meal or drink from loc, asked again when the meal ends
bool may_eat_from( const item_location &loc ) const;
// hunger and thirst as rate_food weighs them for filter's need
std::pair<int, int> consume_wants( consume_filter filter ) const;
/** Returns true if food was consumed, false otherwise. */
bool consume_food( consume_filter filter = consume_filter::any );
bool consume_food_from_camp( consume_filter filter = consume_filter::any );
// camp meal it didn't eat isn't served again for a while
void refuse_camp_meal();
// activity a need goal starts and owns
bool is_self_care_activity() const;
// task waits aside until the rest is over
void suspend_task_for_rest();
void resume_suspended_task();
void drop_suspended_task();
// one turn's drink from a source without container
units::volume sip_of_water() const;
// part of serving that fits now: all, part under the engorged line, or none
units::volume drinkable_part( const units::volume &serving ) const;
// eats loc through the consume activity: real time, interruptible
bool start_eating( const item_location &loc );
// false in harm's way or when the activity doesn't start
bool start_meal( const activity_actor &meal );
// true while a meal from an item or a camp's larder runs
bool is_eating() const;
int get_thirst() const override;
// Movement on the overmap scale
@@ -1568,8 +1615,8 @@ class npc : public Character
const std::string &get_committed_goal() const {
return ai_cache.intention.goal;
}
// tests only: commits without ending the old pursuit or recording an option
void set_committed_goal( const std::string &goal ) {
// commits without ending the old pursuit or recording an option
void set_committed_goal_for_test( const std::string &goal ) {
ai_cache.intention = { goal, calendar::turn, {}, npc_permission::allowed };
}
const npc_intention &get_intention() const {
@@ -1580,9 +1627,8 @@ class npc : public Character
}
// Clear the committed goal and any associated executor state
// (plan + failed_targets + side effects like effect_lying_down).
// Also cancels self-care activities (forage/harvest/fire) that
// bypass BT re-evaluation in move(). Use this instead of
// set_committed_goal("") to avoid stale state across transitions.
// Also cancels a self-care activity a need goal started, which bypasses BT
// re-evaluation in move().
void clear_committed_goal();
using need_result = npc_short_term_cache::need_result;
using need_plan = npc_short_term_cache::need_plan;
@@ -1654,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;
@@ -1662,7 +1710,7 @@ class npc : public Character
need_result execute_need_goal( std::string_view goal );
need_result execute_eat_food();
need_result execute_drink_water();
need_result execute_seek_warmth();
need_result execute_regulate_temperature();
need_result execute_go_to_sleep();
std::vector<npc_short_term_cache::need_candidate> find_sleep_candidates();
enum class fire_spot_check : int {
@@ -1695,8 +1743,8 @@ class npc : public Character
const need_plan &get_water_plan() const {
return ai_cache.water_plan;
}
const need_plan &get_warmth_plan() const {
return ai_cache.warmth_plan;
const need_plan &get_temperature_plan() const {
return ai_cache.temperature_plan;
}
const need_plan &get_sleep_plan() const {
return ai_cache.sleep_plan;
+32 -17
View File
@@ -22,6 +22,7 @@
#include "npc_class.h"
#include "npc_decision_category.h"
#include "npc_trip.h"
#include "stomach.h"
#include "translations.h"
#include "type_id.h"
#include "vehicle.h"
@@ -99,14 +100,16 @@ static need_levels levels_for( const npc::need_goal_id id )
{
switch( id ) {
case npc::need_goal_id::drink_water:
// 520 (thirsty predicate) and 800 on the 1200 dehydration scale, emergency at critical (900)
return { 520.0f / 1200.0f, 800.0f / 1200.0f, need_critical_urgency };
// act level and 800 on the 1200 dehydration scale, emergency at critical (900)
return { npc_thirst_act_level / 1200.0f, 800.0f / 1200.0f, need_critical_urgency };
case npc::need_goal_id::go_to_sleep:
return { static_cast<float>( sleepiness_levels::TIRED ) / 1000.0f,
static_cast<float>( sleepiness_levels::DEAD_TIRED ) / 1000.0f,
static_cast<float>( sleepiness_levels::EXHAUSTED ) / 1000.0f };
case npc::need_goal_id::eat_food:
case npc::need_goal_id::seek_warmth:
// calorie urgency, share of healthy stored calories missing
return { food_act_level, 0.4f, 0.6f };
case npc::need_goal_id::regulate_temperature:
break;
}
// no option costs declared: overrides wait for the critical level
@@ -243,8 +246,8 @@ npc_permission_verdict npc::judge_option( const npc_option_cost &cost ) const
};
const Character &leader = get_player_character();
const std::optional<npc_reach> reach = obligation_reach();
const bool holds_up = cost.needs_walk || cost.occupies;
const bool beyond = cost.where_known && reach &&
const bool holds_up = cost.holds_up();
const bool beyond = cost.where_known() && reach &&
( !reach->contains( cost.where ) || !cost.route_in_reach );
if( is_walking_with() ) {
npc_permission_verdict v{ npc_permission::allowed, npc_obligation::accompany, 0.0f };
@@ -252,7 +255,7 @@ npc_permission_verdict npc::judge_option( const npc_option_cost &cost ) const
// strapped in or bedded down aboard the group's vehicle is riding along, not
// dropping out; the best case assumes such a spot exists
const bool on_a_bed = reach && reach->aboard != nullptr && in_vehicle &&
( !cost.where_known || npc_vehicle_bed_at( cost.where, *reach->aboard ) );
( !cost.where_known() || npc_vehicle_bed_at( cost.where, *reach->aboard ) );
if( beyond ) {
v.permission = npc_permission::emergency_only;
} else if( !leader_is_resting( leader ) && !on_a_bed ) {
@@ -292,7 +295,7 @@ npc_permission_verdict npc::judge_option( const npc_option_cost &cost ) const
npc_need_view npc::need_view( const need_goal_id id, const npc_option_cost &cost ) const
{
npc_need_view view;
view.kind = cost.where_known ? npc_option_kind::actual : npc_option_kind::best_case;
view.kind = cost.where_known() ? npc_option_kind::actual : npc_option_kind::best_case;
view.raw = current_need_urgency( id );
view.verdict = judge_option( cost );
view.floor = npc_need_floor( id, view.verdict );
@@ -312,6 +315,12 @@ npc_need_view npc::need_view( const need_goal_id id,
return need_view( id, *cost );
}
bool calories_short( const Character &who )
{
const int pending = who.stomach.get_calories() + who.guts.get_calories();
return who.get_stored_kcal() + pending < who.get_healthy_kcal() * ( 1.0f - food_act_level );
}
bool should_interrupt( const npc_pursuit &current, const npc_challenger &challenger )
{
if( current.cost == npc_interrupt_cost::wake_only ) {
@@ -394,7 +403,10 @@ void npc::explain_override( const need_goal_id id )
speech = _( "I can't stay awake any longer." );
break;
case need_goal_id::eat_food:
case need_goal_id::seek_warmth:
issue = "need_override_food";
speech = _( "I really need something to eat. I'll be right back." );
break;
case need_goal_id::regulate_temperature:
return;
}
// said even with complaints turned off, unless it would wake the player
@@ -404,19 +416,24 @@ 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 );
if( !intake_useful( 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;
}
@@ -593,13 +610,11 @@ void npc::review_self_care_activity()
// TODO: the pursuit isn't saved, so a load cancels an emergency harvest that
// overrode an obligation; the activity would need to keep its need
if( !may_occupy_here() ) {
cancel_activity();
abandon_activity();
}
}
bool npc::may_occupy_here() const
{
npc_option_cost here = npc_option_cost::in_place( pos_abs() );
here.occupies = true;
return judge_option( here ).permission == npc_permission::allowed;
return judge_option( npc_option_cost::stay( pos_abs() ) ).permission == npc_permission::allowed;
}
+65 -13
View File
@@ -9,31 +9,76 @@
#include "coordinates.h"
#include "point.h"
class Character;
// what keeps the NPC from answering an obligation once at the option's spot
enum class npc_option_hold : int {
// usable from there, adjacent tiles too, left at once
none,
// harvest, forage, warming there: stays put for a while, counts like a walk
occupy
};
// what judging the option's spot rests on
enum class npc_option_basis : int {
// no position yet: spot assumed inside reach, never a concrete option
assumed,
// NPC's own tile
here,
// destination alone: no reach, so no route searched, or the route search failed; a
// trip judged this way may have no way there
destination,
// route found and judged against reach
routed
};
// what an option asks of the NPC's availability, judged against its obligations
struct npc_option_cost {
// where NPC stands while doing it
tripoint_abs_ms where;
// false: usable from here, adjacent tiles included
bool needs_walk = false;
npc_option_hold hold = npc_option_hold::none;
// walks a route first
bool walks = false;
// false when the option leaves the NPC unable to react (asleep)
bool responsive = true;
// the route to be walked keeps to the reach policy
bool route_in_reach = true;
// false only for best_case()
bool where_known = true;
// harvest, forage: holds the NPC in place, counts like a walk; stays last for the factories
bool occupies = false;
npc_option_basis basis = npc_option_basis::here;
bool holds_up() const {
return walks || hold != npc_option_hold::none;
}
bool where_known() const {
return basis != npc_option_basis::assumed;
}
static npc_option_cost in_place( const tripoint_abs_ms &where ) {
return { where, false, true, true, true };
return located( where, npc_option_basis::here );
}
static npc_option_cost trip( const tripoint_abs_ms &where, bool needs_walk, bool responsive,
bool route_in_reach ) {
return { where, needs_walk, responsive, route_in_reach, true };
// activity on the NPC's own tile, keeps it there until done or dropped
static npc_option_cost stay( const tripoint_abs_ms &where ) {
npc_option_cost cost = in_place( where );
cost.hold = npc_option_hold::occupy;
return cost;
}
// no position yet: judged as a spot inside the reach, never a concrete option
static npc_option_cost best_case( const tripoint_abs_ms &spot, bool responsive ) {
return { spot, false, responsive, true, false };
// walk with no route found, judged as one out of reach
static npc_option_cost unrouted( const tripoint_abs_ms &where ) {
npc_option_cost cost = located( where, npc_option_basis::destination );
cost.walks = true;
cost.route_in_reach = false;
return cost;
}
// concrete option at a known spot; every other field keeps its default
static npc_option_cost located( const tripoint_abs_ms &where, const npc_option_basis basis ) {
npc_option_cost cost;
cost.where = where;
cost.basis = basis;
return cost;
}
static npc_option_cost best_case( const tripoint_abs_ms &spot, const bool responsive ) {
npc_option_cost cost = located( spot, npc_option_basis::assumed );
cost.responsive = responsive;
return cost;
}
};
@@ -149,6 +194,13 @@ struct npc_challenger {
constexpr float need_critical_urgency = 0.75f;
// how far an NPC that's resting or drinking notices trouble walking up
constexpr int rest_watch_radius = 6;
// stored calories short of healthy by this share make food worth acquiring
constexpr float food_act_level = 0.2f;
// adjusted thirst above which water need fires
constexpr int npc_thirst_act_level = 520;
// calories stored and on their way short of the healthy amount by food_act_level
bool calories_short( const Character &who );
bool should_interrupt( const npc_pursuit &current, const npc_challenger &challenger );
+11 -3
View File
@@ -264,14 +264,22 @@ std::optional<npc_option_cost> npc::trip_cost( const npc_trip_spec &spec,
const bool at_hand = spec.kind == npc_trip_kind::within_range ?
spec.source.z() == posz() && square_dist( pos_bub(), spec.source ) <= spec.range :
pos_bub() == spec.source;
return npc_option_cost::trip( here.get_abs( spec.source ), !at_hand, responsive, true );
npc_option_cost cost = npc_option_cost::located( here.get_abs( spec.source ),
npc_option_basis::destination );
cost.walks = !at_hand;
cost.responsive = responsive;
return cost;
}
const std::optional<npc_trip> trip = resolve_trip( spec );
if( !trip ) {
return std::nullopt;
}
return npc_option_cost::trip( here.get_abs( trip->dest ), !trip->path.empty(), responsive,
trip->route_in_reach );
npc_option_cost cost = npc_option_cost::located( here.get_abs( trip->dest ),
npc_option_basis::routed );
cost.walks = !trip->path.empty();
cost.responsive = responsive;
cost.route_in_reach = trip->route_in_reach;
return cost;
}
bool npc::move_to_and_verify( const npc_trip_spec &spec )
+491 -292
View File
File diff suppressed because it is too large Load Diff
+21
View File
@@ -392,6 +392,27 @@ bool stomach_contents::would_be_engorged_with( const Character &owner, units::vo
return calorie_deficit ? fills_at_least( held, cap, 1, 1 ) : fills_at_least( held, cap, 5, 6 );
}
// first whole millilitre fills_at_least() accepts
static units::volume first_filling_ml( const units::volume &cap, int num, int den )
{
return units::from_milliliter( static_cast<int>(
( static_cast<int64_t>( units::to_milliliter( cap ) ) * num + den - 1 ) / den ) );
}
units::volume stomach_contents::room_before_engorged( const Character &owner,
bool calorie_deficit ) const
{
const units::volume cap = capacity( owner );
const units::volume line = calorie_deficit ? first_filling_ml( cap, 1, 1 ) :
first_filling_ml( cap, 5, 6 );
return std::max( 0_ml, line - contains() - 1_ml );
}
bool stomach_contents::holds_a_meal( const Character &owner ) const
{
return get_calories() > 0 && contains() >= capacity( owner ) / 2;
}
bool stomach_contents::would_be_full_with( const Character &owner, units::volume intake,
bool calorie_deficit ) const
{
+4
View File
@@ -180,6 +180,10 @@ class stomach_contents
// These functions need a ref to the stomach's owner because capacity() does
bool would_be_engorged_with( const Character &owner, units::volume intake,
bool calorie_deficit ) const;
// most that can still go in before would_be_engorged_with() objects
units::volume room_before_engorged( const Character &owner, bool calorie_deficit ) const;
// food in a stomach at least half full; water alone isn't a meal
bool holds_a_meal( const Character &owner ) const;
bool would_be_full_with( const Character &owner, units::volume intake, bool calorie_deficit ) const;
// how much stomach capacity you have left before you puke from stuffing your gob
units::volume stomach_remaining( const Character &owner ) const;
+26 -30
View File
@@ -223,14 +223,13 @@ TEST_CASE( "check_npc_behavior_tree", "[npc][behavior]" )
weather.clear_temp_cache();
REQUIRE( units::to_fahrenheit( weather.get_temperature( test_npc.pos_bub() ) ) == Approx( 0 ) );
test_npc.update_bodytemp();
REQUIRE( oracle.needs_warmth_badly( "" ) == behavior::status_t::running );
REQUIRE( oracle.needs_temperature_regulation( "" ) == behavior::status_t::running );
CHECK( npc_needs.tick( &oracle ) == "idle" );
test_npc.worn.wear_item( test_npc, item( itype_backpack ), false, false );
item_location sweater = test_npc.i_add( item( itype_sweater ) );
// With warmth unified under seek_warmth, inventory clothing
// triggers can_obtain_warmth (not legacy can_wear_warmer_clothes).
CHECK( oracle.can_obtain_warmth( "" ) == behavior::status_t::running );
CHECK( npc_needs.tick( &oracle ) == "seek_warmth" );
// carried clothing: one way to regulate temperature
CHECK( oracle.can_regulate_temperature( "" ) == behavior::status_t::running );
CHECK( npc_needs.tick( &oracle ) == "regulate_temperature" );
item sweater_copy = *sweater;
test_npc.wear_item( sweater_copy );
sweater.remove_item();
@@ -240,9 +239,8 @@ TEST_CASE( "check_npc_behavior_tree", "[npc][behavior]" )
get_map().furn_set( test_npc.pos_bub() + point::east, furn_f_firering );
get_map().build_map_cache( 0 );
REQUIRE( oracle.can_make_fire( "" ) == behavior::status_t::running );
// Fire supplies make can_obtain_warmth return running (fire is
// a warmth strategy), so npc_obtain_warmth succeeds with seek_warmth.
CHECK( npc_needs.tick( &oracle ) == "seek_warmth" );
// fire supplies: one way to regulate temperature
CHECK( npc_needs.tick( &oracle ) == "regulate_temperature" );
}
SECTION( "Hungry" ) {
test_npc.set_hunger( 500 );
@@ -353,7 +351,7 @@ TEST_CASE( "check_npc_behavior_tree", "[npc][behavior]" )
weather.temperature = units::from_fahrenheit( 0 );
weather.clear_temp_cache();
test_npc.update_bodytemp();
REQUIRE( oracle.needs_warmth_badly( "" ) == behavior::status_t::running );
REQUIRE( oracle.needs_temperature_regulation( "" ) == behavior::status_t::running );
test_npc.set_thirst( 700 );
REQUIRE( oracle.needs_water_badly( "" ) == behavior::status_t::running );
@@ -364,21 +362,21 @@ TEST_CASE( "check_npc_behavior_tree", "[npc][behavior]" )
const item_group::ItemList water_items = item_group::items_from(
Item_spawn_data_test_bottle_water );
test_npc.i_add( water_items.front() );
REQUIRE( oracle.can_obtain_warmth( "" ) == behavior::status_t::running );
REQUIRE( oracle.can_regulate_temperature( "" ) == behavior::status_t::running );
REQUIRE( oracle.has_water( "" ) == behavior::status_t::running );
// warmth_urgency (near 1.0) >> thirst_urgency (0.58)
CHECK( npc_needs.tick( &oracle ) == "seek_warmth" );
// temperature_urgency (near 1.0) >> thirst_urgency (0.58)
CHECK( npc_needs.tick( &oracle ) == "regulate_temperature" );
}
SECTION( "Freezing with fire supplies also wins over thirst" ) {
// Proves the score lives on npc_homeostasis (the fallback branch),
// not on npc_obtain_warmth. If the score were misplaced on the
// not on npc_regulate_temperature. If the score were misplaced on the
// leaf, this path through npc_make_fire would be unscored.
weather_manager &weather = get_weather();
weather.temperature = units::from_fahrenheit( 0 );
weather.clear_temp_cache();
test_npc.update_bodytemp();
REQUIRE( oracle.needs_warmth_badly( "" ) == behavior::status_t::running );
REQUIRE( oracle.needs_temperature_regulation( "" ) == behavior::status_t::running );
test_npc.set_thirst( 700 );
REQUIRE( oracle.needs_water_badly( "" ) == behavior::status_t::running );
@@ -393,13 +391,12 @@ TEST_CASE( "check_npc_behavior_tree", "[npc][behavior]" )
// find_fire_spot needs the map cache.
get_map().furn_set( test_npc.pos_bub() + point::east, furn_f_firering );
get_map().build_map_cache( 0 );
// Fire supplies make can_obtain_warmth return running (fire is
// a warmth strategy inside find_warmth_candidates).
REQUIRE( oracle.can_obtain_warmth( "" ) == behavior::status_t::running );
// fire supplies: one way to regulate temperature
REQUIRE( oracle.can_regulate_temperature( "" ) == behavior::status_t::running );
REQUIRE( oracle.can_make_fire( "" ) == behavior::status_t::running );
REQUIRE( oracle.has_water( "" ) == behavior::status_t::running );
CHECK( npc_needs.tick( &oracle ) == "seek_warmth" );
CHECK( npc_needs.tick( &oracle ) == "regulate_temperature" );
}
SECTION( "Dead tired -- sleep is feasible" ) {
// past TIRED, so can_sleep holds; utility scoring handles duty priority
@@ -522,7 +519,7 @@ TEST_CASE( "check_npc_behavior_tree", "[npc][behavior]" )
weather.temperature = units::from_fahrenheit( 0 );
weather.clear_temp_cache();
test_npc.update_bodytemp();
REQUIRE( oracle.needs_warmth_badly( "" ) == behavior::status_t::running );
REQUIRE( oracle.needs_temperature_regulation( "" ) == behavior::status_t::running );
// No warm clothes, no fire, but indoor tile adjacent
map &here = get_map();
@@ -530,17 +527,16 @@ TEST_CASE( "check_npc_behavior_tree", "[npc][behavior]" )
here.ter_set( adj, ter_t_floor );
REQUIRE( oracle.can_take_shelter( "" ) == behavior::status_t::running );
// seek_warmth fires before legacy take_shelter because
// npc_obtain_warmth is first in the warmth fallback chain.
CHECK( npc_needs.tick( &oracle ) == "seek_warmth" );
// adjacent shelter: one way to regulate temperature
CHECK( npc_needs.tick( &oracle ) == "regulate_temperature" );
}
SECTION( "Freezing outdoors with no shelter" ) {
weather_manager &weather = get_weather();
weather.temperature = units::from_fahrenheit( 0 );
weather.clear_temp_cache();
test_npc.update_bodytemp();
REQUIRE( oracle.needs_warmth_badly( "" ) == behavior::status_t::running );
REQUIRE( oracle.can_obtain_warmth( "" ) == behavior::status_t::failure );
REQUIRE( oracle.needs_temperature_regulation( "" ) == behavior::status_t::running );
REQUIRE( oracle.can_regulate_temperature( "" ) == behavior::status_t::failure );
// No warmth sources, no fire supplies -> idle
CHECK( npc_needs.tick( &oracle ) == "idle" );
@@ -959,20 +955,20 @@ TEST_CASE( "npc_urgency_score_predicates", "[npc][behavior]" )
CHECK( oracle.hunger_urgency( "" ) == Approx( 1.0f ).margin( 0.01f ) );
}
SECTION( "warmth_urgency responds to cold bodyparts" ) {
SECTION( "temperature_urgency responds to cold bodyparts" ) {
// Explicitly set baseline -- clear_character does not guarantee temps
guy.set_all_parts_temp_conv( BODYTEMP_NORM );
CHECK( oracle.warmth_urgency( "" ) < 0.01f );
CHECK( oracle.temperature_urgency( "" ) < 0.01f );
// Single cold bodypart drives the score
guy.set_part_temp_conv( body_part_torso, BODYTEMP_VERY_COLD );
float cold_score = oracle.warmth_urgency( "" );
float cold_score = oracle.temperature_urgency( "" );
CHECK( cold_score > 0.5f );
CHECK( cold_score < 1.0f );
// At BODYTEMP_FREEZING the score saturates near 1.0
guy.set_part_temp_conv( body_part_torso, BODYTEMP_FREEZING );
CHECK( oracle.warmth_urgency( "" ) == Approx( 1.0f ).margin( 0.05f ) );
CHECK( oracle.temperature_urgency( "" ) == Approx( 1.0f ).margin( 0.05f ) );
}
SECTION( "sleepiness_urgency scales from 0 to 1" ) {
@@ -989,7 +985,7 @@ TEST_CASE( "npc_urgency_score_predicates", "[npc][behavior]" )
SECTION( "score predicates registered in score_predicate_map" ) {
CHECK( behavior::score_predicate_map.count( "npc_thirst_urgency" ) == 1 );
CHECK( behavior::score_predicate_map.count( "npc_hunger_urgency" ) == 1 );
CHECK( behavior::score_predicate_map.count( "npc_warmth_urgency" ) == 1 );
CHECK( behavior::score_predicate_map.count( "npc_temperature_urgency" ) == 1 );
CHECK( behavior::score_predicate_map.count( "npc_sleepiness_urgency" ) == 1 );
}
@@ -1270,7 +1266,7 @@ TEST_CASE( "npc_decision_category_mapping", "[npc][behavior]" )
CHECK( bt_goal_to_category( "eat_food" ) == decision_category::needs );
CHECK( bt_goal_to_category( "go_to_sleep" ) == decision_category::needs );
CHECK( bt_goal_to_category( "start_fire" ) == decision_category::needs );
CHECK( bt_goal_to_category( "seek_warmth" ) == decision_category::needs );
CHECK( bt_goal_to_category( "regulate_temperature" ) == decision_category::needs );
// Legacy warmth goals removed from BT; now unmodeled.
CHECK( bt_goal_to_category( "wear_warmer_clothes" ) == decision_category::unmodeled );
CHECK( bt_goal_to_category( "take_shelter" ) == decision_category::unmodeled );
+57
View File
@@ -59,7 +59,10 @@
#include "type_id.h"
#include "value_ptr.h"
static const activity_id ACT_CAMP_MEAL( "ACT_CAMP_MEAL" );
static const efftype_id effect_nausea( "nausea" );
static const efftype_id effect_npc_fire_bad( "npc_fire_bad" );
static const faction_id faction_your_followers( "your_followers" );
@@ -662,6 +665,60 @@ TEST_CASE( "camp_mission_puts_the_workers_craft_aside", "[camp][npc]" )
back_at_the_craft( *comp );
}
}
// start camp meal like a need goal
const auto start_camp_meal = [&]() {
REQUIRE( guy.start_meal( camp_meal_activity_actor( camp_omt, 300 ) ) );
REQUIRE( guy.is_self_care_activity() );
};
GIVEN( "worker eats a camp meal" ) {
start_camp_meal();
go_on_mission();
camp.finish_return( *comp, false, "returns.", skill_menial.str(), 0 );
THEN( "meal not resumed" ) {
REQUIRE( comp->is_active() );
CHECK_FALSE( comp->activity );
CHECK( comp->backlog.empty() );
}
}
GIVEN( "worker stops crafting for a camp meal" ) {
craft_until_progress();
start_camp_meal();
REQUIRE( guy.backlog.size() == backlog_size + 1 );
WHEN( "it goes off on a mission and returns" ) {
go_on_mission();
camp.finish_return( *comp, false, "returns.", skill_menial.str(), 0 );
THEN( "it continues with its craft, not the meal" ) {
back_at_the_craft( *comp );
}
}
WHEN( "saved and loaded during the mission" ) {
go_on_mission();
const shared_ptr_fast<npc> loaded = saved_and_loaded();
camp.finish_return( *loaded, false, "returns.", skill_menial.str(), 0 );
THEN( "it continues with its craft, not the meal" ) {
back_at_the_craft( *loaded );
}
}
WHEN( "harm interrupts the meal" ) {
guy.add_effect( effect_npc_fire_bad, 1_minutes );
guy.set_moves( 100 );
guy.move();
THEN( "meal dropped, craft resumes, larder untouched" ) {
CHECK( guy.activity.id() != ACT_CAMP_MEAL );
const auto has = []( const std::list<player_activity> &acts, const activity_id & id ) {
return std::any_of( acts.begin(), acts.end(), [&id]( const player_activity & act ) {
return act.id() == id;
} );
};
CHECK_FALSE( has( guy.backlog, ACT_CAMP_MEAL ) );
// at its task, or queued behind a step away from harm
CHECK( guy.has_activity() );
CHECK( ( guy.activity.id() == task ||
( !guy.backlog.empty() && guy.backlog.front().id() == task ) ) );
CHECK( fac.food_supply().kcal() == 10000 );
}
}
}
GIVEN( "worker on the way to workbench" ) {
start_walking_to_workbench();
go_on_mission();
File diff suppressed because it is too large Load Diff
+24 -16
View File
@@ -188,7 +188,7 @@ static void make_asleep_on_bed( npc &guy )
get_map().furn_set( guy.pos_bub(), furn_f_bed );
guy.set_sleepiness( 600 );
guy.set_stim( 0 );
guy.set_committed_goal( "go_to_sleep" );
guy.set_committed_goal_for_test( "go_to_sleep" );
guy.set_moves( 100 );
guy.execute_need_goal( "go_to_sleep" );
REQUIRE( asleep( guy ) );
@@ -383,7 +383,7 @@ TEST_CASE( "need_floor_order_holds_for_every_need", "[npc][needs][obligation]" )
{
for( const npc::need_goal_id id : {
npc::need_goal_id::drink_water, npc::need_goal_id::go_to_sleep,
npc::need_goal_id::eat_food, npc::need_goal_id::seek_warmth
npc::need_goal_id::eat_food, npc::need_goal_id::regulate_temperature
} ) {
const float strict = npc_need_floor( id, { npc_permission::discouraged, npc_obligation::none, 0.0f } );
const float emergency = npc_need_floor( id, { npc_permission::emergency_only, npc_obligation::none, 0.0f } );
@@ -410,8 +410,10 @@ TEST_CASE( "accompany_floor_slides_while_the_leader_stays_in_one_place",
sleep_setup();
map &here = get_map();
npc &guy = tired_follower( leader_pos + tripoint( 2, 0, 0 ), 0 );
const npc_option_cost here_rest = npc_option_cost::trip( here.get_abs( leader_pos + tripoint( 2, 0,
0 ) ), false, false, true );
npc_option_cost here_rest = npc_option_cost::located( here.get_abs( leader_pos + tripoint( 2, 0,
0 ) ),
npc_option_basis::routed );
here_rest.responsive = false;
const auto rest_view = [&guy]( const npc_option_cost & c ) {
return guy.need_view( npc::need_goal_id::go_to_sleep, c );
};
@@ -443,8 +445,12 @@ TEST_CASE( "accompany_floor_slides_while_the_leader_stays_in_one_place",
CHECK( w.floor == Approx( 0.575f ) );
}
WHEN( "the spot is past the spacing" ) {
const npc_need_view w = rest_view( npc_option_cost::trip( here.get_abs( leader_pos + tripoint( 8, 0,
0 ) ), true, false, true ) );
npc_option_cost far_rest = npc_option_cost::located( here.get_abs( leader_pos + tripoint( 8, 0,
0 ) ),
npc_option_basis::routed );
far_rest.walks = true;
far_rest.responsive = false;
const npc_need_view w = rest_view( far_rest );
CHECK( w.verdict.by == npc_obligation::accompany );
CHECK( w.floor == Approx( 0.575f ) );
}
@@ -491,7 +497,9 @@ TEST_CASE( "vehicle_bed_relaxes_accompaniment_only_on_a_belted_seat_or_bed",
seat( get_avatar(), leader_pos );
npc &guy = tired_follower( leader_pos + tripoint( 0, 10, 0 ), 0 );
const auto rest_here = [&guy]() {
return guy.judge_option( npc_option_cost::trip( guy.pos_abs(), false, false, true ) );
npc_option_cost rest = npc_option_cost::located( guy.pos_abs(), npc_option_basis::routed );
rest.responsive = false;
return guy.judge_option( rest );
};
SECTION( "a belted seat" ) {
@@ -586,7 +594,7 @@ TEST_CASE( "seated_follower_sleeps_when_leader_sleeps_aboard", "[npc][behavior][
npc &guy = tired_follower( leader_pos + tripoint( 0, 10, 0 ), 250 );
seat( guy, mount_tile( veh, point_rel_ms( -2, 0 ) ) );
run_npc_turns( guy, 20, []( const npc & n ) {
run_npc_turns( guy, 40, []( const npc & n ) {
return n.in_sleep_state();
} );
CAPTURE( guy.pos_bub(), guy.get_committed_goal(), guy.in_vehicle, guy.in_vehicle_bed() );
@@ -646,7 +654,7 @@ TEST_CASE( "npc_settling_ignores_player_sleep_throttle", "[npc][needs][sleep]" )
// a roll now starts the player's 30-minute throttle
guy.can_sleep();
REQUIRE_FALSE( guy.can_sleep() );
guy.set_committed_goal( "go_to_sleep" );
guy.set_committed_goal_for_test( "go_to_sleep" );
guy.set_moves( 100 );
CHECK( guy.execute_need_goal( "go_to_sleep" ) == npc::need_result::progressed );
CHECK( asleep( guy ) );
@@ -663,7 +671,7 @@ TEST_CASE( "npc_settling_rolls_until_asleep_or_gives_the_spot_up", "[npc][needs]
here.furn_set( bed, furn_f_bed );
guy.set_sleepiness( 300 );
guy.set_stim( 100 );
guy.set_committed_goal( "go_to_sleep" );
guy.set_committed_goal_for_test( "go_to_sleep" );
guy.set_moves( 100 );
REQUIRE( guy.execute_need_goal( "go_to_sleep" ) == npc::need_result::holding );
REQUIRE( guy.has_effect( effect_lying_down ) );
@@ -796,7 +804,7 @@ TEST_CASE( "sleep_session_survives_save_and_load", "[npc][sleep]" )
get_map().furn_set( guy.pos_bub(), furn_f_bed );
guy.set_sleepiness( 600 );
guy.set_stim( 100 );
guy.set_committed_goal( "go_to_sleep" );
guy.set_committed_goal_for_test( "go_to_sleep" );
guy.set_moves( 100 );
REQUIRE( guy.execute_need_goal( "go_to_sleep" ) == npc::need_result::holding );
const npc_sleep_session before = guy.get_sleep_session();
@@ -817,7 +825,7 @@ TEST_CASE( "settling_rolls_only_on_its_cadence", "[npc][needs][sleep]" )
get_map().furn_set( guy.pos_bub(), furn_f_bed );
guy.set_sleepiness( 600 );
guy.set_stim( 100 );
guy.set_committed_goal( "go_to_sleep" );
guy.set_committed_goal_for_test( "go_to_sleep" );
guy.set_moves( 100 );
REQUIRE( guy.execute_need_goal( "go_to_sleep" ) == npc::need_result::holding );
// every roll from here succeeds: on a bed at 600 with no stim
@@ -859,7 +867,7 @@ TEST_CASE( "loaded_settler_rechecks_obligations_on_its_first_turn",
npc &guy = tired_follower( leader_pos + tripoint( 2, 0, 0 ), 300 );
get_map().furn_set( guy.pos_bub(), furn_f_bed );
guy.set_stim( 100 );
guy.set_committed_goal( "go_to_sleep" );
guy.set_committed_goal_for_test( "go_to_sleep" );
guy.set_moves( 100 );
REQUIRE( guy.execute_need_goal( "go_to_sleep" ) == npc::need_result::holding );
round_trip( guy );
@@ -988,7 +996,7 @@ TEST_CASE( "woken_below_tired_resumes_only_where_rest_is_allowed", "[npc][needs]
npc &guy = stranger( leader_pos + tripoint( 0, -20, 0 ) );
make_asleep_on_bed( guy );
guy.set_sleepiness( 150 );
guy.set_committed_goal( "" );
guy.set_committed_goal_for_test( "" );
wake_by_noise( guy );
run_npc_turns( guy, 5, settled );
CHECK( settled( guy ) );
@@ -1222,7 +1230,7 @@ TEST_CASE( "wake_up_dialogue_keeps_rule_and_grants_grace", "[npc][sleep]" )
get_map().furn_set( guy.pos_bub(), furn_f_bed );
guy.set_sleepiness( 300 );
guy.set_stim( 100 );
guy.set_committed_goal( "go_to_sleep" );
guy.set_committed_goal_for_test( "go_to_sleep" );
guy.set_moves( 100 );
REQUIRE( guy.execute_need_goal( "go_to_sleep" ) == npc::need_result::holding );
talk_function::wake_up( guy );
@@ -1324,7 +1332,7 @@ TEST_CASE( "npc_that_cannot_sleep_does_not_try", "[npc][needs][sleep]" )
}
SECTION( "the serum takes hold while it lies down: it gets up" ) {
guy.set_stim( 100 );
guy.set_committed_goal( "go_to_sleep" );
guy.set_committed_goal_for_test( "go_to_sleep" );
guy.set_moves( 100 );
REQUIRE( guy.execute_need_goal( "go_to_sleep" ) == npc::need_result::holding );
guy.set_stim( 0 );
+1151 -214
View File
File diff suppressed because it is too large Load Diff
+166 -20
View File
@@ -23,6 +23,7 @@
#include "map_helpers.h"
#include "map_helpers_tests.h"
#include "map_iterator.h"
#include "mapdata.h"
#include "npc.h"
#include "options_helpers.h"
#include "overmap_ui.h"
@@ -37,6 +38,7 @@
#include "vehicle.h"
#include "vpart_position.h"
#include "vpart_range.h"
#include "weather.h"
static const efftype_id effect_sleep( "sleep" );
@@ -48,6 +50,7 @@ static const itype_id itype_orange( "orange" );
static const mtype_id mon_zombie( "mon_zombie" );
static const ter_str_id ter_t_floor( "t_floor" );
static const ter_str_id ter_t_wall( "t_wall" );
static const ter_str_id ter_t_wall_glass( "t_wall_glass" );
static const ter_str_id ter_t_water_sh( "t_water_sh" );
@@ -59,16 +62,21 @@ static const weather_type_id weather_clear( "clear" );
static const zone_type_id zone_type_NPC_NO_GO( "NPC_NO_GO" );
// run by hand with "[npc_trip_bench]"; prints one line per case and phase:
// BENCH,case,phase,npcs,npc_turns,route_calls,ms_total,ms_per_npc_turn,steps,drinks,spins
// BENCH,case,phase,npcs,npc_turns,decisions,executor_calls,route_calls,ms_total,ms_per_npc_turn,
// steps,drinks,fed_turns,indoor_turns,spins
// the needs are topped up every round, so fed_turns and indoor_turns count turns ending in that
// state, not meals or warm-ups completed
namespace
{
// the need the bench keeps topped up every round
enum class bench_need : int { thirst, sleep, hunger, cold };
struct bench_npc {
npc *guy;
tripoint_bub_ms start;
// sleep cases hold tiredness instead of thirst
bool tired = false;
bench_need need = bench_need::thirst;
};
struct phase_total {
@@ -78,7 +86,13 @@ struct phase_total {
int steps = 0;
// turns that ended with water drunk
int drinks = 0;
// turns ended with food in the stomach
int fed_turns = 0;
// turns ended on an indoor tile
int indoor_turns = 0;
int route_calls = 0;
int decisions = 0;
int executor_calls = 0;
// spun-out turns; their time isn't real work
int spins = 0;
};
@@ -103,15 +117,48 @@ npc &make_follower( npc &guy )
return guy;
}
void restore_need( npc &guy, const bench_need need )
{
switch( need ) {
case bench_need::thirst:
guy.stomach.empty();
guy.set_thirst( 600 );
break;
case bench_need::sleep:
guy.set_sleepiness( 300 );
break;
case bench_need::hunger:
guy.stomach.empty();
guy.guts.empty();
guy.set_hunger( 300 );
guy.set_stored_kcal( guy.get_healthy_kcal() / 2 );
break;
case bench_need::cold:
guy.set_all_parts_temp_conv( BODYTEMP_VERY_COLD );
guy.set_all_parts_temp_cur( BODYTEMP_VERY_COLD );
break;
}
}
bool pursuing_need( const npc &guy, const bench_need need )
{
switch( need ) {
case bench_need::thirst:
return guy.get_water_plan().active();
case bench_need::sleep:
return guy.get_sleep_plan().active();
case bench_need::hunger:
return guy.get_food_plan().active();
case bench_need::cold:
return guy.get_temperature_plan().active();
}
return false;
}
// fixed workload: need back every round, start position every reset_every rounds
void restore( bench_npc &b, const int round, const int reset_every )
{
if( b.tired ) {
b.guy->set_sleepiness( 300 );
} else {
b.guy->stomach.empty();
b.guy->set_thirst( 600 );
}
restore_need( *b.guy, b.need );
if( reset_every > 0 && round % reset_every == reset_every - 1 ) {
b.guy->setpos( get_map(), b.start );
b.guy->path.clear();
@@ -123,19 +170,25 @@ void run_case( const std::string &name, std::vector<bench_npc> &npcs, const int
{
map &here = get_map();
std::map<std::string, phase_total> phases;
// need is in place before the first measured round
for( bench_npc &b : npcs ) {
restore_need( *b.guy, b.need );
}
for( int round = 0; round < rounds; ++round ) {
for( bench_npc &b : npcs ) {
const bool pursuing = b.tired ? b.guy->get_sleep_plan().active() :
b.guy->get_water_plan().active();
const std::string phase = pursuing ? "pursue" : "acquire";
const std::string phase = pursuing_need( *b.guy, b.need ) ? "pursue" : "acquire";
const tripoint_bub_ms from = b.guy->pos_bub();
const int routes_before = b.guy->trip_route_calls();
const int decisions_before = b.guy->decisions_made();
const int executor_before = b.guy->executor_calls();
const std::chrono::steady_clock::time_point t0 = std::chrono::steady_clock::now();
const npc_turn_result result = process_npc_turn( here, *b.guy );
const std::chrono::steady_clock::time_point t1 = std::chrono::steady_clock::now();
phase_total &p = phases[phase];
p.turns++;
p.route_calls += b.guy->trip_route_calls() - routes_before;
p.decisions += b.guy->decisions_made() - decisions_before;
p.executor_calls += b.guy->executor_calls() - executor_before;
if( result.spun_out ) {
p.spins++;
}
@@ -144,6 +197,12 @@ void run_case( const std::string &name, std::vector<bench_npc> &npcs, const int
if( b.guy->stomach.get_water() > 0_ml ) {
p.drinks++;
}
if( b.guy->stomach.get_calories() > 0 ) {
p.fed_turns++;
}
if( here.has_flag( ter_furn_flag::TFLAG_INDOORS, b.guy->pos_bub() ) ) {
p.indoor_turns++;
}
}
calendar::turn += 1_turns;
for( bench_npc &b : npcs ) {
@@ -154,10 +213,10 @@ void run_case( const std::string &name, std::vector<bench_npc> &npcs, const int
}
}
for( const auto &[phase, total] : phases ) {
WARN( string_format( "BENCH,%s,%s,%zu,%d,%d,%.3f,%.4f,%d,%d,%d", name, phase, npcs.size(),
total.turns, total.route_calls, total.ms,
total.turns > 0 ? total.ms / total.turns : 0.0, total.steps, total.drinks,
total.spins ) );
WARN( string_format( "BENCH,%s,%s,%zu,%d,%d,%d,%d,%.3f,%.4f,%d,%d,%d,%d,%d", name, phase,
npcs.size(), total.turns, total.decisions, total.executor_calls,
total.route_calls, total.ms, total.turns > 0 ? total.ms / total.turns : 0.0,
total.steps, total.drinks, total.fed_turns, total.indoor_turns, total.spins ) );
}
}
@@ -293,7 +352,7 @@ TEST_CASE( "npc_trip_bench", "[.][npc_trip_bench]" )
npc &guy = make_follower( spawn_thirsty( start ) );
guy.set_thirst( 0 );
guy.set_sleepiness( 300 );
npcs.push_back( { &guy, start, true } );
npcs.push_back( { &guy, start, bench_need::sleep } );
}
// no spot along the leader's walk
zone_manager &mgr = zone_manager::get_manager();
@@ -320,7 +379,7 @@ TEST_CASE( "npc_trip_bench", "[.][npc_trip_bench]" )
guy.set_thirst( 0 );
guy.set_sleepiness( 300 );
guy.set_stim( 100 );
npcs.push_back( { &guy, start, true } );
npcs.push_back( { &guy, start, bench_need::sleep } );
}
// hostile behind a wall: in range, out of view
for( int x = 44; x <= 60; ++x ) {
@@ -332,7 +391,7 @@ TEST_CASE( "npc_trip_bench", "[.][npc_trip_bench]" )
for( bench_npc &b : npcs ) {
if( !b.guy->in_sleep_state() ) {
// lie down the way the game does: the roll fails on stim, so it settles
b.guy->set_committed_goal( "go_to_sleep" );
b.guy->set_committed_goal_for_test( "go_to_sleep" );
b.guy->set_moves( 100 );
b.guy->execute_need_goal( "go_to_sleep" );
}
@@ -405,7 +464,7 @@ TEST_CASE( "npc_trip_bench", "[.][npc_trip_bench]" )
}
for( npc *guy : passengers ) {
REQUIRE( guy->has_effect( effect_sleep ) );
npcs.push_back( { guy, guy->pos_bub(), true } );
npcs.push_back( { guy, guy->pos_bub(), bench_need::sleep } );
}
veh->velocity = 1000;
run_case( "woken_passengers", npcs, rounds, 0, [&]( const int round ) {
@@ -424,4 +483,91 @@ TEST_CASE( "npc_trip_bench", "[.][npc_trip_bench]" )
clear_vehicles();
here.load( origin, false, false );
}
SECTION( "hungry_follower_no_supplies" ) {
bench_setup();
const tripoint_bub_ms leader( 50, 50, 0 );
get_avatar().setpos( get_map(), leader );
get_map().build_map_cache( 0 );
const tripoint_bub_ms start = leader + tripoint::east * 2;
npc &guy = make_follower( spawn_thirsty( start ) );
guy.set_thirst( 0 );
std::vector<bench_npc> npcs{ { &guy, start, bench_need::hunger } };
run_case( "hungry_follower_no_supplies", npcs, rounds, 0 );
}
SECTION( "follower_forbidden_and_unreachable_food" ) {
bench_setup();
map &here = get_map();
const tripoint_bub_ms leader( 50, 50, 0 );
get_avatar().setpos( here, leader );
const tripoint_bub_ms start = leader + tripoint::east * 2;
npc &guy = make_follower( spawn_thirsty( start ) );
guy.set_thirst( 0 );
// six oranges in a zone the follower may not enter, six sealed in glass
zone_manager &mgr = zone_manager::get_manager();
const tripoint_bub_ms forbidden = leader + tripoint( 0, 5, 0 );
mgr.add( "no_food", zone_type_NPC_NO_GO, guy.get_fac_id(), false, true,
here.get_abs( forbidden + tripoint( -3, -1, 0 ) ), here.get_abs( forbidden + tripoint( 3, 1,
0 ) ) );
mgr.cache_data();
for( int i = -3; i <= 2; ++i ) {
here.add_item_or_charges( forbidden + tripoint( i, 0, 0 ), item( itype_orange ) );
}
std::vector<tripoint_bub_ms> sealed;
for( const tripoint_bub_ms &p : here.points_in_radius( start, 6 ) ) {
const bool apart = std::none_of( sealed.begin(), sealed.end(), [&p]( const tripoint_bub_ms & o ) {
return square_dist( o, p ) < 3;
} );
if( sealed.size() >= 6 || rl_dist( p, start ) != 5 || p.y() >= forbidden.y() - 2 || !apart ) {
continue;
}
here.add_item_or_charges( p, item( itype_orange ) );
for( const tripoint_bub_ms &w : here.points_in_radius( p, 1 ) ) {
if( w != p ) {
here.ter_set( w, ter_t_wall_glass );
}
}
sealed.push_back( p );
}
REQUIRE( sealed.size() == 6 );
here.build_map_cache( 0 );
std::vector<bench_npc> npcs{ { &guy, start, bench_need::hunger } };
run_case( "follower_forbidden_and_unreachable_food", npcs, rounds, 0 );
for( const tripoint_bub_ms &p : sealed ) {
CAPTURE( p );
CHECK( here.has_items( p ) );
}
}
SECTION( "cold_guard_far_shelter" ) {
bench_setup();
map &here = get_map();
const tripoint_bub_ms post( 50, 50, 0 );
npc &guy = spawn_thirsty( post );
guy.set_thirst( 0 );
guy.set_mission( NPC_MISSION_GUARD );
guy.set_guard_pos( guy.pos_abs() );
REQUIRE( guy.is_on_shift() );
// floor in sight, past the post's reach
for( const tripoint_bub_ms &p : here.points_in_radius( post + tripoint( 6, 0, 0 ), 1 ) ) {
here.ter_set( p, ter_t_floor );
}
here.build_map_cache( 0 );
std::vector<bench_npc> npcs{ { &guy, post, bench_need::cold } };
run_case( "cold_guard_far_shelter", npcs, rounds, walk_cycle );
}
SECTION( "repeated_decisions_without_movement" ) {
bench_setup();
map &here = get_map();
const tripoint_bub_ms start( 50, 50, 0 );
// walled in by glass: water in sight, no step possible
lay_pond( start + tripoint( 4, 0, 0 ), 1 );
for( const tripoint_bub_ms &w : here.points_in_radius( start, 1 ) ) {
if( w != start ) {
here.ter_set( w, ter_t_wall_glass );
}
}
here.build_map_cache( 0 );
std::vector<bench_npc> npcs{ { &spawn_thirsty( start ), start } };
run_case( "repeated_decisions_without_movement", npcs, rounds, 0 );
CHECK( npcs.front().guy->pos_bub() == start );
}
}
+25
View File
@@ -1,5 +1,6 @@
#include <cstdint>
#include <cstdio>
#include <initializer_list>
#include <string>
#include <vector>
@@ -561,6 +562,30 @@ TEST_CASE( "stomach_fullness_lines", "[stomach]" )
clear_stomach( dummy );
}
TEST_CASE( "room_before_engorged_is_the_last_intake_short_of_the_line", "[stomach]" )
{
avatar &dummy = get_avatar();
clear_avatar();
for( const bool deficit : {
false, true
} ) {
for( const units::volume &held : {
0_ml, 300_ml
} ) {
CAPTURE( deficit, units::to_milliliter( held ) );
clear_stomach( dummy );
dummy.stomach.ingest( food_summary{ 0_ml, held, nutrients{} } );
const units::volume room = dummy.stomach.room_before_engorged( dummy, deficit );
REQUIRE( room > 0_ml );
CHECK_FALSE( dummy.stomach.would_be_engorged_with( dummy, room, deficit ) );
CHECK( dummy.stomach.would_be_engorged_with( dummy, room + 1_ml, deficit ) );
dummy.stomach.ingest( food_summary{ 0_ml, room + 1_ml, nutrients{} } );
CHECK( dummy.stomach.room_before_engorged( dummy, deficit ) == 0_ml );
}
}
clear_stomach( dummy );
}
TEST_CASE( "stomach_fullness_effects_follow_the_lines", "[stomach]" )
{
avatar &dummy = get_avatar();