diff --git a/src/pathfind/astarsearch.cpp b/src/pathfind/astarsearch.cpp index d78374621a25..d0e704573aa3 100644 --- a/src/pathfind/astarsearch.cpp +++ b/src/pathfind/astarsearch.cpp @@ -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 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(index(locs[i]))) + 1, node_comp); + std::push_heap(pq.begin(), std::find(pq.begin(), pq.end(), static_cast(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); } }