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

Typify form_from_map #78698

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions src/crafting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2057,8 +2057,8 @@ std::list<item> Character::consume_items( const comp_selection<item_comp> &is, i
return ret;
}
// populate a grid of spots that can be reached
std::vector<tripoint_bub_ms> reachable_pts;
m.reachable_flood_steps( reachable_pts, pos_bub(), PICKUP_RANGE, 1, 100 );
const std::vector<tripoint_bub_ms> &reachable_pts = m.reachable_flood_steps( pos_bub(),
PICKUP_RANGE, 1, 100 );
return consume_items( m, is, batch, filter, reachable_pts, select_ind );
}

Expand Down
37 changes: 16 additions & 21 deletions src/inventory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,22 @@ void inventory::form_from_map( const tripoint_bub_ms &origin, int range, const C
bool assign_invlet,
bool clear_path )
{
form_from_map( get_map(), origin, range, pl, assign_invlet, clear_path );
map &m = get_map();
// Populate a grid of spots that can be reached
// If we need a clear path we care about the reachability of points
if( clear_path ) {
const std::vector<tripoint_bub_ms> &reachable_pts = m.reachable_flood_steps( origin, range, 1,
100 );
form_from_map( m, reachable_pts, pl, assign_invlet );
} else {
std::vector<tripoint_bub_ms> reachable_pts;
// Fill reachable points with points_in_radius
tripoint_range<tripoint_bub_ms> in_radius = m.points_in_radius( origin, range );
for( const tripoint_bub_ms &p : in_radius ) {
reachable_pts.emplace_back( p );
}
form_from_map( m, reachable_pts, pl, assign_invlet );
}
}

