diff --git a/src/activity_item_handling.cpp b/src/activity_item_handling.cpp index 1cf964b3dff4e..efb1fc20d7e05 100644 --- a/src/activity_item_handling.cpp +++ b/src/activity_item_handling.cpp @@ -3561,8 +3561,7 @@ int get_auto_consume_moves( Character &you, const bool food ) bool try_fuel_fire( player_activity &act, Character &you, const bool starting_fire ) { const tripoint_bub_ms pos = you.pos_bub(); - std::vector adjacent = closest_points_first( pos, PICKUP_RANGE ); - adjacent.erase( adjacent.begin() ); + std::vector adjacent = closest_points_first( pos, 1, PICKUP_RANGE ); map &here = get_map(); std::optional best_fire = diff --git a/src/map.cpp b/src/map.cpp index 3042a73a93858..d9b5821cfbad8 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -5619,8 +5619,7 @@ std::pair map::_add_item_or_charges( const tripoint_bub if( overflow && copies_remaining > 0 ) { // ...otherwise try to overflow to adjacent tiles (if permitted) const int max_dist = 2; - std::vector tiles = closest_points_first( pos, max_dist ); - tiles.erase( tiles.begin() ); // we already tried this position + std::vector tiles = closest_points_first( pos, 1, max_dist ); const int max_path_length = 4 * max_dist; const pathfinding_settings setting( 0, max_dist, max_path_length, 0, false, false, true, false, false, false ); diff --git a/src/monattack.cpp b/src/monattack.cpp index 964f6f8c6cbe6..8fd47df55375f 100644 --- a/src/monattack.cpp +++ b/src/monattack.cpp @@ -2139,9 +2139,7 @@ bool mattack::formblob( monster *z ) } bool didit = false; - std::vector pts = closest_points_first( z->pos(), 1 ); - // Don't check own tile - pts.erase( pts.begin() ); + std::vector pts = closest_points_first( z->pos(), 1, 1 ); creature_tracker &creatures = get_creature_tracker(); for( const tripoint &dest : pts ) { Creature *critter = creatures.creature_at( dest ); diff --git a/src/monmove.cpp b/src/monmove.cpp index a279b139762c7..917ee573f2928 100644 --- a/src/monmove.cpp +++ b/src/monmove.cpp @@ -40,6 +40,7 @@ #include "options.h" #include "pathfinding.h" #include "pimpl.h" +#include "point.h" #include "rng.h" #include "scent_map.h" #include "sounds.h" @@ -1774,11 +1775,14 @@ bool monster::attack_at( const tripoint &p ) static tripoint find_closest_stair( const tripoint &near_this, const ter_furn_flag stair_type ) { - map &here = get_map(); - for( const tripoint &candidate : closest_points_first( near_this, 10 ) ) { - if( here.has_flag( stair_type, candidate ) ) { - return candidate; - } + const map &here = get_map(); + std::optional candidate = find_point_closest_first( near_this, 0, 10, [&here, + stair_type]( const tripoint & candidate ) { + return here.has_flag( stair_type, candidate ); + } ); + + if( candidate != std::nullopt ) { + return *candidate; } // we didn't find it return near_this; diff --git a/src/npc.cpp b/src/npc.cpp index 74a1b92853955..55d0f289707b9 100644 --- a/src/npc.cpp +++ b/src/npc.cpp @@ -1045,7 +1045,7 @@ void npc::place_on_map() return; } - for( const tripoint &p : closest_points_first( pos(), SEEX + 1 ) ) { + for( const tripoint &p : closest_points_first( pos(), 1, SEEX + 1 ) ) { if( g->is_empty( p ) ) { setpos( p ); return; diff --git a/src/npcmove.cpp b/src/npcmove.cpp index d6a1454150182..0283eb609c2b4 100644 --- a/src/npcmove.cpp +++ b/src/npcmove.cpp @@ -3175,8 +3175,7 @@ void npc::avoid_friendly_fire() center.y = std::round( center.y / friend_count ); center.z = std::round( center.z / friend_count ); - std::vector candidates = closest_points_first( pos(), 1 ); - candidates.erase( candidates.begin() ); + std::vector candidates = closest_points_first( pos(), 1, 1 ); std::sort( candidates.begin(), candidates.end(), [&tar, ¢er]( const tripoint & l, const tripoint & r ) { return ( rl_dist( l, tar ) - rl_dist( l, center ) ) < diff --git a/src/point.cpp b/src/point.cpp index bb5b47facc40c..efed6bcd75332 100644 --- a/src/point.cpp +++ b/src/point.cpp @@ -117,6 +117,22 @@ std::istream &operator>>( std::istream &is, tripoint &pos ) return is; } +std::optional rectangle_size( int min_dist, int max_dist ) +{ + min_dist = std::max( min_dist, 0 ); + max_dist = std::max( max_dist, 0 ); + + if( min_dist > max_dist ) { + return std::nullopt; + } + + const int min_edge = min_dist * 2 + 1; + const int max_edge = max_dist * 2 + 1; + + const int n = max_edge * max_edge - ( min_edge - 2 ) * ( min_edge - 2 ) + ( min_dist == 0 ? 1 : 0 ); + return n; +} + std::vector closest_points_first( const tripoint ¢er, int max_dist ) { return closest_points_first( center, 0, max_dist ); diff --git a/src/point.h b/src/point.h index ad0fbd916c45e..94e1ca831c8b1 100644 --- a/src/point.h +++ b/src/point.h @@ -301,21 +301,7 @@ std::optional find_point_closest_first( const Point ¢er, int max_dist // Calculate the number of tiles in a square from min_dist to max_dist about an arbitrary centre. -inline std::optional rectangle_size( int min_dist, int max_dist ) -{ - min_dist = std::max( min_dist, 0 ); - max_dist = std::max( max_dist, 0 ); - - if( min_dist > max_dist ) { - return std::nullopt; - } - - const int min_edge = min_dist * 2 + 1; - const int max_edge = max_dist * 2 + 1; - - const int n = max_edge * max_edge - ( min_edge - 2 ) * ( min_edge - 2 ) + ( min_dist == 0 ? 1 : 0 ); - return n; -}; +std::optional rectangle_size( int min_dist, int max_dist ); inline constexpr tripoint tripoint_min { INT_MIN, INT_MIN, INT_MIN }; inline constexpr tripoint tripoint_max{ INT_MAX, INT_MAX, INT_MAX }; diff --git a/src/ranged.cpp b/src/ranged.cpp index e777c0d032f08..5de099665c374 100644 --- a/src/ranged.cpp +++ b/src/ranged.cpp @@ -2235,11 +2235,14 @@ static void cycle_action( item &weap, const itype_id &ammo, const tripoint &pos { map &here = get_map(); // eject casings and linkages in random direction avoiding walls using player position as fallback - std::vector tiles = closest_points_first( pos, 1 ); - tiles.erase( tiles.begin() ); - tiles.erase( std::remove_if( tiles.begin(), tiles.end(), [&]( const tripoint & e ) { - return !here.passable( e ); - } ), tiles.end() ); + std::vector tiles; + tiles.reserve( 8 ); + find_point_closest_first( pos, 1, 1, [&here, &tiles]( const tripoint & e ) { + if( here.passable( e ) ) { + tiles.push_back( e ); + } + return false; + } ); tripoint eject = tiles.empty() ? pos : random_entry( tiles ); // for turrets try and drop casings or linkages directly to any CARGO part on the same tile @@ -3161,12 +3164,12 @@ tripoint target_ui::choose_initial_target() // Try closest practice target map &here = get_map(); - const std::vector nearby = closest_points_first( src, range ); - const auto target_spot = std::find_if( nearby.begin(), nearby.end(), - [this, &here]( const tripoint & pt ) { + std::optional target_spot = find_point_closest_first( src, range, [this, + &here]( const tripoint & pt ) { return here.tr_at( pt ).id == tr_practice_target && this->you->sees( pt ); } ); - if( target_spot != nearby.end() ) { + + if( target_spot != std::nullopt ) { return *target_spot; }