Skip to content

Commit

Permalink
refactor: use enum class as option for clarity
Browse files Browse the repository at this point in the history
also conform to astyle
  • Loading branch information
scarf005 committed Apr 1, 2023
1 parent b5f26b4 commit 4c0c308
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 16 deletions.
6 changes: 4 additions & 2 deletions src/activity_handlers.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,16 @@ bool generic_multi_activity_handler( player_activity &act, player &p, bool check
void activity_on_turn_fetch( player_activity &, player *p );
void activity_on_turn_wear( player_activity &act, player &p );

enum class consume_type : bool { FOOD, DRINK };

/**
* @brief Find an item to consume automatically
*
* @param food if true, search for food, otherwise search for drink
* @param consume_type type of item to consume
* @return true player ate food or was nauseous
* @return false player did not find anything suitable or is a npc
*/
bool find_auto_consume( player &p, bool food );
bool find_auto_consume( player &p, const consume_type type );
void try_fuel_fire( player_activity &act, player &p, bool starting_fire = false );

enum class item_drop_reason {
Expand Down
50 changes: 38 additions & 12 deletions src/activity_item_handling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3133,7 +3133,7 @@ static cata::optional<tripoint> find_refuel_spot_trap( const std::vector<tripoin
return {};
}

bool find_auto_consume( player &p, const bool food )
bool find_auto_consume( player &p, const consume_type type )
{
// return false if there is no point searching again while the activity is still happening.
if( p.is_npc() ) {
Expand All @@ -3145,7 +3145,7 @@ bool find_auto_consume( player &p, const bool food )
const tripoint pos = p.pos();
map &here = get_map();
zone_manager &mgr = zone_manager::get_manager();
const zone_type_id consume_type_zone( food ? "AUTO_EAT" : "AUTO_DRINK" );
const zone_type_id consume_type_zone( type == consume_type::FOOD ? "AUTO_EAT" : "AUTO_DRINK" );
if( here.check_vehicle_zones( g->get_levz() ) ) {
mgr.cache_vzones();
}
Expand All @@ -3155,17 +3155,43 @@ bool find_auto_consume( player &p, const bool food )
return false;
}

const auto ok_to_consume = [&p, food]( item & it ) -> bool {
const auto ok_to_consume = [&p, type]( item & it ) -> bool {
item &comest = p.get_consumable_from( it );
// *INDENT-OFF*
/* not food. */ if( comest.is_null() || comest.is_craft() || !comest.is_food() ) { return false; }
/* not enjoyable. */ if( p.fun_for( comest ).first < -5 ) { return false; }
/* cannot consume. */ if( !p.can_consume( comest ) ) { return false; }
/* wont eat, e.g cannibal */ if( !p.will_eat( comest, false ).success() ) { return false; }
/* not ours */ if( !it.is_owned_by( p, true ) ) { return false; }
/* not quenching enough */ if( !food && comest.get_comestible()->quench < 15 ) { return false; }
/* Unsafe to drink or eat */ if( comest.has_flag( flag_UNSAFE_CONSUME ) ) { return false; }
// *INDENT-ON*
/* not food. */
if( comest.is_null() || comest.is_craft() || !comest.is_food() )
{
return false;
}
/* not enjoyable. */
if( p.fun_for( comest ).first < -5 )
{
return false;
}
/* cannot consume. */
if( !p.can_consume( comest ) )
{
return false;
}
/* wont eat, e.g cannibal */
if( !p.will_eat( comest, false ).success() )
{
return false;
}
/* not ours */
if( !it.is_owned_by( p, true ) )
{
return false;
}
/* not quenching enough */
if( type == consume_type::DRINK && comest.get_comestible()->quench < 15 )
{
return false;
}
/* Unsafe to drink or eat */
if( comest.has_flag( flag_UNSAFE_CONSUME ) )
{
return false;
}
return true;
};

Expand Down
4 changes: 2 additions & 2 deletions src/player_activity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,12 +282,12 @@ void player_activity::do_turn( player &p )
}
if( *this && !p.is_npc() && type->valid_auto_needs() && !no_food_nearby_for_auto_consume ) {
if( p.get_kcal_percent() < 0.95f ) {
if( !find_auto_consume( p, true ) ) {
if( !find_auto_consume( p, consume_type::FOOD ) ) {
no_food_nearby_for_auto_consume = true;
}
}
if( p.get_thirst() > thirst_levels::thirsty && !no_drink_nearby_for_auto_consume ) {
if( !find_auto_consume( p, false ) ) {
if( !find_auto_consume( p, consume_type::DRINK ) ) {
no_drink_nearby_for_auto_consume = true;
}
}
Expand Down

0 comments on commit 4c0c308

Please sign in to comment.