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

Refactor closest_(tri)points_first functions #37108

Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -2950,7 +2950,7 @@ static cata::optional<tripoint> find_refuel_spot_trap( const std::vector<tripoin
void try_fuel_fire( player_activity &act, player &p, const bool starting_fire )
{
const tripoint pos = p.pos();
auto adjacent = closest_tripoints_first( PICKUP_RANGE, pos );
std::vector<tripoint> adjacent = closest_tripoints_first( pos, PICKUP_RANGE );
adjacent.erase( adjacent.begin() );

cata::optional<tripoint> best_fire = starting_fire ? act.placement : find_best_fire( adjacent,
Expand Down
5 changes: 2 additions & 3 deletions src/crafting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ std::vector<const item *> player::get_eligible_containers_for_crafting() const
}

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

std::vector<tripoint> points = closest_tripoints_first( iRadius, u.pos() );

for( auto &points_p_it : points ) {
for( auto &points_p_it : closest_tripoints_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 @@ -10191,7 +10189,7 @@ void game::vertical_move( int movez, bool force )

if( !npcs_to_bring.empty() ) {
// Would look nicer randomly scrambled
auto candidates = closest_tripoints_first( 1, u.pos() );
std::vector<tripoint> candidates = closest_tripoints_first( u.pos(), 1 );
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 @@ -755,7 +755,7 @@ void iexamine::elevator( player &p, const tripoint &examp )
} else if( g->m.ter( critter.pos() ) == ter_id( "t_elevator" ) ) {
tripoint critter_omt = ms_to_omt_copy( g->m.getabs( critter.pos() ) );
if( critter_omt == new_floor_omt ) {
for( const tripoint &candidate : closest_tripoints_first( 10, critter.pos() ) ) {
for( const tripoint &candidate : closest_tripoints_first( critter.pos(), 10 ) ) {
if( g->m.ter( candidate ) != ter_id( "t_elevator" ) &&
g->m.passable( candidate ) &&
!g->critter_at( candidate ) ) {
Expand All @@ -778,7 +778,7 @@ void iexamine::elevator( player &p, const tripoint &examp )
tripoint critter_omt = ms_to_omt_copy( g->m.getabs( critter.pos() ) );

if( critter_omt == original_floor_omt ) {
for( const tripoint &candidate : closest_tripoints_first( 10, p.pos() ) ) {
for( const tripoint &candidate : closest_tripoints_first( p.pos(), 10 ) ) {
if( g->m.ter( candidate ) == ter_id( "t_elevator" ) &&
candidate != p.pos() &&
!g->critter_at( candidate ) ) {
Expand Down Expand Up @@ -1876,7 +1876,7 @@ void iexamine::egg_sack_generic( player &p, const tripoint &examp,
g->m.furn_set( examp, f_egg_sacke );
int monster_count = 0;
if( one_in( 2 ) ) {
for( const tripoint &p : closest_tripoints_first( 1, examp ) ) {
for( const tripoint &p : closest_tripoints_first( examp, 1 ) ) {
if( !one_in( 3 ) ) {
continue;
} else if( g->place_critter_at( montype, p ) ) {
Expand Down
2 changes: 1 addition & 1 deletion src/inventory_ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1202,7 +1202,7 @@ void inventory_selector::add_vehicle_items( const tripoint &target )
void inventory_selector::add_nearby_items( int radius )
{
if( radius >= 0 ) {
for( const auto &pos : closest_tripoints_first( radius, u.pos() ) ) {
for( const tripoint &pos : closest_tripoints_first( u.pos(), radius ) ) {
// can not reach this -> can not access its contents
if( u.pos() != pos && !g->m.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 @@ -1178,7 +1178,7 @@ static void spawn_spores( const player &p )
{
int spores_spawned = 0;
fungal_effects fe( *g, g->m );
for( const tripoint &dest : closest_tripoints_first( 4, p.pos() ) ) {
for( const tripoint &dest : closest_tripoints_first( p.pos(), 4 ) ) {
if( g->m.impassable( dest ) ) {
continue;
}
Expand Down Expand Up @@ -5376,7 +5376,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( 3, p->pos() );
std::vector<tripoint> ps = closest_tripoints_first( p->pos(), 3 );
for( auto p_it : ps ) {
if( !one_in( 3 ) ) {
g->m.add_field( p_it, fd_fire, 1 + rng( 0, 1 ) * rng( 0, 1 ), 3_minutes );
Expand Down
3 changes: 1 addition & 2 deletions src/iuse_actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -397,9 +397,8 @@ std::unique_ptr<iuse_actor> explosion_iuse::clone() const
// They must also be passable.
static std::vector<tripoint> points_for_gas_cloud( const tripoint &center, int radius )
{
const std::vector<tripoint> gas_sources = closest_tripoints_first( radius, center );
std::vector<tripoint> result;
for( const auto &p : gas_sources ) {
for( const auto &p : closest_tripoints_first( center, radius ) ) {
if( g->m.impassable( p ) ) {
continue;
}
Expand Down
10 changes: 2 additions & 8 deletions src/iuse_software_minesweeper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
#include "optional.h"
#include "point.h"

std::vector<tripoint> closest_tripoints_first( int radius, const tripoint &p );

minesweeper_game::minesweeper_game()
{
iMinY = 8;
Expand Down Expand Up @@ -114,9 +112,7 @@ void minesweeper_game::new_level( const catacurses::window &w_minesweeper )
for( int y = 0; y < iLevelY; y++ ) {
for( int x = 0; x < iLevelX; x++ ) {
if( mLevel[y][x] == static_cast<int>( bomb ) ) {
const auto circle = closest_tripoints_first( 1, {x, y, 0} );

for( const auto &p : circle ) {
for( const point &p : closest_points_first( {x, y}, 1 ) ) {
if( p.x >= 0 && p.x < iLevelX && p.y >= 0 && p.y < iLevelY ) {
if( mLevel[p.y][p.x] != static_cast<int>( bomb ) ) {
mLevel[p.y][p.x]++;
Expand Down Expand Up @@ -217,9 +213,7 @@ int minesweeper_game::start_game()
mLevelReveal[y][x] = seen;

if( mLevel[y][x] == 0 ) {
const auto circle = closest_tripoints_first( 1, {x, y, 0} );

for( const auto &p : circle ) {
for( const point &p : closest_points_first( {x, y}, 1 ) ) {
if( p.x >= 0 && p.x < iLevelX && p.y >= 0 && p.y < iLevelY ) {
if( mLevelReveal[p.y][p.x] != seen ) {
rec_reveal( p.y, p.x );
Expand Down
59 changes: 3 additions & 56 deletions src/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4098,7 +4098,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;
auto tiles = closest_tripoints_first( max_dist, pos );
std::vector<tripoint> tiles = closest_tripoints_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 @@ -6248,10 +6248,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( 1, f );
const std::vector<tripoint> spiral = closest_tripoints_first( f, 1 );
const std::vector<int> pos_index {1, 2, 4, 6, 8, 7, 5, 3};

// All possible constellations (closest_points_first goes clockwise)
// All possible constellations (closest_tripoints_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 Expand Up @@ -7741,59 +7741,6 @@ void map::build_map_cache( const int zlev, bool skip_lightmap )
}
}

//this returns 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<point> closest_points_first( int radius, const point &center )
{
std::vector<point> points;
int X = radius * 2 + 1;
int Y = radius * 2 + 1;
int x = 0;
int y = 0;
int dx = 0;
int dy = -1;
int t = std::max( X, Y );
int maxI = t * t;
for( int i = 0; i < maxI; i++ ) {
if( -X / 2 <= x && x <= X / 2 && -Y / 2 <= y && y <= Y / 2 ) {
points.push_back( center + point( x, y ) );
}
if( x == y || ( x < 0 && x == -y ) || ( x > 0 && x == 1 - y ) ) {
t = dx;
dx = -dy;
dy = t;
}
x += dx;
y += dy;
}
return points;
}

std::vector<tripoint> closest_tripoints_first( int radius, const tripoint &center )
{
std::vector<tripoint> points;
int X = radius * 2 + 1;
int Y = radius * 2 + 1;
int x = 0;
int y = 0;
int dx = 0;
int dy = -1;
int t = std::max( X, Y );
int maxI = t * t;
for( int i = 0; i < maxI; i++ ) {
if( -X / 2 <= x && x <= X / 2 && -Y / 2 <= y && y <= Y / 2 ) {
points.push_back( center + point( x, y ) );
}
if( x == y || ( x < 0 && x == -y ) || ( x > 0 && x == 1 - y ) ) {
t = dx;
dx = -dy;
dy = t;
}
x += dx;
y += dy;
}
return points;
}
//////////
///// coordinate helpers

Expand Down
3 changes: 0 additions & 3 deletions src/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -1826,9 +1826,6 @@ class map
template<int SIZE, int MULTIPLIER>
void shift_bitset_cache( std::bitset<SIZE *SIZE> &cache, const point &s );

std::vector<point> closest_points_first( int radius, const point &center );
// Does not build "piles" - does the same as above functions, except in tripoints
std::vector<tripoint> closest_tripoints_first( int radius, const tripoint &center );
bool ter_furn_has_flag( const ter_t &ter, const furn_t &furn, ter_bitflags flag );
class tinymap : public map
{
Expand Down
2 changes: 1 addition & 1 deletion src/map_selector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

map_selector::map_selector( const tripoint &pos, int radius, bool accessible )
{
for( const auto &e : closest_tripoints_first( radius, pos ) ) {
for( const tripoint &e : closest_tripoints_first( pos, radius ) ) {
if( !accessible || g->m.clear_path( pos, e, radius, 1, 100 ) ) {
data.emplace_back( e );
}
Expand Down
3 changes: 1 addition & 2 deletions src/mapgen_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3439,8 +3439,7 @@ void mapgen_lake_shore( mapgendata &dat )
const auto draw_shallow_water = [&]( const point & from, const point & to ) {
std::vector<point> points = line_to( from, to );
for( auto &p : points ) {
std::vector<point> buffered_points = closest_points_first( 1, p );
for( const point &bp : buffered_points ) {
for( const point &bp : closest_points_first( p, 1 ) ) {
if( !map_boundaries.contains_inclusive( bp ) ) {
continue;
}
Expand Down
6 changes: 3 additions & 3 deletions src/monattack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2282,7 +2282,7 @@ bool mattack::formblob( monster *z )
}

bool didit = false;
auto pts = closest_tripoints_first( 1, z->pos() );
std::vector<tripoint> pts = closest_tripoints_first( z->pos(), 1 );
// Don't check own tile
pts.erase( pts.begin() );
for( const tripoint &dest : pts ) {
Expand Down Expand Up @@ -2368,7 +2368,7 @@ bool mattack::callblobs( monster *z )
// and keep the rest near the brain blob for protection.
tripoint enemy = g->u.pos();
std::list<monster *> allies;
std::vector<tripoint> nearby_points = closest_tripoints_first( 3, z->pos() );
std::vector<tripoint> nearby_points = closest_tripoints_first( z->pos(), 3 );
for( monster &candidate : g->all_monsters() ) {
if( candidate.type->in_species( BLOB ) && candidate.type->id != mon_blob_brain ) {
// Just give the allies consistent assignments.
Expand Down Expand Up @@ -2401,7 +2401,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( 3, z->pos() );
std::vector<tripoint> nearby_points = closest_tripoints_first( z->pos(), 3 );
for( monster &candidate : g->all_monsters() ) {
if( candidate.type->in_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 @@ -114,7 +114,7 @@ void mdefense::acidsplash( monster &m, Creature *const source,
const tripoint initial_target = source == nullptr ? m.pos() : source->pos();

// Don't splatter directly on the `m`, that doesn't work well
auto pts = closest_tripoints_first( 1, initial_target );
std::vector<tripoint> pts = closest_tripoints_first( initial_target, 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 @@ -1390,7 +1390,7 @@ bool monster::attack_at( const tripoint &p )

static tripoint find_closest_stair( const tripoint &near_this, const ter_bitflags stair_type )
{
for( const tripoint &candidate : closest_tripoints_first( 10, near_this ) ) {
for( const tripoint &candidate : closest_tripoints_first( near_this, 10 ) ) {
if( g->m.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 @@ -734,7 +734,7 @@ void npc::place_on_map()
return;
}

for( const tripoint &p : closest_tripoints_first( SEEX + 1, pos() ) ) {
for( const tripoint &p : closest_tripoints_first( pos(), SEEX + 1 ) ) {
if( g->is_empty( p ) ) {
setpos( p );
return;
Expand Down
11 changes: 5 additions & 6 deletions src/npcmove.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -973,8 +973,7 @@ void npc::execute_action( npc_action action )
// Find a nice spot to sleep
int best_sleepy = sleep_spot( pos() );
tripoint best_spot = pos();
const auto points = closest_tripoints_first( 6, pos() );
for( const tripoint &p : points ) {
for( const tripoint &p : closest_tripoints_first( pos(), 6 ) ) {
if( !could_move_onto( p ) || !g->is_empty( p ) ) {
continue;
}
Expand Down Expand Up @@ -2444,7 +2443,7 @@ void npc::avoid_friendly_fire()
center.y = round( center.y / friend_count );
center.z = round( center.z / friend_count );

auto candidates = closest_tripoints_first( 1, pos() );
std::vector<tripoint> candidates = closest_tripoints_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 @@ -2558,7 +2557,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
auto candidates = closest_tripoints_first( 1, p );
std::vector<tripoint> candidates = closest_tripoints_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 @@ -2627,7 +2626,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 )
{
for( const tripoint &p : closest_tripoints_first( 6, pos() ) ) {
for( const tripoint &p : closest_tripoints_first( pos(), 6 ) ) {
if( g->m.sees_some_items( p, *this ) && sees( p ) ) {
for( const item &it : g->m.i_at( p ) ) {
if( one_in( 100 ) && ( it.typeId() == object ) ) {
Expand Down Expand Up @@ -2728,7 +2727,7 @@ void npc::find_item()
}
};

for( const tripoint &p : closest_tripoints_first( range, pos() ) ) {
for( const tripoint &p : closest_tripoints_first( pos(), range ) ) {
// TODO: Make this sight check not overdraw nearby tiles
// TODO: Optimize that zone check
if( is_player_ally() && g->check_zone( no_pickup, p ) ) {
Expand Down
20 changes: 11 additions & 9 deletions src/overmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2274,10 +2274,11 @@ void overmap::place_forest_trailheads()

const auto trailhead_close_to_road = [&]( const tripoint & trailhead ) {
bool close = false;
for( const point &nearby_point : closest_points_first(
settings.forest_trail.trailhead_road_distance,
trailhead.xy() ) ) {
if( check_ot( "road", ot_match_type::contains, tripoint( nearby_point, 0 ) ) ) {
for( const tripoint &nearby_point : closest_tripoints_first(
trailhead,
settings.forest_trail.trailhead_road_distance
) ) {
if( check_ot( "road", ot_match_type::contains, nearby_point ) ) {
close = true;
}
}
Expand Down Expand Up @@ -2644,9 +2645,10 @@ void overmap::place_swamps()
for( int y = 0; y < OMAPY; y++ ) {
const tripoint pos( x, y, 0 );
if( is_ot_match( "river", ter( pos ), ot_match_type::contains ) ) {
std::vector<point> buffered_points = closest_points_first( rng(
settings.overmap_forest.river_floodplain_buffer_distance_min,
settings.overmap_forest.river_floodplain_buffer_distance_max ), pos.xy() );
std::vector<point> buffered_points = closest_points_first( pos.xy(),
rng(
settings.overmap_forest.river_floodplain_buffer_distance_min,
settings.overmap_forest.river_floodplain_buffer_distance_max ) );
for( const point &p : buffered_points ) {
if( !inbounds( p ) ) {
continue;
Expand Down Expand Up @@ -4157,8 +4159,8 @@ void overmap::place_specials( overmap_special_batch &enabled_specials )
std::vector<point> nearest_candidates;
// Since this starts at enabled_specials::origin, it will only place new overmaps
// in the 5x5 area surrounding the initial overmap, bounding the amount of work we will do.
for( const point &candidate_addr : closest_points_first( 2,
custom_overmap_specials.get_origin() ) ) {
for( const point &candidate_addr : closest_points_first(
custom_overmap_specials.get_origin(), 2 ) ) {
if( !overmap_buffer.has( candidate_addr ) ) {
int current_distance = square_dist( pos(), candidate_addr );
if( nearest_candidates.empty() || current_distance == previous_distance ) {
Expand Down
Loading