Skip to content

Commit

Permalink
Use find_point_closest_first where appropriate.
Browse files Browse the repository at this point in the history
  • Loading branch information
CLIDragon committed Sep 5, 2024
1 parent 6956fa0 commit 0dde0b1
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 39 deletions.
3 changes: 1 addition & 2 deletions src/activity_item_handling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<tripoint_bub_ms> adjacent = closest_points_first( pos, PICKUP_RANGE );
adjacent.erase( adjacent.begin() );
std::vector<tripoint_bub_ms> adjacent = closest_points_first( pos, 1, PICKUP_RANGE );

map &here = get_map();
std::optional<tripoint_bub_ms> best_fire =
Expand Down
3 changes: 1 addition & 2 deletions src/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5619,8 +5619,7 @@ std::pair<item *, tripoint_bub_ms> 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<tripoint_bub_ms> tiles = closest_points_first( pos, max_dist );
tiles.erase( tiles.begin() ); // we already tried this position
std::vector<tripoint_bub_ms> 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 );
Expand Down
4 changes: 1 addition & 3 deletions src/monattack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2139,9 +2139,7 @@ bool mattack::formblob( monster *z )
}

bool didit = false;
std::vector<tripoint> pts = closest_points_first( z->pos(), 1 );
// Don't check own tile
pts.erase( pts.begin() );
std::vector<tripoint> 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 );
Expand Down
14 changes: 9 additions & 5 deletions src/monmove.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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<tripoint> 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;
Expand Down
2 changes: 1 addition & 1 deletion src/npc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions src/npcmove.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<tripoint> candidates = closest_points_first( pos(), 1 );
candidates.erase( candidates.begin() );
std::vector<tripoint> candidates = closest_points_first( pos(), 1, 1 );
std::sort( candidates.begin(), candidates.end(),
[&tar, &center]( const tripoint & l, const tripoint & r ) {
return ( rl_dist( l, tar ) - rl_dist( l, center ) ) <
Expand Down
16 changes: 16 additions & 0 deletions src/point.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,22 @@ std::istream &operator>>( std::istream &is, tripoint &pos )
return is;
}

std::optional<int> 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<tripoint> closest_points_first( const tripoint &center, int max_dist )
{
return closest_points_first( center, 0, max_dist );
Expand Down
16 changes: 1 addition & 15 deletions src/point.h
Original file line number Diff line number Diff line change
Expand Up @@ -301,21 +301,7 @@ std::optional<Point> find_point_closest_first( const Point &center, int max_dist


// Calculate the number of tiles in a square from min_dist to max_dist about an arbitrary centre.
inline std::optional<int> 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<int> 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 };
Expand Down
21 changes: 12 additions & 9 deletions src/ranged.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<tripoint> 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<tripoint> 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
Expand Down Expand Up @@ -3161,12 +3164,12 @@ tripoint target_ui::choose_initial_target()

// Try closest practice target
map &here = get_map();
const std::vector<tripoint> nearby = closest_points_first( src, range );
const auto target_spot = std::find_if( nearby.begin(), nearby.end(),
[this, &here]( const tripoint & pt ) {
std::optional<tripoint> 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;
}

Expand Down

0 comments on commit 0dde0b1

Please sign in to comment.