void inventory::form_from_zone( map &m, std::unordered_set<tripoint_abs_ms> &zone_pts,
Expand All @@ -500,26 +515,6 @@ void inventory::form_from_zone( map &m, std::unordered_set<tripoint_abs_ms> &zon
form_from_map( m, pts, pl, assign_invlet );
}

void inventory::form_from_map( map &m, const tripoint_bub_ms &origin, int range,
const Character *pl,
bool assign_invlet,
bool clear_path )
{
// populate a grid of spots that can be reached
std::vector<tripoint_bub_ms> reachable_pts = {};
// If we need a clear path we care about the reachability of points
if( clear_path ) {
m.reachable_flood_steps( reachable_pts, origin, range, 1, 100 );
} else {
// Fill reachable points with points_in_radius
tripoint_range<tripoint_bub_ms> in_radius = m.points_in_radius( origin, range );
for( const tripoint_bub_ms &p : in_radius ) {
reachable_pts.emplace_back( p );
}
}
form_from_map( m, reachable_pts, pl, assign_invlet );
}

void inventory::form_from_map( map &m, std::vector<tripoint_bub_ms> pts, const Character *pl,
bool assign_invlet )
{
Expand Down
3 changes: 0 additions & 3 deletions src/inventory.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,6 @@ class inventory : public visitable
void form_from_map( const tripoint_bub_ms &origin, int range, const Character *pl = nullptr,
bool assign_invlet = true,
bool clear_path = true );
void form_from_map( map &m, const tripoint_bub_ms &origin, int range, const Character *pl = nullptr,
bool assign_invlet = true,
bool clear_path = true );
void form_from_map( map &m, std::vector<tripoint_bub_ms> pts, const Character *pl,
bool assign_invlet = true );
/**
Expand Down
118 changes: 31 additions & 87 deletions src/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6252,20 +6252,6 @@ std::list<item_location> map::items_with( const tripoint_bub_ms &p,
return ret;
}

std::list<item> map::use_amount( const std::vector<tripoint> &reachable_pts, const itype_id &type,
int &quantity, const std::function<bool( const item & )> &filter, bool select_ind )
{
std::vector<tripoint_bub_ms> temp;
temp.reserve( reachable_pts.size() );

for( const tripoint p : reachable_pts ) {
const tripoint_bub_ms pt( p );
temp.emplace_back( pt );
}

return map::use_amount( temp, type, quantity, filter, select_ind );
}

std::list<item> map::use_amount( const std::vector<tripoint_bub_ms> &reachable_pts,
const itype_id &type,
int &quantity, const std::function<bool( const item & )> &filter, bool select_ind )
Expand Down Expand Up @@ -6308,8 +6294,7 @@ std::list<item> map::use_amount( const tripoint_bub_ms &origin, const int range,
const itype_id &type,
int &quantity, const std::function<bool( const item & )> &filter, bool select_ind )
{
std::vector<tripoint_bub_ms> reachable_pts;
reachable_flood_steps( reachable_pts, origin, range, 1, 100 );
const std::vector<tripoint_bub_ms> &reachable_pts = reachable_flood_steps( origin, range, 1, 100 );
return use_amount( reachable_pts, type, quantity, filter, select_ind );
}

Expand Down Expand Up @@ -6375,22 +6360,6 @@ static void use_charges_from_furn( const furn_t &f, const itype_id &type, int &q
}
}

std::list<item> map::use_charges( const std::vector<tripoint> &reachable_pts,
const itype_id &type, int &quantity,
const std::function<bool( const item & )> &filter,
basecamp *bcp, bool in_tools )
{
std::vector<tripoint_bub_ms> temp;
temp.reserve( reachable_pts.size() );

for( tripoint p : reachable_pts ) {
const tripoint_bub_ms pt( p );
temp.emplace_back( pt );
}

return map::use_charges( temp, type, quantity, filter, bcp, in_tools );
}

std::list<item> map::use_charges( const std::vector<tripoint_bub_ms> &reachable_pts,
const itype_id &type, int &quantity,
const std::function<bool( const item & )> &filter,
Expand Down Expand Up @@ -6447,38 +6416,16 @@ std::list<item> map::use_charges( const std::vector<tripoint_bub_ms> &reachable_
return ret;
}

std::list<item> map::use_charges( const tripoint &origin, const int range,
const itype_id &type, int &quantity,
const std::function<bool( const item & )> &filter,
basecamp *bcp, bool in_tools )
{
return map::use_charges( tripoint_bub_ms( origin ), range, type, quantity, filter, bcp, in_tools );
}

std::list<item> map::use_charges( const tripoint_bub_ms &origin, const int range,
const itype_id &type, int &quantity,
const std::function<bool( const item & )> &filter,
basecamp *bcp, bool in_tools )
{
// populate a grid of spots that can be reached
std::vector<tripoint_bub_ms> reachable_pts;
reachable_flood_steps( reachable_pts, origin, range, 1, 100 );
const std::vector<tripoint_bub_ms> &reachable_pts = reachable_flood_steps( origin, range, 1, 100 );
return use_charges( reachable_pts, type, quantity, filter, bcp, in_tools );
}

units::energy map::consume_ups( const std::vector<tripoint> &reachable_pts, units::energy qty )
{
std::vector<tripoint_bub_ms> temp;
temp.reserve( reachable_pts.size() );

for( const tripoint p : reachable_pts ) {
const tripoint_bub_ms pt( p );
temp.emplace_back( pt );
}

return map::consume_ups( temp, qty );
}

units::energy map::consume_ups( const std::vector<tripoint_bub_ms> &reachable_pts,
units::energy qty )
{
Expand Down Expand Up @@ -6507,8 +6454,7 @@ units::energy map::consume_ups( const std::vector<tripoint_bub_ms> &reachable_pt
units::energy map::consume_ups( const tripoint_bub_ms &origin, const int range, units::energy qty )
{
// populate a grid of spots that can be reached
std::vector<tripoint_bub_ms> reachable_pts;
reachable_flood_steps( reachable_pts, origin, range, 1, 100 );
const std::vector<tripoint_bub_ms> &reachable_pts = reachable_flood_steps( origin, range, 1, 100 );
return consume_ups( reachable_pts, qty );
}

Expand Down Expand Up @@ -8035,20 +7981,8 @@ std::vector<tripoint_bub_ms> map::find_clear_path( const tripoint_bub_ms &source
return line_to( source, destination, ideal_start_offset, 0 );
}

void map::reachable_flood_steps( std::vector<tripoint> &reachable_pts, const tripoint &f,
int range, const int cost_min, const int cost_max ) const
{
std::vector<tripoint_bub_ms> temp_points;
map::reachable_flood_steps( temp_points, tripoint_bub_ms( f ), range, cost_min, cost_max );

for( const tripoint_bub_ms pt : temp_points ) {
reachable_pts.push_back( pt.raw() );
}
}

void map::reachable_flood_steps( std::vector<tripoint_bub_ms> &reachable_pts,
const tripoint_bub_ms &f,
int range, const int cost_min, const int cost_max ) const
std::vector<tripoint_bub_ms> map::reachable_flood_steps( const tripoint_bub_ms &f, int range,
const int cost_min, const int cost_max ) const
{
struct pq_item {
int dist;
Expand All @@ -8063,8 +7997,9 @@ void map::reachable_flood_steps( std::vector<tripoint_bub_ms> &reachable_pts,

// temp buffer for grid
const int grid_dim = range * 2 + 1;
const size_t grid_area = static_cast<size_t>( grid_dim ) * grid_dim;
// init to -1 as "not visited yet"
std::vector< int > t_grid( static_cast<size_t>( grid_dim * grid_dim ), -1 );
std::vector<int> t_grid( grid_area, -1 );
const tripoint_rel_ms origin_offset = { range, range, 0 };
const int initial_visit_distance = range * range; // Large unreachable value

Expand All @@ -8087,12 +8022,12 @@ void map::reachable_flood_steps( std::vector<tripoint_bub_ms> &reachable_pts,
// Up to 8 neighbors
int new_cost = elem.dist + 1;
// *INDENT-OFF*
std::array<int, 8> ox = {
const std::array<int, 8> ox = {
-1, 0, 1,
-1, 1,
-1, 0, 1
};
std::array<int, 8> oy = {
const std::array<int, 8> oy = {
-1, -1, -1,
0, 0,
1, 1, 1
Expand Down Expand Up @@ -8127,33 +8062,42 @@ void map::reachable_flood_steps( std::vector<tripoint_bub_ms> &reachable_pts,
}
}
}
std::vector<char> o_grid( static_cast<size_t>( grid_dim * grid_dim ), 0 );
std::vector<bool> o_grid( grid_area );
int count = 0;
for( int y = 0, ndx = 0; y < grid_dim; ++y ) {
for( int x = 0; x < grid_dim; ++x, ++ndx ) {
if( t_grid[ndx] != -1 && t_grid[ndx] < initial_visit_distance ) {
// set self and neighbors to 1
for( int dy = -1; dy <= 1; ++dy ) {
for( int dx = -1; dx <= 1; ++dx ) {
point t2( dx + x, dy + y );

if( t2.x >= 0 && t2.x < grid_dim && t2.y >= 0 && t2.y < grid_dim ) {
o_grid[t2.x + t2.y * grid_dim] = 1;
for( int y2 = y - 1; y2 <= y + 1; ++y2 ) {
for( int x2 = x - 1; x2 <= x + 1; ++x2 ) {
if( x2 >= 0 && x2 < grid_dim && y2 >= 0 && y2 < grid_dim ) {
const int index = x2 + ( y2 * grid_dim );
if( !o_grid[ index ] ) {
count++;
o_grid[ index ] = true;
}
}
}
}
}
}
}

// Now go over again to pull out all of the reachable points
for( int y = 0, ndx = 0; y < grid_dim; ++y ) {
for( int x = 0; x < grid_dim; ++x, ++ndx ) {
if( o_grid[ndx] ) {
tripoint_bub_ms t = f - origin_offset + tripoint{ x, y, 0 };
reachable_pts.push_back( t );

std::vector<tripoint_bub_ms> reachable_pts;
if( count != 0 ) {
reachable_pts.reserve( count );
// Now go over again to pull out all of the reachable points
for( int y = 0, ndx = 0; y < grid_dim; ++y ) {
for( int x = 0; x < grid_dim; ++x, ++ndx ) {
if( o_grid[ndx] ) {
tripoint_bub_ms t = f - origin_offset + tripoint{ x, y, 0 };
reachable_pts.push_back( t );
}
}
}
}
return reachable_pts;
}

bool map::clear_path( const tripoint &f, const tripoint &t, const int range,
Expand Down
26 changes: 3 additions & 23 deletions src/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -736,13 +736,8 @@ class map
* 1. Checks if a point is reachable using a flood fill and if it is, adds it to a vector.
*
*/
// TODO: Get rid of untyped overload.
void reachable_flood_steps( std::vector<tripoint> &reachable_pts, const tripoint &f,
int range,
int cost_min, int cost_max ) const;
void reachable_flood_steps( std::vector<tripoint_bub_ms> &reachable_pts, const tripoint_bub_ms &f,
int range,
int cost_min, int cost_max ) const;
std::vector<tripoint_bub_ms> reachable_flood_steps( const tripoint_bub_ms &f, int range,
int cost_min, int cost_max ) const;

/**
* Iteratively tries Bresenham lines with different biases
Expand Down Expand Up @@ -1572,33 +1567,20 @@ class map
std::list<item> use_amount( const tripoint_bub_ms &origin, int range, const itype_id &type,
int &quantity,
const std::function<bool( const item & )> &filter = return_true<item>, bool select_ind = false );
// TODO: Get rid of untyped overload.
std::list<item> use_amount( const std::vector<tripoint> &reachable_pts, const itype_id &type,
int &quantity,
const std::function<bool( const item & )> &filter = return_true<item>, bool select_ind = false );
std::list<item> use_amount( const std::vector<tripoint_bub_ms> &reachable_pts, const itype_id &type,
int &quantity,
const std::function<bool( const item & )> &filter = return_true<item>, bool select_ind = false );
// TODO: Get rid of untyped overload.
std::list<item> use_charges( const tripoint &origin, int range, const itype_id &type,
int &quantity, const std::function<bool( const item & )> &filter = return_true<item>,
basecamp *bcp = nullptr, bool in_tools = false );
std::list<item> use_charges( const tripoint_bub_ms &origin, int range, const itype_id &type,
int &quantity, const std::function<bool( const item & )> &filter = return_true<item>,
basecamp *bcp = nullptr, bool in_tools = false );
// TODO: Get rid of untyped overload.
std::list<item> use_charges( const std::vector<tripoint> &reachable_pts, const itype_id &type,
int &quantity,
const std::function<bool( const item & )> &filter = return_true<item>,
basecamp *bcp = nullptr, bool in_tools = false );
std::list<item> use_charges( const std::vector<tripoint_bub_ms> &reachable_pts,
const itype_id &type,
int &quantity,
const std::function<bool( const item & )> &filter = return_true<item>,
basecamp *bcp = nullptr, bool in_tools = false );

/** Find items located at point p (on map or in vehicles) that pass the filter */
// TODO: Get rid of untyped overload.
// TODO: Get rid of untyped overload, should be removable once #78677 merges.
std::list<item_location> items_with( const tripoint &p,
const std::function<bool( const item & )> &filter );
std::list<item_location> items_with( const tripoint_bub_ms &p,
Expand All @@ -1611,8 +1593,6 @@ class map
* @param qty amount of energy to consume. Is rounded down to kJ precision. Do not use negative values.
* @return Actual amount of energy consumed
*/
// TODO: Get rid of untyped overload.
units::energy consume_ups( const std::vector<tripoint> &reachable_pts, units::energy qty );
units::energy consume_ups( const std::vector<tripoint_bub_ms> &reachable_pts, units::energy qty );
units::energy consume_ups( const tripoint_bub_ms &origin, int range, units::energy qty );

Expand Down
Loading