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 Aug 29, 2024
1 parent b039e74 commit 1fff4af
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 27 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
6 changes: 2 additions & 4 deletions src/monattack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2234,9 +2234,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 Expand Up @@ -4765,4 +4763,4 @@ bool mattack::speaker( monster *z )
sounds::sound( z->pos(), 60, sounds::sound_t::order,
SNIPPET.random_from_category( "speaker_warning" ).value_or( translation() ) );
return true;
}
}
12 changes: 9 additions & 3 deletions src/monmove.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1774,11 +1774,17 @@ 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 ) ) {
const map &here = get_map();
std::optional<tripoint> candidate = find_point_closest_first( near_this, 10, [&here,
stair_type]( const tripoint & candidate ) {
if( here.has_flag( stair_type, candidate ) ) {
return candidate;
return true;
}
}
);

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 @@ -3176,8 +3176,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
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 ) {
const 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
12 changes: 8 additions & 4 deletions src/vehicle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7666,10 +7666,14 @@ void vehicle::leak_fuel( vehicle_part &pt ) const

map &here = get_map();
// leak in random directions but prefer closest tiles and avoid walls or other obstacles
std::vector<tripoint> tiles = closest_points_first( global_part_pos3( pt ), 1 );
tiles.erase( std::remove_if( tiles.begin(), tiles.end(), [&here]( const tripoint & e ) {
return !here.passable( e );
} ), tiles.end() );
std::vector<tripoint> tiles;
tiles.reserve( 9 );
find_point_closest_first( global_part_pos3( pt ), 1, [&here, &tiles]( const tripoint & e ) {
if( here.passable( e ) ) {
tiles.push_back( e );
}
return false;
} );

// leak up to 1/3 of remaining fuel per iteration and continue until the part is empty
const itype *fuel = item::find_type( pt.ammo_current() );
Expand Down

0 comments on commit 1fff4af

Please sign in to comment.