From 4f597dc72a631f31835163f383d83b500d06f8b5 Mon Sep 17 00:00:00 2001 From: John Bytheway Date: Wed, 8 Jul 2020 21:30:13 -0400 Subject: [PATCH] Make closest_points_first work for all point types Previously we had separate functions closest_points_first and closest_tripoints_first. We'd like to extend this functionality to coord_point, but it's easier to do if both the raw underlying functions have the same name (so the generic wrapper can be the same). To that end, rename closest_tripoints_first to closest_points_first (they are chosen between based on overload resolution), and add a wrapper to allow coord_point to be used similarly. --- src/activity_item_handling.cpp | 2 +- src/coordinates.h | 22 ++++++++++++++ src/crafting.cpp | 4 +-- src/game.cpp | 4 +-- src/iexamine.cpp | 6 ++-- src/inventory_ui.cpp | 2 +- src/iuse.cpp | 4 +-- src/iuse_actor.cpp | 2 +- src/map.cpp | 6 ++-- src/map_selector.cpp | 2 +- src/monattack.cpp | 6 ++-- src/mondefense.cpp | 2 +- src/monmove.cpp | 2 +- src/npc.cpp | 2 +- src/npcmove.cpp | 10 +++---- src/overmap.cpp | 2 +- src/overmapbuffer.cpp | 2 +- src/point.cpp | 6 ++-- src/point.h | 4 +-- src/ranged.cpp | 4 +-- src/vehicle.cpp | 2 +- src/vehicle_selector.cpp | 4 +-- src/wish.cpp | 2 +- tests/explosion_balance_test.cpp | 2 +- tests/point_test.cpp | 50 +++++++++++++++++++++++++++----- tests/visitable_remove_test.cpp | 8 ++--- 26 files changed, 109 insertions(+), 53 deletions(-) diff --git a/src/activity_item_handling.cpp b/src/activity_item_handling.cpp index 185b78ee9ab7e..0fbd1e36f82d9 100644 --- a/src/activity_item_handling.cpp +++ b/src/activity_item_handling.cpp @@ -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 adjacent = closest_tripoints_first( pos, PICKUP_RANGE ); + std::vector adjacent = closest_points_first( pos, PICKUP_RANGE ); adjacent.erase( adjacent.begin() ); cata::optional best_fire = starting_fire ? act.placement : find_best_fire( adjacent, diff --git a/src/coordinates.h b/src/coordinates.h index 59003e62c602c..ccb1cdfc6c7c1 100644 --- a/src/coordinates.h +++ b/src/coordinates.h @@ -378,6 +378,28 @@ using tripoint_abs_om = coords::coord_point +std::vector> + closest_points_first( const coords::coord_point &loc, + int min_dist, int max_dist ) +{ + std::vector raw_result = closest_points_first( loc.raw(), min_dist, max_dist ); + std::vector> 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( p ); + } ); + return result; +} +template +std::vector> + closest_points_first( const coords::coord_point &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. diff --git a/src/crafting.cpp b/src/crafting.cpp index 6699083b62870..1166876872c14 100644 --- a/src/crafting.cpp +++ b/src/crafting.cpp @@ -472,7 +472,7 @@ std::vector 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; @@ -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() ) { diff --git a/src/game.cpp b/src/game.cpp index 5add135c1a499..ba2cd7c5c33aa 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -7225,7 +7225,7 @@ std::vector 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 ) ) { @@ -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 candidates = closest_tripoints_first( u.pos(), 1 ); + std::vector candidates = closest_points_first( u.pos(), 1 ); candidates.erase( std::remove_if( candidates.begin(), candidates.end(), [this]( const tripoint & c ) { return !is_empty( c ); diff --git a/src/iexamine.cpp b/src/iexamine.cpp index e8ebe6038b86e..667b4ff8d0a16 100644 --- a/src/iexamine.cpp +++ b/src/iexamine.cpp @@ -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 ) ) { @@ -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 ) ) { @@ -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 ) ) { diff --git a/src/inventory_ui.cpp b/src/inventory_ui.cpp index d486a1490229c..ab54eccdd4600 100644 --- a/src/inventory_ui.cpp +++ b/src/inventory_ui.cpp @@ -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; diff --git a/src/iuse.cpp b/src/iuse.cpp index 194b57f7693b5..d6fdb8bd607fd 100644 --- a/src/iuse.cpp +++ b/src/iuse.cpp @@ -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; } @@ -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 ps = closest_tripoints_first( p->pos(), 3 ); + std::vector 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 ); diff --git a/src/iuse_actor.cpp b/src/iuse_actor.cpp index bee1910357390..1493dc96a074f 100644 --- a/src/iuse_actor.cpp +++ b/src/iuse_actor.cpp @@ -478,7 +478,7 @@ static std::vector points_for_gas_cloud( const tripoint ¢er, int r { map &here = get_map(); std::vector result; - for( const auto &p : closest_tripoints_first( center, radius ) ) { + for( const auto &p : closest_points_first( center, radius ) ) { if( here.impassable( p ) ) { continue; } diff --git a/src/map.cpp b/src/map.cpp index 19353efe76307..2dabc1d774a47 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -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 tiles = closest_tripoints_first( pos, max_dist ); + std::vector 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, @@ -6342,10 +6342,10 @@ std::vector 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 line = line_to( f, t, 0, 0 ); - const std::vector spiral = closest_tripoints_first( f, 1 ); + const std::vector spiral = closest_points_first( f, 1 ); const std::vector 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 diff --git a/src/map_selector.cpp b/src/map_selector.cpp index 8c14a7c3a9c7f..601c53201ebe0 100644 --- a/src/map_selector.cpp +++ b/src/map_selector.cpp @@ -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 ); } diff --git a/src/monattack.cpp b/src/monattack.cpp index 87c12fa770fbb..c767a6ddada5c 100644 --- a/src/monattack.cpp +++ b/src/monattack.cpp @@ -2371,7 +2371,7 @@ bool mattack::formblob( monster *z ) } bool didit = false; - std::vector pts = closest_tripoints_first( z->pos(), 1 ); + std::vector pts = closest_points_first( z->pos(), 1 ); // Don't check own tile pts.erase( pts.begin() ); for( const tripoint &dest : pts ) { @@ -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 allies; - std::vector nearby_points = closest_tripoints_first( z->pos(), 3 ); + std::vector 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. @@ -2490,7 +2490,7 @@ bool mattack::jackson( monster *z ) { // Jackson draws nearby zombies into the dance. std::list allies; - std::vector nearby_points = closest_tripoints_first( z->pos(), 3 ); + std::vector 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. diff --git a/src/mondefense.cpp b/src/mondefense.cpp index cdaaafa3d690f..e8797d9f9a5b0 100644 --- a/src/mondefense.cpp +++ b/src/mondefense.cpp @@ -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 pts = closest_tripoints_first( source->pos(), 1 ); + std::vector pts = closest_points_first( source->pos(), 1 ); pts.erase( std::remove( pts.begin(), pts.end(), m.pos() ), pts.end() ); projectile prj; diff --git a/src/monmove.cpp b/src/monmove.cpp index da20cead5ac78..c7b5f7c7cffcd 100644 --- a/src/monmove.cpp +++ b/src/monmove.cpp @@ -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; } diff --git a/src/npc.cpp b/src/npc.cpp index 403dbecfe3406..a0d2519d9e4db 100644 --- a/src/npc.cpp +++ b/src/npc.cpp @@ -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; diff --git a/src/npcmove.cpp b/src/npcmove.cpp index 462f02cbfd799..3e20cdbbfa31a 100644 --- a/src/npcmove.cpp +++ b/src/npcmove.cpp @@ -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; } @@ -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 candidates = closest_tripoints_first( pos(), 1 ); + std::vector candidates = closest_points_first( pos(), 1 ); candidates.erase( candidates.begin() ); std::sort( candidates.begin(), candidates.end(), [&tar, ¢er]( const tripoint & l, const tripoint & r ) { @@ -2675,7 +2675,7 @@ static cata::optional 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 candidates = closest_tripoints_first( p, 1 ); + std::vector 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 ); @@ -2746,7 +2746,7 @@ void npc::move_away_from( const std::vector &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 ) ) { @@ -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 ) ) { diff --git a/src/overmap.cpp b/src/overmap.cpp index 9284303d12ab4..fb32f80a13ae1 100644 --- a/src/overmap.cpp +++ b/src/overmap.cpp @@ -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 ) ) { diff --git a/src/overmapbuffer.cpp b/src/overmapbuffer.cpp index d48aa6cfb6225..6e9aef594e765 100644 --- a/src/overmapbuffer.cpp +++ b/src/overmapbuffer.cpp @@ -994,7 +994,7 @@ std::vector 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 ); } diff --git a/src/point.cpp b/src/point.cpp index 56600f031a731..e2f9d33c5cb73 100644 --- a/src/point.cpp +++ b/src/point.cpp @@ -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 closest_tripoints_first( const tripoint ¢er, int max_dist ) +std::vector closest_points_first( const tripoint ¢er, int max_dist ) { - return closest_tripoints_first( center, 0, max_dist ); + return closest_points_first( center, 0, max_dist ); } -std::vector closest_tripoints_first( const tripoint ¢er, int min_dist, int max_dist ) +std::vector closest_points_first( const tripoint ¢er, int min_dist, int max_dist ) { const std::vector points = closest_points_first( center.xy(), min_dist, max_dist ); diff --git a/src/point.h b/src/point.h index 2c8fe42fc5728..498d251a17590 100644 --- a/src/point.h +++ b/src/point.h @@ -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 closest_tripoints_first( const tripoint ¢er, int max_dist ); -std::vector closest_tripoints_first( const tripoint ¢er, int min_dist, int max_dist ); +std::vector closest_points_first( const tripoint ¢er, int max_dist ); +std::vector closest_points_first( const tripoint ¢er, int min_dist, int max_dist ); std::vector closest_points_first( const point ¢er, int max_dist ); std::vector closest_points_first( const point ¢er, int min_dist, int max_dist ); diff --git a/src/ranged.cpp b/src/ranged.cpp index 9b6adce41b8b7..9f28a8e586fbe 100644 --- a/src/ranged.cpp +++ b/src/ranged.cpp @@ -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 tiles = closest_tripoints_first( pos, 1 ); + 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 ); @@ -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 nearby = closest_tripoints_first( src, range ); + 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 ) { return here.tr_at( pt ).id == tr_practice_target && this->you->sees( pt ); diff --git a/src/vehicle.cpp b/src/vehicle.cpp index ba31befd91ba6..b129659941fc0 100644 --- a/src/vehicle.cpp +++ b/src/vehicle.cpp @@ -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 tiles = closest_tripoints_first( global_part_pos3( pt ), 1 ); + std::vector 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() ); diff --git a/src/vehicle_selector.cpp b/src/vehicle_selector.cpp index a1dcc3b14701c..4c6ff142c67e6 100644 --- a/src/vehicle_selector.cpp +++ b/src/vehicle_selector.cpp @@ -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 ) ) { @@ -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 ) { diff --git a/src/wish.cpp b/src/wish.cpp index 186062f20f61f..28284f0a2b7d3 100644 --- a/src/wish.cpp +++ b/src/wish.cpp @@ -409,7 +409,7 @@ void debug_menu::wishmonster( const cata::optional &p ) const mtype_id &mon_type = mtypes[ wmenu.ret ]->id; if( cata::optional 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; diff --git a/tests/explosion_balance_test.cpp b/tests/explosion_balance_test.cpp index 12aade2b89328..c1869945fdccc 100644 --- a/tests/explosion_balance_test.cpp +++ b/tests/explosion_balance_test.cpp @@ -48,7 +48,7 @@ static void check_lethality( const std::string &explosive_id, const int range, f // Spawn some monsters in a circle. tripoint origin( 30, 30, 0 ); int num_subjects_this_time = 0; - for( const tripoint &monster_position : closest_tripoints_first( origin, range ) ) { + for( const tripoint &monster_position : closest_points_first( origin, range ) ) { if( rl_dist( monster_position, origin ) != range ) { continue; } diff --git a/tests/point_test.cpp b/tests/point_test.cpp index 596d9c6d103bd..55fd1c820808d 100644 --- a/tests/point_test.cpp +++ b/tests/point_test.cpp @@ -2,6 +2,7 @@ #include #include "catch/catch.hpp" +#include "coordinates.h" #include "point.h" TEST_CASE( "rectangle_containment", "[point]" ) @@ -65,27 +66,27 @@ TEST_CASE( "tripoint_xy", "[point]" ) CHECK( p.xy() == point( 1, 2 ) ); } -TEST_CASE( "closest_tripoints_first", "[point]" ) +TEST_CASE( "closest_points_first_tripoint", "[point]" ) { const tripoint center = { 1, -1, 2 }; GIVEN( "min_dist > max_dist" ) { - const std::vector result = closest_tripoints_first( center, 1, 0 ); + const std::vector result = closest_points_first( center, 1, 0 ); CHECK( result.empty() ); } GIVEN( "min_dist = max_dist = 0" ) { - const std::vector result = closest_tripoints_first( center, 0, 0 ); + const std::vector result = closest_points_first( center, 0, 0 ); - CHECK( result.size() == 1 ); + REQUIRE( result.size() == 1 ); CHECK( result[0] == tripoint{ 1, -1, 2 } ); } GIVEN( "min_dist = 0, max_dist = 1" ) { - const std::vector result = closest_tripoints_first( center, 0, 1 ); + const std::vector result = closest_points_first( center, 0, 1 ); - CHECK( result.size() == 9 ); + REQUIRE( result.size() == 9 ); CHECK( result[0] == tripoint{ 1, -1, 2 } ); CHECK( result[1] == tripoint{ 2, -1, 2 } ); CHECK( result[2] == tripoint{ 2, 0, 2 } ); @@ -98,9 +99,9 @@ TEST_CASE( "closest_tripoints_first", "[point]" ) } GIVEN( "min_dist = 2, max_dist = 2" ) { - const std::vector result = closest_tripoints_first( center, 2, 2 ); + const std::vector result = closest_points_first( center, 2, 2 ); - CHECK( result.size() == 16 ); + REQUIRE( result.size() == 16 ); CHECK( result[0] == tripoint{ 3, -2, 2 } ); CHECK( result[1] == tripoint{ 3, -1, 2 } ); @@ -120,3 +121,36 @@ TEST_CASE( "closest_tripoints_first", "[point]" ) CHECK( result[15] == tripoint{ 3, -3, 2 } ); } } + +TEST_CASE( "closest_points_first_point_abs_omt", "[point]" ) +{ + const point_abs_omt center( point( 1, 3 ) ); + + GIVEN( "min_dist > max_dist" ) { + const std::vector result = closest_points_first( center, 1, 0 ); + + CHECK( result.empty() ); + } + + GIVEN( "min_dist = max_dist = 0" ) { + const std::vector result = closest_points_first( center, 0, 0 ); + + REQUIRE( result.size() == 1 ); + CHECK( result[0].raw() == point{ 1, 3 } ); + } + + GIVEN( "min_dist = 0, max_dist = 1" ) { + const std::vector result = closest_points_first( center, 0, 1 ); + + REQUIRE( result.size() == 9 ); + CHECK( result[0].raw() == point{ 1, 3 } ); + CHECK( result[1].raw() == point{ 2, 3 } ); + CHECK( result[2].raw() == point{ 2, 4 } ); + CHECK( result[3].raw() == point{ 1, 4 } ); + CHECK( result[4].raw() == point{ 0, 4 } ); + CHECK( result[5].raw() == point{ 0, 3 } ); + CHECK( result[6].raw() == point{ 0, 2 } ); + CHECK( result[7].raw() == point{ 1, 2 } ); + CHECK( result[8].raw() == point{ 2, 2 } ); + } +} diff --git a/tests/visitable_remove_test.cpp b/tests/visitable_remove_test.cpp index fc6cd722d4894..3dbd44a810414 100644 --- a/tests/visitable_remove_test.cpp +++ b/tests/visitable_remove_test.cpp @@ -54,7 +54,7 @@ TEST_CASE( "visitable_remove", "[visitable]" ) // check if all tiles within radius are loaded within current submap and passable const auto suitable = [&here]( const tripoint & pos, const int radius ) { - std::vector tiles = closest_tripoints_first( pos, radius ); + std::vector tiles = closest_points_first( pos, radius ); return std::all_of( tiles.begin(), tiles.end(), [&here]( const tripoint & e ) { if( !here.inbounds( e ) ) { return false; @@ -72,7 +72,7 @@ TEST_CASE( "visitable_remove", "[visitable]" ) // move player randomly until we find a suitable position while( !suitable( p.pos(), 1 ) ) { CHECK( !p.in_vehicle ); - p.setpos( random_entry( closest_tripoints_first( p.pos(), 1 ) ) ); + p.setpos( random_entry( closest_points_first( p.pos(), 1 ) ) ); } item temp_liquid( liquid_id ); @@ -295,7 +295,7 @@ TEST_CASE( "visitable_remove", "[visitable]" ) } GIVEN( "A player surrounded by several bottles of water" ) { - std::vector tiles = closest_tripoints_first( p.pos(), 1 ); + std::vector tiles = closest_points_first( p.pos(), 1 ); tiles.erase( tiles.begin() ); // player tile int our = 0; // bottles placed on player tile @@ -412,7 +412,7 @@ TEST_CASE( "visitable_remove", "[visitable]" ) } GIVEN( "An adjacent vehicle contains several bottles of water" ) { - std::vector tiles = closest_tripoints_first( p.pos(), 1 ); + std::vector tiles = closest_points_first( p.pos(), 1 ); tiles.erase( tiles.begin() ); // player tile tripoint veh = random_entry( tiles ); REQUIRE( here.add_vehicle( vproto_id( "shopping_cart" ), veh, 0, 0, 0 ) );