From 73da9d21994a227a81d0c8a067c4a3e102a14775 Mon Sep 17 00:00:00 2001 From: Eric Pierce Date: Thu, 6 Feb 2020 20:48:32 -0700 Subject: [PATCH 1/2] Add new map::has_nearby_table function The algorithm for nearby table location is done in at least 3 places: - src/activity_handlers.cpp:L412-419 - src/activity_item_handling.cpp:L1428-1435 - src/consumption.cpp:L845-850 Moving `has_nearby_table` as a new function it into map.cpp made it obvious that this is where it actually belongs; there it can fit neatly alongside its brethren like `has_nearby_fire`. This commit only adds the new function; refactoring will come next. --- src/map.cpp | 13 +++++++++++++ src/map.h | 5 +++++ 2 files changed, 18 insertions(+) diff --git a/src/map.cpp b/src/map.cpp index 2ed3054360beb..30c10559c0fef 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -2587,6 +2587,19 @@ bool map::has_nearby_fire( const tripoint &p, int radius ) return false; } +bool map::has_nearby_table( const tripoint &p, int radius ) +{ + for( const tripoint &pt : points_in_radius( p, radius ) ) { + const optional_vpart_position vp = veh_at( p ); + if( has_flag( "FLAT_SURF", pt ) ) { + return true; + } + if( vp && ( vp->vehicle().has_part( "KITCHEN" ) || vp->vehicle().has_part( "FLAT_SURF" ) ) ) { + return true; + } + } +} + bool map::mop_spills( const tripoint &p ) { bool retval = false; diff --git a/src/map.h b/src/map.h index afe4be392d29f..dd53486b1e3b7 100644 --- a/src/map.h +++ b/src/map.h @@ -681,6 +681,11 @@ class map // "fire" item to be used for example when crafting or when // a iuse function needs fire. bool has_nearby_fire( const tripoint &p, int radius = 1 ); + /** + * Check whether a table/workbench/vehicle kitchen or other flat + * surface is nearby that could be used for crafting or eating. + */ + bool has_nearby_table( const tripoint &p, int radius = 1 ); /** * Check if creature can see some items at p. Includes: * - check for items at this location (has_items(p)) From f4d6648671b5aa87cede20fa4c6b8fdcf8801364 Mon Sep 17 00:00:00 2001 From: Eric Pierce Date: Thu, 6 Feb 2020 21:24:46 -0700 Subject: [PATCH 2/2] Use map::has_nearby_table for eating/butchering Factoring out the similar loops to check for nearby tables, and using the new `map::has_nearby_table` function instead. --- src/activity_handlers.cpp | 10 +--------- src/activity_item_handling.cpp | 10 +--------- src/consumption.cpp | 8 +------- src/map.cpp | 1 + 4 files changed, 4 insertions(+), 25 deletions(-) diff --git a/src/activity_handlers.cpp b/src/activity_handlers.cpp index 1ab05183d41c0..506bc1952fdaf 100644 --- a/src/activity_handlers.cpp +++ b/src/activity_handlers.cpp @@ -409,14 +409,6 @@ static void set_up_butchery( player_activity &act, player &u, butcher_type actio } } - bool has_table_nearby = false; - for( const tripoint &pt : g->m.points_in_radius( u.pos(), 2 ) ) { - if( g->m.has_flag_furn( flag_FLAT_SURF, pt ) || g->m.has_flag( flag_FLAT_SURF, pt ) || - ( ( g->m.veh_at( pt ) && ( g->m.veh_at( pt )->vehicle().has_part( flag_KITCHEN ) || - g->m.veh_at( pt )->vehicle().has_part( flag_FLAT_SURF ) ) ) ) ) { - has_table_nearby = true; - } - } bool has_tree_nearby = false; for( const tripoint &pt : g->m.points_in_radius( u.pos(), 2 ) ) { if( g->m.has_flag( flag_TREE, pt ) ) { @@ -448,7 +440,7 @@ static void set_up_butchery( player_activity &act, player &u, butcher_type actio act.targets.pop_back(); return; } - if( !has_table_nearby ) { + if( !g->m.has_nearby_table( u.pos(), 2 ) ) { u.add_msg_if_player( m_info, _( "To perform a full butchery on a corpse this big, you need a table nearby or something else with a flat surface. A leather tarp spread out on the ground could suffice." ) ); act.targets.pop_back(); diff --git a/src/activity_item_handling.cpp b/src/activity_item_handling.cpp index 840b8a65ebdb6..42bbd87860690 100644 --- a/src/activity_item_handling.cpp +++ b/src/activity_item_handling.cpp @@ -1425,14 +1425,6 @@ static activity_reason_info can_do_activity_there( const activity_id &act, playe corpses.push_back( i ); } } - bool has_table_nearby = false; - for( const tripoint &pt : g->m.points_in_radius( src_loc, 2 ) ) { - if( g->m.has_flag_furn( flag_FLAT_SURF, pt ) || g->m.has_flag( flag_FLAT_SURF, pt ) || - ( ( g->m.veh_at( pt ) && ( g->m.veh_at( pt )->vehicle().has_part( flag_KITCHEN ) || - g->m.veh_at( pt )->vehicle().has_part( flag_FLAT_SURF ) ) ) ) ) { - has_table_nearby = true; - } - } bool b_rack_present = false; for( const tripoint &pt : g->m.points_in_radius( src_loc, 2 ) ) { if( g->m.has_flag_furn( flag_BUTCHER_EQ, pt ) ) { @@ -1441,7 +1433,7 @@ static activity_reason_info can_do_activity_there( const activity_id &act, playe } if( !corpses.empty() ) { if( big_count > 0 && small_count == 0 ) { - if( !has_table_nearby || !b_rack_present ) { + if( !b_rack_present || !g->m.has_nearby_table( src_loc, 2 ) ) { return activity_reason_info::fail( NO_ZONE ); } if( p.has_quality( quality_id( qual_BUTCHER ), 1 ) && ( p.has_quality( qual_SAW_W ) || diff --git a/src/consumption.cpp b/src/consumption.cpp index 86c2b2a840105..a05d41ad5cd91 100644 --- a/src/consumption.cpp +++ b/src/consumption.cpp @@ -840,20 +840,14 @@ bool player::eat( item &food, bool force ) add_msg_player_or_npc( _( "You eat your %s." ), _( " eats a %s." ), food.tname() ); if( !spoiled && !food.has_flag( flag_ALLERGEN_JUNK ) ) { - bool has_table_nearby = false; bool has_chair_nearby = false; for( const tripoint &pt : g->m.points_in_radius( pos(), 1 ) ) { - if( g->m.has_flag_furn( flag_FLAT_SURF, pt ) || g->m.has_flag( flag_FLAT_SURF, pt ) || - ( g->m.veh_at( pt ) && ( g->m.veh_at( pt )->vehicle().has_part( "KITCHEN" ) || - g->m.veh_at( pt )->vehicle().has_part( "FLAT_SURF" ) ) ) ) { - has_table_nearby = true; - } if( g->m.has_flag_furn( flag_CAN_SIT, pt ) || g->m.has_flag( flag_CAN_SIT, pt ) || ( g->m.veh_at( pt ) && ( g->m.veh_at( pt )->vehicle().has_part( "SEAT" ) ) ) ) { has_chair_nearby = true; } } - if( has_chair_nearby && has_table_nearby ) { + if( has_chair_nearby && g->m.has_nearby_table( pos(), 1 ) ) { if( has_trait( trait_TABLEMANNERS ) ) { rem_morale( MORALE_ATE_WITHOUT_TABLE ); add_morale( MORALE_ATE_WITH_TABLE, 3, 3, 3_hours, 2_hours, true ); diff --git a/src/map.cpp b/src/map.cpp index 30c10559c0fef..52b95ecacddd5 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -2598,6 +2598,7 @@ bool map::has_nearby_table( const tripoint &p, int radius ) return true; } } + return false; } bool map::mop_spills( const tripoint &p )