void apply_astar(const vec2 & src, const vec2 & tar)
{
clear_res();
reset_vertex();
GraphVertex* srcvert = query_vertex(src, 5);
GraphVertex* tarvert = query_vertex(tar, 5);
GLFASSERT(srcvert != nullptr && tarvert != nullptr);
priority_queue <GraphVertex*, vector<GraphVertex*>, SearchCmpAstar> frontier;
srcvert->_cost_to_this = 0;
srcvert->_cost_to_goal_bypass = (int)glfMath::dist(srcvert->_Pos, tarvert->_Pos);
frontier.push(srcvert);
while (!frontier.empty())
{
GraphVertex* nextvert = frontier.top();
frontier.pop();
if (nextvert->_Index == tarvert->_Index)
{
while (nextvert)
{
_result.push_back(nextvert);
nextvert = nextvert->_prev;
}
return;
}
nextvert->_frontier = false;
nextvert->_explored = true;
for (int i = 0; i < nextvert->_Neighbors.size(); i++)
{
if (nextvert->_Neighbors[i]->_explored == false)
{
int gscore = nextvert->_Dists[i] + nextvert->_cost_to_this;
int hscore = (int)glfMath::dist(nextvert->_Neighbors[i]->_Pos, tarvert->_Pos);
if (nextvert->_Neighbors[i]->_frontier == false)
{
nextvert->_Neighbors[i]->_frontier = true;
nextvert->_Neighbors[i]->_cost_to_this = gscore;
nextvert->_Neighbors[i]->_prev = nextvert;
nextvert->_Neighbors[i]->_cost_to_goal_bypass = gscore + hscore;
/* why the fuck this must be after three lines above?!! fuck?!! */
frontier.push(nextvert->_Neighbors[i]);
}
elseif (gscore < nextvert->_Neighbors[i]->_cost_to_this)
{
nextvert->_Neighbors[i]->_cost_to_this = gscore;
nextvert->_Neighbors[i]->_prev = nextvert;
nextvert->_Neighbors[i]->_cost_to_goal_bypass = gscore + hscore;
}
}
}
}
cout << "A star search failed!\n";
}
it takes two position on screen coordinate and translate them to graph nodes. With in a certain distance of the searching path it works ok. But when the earching path is more than that limit(I dont know how much), an exception is throwed at frontier.pop() with message "invalid heap". Why this is happening? thanks!
I've checked the pointer and they are all valid even when exception is thrown...not infinite loop too, because exception is thrown only after 8 or 9 times loop..Thanks anyway