Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make closest_points_first work for all point types #41962

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/activity_item_handling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3014,7 +3014,7 @@ int get_auto_consume_moves( player &p, const bool food )
void try_fuel_fire( player_activity &act, player &p, const bool starting_fire )
{
const tripoint pos = p.pos();
std::vector<tripoint> adjacent = closest_tripoints_first( pos, PICKUP_RANGE );
std::vector<tripoint> adjacent = closest_points_first( pos, PICKUP_RANGE );
adjacent.erase( adjacent.begin() );

cata::optional<tripoint> best_fire = starting_fire ? act.placement : find_best_fire( adjacent,
Expand Down
22 changes: 22 additions & 0 deletions src/coordinates.h
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,28 @@ using tripoint_abs_om = coords::coord_point<tripoint, coords::origin::abs, coord
using coords::project_to;
using coords::project_remain;

template<typename Point, coords::origin Origin, coords::scale Scale>
std::vector<coords::coord_point<Point, Origin, Scale>>
closest_points_first( const coords::coord_point<Point, Origin, Scale> &loc,
int min_dist, int max_dist )
{
std::vector<Point> raw_result = closest_points_first( loc.raw(), min_dist, max_dist );
std::vector<coords::coord_point<Point, Origin, Scale>> result;
result.reserve( raw_result.size() );
std::transform( raw_result.begin(), raw_result.end(), std::back_inserter( result ),
[]( const Point & p ) {
return coords::coord_point<Point, Origin, Scale>( p );
} );
return result;
}
template<typename Point, coords::origin Origin, coords::scale Scale>
std::vector<coords::coord_point<Point, Origin, Scale>>
closest_points_first( const coords::coord_point<Point, Origin, Scale> &loc,
int max_dist )
{
return closest_points_first( loc, 0, max_dist );
}

/* find appropriate subdivided coordinates for absolute tile coordinate.
* This is less obvious than one might think, for negative coordinates, so this
* was created to give a definitive answer.
Expand Down
4 changes: 2 additions & 2 deletions src/crafting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ std::vector<const item *> player::get_eligible_containers_for_crafting() const

map &here = get_map();
// get all potential containers within PICKUP_RANGE tiles including vehicles
for( const tripoint &loc : closest_tripoints_first( pos(), PICKUP_RANGE ) ) {
for( const tripoint &loc : closest_points_first( pos(), PICKUP_RANGE ) ) {
// can not reach this -> can not access its contents
if( pos() != loc && !here.clear_path( pos(), loc, PICKUP_RANGE, 1, 100 ) ) {
continue;
Expand Down Expand Up @@ -663,7 +663,7 @@ static item *set_item_inventory( player &p, item &newit )
static item_location set_item_map( const tripoint &loc, item &newit )
{
// Includes loc
for( const tripoint &tile : closest_tripoints_first( loc, 2 ) ) {
for( const tripoint &tile : closest_points_first( loc, 2 ) ) {
// Pass false to disallow overflow, null_item_reference indicates failure.
item *it_on_map = &get_map().add_item_or_charges( tile, newit, false );
if( it_on_map != &null_item_reference() ) {
Expand Down
4 changes: 2 additions & 2 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7225,7 +7225,7 @@ std::vector<map_item_stack> game::find_nearby_items( int iRadius )
return ret;
}

for( auto &points_p_it : closest_tripoints_first( u.pos(), iRadius ) ) {
for( auto &points_p_it : closest_points_first( u.pos(), iRadius ) ) {
if( points_p_it.y >= u.posy() - iRadius && points_p_it.y <= u.posy() + iRadius &&
u.sees( points_p_it ) &&
m.sees_some_items( points_p_it, u ) ) {
Expand Down Expand Up @@ -10796,7 +10796,7 @@ void game::vertical_move( int movez, bool force, bool peeking )
}
if( !npcs_to_bring.empty() ) {
// Would look nicer randomly scrambled
std::vector<tripoint> candidates = closest_tripoints_first( u.pos(), 1 );
std::vector<tripoint> candidates = closest_points_first( u.pos(), 1 );
candidates.erase( std::remove_if( candidates.begin(), candidates.end(),
[this]( const tripoint & c ) {
return !is_empty( c );
Expand Down
6 changes: 3 additions & 3 deletions src/iexamine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -899,7 +899,7 @@ void iexamine::elevator( player &p, const tripoint &examp )
} else if( here.ter( critter.pos() ) == ter_id( "t_elevator" ) ) {
tripoint critter_omt = ms_to_omt_copy( here.getabs( critter.pos() ) );
if( critter_omt == new_floor_omt ) {
for( const tripoint &candidate : closest_tripoints_first( critter.pos(), 10 ) ) {
for( const tripoint &candidate : closest_points_first( critter.pos(), 10 ) ) {
if( here.ter( candidate ) != ter_id( "t_elevator" ) &&
here.passable( candidate ) &&
!g->critter_at( candidate ) ) {
Expand All @@ -922,7 +922,7 @@ void iexamine::elevator( player &p, const tripoint &examp )
tripoint critter_omt = ms_to_omt_copy( here.getabs( critter.pos() ) );

if( critter_omt == original_floor_omt ) {
for( const tripoint &candidate : closest_tripoints_first( p.pos(), 10 ) ) {
for( const tripoint &candidate : closest_points_first( p.pos(), 10 ) ) {
if( here.ter( candidate ) == ter_id( "t_elevator" ) &&
candidate != p.pos() &&
!g->critter_at( candidate ) ) {
Expand Down Expand Up @@ -2002,7 +2002,7 @@ void iexamine::egg_sack_generic( player &p, const tripoint &examp,
here.furn_set( examp, f_egg_sacke );
int monster_count = 0;
if( one_in( 2 ) ) {
for( const tripoint &nearby_pos : closest_tripoints_first( examp, 1 ) ) {
for( const tripoint &nearby_pos : closest_points_first( examp, 1 ) ) {
if( !one_in( 3 ) ) {
continue;
} else if( g->place_critter_at( montype, nearby_pos ) ) {
Expand Down
2 changes: 1 addition & 1 deletion src/inventory_ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1373,7 +1373,7 @@ void inventory_selector::add_nearby_items( int radius )
{
if( radius >= 0 ) {
map &here = get_map();
for( const tripoint &pos : closest_tripoints_first( u.pos(), radius ) ) {
for( const tripoint &pos : closest_points_first( u.pos(), radius ) ) {
// can not reach this -> can not access its contents
if( u.pos() != pos && !here.clear_path( u.pos(), pos, rl_dist( u.pos(), pos ), 1, 100 ) ) {
continue;
Expand Down
4 changes: 2 additions & 2 deletions src/iuse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1306,7 +1306,7 @@ static void spawn_spores( const player &p )
int spores_spawned = 0;
map &here = get_map();
fungal_effects fe( *g, here );
for( const tripoint &dest : closest_tripoints_first( p.pos(), 4 ) ) {
for( const tripoint &dest : closest_points_first( p.pos(), 4 ) ) {
if( here.impassable( dest ) ) {
continue;
}
Expand Down Expand Up @@ -5597,7 +5597,7 @@ int iuse::artifact( player *p, item *it, bool, const tripoint & )

case AEA_FIRESTORM: {
p->add_msg_if_player( m_bad, _( "Fire rains down around you!" ) );
std::vector<tripoint> ps = closest_tripoints_first( p->pos(), 3 );
std::vector<tripoint> ps = closest_points_first( p->pos(), 3 );
for( auto p_it : ps ) {
if( !one_in( 3 ) ) {
here.add_field( p_it, fd_fire, 1 + rng( 0, 1 ) * rng( 0, 1 ), 3_minutes );
Expand Down
2 changes: 1 addition & 1 deletion src/iuse_actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ static std::vector<tripoint> points_for_gas_cloud( const tripoint &center, int r
{
map &here = get_map();
std::vector<tripoint> result;
for( const auto &p : closest_tripoints_first( center, radius ) ) {
for( const auto &p : closest_points_first( center, radius ) ) {
if( here.impassable( p ) ) {
continue;
}
Expand Down
6 changes: 3 additions & 3 deletions src/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4218,7 +4218,7 @@ item &map::add_item_or_charges( const tripoint &pos, item obj, bool overflow )
} else if( overflow ) {
// ...otherwise try to overflow to adjacent tiles (if permitted)
const int max_dist = 2;
std::vector<tripoint> tiles = closest_tripoints_first( pos, max_dist );
std::vector<tripoint> tiles = closest_points_first( pos, max_dist );
tiles.erase( tiles.begin() ); // we already tried this position
const int max_path_length = 4 * max_dist;
const pathfinding_settings setting( 0, max_dist, max_path_length, 0, false, true, false, false,
Expand Down Expand Up @@ -6342,10 +6342,10 @@ std::vector<tripoint> map::get_dir_circle( const tripoint &f, const tripoint &t

// The line below can be crazy expensive - we only take the FIRST point of it
const std::vector<tripoint> line = line_to( f, t, 0, 0 );
const std::vector<tripoint> spiral = closest_tripoints_first( f, 1 );
const std::vector<tripoint> spiral = closest_points_first( f, 1 );
const std::vector<int> pos_index {1, 2, 4, 6, 8, 7, 5, 3};

// All possible constellations (closest_tripoints_first goes clockwise)
// All possible constellations (closest_points_first goes clockwise)
// 753 531 312 124 246 468 687 875
// 8 1 7 2 5 4 3 6 1 8 2 7 4 5 6 3
// 642 864 786 578 357 135 213 421
Expand Down
2 changes: 1 addition & 1 deletion src/map_selector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

map_selector::map_selector( const tripoint &pos, int radius, bool accessible )
{
for( const tripoint &e : closest_tripoints_first( pos, radius ) ) {
for( const tripoint &e : closest_points_first( pos, radius ) ) {
if( !accessible || get_map().clear_path( pos, e, radius, 1, 100 ) ) {
data.emplace_back( e );
}
Expand Down
6 changes: 3 additions & 3 deletions src/monattack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2371,7 +2371,7 @@ bool mattack::formblob( monster *z )
}

bool didit = false;
std::vector<tripoint> pts = closest_tripoints_first( z->pos(), 1 );
std::vector<tripoint> pts = closest_points_first( z->pos(), 1 );
// Don't check own tile
pts.erase( pts.begin() );
for( const tripoint &dest : pts ) {
Expand Down Expand Up @@ -2457,7 +2457,7 @@ bool mattack::callblobs( monster *z )
// and keep the rest near the brain blob for protection.
tripoint enemy = get_player_character().pos();
std::list<monster *> allies;
std::vector<tripoint> nearby_points = closest_tripoints_first( z->pos(), 3 );
std::vector<tripoint> nearby_points = closest_points_first( z->pos(), 3 );
for( monster &candidate : g->all_monsters() ) {
if( candidate.type->in_species( species_BLOB ) && candidate.type->id != mon_blob_brain ) {
// Just give the allies consistent assignments.
Expand Down Expand Up @@ -2490,7 +2490,7 @@ bool mattack::jackson( monster *z )
{
// Jackson draws nearby zombies into the dance.
std::list<monster *> allies;
std::vector<tripoint> nearby_points = closest_tripoints_first( z->pos(), 3 );
std::vector<tripoint> nearby_points = closest_points_first( z->pos(), 3 );
for( monster &candidate : g->all_monsters() ) {
if( candidate.type->in_species( species_ZOMBIE ) && candidate.type->id != mon_zombie_jackson ) {
// Just give the allies consistent assignments.
Expand Down
2 changes: 1 addition & 1 deletion src/mondefense.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ void mdefense::acidsplash( monster &m, Creature *const source,
}

// Don't splatter directly on the `m`, that doesn't work well
std::vector<tripoint> pts = closest_tripoints_first( source->pos(), 1 );
std::vector<tripoint> pts = closest_points_first( source->pos(), 1 );
pts.erase( std::remove( pts.begin(), pts.end(), m.pos() ), pts.end() );

projectile prj;
Expand Down
2 changes: 1 addition & 1 deletion src/monmove.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1462,7 +1462,7 @@ bool monster::attack_at( const tripoint &p )
static tripoint find_closest_stair( const tripoint &near_this, const ter_bitflags stair_type )
{
map &here = get_map();
for( const tripoint &candidate : closest_tripoints_first( near_this, 10 ) ) {
for( const tripoint &candidate : closest_points_first( near_this, 10 ) ) {
if( here.has_flag( stair_type, candidate ) ) {
return candidate;
}
Expand Down
2 changes: 1 addition & 1 deletion src/npc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,7 @@ void npc::place_on_map()
return;
}

for( const tripoint &p : closest_tripoints_first( pos(), SEEX + 1 ) ) {
for( const tripoint &p : closest_points_first( pos(), SEEX + 1 ) ) {
if( g->is_empty( p ) ) {
setpos( p );
return;
Expand Down
10 changes: 5 additions & 5 deletions src/npcmove.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -983,7 +983,7 @@ void npc::execute_action( npc_action action )
// Find a nice spot to sleep
int best_sleepy = sleep_spot( pos() );
tripoint best_spot = pos();
for( const tripoint &p : closest_tripoints_first( pos(), 6 ) ) {
for( const tripoint &p : closest_points_first( pos(), 6 ) ) {
if( !could_move_onto( p ) || !g->is_empty( p ) ) {
continue;
}
Expand Down Expand Up @@ -2465,7 +2465,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_tripoints_first( pos(), 1 );
std::vector<tripoint> candidates = closest_points_first( pos(), 1 );
candidates.erase( candidates.begin() );
std::sort( candidates.begin(), candidates.end(),
[&tar, &center]( const tripoint & l, const tripoint & r ) {
Expand Down Expand Up @@ -2675,7 +2675,7 @@ static cata::optional<tripoint> nearest_passable( const tripoint &p, const tripo

// We need to path to adjacent tile, not the exact one
// Let's pick the closest one to us that is passable
std::vector<tripoint> candidates = closest_tripoints_first( p, 1 );
std::vector<tripoint> candidates = closest_points_first( p, 1 );
std::sort( candidates.begin(), candidates.end(), [ closest_to ]( const tripoint & l,
const tripoint & r ) {
return rl_dist( closest_to, l ) < rl_dist( closest_to, r );
Expand Down Expand Up @@ -2746,7 +2746,7 @@ void npc::move_away_from( const std::vector<sphere> &spheres, bool no_bashing )
void npc::see_item_say_smth( const itype_id &object, const std::string &smth )
{
map &here = get_map();
for( const tripoint &p : closest_tripoints_first( pos(), 6 ) ) {
for( const tripoint &p : closest_points_first( pos(), 6 ) ) {
if( here.sees_some_items( p, *this ) && sees( p ) ) {
for( const item &it : here.i_at( p ) ) {
if( one_in( 100 ) && ( it.typeId() == object ) ) {
Expand Down Expand Up @@ -2850,7 +2850,7 @@ void npc::find_item()
}
};

for( const tripoint &p : closest_tripoints_first( pos(), range ) ) {
for( const tripoint &p : closest_points_first( pos(), range ) ) {
// TODO: Make this sight check not overdraw nearby tiles
// TODO: Optimize that zone check
if( is_player_ally() && g->check_zone( zone_type_no_npc_pickup, p ) ) {
Expand Down
2 changes: 1 addition & 1 deletion src/overmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2320,7 +2320,7 @@ void overmap::place_forest_trailheads()

const auto trailhead_close_to_road = [&]( const tripoint & trailhead ) {
bool close = false;
for( const tripoint &nearby_point : closest_tripoints_first(
for( const tripoint &nearby_point : closest_points_first(
trailhead,
settings.forest_trail.trailhead_road_distance
) ) {
Expand Down
2 changes: 1 addition & 1 deletion src/overmapbuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -994,7 +994,7 @@ std::vector<tripoint> overmapbuffer::find_all( const tripoint &origin,
const int min_dist = params.min_distance;
const int max_dist = params.search_range ? params.search_range : OMAPX;

for( const tripoint &loc : closest_tripoints_first( origin, min_dist, max_dist ) ) {
for( const tripoint &loc : closest_points_first( origin, min_dist, max_dist ) ) {
if( is_findable_location( loc, params ) ) {
result.push_back( loc );
}
Expand Down
6 changes: 3 additions & 3 deletions src/point.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ point clamp( const point &p, const inclusive_rectangle &r )
return point( clamp( p.x, r.p_min.x, r.p_max.x ), clamp( p.y, r.p_min.y, r.p_max.y ) );
}

std::vector<tripoint> closest_tripoints_first( const tripoint &center, int max_dist )
std::vector<tripoint> closest_points_first( const tripoint &center, int max_dist )
{
return closest_tripoints_first( center, 0, max_dist );
return closest_points_first( center, 0, max_dist );
}

std::vector<tripoint> closest_tripoints_first( const tripoint &center, int min_dist, int max_dist )
std::vector<tripoint> closest_points_first( const tripoint &center, int min_dist, int max_dist )
{
const std::vector<point> points = closest_points_first( center.xy(), min_dist, max_dist );

Expand Down
4 changes: 2 additions & 2 deletions src/point.h
Original file line number Diff line number Diff line change
Expand Up @@ -372,8 +372,8 @@ struct sphere {
* Following functions return points in a spiral pattern starting at center_x/center_y until it hits the radius. Clockwise fashion.
* Credit to Tom J Nowell; http://stackoverflow.com/a/1555236/1269969
*/
std::vector<tripoint> closest_tripoints_first( const tripoint &center, int max_dist );
std::vector<tripoint> closest_tripoints_first( const tripoint &center, int min_dist, int max_dist );
std::vector<tripoint> closest_points_first( const tripoint &center, int max_dist );
std::vector<tripoint> closest_points_first( const tripoint &center, int min_dist, int max_dist );

std::vector<point> closest_points_first( const point &center, int max_dist );
std::vector<point> closest_points_first( const point &center, int min_dist, int max_dist );
Expand Down
4 changes: 2 additions & 2 deletions src/ranged.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1542,7 +1542,7 @@ static void cycle_action( item &weap, 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_tripoints_first( pos, 1 );
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 );
Expand Down Expand Up @@ -2410,7 +2410,7 @@ bool target_ui::choose_initial_target( bool reentered, tripoint &new_dst )
// Try to find at least something
if( targets.empty() ) {
// The closest practice target
const std::vector<tripoint> nearby = closest_tripoints_first( src, range );
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 ) {
return here.tr_at( pt ).id == tr_practice_target && this->you->sees( pt );
Expand Down
2 changes: 1 addition & 1 deletion src/vehicle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6602,7 +6602,7 @@ void vehicle::leak_fuel( vehicle_part &pt )

map &here = get_map();
// leak in random directions but prefer closest tiles and avoid walls or other obstacles
std::vector<tripoint> tiles = closest_tripoints_first( global_part_pos3( pt ), 1 );
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() );
Expand Down
4 changes: 2 additions & 2 deletions src/vehicle_selector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ vehicle_selector::vehicle_selector( const tripoint &pos, int radius, bool access
bool visibility_only )
{
map &here = get_map();
for( const tripoint &e : closest_tripoints_first( pos, radius ) ) {
for( const tripoint &e : closest_points_first( pos, radius ) ) {
if( !accessible ||
( visibility_only ? here.sees( pos, e, radius ) : here.clear_path( pos, e, radius, 1, 100 ) ) ) {
if( const optional_vpart_position vp = here.veh_at( e ) ) {
Expand All @@ -26,7 +26,7 @@ vehicle_selector::vehicle_selector( const tripoint &pos, int radius, bool access
const vehicle &ignore )
{
map &here = get_map();
for( const tripoint &e : closest_tripoints_first( pos, radius ) ) {
for( const tripoint &e : closest_points_first( pos, radius ) ) {
if( !accessible || here.clear_path( pos, e, radius, 1, 100 ) ) {
if( const optional_vpart_position vp = here.veh_at( e ) ) {
if( &vp->vehicle() != &ignore ) {
Expand Down
2 changes: 1 addition & 1 deletion src/wish.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ void debug_menu::wishmonster( const cata::optional<tripoint> &p )
const mtype_id &mon_type = mtypes[ wmenu.ret ]->id;
if( cata::optional<tripoint> spawn = p ? p : g->look_around() ) {
int num_spawned = 0;
for( const tripoint &destination : closest_tripoints_first( *spawn, cb.group ) ) {
for( const tripoint &destination : closest_points_first( *spawn, cb.group ) ) {
monster *const mon = g->place_critter_at( mon_type, destination );
if( !mon ) {
continue;
Expand Down
Loading