Pathfind/Teleport: made getters return their sets rather than taking them as arguments

Implementations were cleaned up in the process.
This commit is contained in:
Charles Dang 2021-01-16 21:24:08 +11:00
parent e3055d47ae
commit 5b39a1c21e
5 changed files with 41 additions and 45 deletions

View File

@ -469,8 +469,7 @@ namespace { // Private helpers for move_unit()
// block the tunnel if pass_allied_units=true. Whether the teleport is possible
// is checked by getting the teleport map with the see_all flag set to true.
const pathfind::teleport_map teleports = pathfind::get_teleport_locations(*move_it_, *current_team_, true, false, false);
std::set<map_location> allowed_teleports;
teleports.get_adjacents(allowed_teleports, prev_hex);
auto allowed_teleports = teleports.get_adjacents(prev_hex);
bool found_valid_teleport = false;
std::set<map_location>::const_iterator it = allowed_teleports.begin();

View File

@ -85,8 +85,7 @@ struct node {
if (teleports && !teleports->empty()) {
double new_srch = 1.0;
std::set<map_location> sources;
teleports->get_sources(sources);
auto sources = teleports->get_sources();
std::set<map_location>::const_iterator it = sources.begin();
for(; it != sources.end(); ++it) {
@ -94,10 +93,8 @@ struct node {
if (tmp_srch < new_srch) { new_srch = tmp_srch; }
}
double new_dsth = 1.0;
std::set<map_location> targets;
teleports->get_targets(targets);
auto targets = teleports->get_targets();
for(it = targets.begin(); it != targets.end(); ++it) {
const double tmp_dsth = heuristic(*it, dst);
@ -188,9 +185,7 @@ plain_route a_star_search(const map_location& src, const map_location& dst,
std::vector<map_location> locs(6);
if (teleports && !teleports->empty()) {
std::set<map_location> allowed_teleports;
teleports->get_adjacents(allowed_teleports, n.curr);
auto allowed_teleports = teleports->get_adjacents(n.curr);
locs.insert(locs.end(), allowed_teleports.begin(), allowed_teleports.end());
}

View File

@ -354,8 +354,7 @@ static void find_routes(
adj_locs.erase(off_board_it, adj_locs.end());
if ( teleporter ) {
std::set<map_location> allowed_teleports;
teleports.get_adjacents(allowed_teleports, cur_hex);
auto allowed_teleports = teleports.get_adjacents(cur_hex);
adj_locs.insert(adj_locs.end(), allowed_teleports.begin(), allowed_teleports.end());
}
for ( int i = adj_locs.size()-1; i >= 0; --i ) {

View File

@ -226,36 +226,41 @@ teleport_map::teleport_map(
}
}
void teleport_map::get_adjacents(std::set<map_location>& adjacents, map_location loc) const {
if (teleport_map_.count(loc) == 0) {
return;
} else {
const std::set<std::string>& keyset = (teleport_map_.find(loc)->second);
for(std::set<std::string>::const_iterator it = keyset.begin(); it != keyset.end(); ++it) {
const std::set<map_location>& target = targets_.find(*it)->second;
adjacents.insert(target.begin(), target.end());
}
std::set<map_location> teleport_map::get_adjacents(map_location loc) const
{
const auto iter = teleport_map_.find(loc);
if(iter == teleport_map_.end()) {
return {};
}
std::set<map_location> res;
for(const std::string& key : iter->second) {
const auto& target = targets_.find(key)->second;
res.insert(target.begin(), target.end());
}
return res;
}
void teleport_map::get_sources(std::set<map_location>& sources) const {
std::map<std::string, std::set<map_location>>::const_iterator it;
for(it = sources_.begin(); it != sources_.end(); ++it) {
sources.insert(it->second.begin(), it->second.end());
std::set<map_location> teleport_map::get_sources() const
{
std::set<map_location> res;
for(const auto& src : sources_) {
res.insert(src.second.begin(), src.second.end());
}
return res;
}
void teleport_map::get_targets(std::set<map_location>& targets) const {
std::map<std::string, std::set<map_location>>::const_iterator it;
for(it = targets_.begin(); it != targets_.end(); ++it) {
targets.insert(it->second.begin(), it->second.end());
std::set<map_location> teleport_map::get_targets() const
{
std::set<map_location> res;
for(const auto& tgt : targets_) {
res.insert(tgt.second.begin(), tgt.second.end());
}
}
return res;
}
const teleport_map get_teleport_locations(const unit &u,
const team &viewing_team,

View File

@ -114,19 +114,17 @@ public:
teleport_map() :
teleport_map_(), sources_(), targets_() {}
/*
* @param adjacents used to return the adjacent hexes
/**
* @param loc the map location for which we want to know the adjacent hexes
* @todo what does this function actually have to do with adjacent hexes?
*/
void get_adjacents(std::set<map_location>& adjacents, map_location loc) const;
/*
* @param sources used to return the locations that are an entrance of the tunnel
*/
void get_sources(std::set<map_location>& sources) const;
/*
* @param targets used to return the locations that are an exit of the tunnel
*/
void get_targets(std::set<map_location>& targets) const;
std::set<map_location> get_adjacents(map_location loc) const;
/** Returns the locations that are an entrance of the tunnel. */
std::set<map_location> get_sources() const;
/** Returns the locations that are an exit of the tunnel. */
std::set<map_location> get_targets() const;
/*
* @returns whether the teleport_map does contain any defined tunnel