Skip to content

Commit

Permalink
Minor cleanup in a_star_search
Browse files Browse the repository at this point in the history
  • Loading branch information
Vultraz committed Jan 16, 2021
1 parent 3f1ed55 commit 76389cb
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions src/pathfind/astarsearch.cpp
Expand Up @@ -183,35 +183,34 @@ plain_route a_star_search(const map_location& src, const map_location& dst,
if (n.t >= nodes[index(dst)].g) break;

std::vector<map_location> locs(6);
get_adjacent_tiles(n.curr, locs.data());

if (teleports && !teleports->empty()) {
auto allowed_teleports = teleports->get_adjacents(n.curr);
locs.insert(locs.end(), allowed_teleports.begin(), allowed_teleports.end());
}

int i = locs.size();

get_adjacent_tiles(n.curr, locs.data());
for(auto i = locs.rbegin(); i != locs.rend(); ++i) {
const map_location& loc = *i;

for (; i-- > 0;) {
if (!locs[i].valid(width, height, border)) continue;
if (locs[i] == n.curr) continue;
node& next = nodes[index(locs[i])];
if (!loc.valid(width, height, border)) continue;
if (loc == n.curr) continue;
node& next = nodes[index(loc)];

double thresh = (next.in - search_counter <= 1u) ? next.g : stop_at + 1;
// cost() is always >= 1 (assumed and needed by the heuristic)
if (n.g + 1 >= thresh) continue;
double cost = n.g + calc.cost(locs[i], n.g);
double cost = n.g + calc.cost(loc, n.g);
if (cost >= thresh) continue;

bool in_list = next.in == search_counter + 1;

next = node(cost, locs[i], n.curr, dst, true, teleports);
next = node(cost, loc, n.curr, dst, true, teleports);

if (in_list) {
std::push_heap(pq.begin(), std::find(pq.begin(), pq.end(), static_cast<int>(index(locs[i]))) + 1, node_comp);
std::push_heap(pq.begin(), std::find(pq.begin(), pq.end(), static_cast<int>(index(loc))) + 1, node_comp);
} else {
pq.push_back(index(locs[i]));
pq.push_back(index(loc));
std::push_heap(pq.begin(), pq.end(), node_comp);
}
}
Expand Down

0 comments on commit 76389cb

Please sign in to comment.